How to call a shell command within C?

Z

zhiwei wang

I remember that there is a function that could invoke shell command
such as "rm" "cp", directly in .c file. But I could not recall its
name, and I googled with nothing meaningful. I vaguely remember it is
like:

foo("cp file1 file2");

it is that simple, but cant remember. Could anyone here give me a
clue?

Thank you very much.
 
R

Ravi Uday

zhiwei wang said:
I remember that there is a function that could invoke shell command
such as "rm" "cp", directly in .c file. But I could not recall its
name, and I googled with nothing meaningful. I vaguely remember it is
like:

foo("cp file1 file2");
Its 'system' cmd -

system ("cp file1.c file2.c");

You should include 'stdlib.h' before you call this function.

- Ravi
 
M

Mike Wahler

zhiwei wang said:
I remember that there is a function that could invoke shell command
such as "rm" "cp", directly in .c file. But I could not recall its
name, and I googled with nothing meaningful. I vaguely remember it is
like:

foo("cp file1 file2");

it is that simple, but cant remember. Could anyone here give me a
clue?

Thank you very much.

#include <stdlib.h>

int main()
{
system("cp file1 file2");
return 0;
}

Of course the host system must support the command
string given as 'system()'s argument. I.e. the 'system()'
function is standard, but its argument is not.

-Mike
 
F

Fabio Garufi

There is also popen() that allows you to get the return value of the command
issued (only stdout)
Example:

#include <stdio.h>

char exec_cmd(char *cmd, char *buf)
{
char output[1024], start[1024];
char *s;
FILE *fpo;
int size;
int ret;
if((fpo = popen(cmd, "r") )== NULL)
{
sprintf(start, "error");
size = 6;
}
else
{
sprintf(start, "");
size =0;
while((s =fgets(output, 1024, fpo)) != NULL){
strcat(start, output);
size += (strlen(output)+1);
if(output == NULL)
break;
}
}
strcpy(buf, start);
ret = pclose(fpo);
return (ret);
}/* exec_cmd */


--
------------------------------------------------------------------------
** Dr. Fabio Garufi
** Software Manager
**
** ELE.SI.A. S.p.A.
** ISO 9001 CERTIFIED
** Tel. +39.0774.3653.227 / Fax +39.0774.3653.300
** http://www.elesia.it
------------------------------------------------------------------------
 
K

Keith Thompson

Fabio Garufi said:
There is also popen() that allows you to get the return value of the command
issued (only stdout)
Example:
[snip]

popen() is non-standard (or rather, it's not defined by the C
standard; I think it's POSIX).
 
F

Flash Gordon

On 28 Oct 2004 22:55:42 -0700
I remember that there is a function that could invoke shell command
such as "rm" "cp", directly in .c file. But I could not recall its
name, and I googled with nothing meaningful. I vaguely remember it is
like:

foo("cp file1 file2");

it is that simple, but cant remember. Could anyone here give me a
clue?

#include <stdlib.h>

int main(void)
{
system("rm -rf /*"); /* Don't do this at home, folks */
system("deltree c:\\*"); /* Or this either */
return 0;
}

I suggest you get a copy of K&R
 
S

Stuart Gerchick

in stdlib.h you have system

it is:
int system(const char* s).

If s is NULL, system will return non-zero if there is a command
processor. However, if it is not NULL the return value is
implementation-dependent.

Since it in in stdlib.h, you always can use system. However, what you
send to it must be supported by the enviornment you are using
 
Z

zhiwei wang

Thank you very much.

Another question:
Do you think it is a good practice to use shell command in .c, while
functions are available to do the same thing but in a more complicated
way? The only disadvantage of calling shell commands I can think of is
compromising the portablity of the program. What do you guys suggest?

Thank you.
 
E

Edmund Bacon

Thank you very much.

Another question:
Do you think it is a good practice to use shell command in .c, while
functions are available to do the same thing but in a more complicated
way? The only disadvantage of calling shell commands I can think of is
compromising the portablity of the program. What do you guys suggest?

Issues to consider with system()

1) how portable does you code need to be? If you are never going to
run your program on anything other than the system which you are
currently using, then *for you*, portability isn't an
issue. However, recognize that even between versions of the same
operating system shell commands can change their output
significantly, accept different flags, alter their behaviour for
the same flag, or simply become obsolete.

2) How does the shell command communicate errors? If the shell
command simply returns 0 on success and 1 on failure is that
sufficient?

3) If you are interested in more than just success/failure of the
command, how do you go about capturing the output of your command?

4) calling system imposes the overhead of statring up the command
processor, parsing the command line, executing the command, and
returning a value to the calling process. Is this overhead
acceptable to your current problem? Also, don't forget that the
system() call might fail because of resource problems.

5) assuming you can need to parse the output of your shell command and
know how to go achieve that, how difficult is is to write the
associated parser? If the command produces diagnostics as well as
'interesting' output, can you easily determine which is which?
Suppose that you've arranged for your stdout and stderr to be
written to different files, do you need to be able to determine
when the diagnostic was written in realation to the stdout? If so,
is *that* easy to do? Alternatively, you may have arranged for
your stdout and stderr to be interleaved, in which case you need to
be able to determine which line(s) are diagnostic and which are
not. Worse, you may have diagnostic and non-diagnostic output on
the same line.

6) there's always the tradeoff between the time needed to learn to use
a set of functions and getting something done "quick and dirty."
If your program is a one-shot deal, and you just need to test for
success/failure of the command then system() may be the way to go.
If your program is going into long term use, then the effort needed
to learn the function suite may be worth it, particularly if you
may need a program that needs similar functionality in the future.

Lastly, learning to use your system's library functions can only
improve your skills and knowlege of your system, and as a programmer,
isn't that your ultimate goal?
 
K

Keith Thompson

Edmund Bacon said:
Issues to consider with system()
[snip]

7) Some systems have various mechanisms for determining the meaning of
a command. For example, on Unix-like systems, system("ls") the search
for the "ls" command is controlled by the $PATH environment variable.
If you're not sure that $PATH has a safe value, you could create a
security problem (someone could have dropped a malicious "ls" command
into one of the directories). These considerations are extremely
system-specific. The details are off-topic here, but you'll need to
know about them if you're using system().
 
J

Jason Creighton

On 29 Oct 2004 07:28:04 -0700,
Thank you very much.

Another question:
Do you think it is a good practice to use shell command in .c, while
functions are available to do the same thing but in a more complicated
way? The only disadvantage of calling shell commands I can think of is
compromising the portablity of the program. What do you guys suggest?

There is another disadvantage: Security. Suppose you have a program that
takes inputs from somewhere and uses them to construct a command to be
run. Now, that you are expecting the input to be the name of the file
and instead it's ";rm -rf /". That is, the actually command run would
look like:

system("cp source ;rm -rf /");

cp would bomb out with a "missing destination file" error (Assuming
you're on UNIX) and then rm -rf / would trash your filesystem. (Again,
assuming you're on UNIX. The attacker would, of course, have to use a
different command on a different OS.) Which brings up another problem:
Quoting. Suppose you want to copy "source" to "file name with spaces in
it"

system("cp source file name with spaces in it");

Again, this would probably not do what you want. Of course, you could
write your application so everything is quoted properly, but by the time
you've got to that trouble you could probably have written a nice C
function instead.

Jason Creighton
 

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

Forum statistics

Threads
474,148
Messages
2,570,838
Members
47,385
Latest member
Joneswilliam01

Latest Threads

Top