开发者

UNIX simple shell in C, execve and parameters

[...] 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 th开发者_高级运维e execve() function incorrectly.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜