c语言中的命令行intepreter

c语言中的命令行intepreter,c,shell,warnings,C,Shell,Warnings,我想创建一个命令行解释器,每行有多个comamnd。 我将从keybord命令输入,如:pwd;ls;cat文件;ls-l 这是我写的,有人能帮我警告一下吗 警告如下: ex.c: In function ‘Execute’: ex.c:33:18: warning: passing argument 1 of ‘execvp’ from incompatible pointer type [-Wincompatible-pointer-types] if(execvp(cmd

我想创建一个命令行解释器,每行有多个comamnd。 我将从keybord命令输入,如:
pwd;ls;cat文件;ls-l

这是我写的,有人能帮我警告一下吗

警告如下:

ex.c: In function ‘Execute’:

ex.c:33:18: warning: passing argument 1 of ‘execvp’ from incompatible pointer type [-Wincompatible-pointer-types]
        if(execvp(cmd, pin) == -1) {

In file included from ex.c:3:0:

/usr/include/unistd.h:581:12: note: expected ‘const char *’ but argument is of type ‘char **’
 extern int execvp (const char *__file, char *const __argv[])


ex.c: In function ‘main’:

ex.c:105:17: warning: passing argument 1 of ‘Execute’ from incompatible pointer type [-Wincompatible-pointer-types]

         Execute(pin[0],pin);


ex.c:21:6: note: expected ‘char **’ but argument is of type ‘char *’
 void Execute(char* cmd[],char* pin[]) {
      ^~~~~~~
对不起,英语不好

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

#define MAX_SIZE 512

//Clean function
void Clean(char* cmd[],char* pin[]) {          //Clean Array

    int a;
    for(a=0; a < 40; a++){
            cmd[a] = NULL;
            pin[a] = NULL;
    }        
}

//execute commands 

void Execute(char* cmd[],char* pin[]) { 

    pid_t pid;
    pid = fork();    

    switch(pid) {
        case -1:  
          printf("DEBUG:Fork Failure\n");
          exit(-1);
        case  0:
          execvp(*cmd, pin);

          if(execvp(cmd, pin) == -1) {
                printf("Command Not Found\n");
                exit(0);
          }

        default:  
        wait(NULL);
        printf("DEBUG:Child Finished\n");    
    }
}

int main() {
    char* cnd;
    char* cmd[40];
    char* pin[40];
    char* array;
    int how_much;

    char *input = malloc (MAX_SIZE);

    if (input == NULL) {
        printf ("No memory\n");
        return 1;
    }

    Clean(cmd,pin);

    printf("shell> ");

    fgets (input, MAX_SIZE, stdin);

    if ((strlen(input)>0) && (input[strlen (input) - 1] == '\n')) {
        input[strlen (input) - 1] = '\0';
    }

    printf("INPUT: %s\n", input);

//searching for ";"

    cnd = strtok(input, ";");

    int i = 0;

    while(cnd != NULL) {    
        cmd[i] = cnd;
        i++;
        cnd = strtok(NULL, ";");
    }

    cmd[i] = NULL;
    how_much = i;

// searching for " "    

    for(int j=0; j < how_much; j++) {            
        array = strtok(input, " ");

        int k=0;

        while(array != NULL) {  
            pin[k] = array;
            k++;
            array = strtok(NULL, " ");
        }

        pin[k] = NULL;

        Execute(pin[0],pin);
    }

    free (input);

   return 1;
}
#包括
#包括
#包括
#包括
#包括
#包括
#定义最大尺寸512
//清洁功能
void Clean(char*cmd[],char*pin[]){//Clean数组
INTA;
对于(a=0;a<40;a++){
cmd[a]=NULL;
pin[a]=NULL;
}        
}
//执行命令
无效执行(char*cmd[],char*pin[]){
pid_t pid;
pid=fork();
开关(pid){
案例1:
printf(“调试:Fork失败\n”);
出口(-1);
案例0:
execvp(*cmd,pin);
if(execvp(cmd,pin)=-1){
printf(“未找到命令\n”);
出口(0);
}
违约:
等待(空);
printf(“调试:子项完成\n”);
}
}
int main(){
char*cnd;
char*cmd[40];
char*pin[40];
字符*数组;
int多少;
字符*输入=malloc(最大大小);
如果(输入==NULL){
printf(“无内存”);
返回1;
}
清洁(cmd、pin);
printf(“shell>”);
fgets(输入,最大尺寸,标准尺寸);
如果((strlen(input)>0)和&(input[strlen(input)-1]=='\n')){
输入[strlen(输入)-1]='\0';
}
printf(“输入:%s\n”,输入);
//搜索“;”
cnd=strtok(输入“;”);
int i=0;
而(cnd!=NULL){
cmd[i]=cnd;
i++;
cnd=strtok(空,“;”);
}
cmd[i]=NULL;
多少=我;
//正在搜索“”
对于(intj=0;j<多少;j++){
数组=strtok(输入“”);
int k=0;
while(数组!=NULL){
pin[k]=阵列;
k++;
数组=strtok(空,“”);
}
pin[k]=NULL;
执行(引脚[0],引脚);
}
免费(输入);
返回1;
}

执行
功能中:

...
case  0:
  execvp(*cmd, pin);

  if (execvp(cmd, pin) == -1) {
  ...
首先是
execvp(*cmd,pin)
,然后是
execvp(cmd,pin)
。 这是否表明你出了什么问题

在这里:

Execute(pin[0],pin);
在第一个参数中,
Execute
需要一个
char*
数组,但是您提供了一个
char*


可能还有更多的问题。

BTW
array=strtok(输入“”)可能是错误。我包括了我修改过的所有include和pinakas=pin(抱歉出错),这是我的真实代码警告非常清楚。我们没有辅导服务;也许你还需要更多的东西。。。基本的。。。而不是学习语言。一本好的C语言书也会有所帮助。