Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 如何在Linux中接受system()中的多个参数_C_Linux_Unix - Fatal编程技术网

C 如何在Linux中接受system()中的多个参数

C 如何在Linux中接受system()中的多个参数,c,linux,unix,C,Linux,Unix,我正在尝试使用fork()和execl()实现一个system()。我无法接受像ls-l和ps-a1这样的多个参数。该代码适用于ls和ps等参数。我无法执行此操作。不允许更改my_system()的参数和返回类型。我是初学者。谢谢:) int my_系统(const char*命令) { int-ret=0; ret=execl(“/bin/sh”,“sh”,“-c”,命令,(char*)NULL); 如果(ret==-1) 错误(1,0,“execl()系统调用中出现错误\n”); 返回0;

我正在尝试使用fork()和execl()实现一个system()。我无法接受像ls-l和ps-a1这样的多个参数。该代码适用于ls和ps等参数。我无法执行此操作。不允许更改my_system()的参数和返回类型。我是初学者。谢谢:)

int my_系统(const char*命令)
{
int-ret=0;
ret=execl(“/bin/sh”,“sh”,“-c”,命令,(char*)NULL);
如果(ret==-1)
错误(1,0,“execl()系统调用中出现错误\n”);
返回0;
}
int main(int argc,char*argv[])
{
pid_t pid;
pid_t ret;
char*命令;
int ret_系统;
int wstatus;
如果(argc<2)
错误(1,0,“参数太少”\n);
printf(“参数的数量为:%d”,argc);
command=argv[1];
printf(“父进程的pid为:%d\n”,getpid());
pid=fork();
如果(pid==-1){
错误(1,0,“创建子进程时出错”);
}否则如果(pid==0){
printf(“子进程的pid为:%d\n”,getpid());
ret_系统=我的_系统(命令);
}否则{
ret=waitpid(-1,&wstatus,0);
printf(“已终止的子级的pid为%d,退出状态为%d\n”,ret,wstatus);
}
返回0;
}

您需要进行3项更改:

int my_system(char *argv[])
...
    ret = execvp(argv[1], &argv[1]);
...
        ret_system = my_system(argv);

int my_系统(const char*命令)
{
int-ret=0;
ret=execl(“/bin/sh”,“sh”,“-c”,命令,(char*)NULL);
如果(ret==-1)
错误(1,0,“execl()系统调用中出现错误\n”);
返回0;
}
char*get_命令(int argc,char**argv)
{
int i=0;
静态字符命令[size];
strcpy(命令,argv[1]);
对于(i=2;i
为什么在我的系统中使用exec shell。为什么不执行“命令”?某些exec变体允许参数。此外,您可以考虑将移动叉移到MySySoal.您的程序获取命令作为独立参数运行的参数。您需要自己将它们连接在一起。如果您像
/foo ls-l
那样运行程序,程序将看到3个参数(包括
argv[0]
中的程序名本身)。您可能需要使用引用为单个参数的命令运行程序,如
/foo“ls-l”
。然后程序将看到两个参数-程序名在
argv[0]
中,要执行的命令在
argv[1]
@kjohri中。请告诉我fork()到我的系统函数的移动情况。如果你能解释一下,那会很有帮助的。非常感谢:)@dbush非常感谢。
int my_system(char *argv[])
...
    ret = execvp(argv[1], &argv[1]);
...
        ret_system = my_system(argv);

int my_system(const char *command)
{
    int ret = 0;

    ret = execl("/bin/sh", "sh", "-c", command, (char *)NULL);
    if (ret == -1)
        error(1, 0, "error occcured in the execl() system call\n");
    return 0;
}

char *get_command(int argc, char **argv)
{
    int i = 0;
    static char command[size];

    strcpy(command, argv[1]);
    for (i = 2; i < argc; i++) {
        strcat(command, " ");
        strcat(command, argv[i]);
    }
    return command;
}

int main(int argc, char *argv[])
{
    pid_t pid;
    pid_t ret;
    int ret_system;
    int i = 0;
    int wstatus;
    char *command;

    if (argc < 2)
        error(1, 0, "Too few arguments\n");
    printf("The pid of the parent-process is :%d\n", getpid());
    pid = fork();
    if (pid == -1) {
        error(1, 0, "error in creating the sub-process\n");
    } else if (pid == 0) {
        printf("The pid of the child- process is :%d\n", getpid());
        command = get_command(argc, argv);
        ret_system = my_system(command);
    } else {
        ret = waitpid(-1, &wstatus, 0);
        printf("The pid of the child that has terminated is %d and the status of exit is %d\n", ret, wstatus);
    }
    return 0;
}