#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int n;
char buf[1000], *p;
/* This lacks checks for too-few arguments, or buffer-overflow */
p = buf + sprintf(buf, "copy");
for (n=1; n<argc; n++)
p += sprintf(p, "%c%s", (n==1 || n==argc-1 ? ' ' : '+'), argv[n]);
puts(buf); /* Change "puts" to "system" in the final version */
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int n;
char buf[1000], *p;
/* This lacks checks for too-few arguments, or buffer-overflow */
p = buf + sprintf(buf, "copy");
for (n=1; n<argc; n++)
p += sprintf(p, "%c%s", (n==1 || n==argc-1 ? ' ' : '+'), argv[n]);
puts(buf); /* Change "puts" to "system" in the final version */
return 0;
}
That is not my homework.
I just want to know the usge of "system()" because I found the system function is like this:int system(char *command)
It is ok when executing command like "dir" that contains no argument.
But I don't know how to do if the command contains arguments(something like what I asked)
Anyway, thanks a lot.
The difference is that system() can be used to execute any unix command as if you type it at a shell command prompt. You just pass a single string.
e.g. in your C code
system("xterm");
When the system is executed, an xterm will be launched. Default shell is sh so only those command known by shell can be run.
exec() can do the same but it gives you more control. You can specify path to an application and pass arguments to it. For example, if exec() fails to lauch the xterm, the path you specified could be wrong. Like I said, more control.