所以我要定义system()函数!这是我的功能:
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
void mySystem (char *command)
{
execlp (command, command, (char*) 0);
}
int main (int argc, char* argv[])
{
for (int i = 1; i < argc; i++)
{
char command[50];
strcpy(command, argv[i]);
mySystem(command);
}
return 0;
}
然后我试了一下,结果是这样的:
gcc exe6.c;
./a.out ls ls
在这种情况下,它只执行一次 ls。
./a.out "ls -l"
在这种情况下不做任何事情。我做错了什么?
最佳答案
man page实际上告诉你怎么做:
The system() library function uses fork(2) to create a child process that executes the shell command specified in command using execl(3) as follows:
execl("/bin/sh", "sh", "-c", command, (char *) 0);
system() returns after the command has been completed.
关于c - 如何定义系统(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49479039/