与execv有一些麻烦(bash c)

时间:2016-03-30 20:47:54

标签: c bash execv

我想编写一个程序来执行在.c文件中传递参数的命令,但是我从一个char指针数组的声明中得到了难以理解的错误;我的代码如下:

#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>


int main(int argc, char const *argv[])
{

 if(argc < 2)
 {
    printf("Incorrect number of arguments.\n");
    return -1;
 }

 char* args[1 + argc - 2]; // this is causing the problem
 char cmd[10];
 /*
 * some operations...
 */

 char* test[] = {"ps","-l","NULL"};
 execv("/bin/ps",test);

 return 0;
}

execv的参数通常应该是cmd和args,但它没有用,所以我尝试了一个简单的命令,也没有工作,我收到以下错误:

error: unsupported option (BSD syntax)

Usage:
ps [options]

Try 'ps --help <simple|list|output|threads|misc|all>'
 or 'ps --help <s|l|o|t|m|a>'
for additional help text.

For more details see ps(1).

但是,如果我把1作为args数组的大小,则执行ps命令...我很困惑,我会感激任何帮助

0 个答案:

没有答案