472,983 Members | 2,801 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,983 software developers and data experts.

How to call a shell command within C?

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
8 34973

"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

"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
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
"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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Andreas Paasch | last post by:
According to the manual for PHP, I should be able to run a shell command within php. I'm trying to copy some php files from one location to another one using exec() but fail. ...
4
by: Yann.K | last post by:
Hello. Using Tkinter, i would create a widget which display a shell command return. This return is long, and i would display a real time display (like with the tail -f commande on Linux) I...
8
by: Siemel Naran | last post by:
Hi. I'm writing a command shell that reads commands from standard input. At this point I have the command in a std::string. Now I want to execute this command in the shell. From the Borland...
1
by: VRWC | last post by:
Hello dear people, In an A2K app, I have attempted to use the following command in some VBA code with IDENTICAL results with every single version of the following: Shell "outlook.exe", vbHide...
2
by: tripyre | last post by:
I recently resolved an issue I had with passing a variable to a call shell command, but now I need it to pause or leave the window open so I can manually close it. Below is my code, and I am not...
4
by: Kevin Mansel via .NET 247 | last post by:
Ok, basically this is my problem. I'm building a console app tocall a dos program. So i'm using the Shell command to call theprogram, now depending on what happens, I want to read theoutput that...
2
by: ¹é¿ø¼® | last post by:
Hello, everybody. I want to call any shell command from php script using shell_exec(). The problem is that the next line of the php script would not be run until a few minutes after running the...
4
by: RLN | last post by:
Re: Access 2003/WinXP Pro-SP3 I have a shell command that does not run correctly. I have two programs located on a file server: Pgm1.mdb and Pgm2.mdb Pgm1 launches Pgm2 via a command button...
6
by: sushi boi | last post by:
hi, I'm a first year student at UNSW Sydney, i was wondering if somebody could tell me how to run a terminal command from within OS X. A easy to understand example of why i would want this is: 1....
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.