C语言 如何在c中将字符串匹配到execvp
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23417442/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to match string to execvp in c
提问by tubu13
Hello I need to run a program using execvp()the thing is that im getting a sting as argument
and i cant seem to match the syntax of the command to work properly here is the code:
你好,我需要使用一个程序来运行一个程序execvp(),因为我将一个刺作为参数,我似乎无法匹配命令的语法才能正常工作,这里是代码:
int executeUserCommand(char *command, int runInBackground) {
pid_t pid;
int status;
pid = fork();
if (pid == 0) {
//child process
execvp(command, &command);
exit(1);
} else {
//perent
if (!runInBackground) {
waitpid(pid, &status, 0);
return WEXITSTATUS(status);
}
}
return 0;
}
and i am using executeUserCommand("./test 1 2 3", 0)the thing is that the program is running but without the arguments..
what is the problem and how i solve it?
我正在使用executeUserCommand("./test 1 2 3", 0)的是程序正在运行但没有参数..问题是什么以及我如何解决它?
EDIT: i have added two fuctions to help me with the spliting and its still not working.
编辑:我添加了两个功能来帮助我进行拆分,但它仍然无法正常工作。
char *getCommand(char *commandAndArguments) {
char command[256];
memset(command,0,MAX_LENGTH_OF_LINE);
sscanf(commandAndArguments,"%s ", command);
//strncpy(command, commandAndArguments, command - commandAndArguments);
return command;
}
void commandToArrguments(char *stringToSplit) {
const char *p = stringToSplit;
int n, len;
int i = 0;
for (n = 0; n < MAX_NUMBER_OF_COMMANDS; n++) {
for (len=0; len < MAX_LENGTH_OF_LINE-1; len++) {
if (p[0] == 'char* args[5];
args[0]="./test";
args[1]="1";
args[2]="2";
args[3]="3";
args[4]=NULL;
execvp(args[0],args);
') {
break;
}
if (p[0] == ' ') {
p += 1;
break;
}
commandArgumnets[n][len] = *p;
p++;
}
commandArgumnets[n][len] = 'void execute(char **argv)
{
pid_t pid;
int status;
if ((pid = fork()) < 0) { /* fork a child process */
printf("*** ERROR: forking child process failed\n");
exit(1);
}
else if (pid == 0) { /* for the child process: */
if (execvp(*argv, argv) < 0) { /* execute the command */
printf("*** ERROR: exec failed\n");
exit(1);
}
}
else { /* for the parent: */
while (wait(&status) != pid) /* wait for completion */
;
}
}
void main(void)
{
char line[1024]; /* the input line */
char *argv[64]; /* the command line argument */
while (1) { /* repeat until done .... */
printf("Shell -> "); /* display a prompt */
gets(line); /* read in the command line */
printf("\n");
parse(line, argv); /* parse the line */
if (strcmp(argv[0], "exit") == 0) /* is it an "exit"? */
exit(0); /* exit if it is */
execute(argv); /* otherwise, execute the command */
}
}
';
}
}
int executeUserCommand(char *command, int runInBackground) {
pid_t pid;
int status;
pid = fork();
char *commandToExecute = getCommand(command);
if (pid == 0) {
//child process
execvp(commandToExecute, commandArgumnets);
exit(1);
} else {
//perent
if (!runInBackground) {
waitpid(pid, &status, 0);
return WEXITSTATUS(status);
}
}
return 0;
}
}
commandArgumnetsis defined as global and still not working
commandArgumnets被定义为全局并且仍然不起作用
回答by tylerl
The execvpcall expects arguments to be separated out in an array. You're providing only a single argument, which tells the OS to run a program called ./test 1 2 3instead of running ./testwith the parameters ./test, 1, 2, and 3(yes, the 0th parameter should be the executable).
该execvp调用需要在数组中分离出参数。你只提供一个参数,它告诉OS运行调用程序./test 1 2 3,而不是运行./test与参数./test,1,2,和3(是的,第0个参数应该是可执行文件)。
So you need to construct an array of strings by splitting your command into separate arguments (perhaps splitting along all spaces, perhaps taking quotes into consideration). How you do that is up to you. Each string in your array should be just one argument without spaces.
因此,您需要通过将命令拆分为单独的参数来构造一个字符串数组(也许沿着所有空格拆分,也许考虑到引号)。你如何做到这一点取决于你。数组中的每个字符串应该只是一个没有空格的参数。
Like this:
像这样:
##代码##回答by ryyker
Here is another example similar to your function: (From Here)
这是另一个类似于您的功能的示例:(从这里)
Notice how argvis set up in main()..., then passed to execute(). Notice also the prototype used to pass argv... char **
Use a similar argumenttype (i.e. array of strings) in your version.
注意如何argv在main()... 中设置,然后传递给execute(). 还要注意用于传递的原型argv...在您的版本中char **
使用类似的参数类型(即字符串数组)。

