user3562406
user3562406

Reputation: 307

execvp() not letting me

I'm trying to implement a basic shell in C, but whenever my shell executes a command using execvp(), it skips out of the loop I want it to stay in. I suspect this is because I'm not too familiar with execvp.

Relevant code:

int main()
{
    int nCmd = 1;                   // Command number
    char *line;                     // Initial command line
    token *list;                    // Linked list of tokens
    CMD *cmd;                       // Parsed command
    int process (CMD *);

    for ( ; ; ) {

    printf ("(%d)$ ", nCmd);                // Prompt for command
    fflush (stdout);
    if ((line = getLine (stdin)) == NULL)   // Read line
        break;                              //   Break on end of file

    list = lex (line);
    // Lex line into tokens
    free (line);
    if (list == NULL) {
        continue;
    } else if (getenv ("DUMP_LIST")) {      // Dump token list only if
        dumpList (list);                    //   environment variable set
        printf ("\n");
    }

    cmd = parse (list);                     // Parsed command?
    freeList (list);
    if (cmd == NULL) {
        continue;
    } else if (getenv ("DUMP_TREE")) {      // Dump command tree only if
        dumpTree (cmd, 0);                  //   environment variable set
        printf ("\n");
    }

    process (cmd);                          // Execute command

    freeCMD (cmd);                          // Free associated storage
    nCmd++;                                 // Adjust prompt

    }

    return EXIT_SUCCESS;
}

Here are the relevant parts of "process":

int process (CMD *cmdList)
{
    if ((cmdList->nLocal)>0)
    {
        for (int i = 0; i<cmdList->nLocal; i++)
        {
            setenv(cmdList->locVar[i], cmdList->locVal[i], 0);
        }
    }
    if (cmdList->type==SIMPLE)
    {
        execvp(cmdList->argv[0],cmdList->argv);
    }
    return 0;
}

What is happening is that I get through the first process in main's for loop fine. However, instead of reading command line in like I want, after the command is executed, the program just ends. How do I get it to stay in the for loop?

Upvotes: 1

Views: 447

Answers (2)

randomusername
randomusername

Reputation: 8087

The family of functions of the form exec* are unique in that as soon as the program reaches them, the entire process memory is erased and rewritten with the process memory of the new program. Your code isn't jumping out of the loop, the loop doesn't exist any more.

If you want to mimic the way the shell works, you'll have to use another system call to create a separate process that can be overwritten: fork. Look it up for more information.

Upvotes: 1

R Sahu
R Sahu

Reputation: 206557

execvp replaces the current process with the process you are asking it to run.

You need to use fork to create a child process and run execvp from the child process. That way, the parent will be still alive and be able to process more user input.

Upvotes: 2

Related Questions