473,396 Members | 2,024 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 35014

"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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.