本文介绍了用execvp在构建串问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图写一个shell,它的建设的一部分,是由用户输入的字符串(缓冲区)执行code。然而,当我试图用execvp附加输入字符串(AE,回声),它总是螺丝狗并返回-1。我不知所措,为什么。下面是相关的部分:
的char *缓冲区=释放calloc(100的sizeof(字符));
...
与fgets(缓冲,100,标准输入);
缓冲液[strlen的(缓冲器) - 1] = 0;因为与fgets插入新行的//必要
...
CMD = strsep(安培;缓冲器,);
字符*海峡=的malloc(50 * sizeof的(炭));
strcat的(STR,./);
strcat的(STR,CMD);
strcat的(STR.OUT);
...
I = execvp(STR(字符*。*)缓冲区);
解决方案
参数缓存
是错误的。 execvp
的第二个参数是一个指针数组。与此转换,你是hidding编译器警告,但它不工作
I'm trying to write a shell, and part of its construction is executing code from a user-inputted string (buffer). However, when I attempt to execvp the string with additional inputs (ae. echo a), it always screws the pooch and returns -1. I'm at a loss as to why. Here's the relevant pieces:
char * buffer = calloc(100, sizeof(char));
...
fgets(buffer, 100, stdin);
buffer[strlen(buffer) - 1] = 0; // necessary because of a newline inserted by fgets
...
cmd = strsep(&buffer, " ");
char * str = malloc(50 * sizeof(char));
strcat(str, "./");
strcat(str, cmd);
strcat(str, ".out");
...
i = execvp(str, (char * *) buffer);
解决方案
The argument buffer
is wrong. The second argument of execvp
is an array of pointers. With this cast, you are hidding a compiler warning, but it does not work.
这篇关于用execvp在构建串问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!