By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,077 Members | 2,121 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,077 IT Pros & Developers. It's quick & easy.

How to call a shell command within C?

P: n/a
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.
Nov 14 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a

"zhiwei wang" <zh********@gmail.com> wrote in message
news:35**************************@posting.google.c om...
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
Nov 14 '05 #2

P: n/a

"zhiwei wang" <zh********@gmail.com> wrote in message
news:35**************************@posting.google.c om...
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
Nov 14 '05 #3

P: n/a
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
------------------------------------------------------------------------
"Ravi Uday" <ra******@gmail.com> ha scritto nel messaggio
news:1099030598.654883@sj-nntpcache-5...

"zhiwei wang" <zh********@gmail.com> wrote in message
news:35**************************@posting.google.c om...
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

Nov 14 '05 #4

P: n/a
"Fabio Garufi" <fg*****@elesia.it> writes:
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).

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #5

P: n/a
On 28 Oct 2004 22:55:42 -0700
zh********@gmail.com (zhiwei wang) wrote:
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
--
Flash Gordon
Sometimes I think shooting would be far too good for some people.
Although my email address says spam, it is real and I read it.
Nov 14 '05 #6

P: n/a
zh********@gmail.com (zhiwei wang) writes:
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?

--
Remove -42 for email
Nov 14 '05 #7

P: n/a
Edmund Bacon <eb*********@onesystem.com> writes:
zh********@gmail.com (zhiwei wang) writes:
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()

[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().

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #8

P: n/a
On 29 Oct 2004 07:28:04 -0700,
zh********@gmail.com (zhiwei wang) wrote:
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
Nov 14 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.