Udit Gupta
Udit Gupta

Reputation: 3272

Passing a C character string to linux command line

I have a string in my function defined like ..

 char *key="anyvalue";

Now I use a linux command as ...

 $openssl dgst -md5 -hmac "anyvalue" file.txt

Now the problem is I need to carry out following task through a C function ..

Here is the code ...

  void func (char *key) {

     char *key_new=key;

     system("openssl -dgst md5 -hmac <got stuck here> file.txt");

  }

How could I pass the key value to the portion labled ??

I did this pretty simply in php. ...

   $key="somevalue"

   exec("openssl -dgst md5 -hmac $key file.txt");

Is there something similiar avaliable in C ???

If not then Please tell me any other possible way ???

Limitation :

The key has to be passed through function .

I can't take it as a C command line argument.

Edit :

I tried with this one ... but first of all I would like to mention that its a small file in a big project and warning being treated as error .. so I need to take care of them also

Here is what I did -

    char *sstring=NULL;
    sprintf(sstring, "openssl dgst -md5 -hmac \"%s\"
    -out data3.md5 data3.txt",(char *)key);
    system(sstring);

if I won't initialize then here comes the warning ..

    gcc -o hmacmd5.so -I.. -fPIC -fsigned-char -pipe -Wall 
    -Wpointer-arith -Wwrite-strings -Wstrict-prototypes -Wnested-externs
    -Winline -Werror -g -Wcast-align -DSENDIP_LIBS=\"/usr/local/lib/sendip\"
    -shared hmacmd5.c ../libsendipaux.a ../libsendipaux.a

    cc1: warnings being treated as errors
    hmacmd5.c: In function ‘xoricv’:
    hmacmd5.c:271:9: error: ‘sstring’ is used uninitialized in this function
    make: *** [hmacmd5.so] Error 1

Upvotes: 0

Views: 734

Answers (5)

Shahbaz
Shahbaz

Reputation: 47563

If you want to write the character " inside a C string, you could write it with \"

So, you write

system("openssl dgst -md5 -hmac \"key\" file.txt");

If you key is not a constant, you should use snprintf

Something like this:

char buffer[/*enough size*/];
snprintf(buffer, /*the size*/, "openssl dgst -md5 -hmac \"%s\" file.txt", key);

And after

system(buffer);

Upvotes: 2

Nate
Nate

Reputation: 12839

I think you are looking for sprintf:

int sprintf(char *STR, const char *FORMAT, ...);

In your case, you would use it as follows:

sprintf(some_allocated_output_string, "openssl -dgst md5 -hmac %s", key);
system(some_allocated_output_string);

EDIT:

After seeing the code you tried, I can see I didn't provide you a complete answer.

You have two choices here (assume that STRING_SIZE below is some #defined size, like 300 or something):

1) use a preallocated buffer:

char sstring[STRING_SIZE];
sprintf(sstring, "openssl -dgst md5 -hmac \"%s\" -out data3.md5 data3.txt",(char *)key);
system(sstring);

2) use malloc/free:

#include <stdlib.h>
//blah blah blah
char *sstring=NULL;
//blah blah blah
sstring = malloc(STRING_SIZE);
sprintf(sstring, "openssl -dgst md5 -hmac \"%s\" -out data3.md5 data3.txt",(char *)key);
system(sstring);
free(sstring);

I would suggest the first approach. Along with this, I would highly suggest taking care to use @pmg's suggestion of snprintf, if your compiler supports it. This would look like this:

char sstring[STRING_SIZE];
int result = 0;
result = snprintf(sstring, STRING_SIZE, "openssl -dgst md5 -hmac \"%s\" -out data3.md5 data3.txt",(char *)key);
// Perform a check on result here, in case you ran out of space.
// If result > STRING_SIZE, you need to try a larger buffer.
system(sstring);

Upvotes: 4

harald
harald

Reputation: 6126

Another alternative:

void func(char * key)
{
    char cmd[255] = "openssl dgst -md5 -hmac ";
    assert(sizeof cmd > strlen(cmd) + strlen(key));
    strcpy(cmd, key);
    system(cmd);
}

Upvotes: 0

Nick Shaw
Nick Shaw

Reputation: 2113

How about:

 void func (char *key) {

     char *cmd = "openssl -dgst md5 -hmac ";
     char *fullmsg = _malloc( strlen(key) + strlen(cmd) );
     if (fullmsg != NULL) {
       sprintf_s( fullmsg, sizeof(fullmsg), "%s%s", cmd, key );
       system( fullmsg );
       free( fullmsg );
     } // else out of memory

  }

Upvotes: 0

bmargulies
bmargulies

Reputation: 100143

You should learn to use the openssl API instead of invoking command lines.

Failing that, you need to use system, not fork.

Upvotes: 3

Related Questions