C 语言的 UNIX 简单 shell、execve 和参数

发布于 2024-12-09 12:07:13 字数 2216 浏览 1 评论 0原文

[...] 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() 函数。

[...] 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
            }

    [...]
}

The basic idea of the code is to read the input command by the user (done in the read_command() function) (ex: ls -l). Then I divide the input string in little strings and store them in an array. The point is to store the command in parameter[0] (ex: ls) and the parameters in parameter[1,2,3 etc.] (ex: -l). However, I think I executing the execve() function incorrectly.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

街道布景 2024-12-16 12:07:13

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

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

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

There are all types of issues with your code including the following (some of them are correctly pointed out by Jonathan Leffler):

  1. "/bin/" is misspelled as "/bn/"
  2. Since parameter[0] points to a string literal ("/bn/") in strcat(parameter[0], cp); you are trying to append to this string literal which is incorrect. You should allocate a buffer to hold the concatenated string instead.
  3. Your tokenizing code doesn't handle the trailing newline in command properly.
  4. env should point to a NULL-terminated array of strings.

In general, I think you should focus on implementing and testing parts of your code properly before integrating them in a larger program. If you tested the read_command before trying to pass its results to execve, you would notice that it doesn't work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文