C、execve和参数中的UNIX简单shell […]预处理器指令 void read_命令() { int i;//存储在参数[]中的数组的索引 char*cp;//指向命令[] const char*hash=“”;//计算以空格分隔的字符串 memset(命令,01100);//清除数组的内存 参数[0]=“/bn/”;//初始化路径 //获取用户输入并检查输入是否发生 if(fgets(command,sizeof(command),stdin)==NULL) { printf(“退出!\n”); 出口(0); } //拆分命令并查找并将每个字符串存储在参数[]中 cp=strtok(命令“”;//获取初始字符串(命令) strcat(参数[0],cp);//在路径后追加命令 对于(i=1;i

C、execve和参数中的UNIX简单shell […]预处理器指令 void read_命令() { int i;//存储在参数[]中的数组的索引 char*cp;//指向命令[] const char*hash=“”;//计算以空格分隔的字符串 memset(命令,01100);//清除数组的内存 参数[0]=“/bn/”;//初始化路径 //获取用户输入并检查输入是否发生 if(fgets(command,sizeof(command),stdin)==NULL) { printf(“退出!\n”); 出口(0); } //拆分命令并查找并将每个字符串存储在参数[]中 cp=strtok(命令“”;//获取初始字符串(命令) strcat(参数[0],cp);//在路径后追加命令 对于(i=1;i,c,shell,unix,parameters,execve,C,Shell,Unix,Parameters,Execve,代码的基本思想是由用户读取输入命令(在read_command()函数中完成)(例如:ls-l)。然后,我将输入字符串分成几个小字符串,并将它们存储在一个数组中。要点是将命令存储在参数[0](例如:ls)中,并将参数存储在参数[1、2、3等](例如:l)中。但是,我认为我没有正确地执行execve()函数。您的代码存在各种类型的问题,包括以下问题(Jonathan Leffler正确地指出了其中一些问题): “/bin/”拼写错误为“/bn/” 由于参数[0]指向strcat(参数[0],cp)

代码的基本思想是由用户读取输入命令(在
read_command()
函数中完成)(例如:
ls-l
)。然后,我将输入字符串分成几个小字符串,并将它们存储在一个数组中。要点是将命令存储在参数[0](例如:ls)中,并将参数存储在参数[1、2、3等](例如:l)中。但是,我认为我没有正确地执行
execve()
函数。

您的代码存在各种类型的问题,包括以下问题(Jonathan Leffler正确地指出了其中一些问题):

  • “/bin/”
    拼写错误为
    “/bn/”
  • 由于
    参数[0]
    指向
    strcat(参数[0],cp)中的字符串文字(
    “/bn/”
    您试图附加到此字符串文字,这是不正确的。您应该分配一个缓冲区来保存连接的字符串
  • 标记化代码无法正确处理
    命令中的尾随换行符
  • env
    应指向以NULL结尾的字符串数组

  • 一般来说,我认为您应该在将部分代码集成到更大的程序中之前,将重点放在正确地实现和测试部分代码上。如果您在尝试将结果传递给
    execve
    之前测试了
    read\u命令,您会注意到它不起作用。

    请澄清:您的问题是什么,为什么您认为execve()函数执行不正确?程序的输出是什么?声明的
    参数
    数组在哪里?如何申报?
    /bn/
    是您的目录名还是键入的
    /bin/
    ?如果是后者,您如何知道命令不在
    /usr/bin/
    中?
    cp=NULL中断后的code>应生成编译器警告。为什么将环境设置为空指针?这有点可疑——即使事实上没有错。POSIX要求“参数envp是指向以null结尾的字符串的字符指针数组。这些字符串应构成新工艺图像的环境。envp数组被空指针终止,'并且您的
    env
    不符合要求。
    [...] Preprocesser directives
    
    void read_command()
    {
        int i;                                //index to the arrays stored in parameter[]
        char *cp;                             //points to the command[]
        const char *hash = " ";               //figures out the strings seperated by spaces
        memset(command, 0, 100);              //Clear the memory for array
        parameter[0] = "/bn/";                //Initialize the path
    
        //Get the user input and check if an input did occur
        if(fgets(command, sizeof(command), stdin) == NULL)
        {
            printf("Exit!\n");
            exit(0);
        }
    
        //Split the command and look store each string in parameter[]
        cp = strtok(command, " ");            //Get the initial string (the command)
        strcat(parameter[0], cp);             //Append the command after the path
        for(i = 1; i < MAX_ARG; i++)
        {
            cp = strtok(NULL, " ");           //Check for each string in the array
            parameter[i] = cp;                //Store the result string in an indexed off array
            if(parameter[i]  == NULL)
            {
                break;
                cp = NULL;
            }
        }
        //Exit the shell when the input is "exit"
        if(strcmp(parameter[0], "exit") == 0)
        {
            printf("Exit!\n");
            exit(0);
        }
    
    }
    
    
    int main()
    {
    
        [...]
    
            read_command();
            env = NULL;                                 //There is no environment variable
    
                proc = fork();
                if(proc == -1)                              //Check if forked properly
                {
                    perror("Error");
                    exit(1);
                }
                if (proc == 0)                             //Child process
                {
                    execve(parameter[0], parameter, env);  //Execute the process
                }
                else                                       //Parent process
                {
                    waitpid(-1, &status, 0);               //Wait for the child to be done
                }
    
        [...]
    }