using variables in arguments in popen with C

T

tntelle

Hello - I have a bit of C code that executes an external script
using popen and reads the last line of output. The problem is I want
to send argugments to the external shell script - which is not a big
deal, the problem is using a variable:

using arguments like this:

FILE * f = popen("./ext.ksh arg1", "r");
size_t r;
while((r = fread(RETURN_CODE, sizeof(char), BUFSIZE - 1, f)) > 0) {
RETURN_CODE[r+1] = '\0';

i want arg1 to be a variable

char arg1[] = "TEST"

FILE * f = popen("./ext.ksh %s", "r");

how do you get this to work ? is it possible or is there an
alternative ?

Thank you in advance!
 
L

Loïc Domaigné

Good Morning,
  Hello - I have a bit of C code that executes an external script
using popen and reads the last line of output. The problem is I want
to send argugments to the external shell script - which is not a big
deal, the problem is using a variable:

using arguments like this:

  FILE * f = popen("./ext.ksh  arg1", "r");
  size_t r;
  while((r = fread(RETURN_CODE, sizeof(char), BUFSIZE - 1, f)) > 0) {
        RETURN_CODE[r+1] = '\0';

i want arg1 to be a variable

char arg1[] = "TEST"

  FILE * f = popen("./ext.ksh  %s", "r");

how do you get this to work ? is it possible or is there an
alternative ?

You have to first build the command string for popen() using strings
function (e.g. snprintf() etc), and then use this string in popen.

HTH,
Loïc
 
B

Ben Bacarisse

tntelle said:
Hello - I have a bit of C code that executes an external script
using popen and reads the last line of output. The problem is I want
to send argugments to the external shell script - which is not a big
deal, the problem is using a variable:

using arguments like this:

FILE * f = popen("./ext.ksh arg1", "r");
size_t r;
while((r = fread(RETURN_CODE, sizeof(char), BUFSIZE - 1, f)) > 0) {
RETURN_CODE[r+1] = '\0';

i want arg1 to be a variable

char arg1[] = "TEST"

FILE * f = popen("./ext.ksh %s", "r");

how do you get this to work ? is it possible or is there an
alternative ?

On way is to use sprintf (or, if you have it, snprintf):

char command[CMD_SIZE];
snprintf(command, sizeof command, "./ext.ksh %s", arg1);

and, if everything is OK,

FILE *f = popen(command, "r");

The test for everything being OK, will depend on your circumstances,
but it is likely to involve checking that the snprintf did not
truncate the command.

Of course, there are more efficent schemes in the special case of
adding one string to the end of another of known length, but snprintf
is more general at the expense of being slower.
 
N

Nobody

FILE * f = popen("./ext.ksh %s", "r");

how do you get this to work ? is it possible or is there an
alternative ?

On way is to use sprintf (or, if you have it, snprintf):

char command[CMD_SIZE];
snprintf(command, sizeof command, "./ext.ksh %s", arg1);

I'd suggest a minor enhancement:

snprintf(command, sizeof command, "./ext.ksh '%s'", arg1);

This fails if arg1 contains a single-quote character, but as is so often
the case, handling the last 1% means a significant increase in complexity,
e.g.:

strcpy(command, "./ext.ksh");
j = strlen(command);
command[j++] = ' ';
command[j++] = '\'';
for (i = 0; arg1; i++) {
if (arg1 == '\'') {
if (j + 6 > sizeof(command))
fatal_error();
command[j++] = '\'';
command[j++] = '\\';
command[j++] = '\'';
command[j++] = '\'';
}
else {
if (j + 3 > sizeof(command))
fatal_error();
command[j++] = arg1;
}
}
command[j++] = '\'';
command[j++] = '\0';
 
T

tntelle

I was hoping for something cryptic at best and instead i got an
argument around the best way to do it =P haha thanks! This works great
- I have no intention on using single quotes or other characters
outside of the norm - thank you for your help!!!
 
B

Ben Bacarisse

You don't quote anything of mine so it is clearer to remove the
attribution.
it doesn't check if (sizeof command)> strlen("./ext.ksh")
i see it not indentation here;

FYI: the message you are replying to had proper indentation when I saw
it in my new reader.

<snip>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,996
Messages
2,570,237
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top