【问题标题】:UNIX simple shell in C, execve and parametersC、execve 和参数中的 UNIX 简单 shell
【发布时间】:2011-10-12 02:46:39
【问题描述】:
[...] 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
            }

    [...]
}

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

【问题讨论】:

  • 请澄清:您的问题是什么,为什么您认为您执行 execve() 函数不正确?程序的输出是什么?
  • parameter 数组声明在哪里?它是如何声明的? /bn/ 是您的目录名称还是 /bin/ 的拼写错误?如果是后者,你怎么知道命令不在/usr/bin/ 中呢? break; 之后的 cp = NULL; 应该会生成编译器警告。
  • 为什么要将环境设置为空指针?这有点可疑——即使实际上并没有错。 POSIX 要求 ' 参数 envp 是指向以空字符结尾的字符串的字符指针数组。这些字符串应构成新过程映像的环境。 envp 数组以空指针终止。' 而你的env 不符合要求。

标签: c shell unix parameters execve


【解决方案1】:

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

  1. "/bin/" 拼写错误为 "/bn/"
  2. 由于parameter[0] 指向strcat(parameter[0], cp); 中的字符串文字("/bn/"),因此您尝试附加到此字符串文字,但这是不正确的。您应该分配一个缓冲区来保存连接的字符串。
  3. 您的标记化代码无法正确处理 command 中的尾随换行符。
  4. env 应该指向一个以 NULL 结尾的字符串数组。

总的来说,我认为在将代码集成到更大的程序中之前,您应该专注于正确地实现和测试部分代码。如果您在尝试将结果传递给execve 之前测试了read_command,您会注意到它不起作用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-12
    • 1970-01-01
    • 2023-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-14
    相关资源
    最近更新 更多