如何在c中编写迷你shell时解析iinput命令行
我为我的linux迷你shell任务编写了以下代码 继续获取以下错误消息。 -警告:从不兼容的指针类型传递'strcat'的参数2 -/usr/include/string.h:136:14:注意:应为“const char*restrict”,但 参数的类型为“char**”如何在c中编写迷你shell时解析iinput命令行,c,C,我为我的linux迷你shell任务编写了以下代码 继续获取以下错误消息。 -警告:从不兼容的指针类型传递'strcat'的参数2 -/usr/include/string.h:136:14:注意:应为“const char*restrict”,但 参数的类型为“char**” 有人检查一下,告诉我出了什么问题吗 守则: int countArgs(char n_args[], ...){ va_list ap; int i, t; va_start(ap, n_arg
int countArgs(char n_args[], ...){
va_list ap;
int i, t;
va_start(ap, n_args);
for(i=0;t = va_arg(ap, int);i++){ return t; }
va_end(ap);
}
char *parse(char buffer[],int num_of_args, char *arguments[])
{
arguments[num_of_args+1]=NULL;
int i;
for(i=0;i<num_of_args+1;i++){
do
{
arguments[i]= strtok(buffer, " ");
}
while(arguments!=NULL);
}
return arguments;
}
int main(int argc, char **argv)
char buffer[512];
char *path = "/bin/";
while(1)
{
//print the prompt
printf("myShell>");
//get input
fgets(buffer, 512, stdin);
//fork!
int pid = fork(); //Error checking to see if fork works
//If pid !=0 then it's the parent
if(pid!=0)
{
wait(NULL);
}
else
{
//if pid = 0 then we're at teh child
//Count the number of arguments
int num_of_args = countArgs(buffer);
//create an array of pointers for the arguments to be
//passed to execcv.
char *arguments[num_of_args+1];
//parse the input and arguments will have all the arguments
// to be passed to the program
parse(buffer, num_of_args, arguments);
arguments[num_of_args+1] = NULL;
//This will be the final path to the program that we will pass to execv
char prog[512];
//First we copy a /bin/ to prog
strcpy(prog, path);
//Then we concancate the program name to /bin/
//If the program name is ls, then it'll be /bin/ls
strcat(prog, arguments);
//pass the prepared arguments to execv and we're done!
int rv = execv(prog, arguments);
}
}
return 0;
}
int countArgs(字符n_args[],…){
va_列表ap;
int i,t;
va_启动(ap,n_参数);
对于(i=0;t=va_arg(ap,int);i++){return t;}
va_端(ap);
}
char*parse(char buffer[],int num_of_args,char*arguments[]))
{
参数[num_of_args+1]=NULL;
int i;
对于(i=0;i的第二个参数的类型为const char*
,但您试图传递一个字符指针数组
如果要将所有参数
数组添加到字符串中,则应在数组中循环,每次使用单个数组项调用strcat
for (i=0; i<num_of_args; i++) {
strcat(prog, arguments[i]);
}
他忘了提到它是什么错误。代码本身就是错误?(下面的错误后面跟着代码)这就是错误,因此cld u告诉我如何修复它以及在哪里查找??警告:从不兼容的指针类型/usr/include/string传递'strcat'的参数2。h:136:14:注意:应为'const char*restrict',但继电器的参数类型为'char**'tnx,但我没有e for loop,我收到了这个错误消息--警告:从不兼容的指针类型返回。您确定这不是另一个错误吗。您的解析函数也是错误的-它尝试返回char*数组而不是char*。既然您在这里不使用返回类型,为什么不直接返回void呢?
strcat(prog, arguments[index]);