制作一个模拟UNIX shell

时间:2015-04-10 22:10:16

标签: c shell unix

您好我正在尝试使用execv和fork创建一个模拟shell,但我在我所做的功能中遇到了几个问题。几乎该程序应该像shell一样运行,允许前台和后台的简单命令。 所以现在我修复了大部分提到的错误,现在我在ptr [args] = NULL上留下了两个错误;其中它表示赋值从指针生成整数而没有强制转换,第二个错误是strcat,它表示无效的参数类型' unary *'(有' int')请帮助我不知道如何解决它们。

int argCount(char *numofArgs){
        int count = 0, index = 0;

        for(count; numofArgs[index] != '\0'; index++){
                if(numofArgs[index] == ' ' || (numofArgs[index] ==  ' ' && numofArgs[index+1] == ' ')){
                        count++;
                }
        }
        count++;
}

 void parse(char *str, int numOfArgs, char* args){
        char *token1;
        const char s[1] = " ";
        //gets first token
        token1 =  strtok(str, s);
        while(token1 != NULL){
                token1 = strtok(NULL, s);
        }
}

int main(int argc, char **argv[]){
        //holds commands
        char hcomm[512];
        char *path = "/bin", *ptrarr;
        char dummy[512];
        int args, exe;
        while(1){
                printf("dummy&gt");
                fgets(hcomm, 512, stdin);
                int pid = fork();
                        if(pid != 0){
                                wait(NULL);
                        }
                        else{
                                //child if pid = 0
                                //args = counts number of args
                                args = argCount(hcomm);
                                //array of pointers for args to passed into
                                ptrarr[args + 1];

                                //need to parse the input and arguments
                                parse(hcomm, args, ptrarr);
                                //set last pointer to NULL
                                ptrarr[args] = NULL;
                                strcpy(dummy, path);
                                //concat dummy to /bin/
                                strcat(dummy, *ptrarr[0]);
                                //pass to exe
                                exe = execv(dummy, ptrarr);
                        }
        }
        return 0;
}

2 个答案:

答案 0 :(得分:1)

那里有很多警告/错误,我不太愿意解决所有问题。这是因为即使只是一个错误也说明了对正在发生的事情的深刻误解。所以我要谈谈第一个警告:

  

dummyshell.c:10:警告:传递'strcmp'的参数1使得指针来自整数

我认为这是指:

strcmp(numofArgs[index], " ") = 0

首先,strcmp需要一个字符串。我的意思是指向一组字符的指针。传递它numofArgs[index]会导致它将该值视为指针。然后它将尝试处理该指针指向的字符串。我非常怀疑该内存地址实际上包含一个字符串。例如,如果numofArgs[index]恰好包含字符'A',则会转换为内存地址0x00000041(因为'A'的ASCII值为0x41)。如果巧合地址包含一个字符串,那么你就是安全的。但即便如此,我也不认为它会做你认为它应该做的事情。

其次,strcmp处理字符串,直到找到NUL终止符(0x00)。因此,如果输入中的字符'A'碰巧指向恰好是空格字符的内存区域,则它仍可能无法与您提供的空格字符正确比较。例如,输入字符为A。如上所述,这意味着您将位于0x00000041的字符串传递给strcmp。比如说该位置的字符串碰巧是" ok"(空格,o,k)。在这种情况下,strcmp仍然不会返回0,因为它将" ok"" "进行比较。

简而言之,strcmp用于比较字符串(NUL终止的char数组)。它不是用于比较字符。

你可能的意思是:

if(numofArgs[index] == ' ' || (numofArgs[index] == ' ' && numofArgs[index+1] == ' ')){
    count++;
}

这种逻辑仍然非常混乱,可以改进,但至少它是正确的。

答案 1 :(得分:0)

  • 请勿使用赋值运算符=代替测试相等==

  • 您将多个变量(numofArgsptrarr)声明为const char*char*。如果你想让每个人指向一个字符串,这很好。如果你想拥有一个字符串数组(看起来就像你的意思),请使用const char*[32](初始化时)或const char**(在参数列表中)。

  • 您的部分陈述无效:ptrarr[args + 1];