executing shell commands from c?

K

Kentor

I know I can use the system function to execute commands, but I need
to pass parameters to it and it doesn't want to accept them...
is there a way to do system("mv %s temp123", myvar) ?
or maybe there's another function that accepts parameters?
thanks for your help.

Simon
 
B

Ben Pfaff

Kentor said:
I know I can use the system function to execute commands, but I need
to pass parameters to it and it doesn't want to accept them...
is there a way to do system("mv %s temp123", myvar) ?

Use a function such as sprintf to construct the command in a
temporary string, then pass that string to system().
 
I

Ian Collins

Kentor said:
I know I can use the system function to execute commands, but I need
to pass parameters to it and it doesn't want to accept them...
is there a way to do system("mv %s temp123", myvar) ?
or maybe there's another function that accepts parameters?

Build the string you want with sprintf() and pass it to system().
 
G

Gordon Burditt

I know I can use the system function to execute commands, but I need
to pass parameters to it and it doesn't want to accept them...

Generate a string that contains the command you wish to execute,
including all the parameters. It does not have to be a string
literal. Then pass it to system().
is there a way to do system("mv %s temp123", myvar) ?

No, but take a look at the sprintf() function, and consider what
happens if you pass the resulting string to system(). There are
lots of ways to construct strings in C other than sprintf(), but
sprintf() is often convenient for this purpose. Beware that it's
YOUR problem to allocate a sufficiently long buffer to hold the
command, either with a character array, or with malloc().
 
S

SM Ryan

# I know I can use the system function to execute commands, but I need
# to pass parameters to it and it doesn't want to accept them...
# is there a way to do system("mv %s temp123", myvar) ?
# or maybe there's another function that accepts parameters?
# thanks for your help.

More specifically, you can do things like

static char F[] = "mv %s temp123";
char f[sizeof F+strlen(myvar)+1];
sprintf(f,F,myvar);
int rc = system(f);
 
B

Ben Pfaff

SM Ryan said:
static char F[] = "mv %s temp123";
char f[sizeof F+strlen(myvar)+1];
sprintf(f,F,myvar);

sizeof F includes F's null terminator in its value, so you don't
have to add an additional 1 to the size of f. (You could, in
fact, subtract 2, because f will not include %s.) I can see why
you'd want to be conservative, though.

Also, this is not valid C89, because the size of f is not a
constant expression.
 
M

Martin Ambuhl

Kentor said:
I know I can use the system function to execute commands, but I need
to pass parameters to it and it doesn't want to accept them...
is there a way to do system("mv %s temp123", myvar) ?
or maybe there's another function that accepts parameters?
thanks for your help.

Build the command string with sprintf (obviously other ways are available),
Use the built command string as the argument for system
 
K

Kentor

# I know I can use the system function to execute commands, but I need
# to pass parameters to it and it doesn't want to accept them...
# is there a way to do system("mv %s temp123", myvar) ?
# or maybe there's another function that accepts parameters?
# thanks for your help.

More specifically, you can do things like

static char F[] = "mv %s temp123";
char f[sizeof F+strlen(myvar)+1];
sprintf(f,F,myvar);
int rc = system(f);

Great thanks everyone, and thanks even more for the example Ryan.
 
C

CBFalconer

Ben said:
SM Ryan said:
static char F[] = "mv %s temp123";
char f[sizeof F+strlen(myvar)+1];
sprintf(f,F,myvar);

sizeof F includes F's null terminator in its value, so you don't
have to add an additional 1 to the size of f. (You could, in
fact, subtract 2, because f will not include %s.) I can see why
you'd want to be conservative, though.

Also, this is not valid C89, because the size of f is not a
constant expression.

ITYM strlen(myvar) is not a constant expression.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>

"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews
 
M

muntyan

I know I can use the system function to execute commands, but I need
to pass parameters to it and it doesn't want to accept them...
is there a way to do system("mv %s temp123", myvar) ?
or maybe there's another function that accepts parameters?

In addition to creating that string make sure myvar makes
sense, learn shell quoting rules, and use '--' argument for
mv.

Best regards,
Yevgen
 
B

Ben Pfaff

CBFalconer said:
Ben said:
SM Ryan said:
static char F[] = "mv %s temp123";
char f[sizeof F+strlen(myvar)+1];

Also, this is not valid C89, because the size of f is not a
constant expression.

ITYM strlen(myvar) is not a constant expression.

That's what I said: the expression inside the brackets is the
size of f.
 

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,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top