Weird behavior of fork() and execvp() in C

Posted by ron on Stack Overflow See other posts from Stack Overflow or by ron
Published on 2012-05-19T20:20:30Z Indexed on 2012/09/15 3:38 UTC
Read the original article Hit count: 164

Filed under:
|
|
|

After some remarks from my previous post , I made the following modifications :

int main()
{
char errorStr[BUFF3];
 while (1)
 {

     int i , errorFile;

     char *line = malloc(BUFFER);
     char *origLine = line;
     fgets(line, 128, stdin);   // get a line from stdin

     // get complete diagnostics on the given string
     lineData info = runDiagnostics(line);

     char command[20];
     sscanf(line, "%20s ", command);
     line = strchr(line, ' ');          // here I remove the command from the line , the command is stored in "commmand" above

     printf("The Command is: %s\n", command);

     int currentCount = 0;                  // number of elements in the line
     int *argumentsCount = &currentCount;   // pointer to that

     // get the elements separated
     char** arguments = separateLineGetElements(line,argumentsCount);

     printf("\nOutput after separating the given line from the user\n");
     for (i = 0; i < *argumentsCount; i++) {
         printf("Argument %i is: %s\n", i, arguments[i]);
     }

     // here we call a method that would execute the commands
     pid_t pid ;

     if (-1 == (pid = fork()))
     {
        sprintf(errorStr,"fork: %s\n",strerror(errno));
        write(errorFile,errorStr,strlen(errorStr + 1));
        perror("fork");
        exit(1);
     }
     else if (pid == 0)  // fork was successful
     {
        printf("\nIn son process\n");

        // if (execvp(arguments[0],arguments)  < 0)       // for the moment I ignore this line
        if (execvp(command,arguments)  < 0)       // execute the command
        {
            perror("execvp");
            printf("ERROR: execvp failed\n");
            exit(1);
        }
     }
     else // parent
     {
        int status = 0;
        pid = wait(&status);
        printf("Process %d returned with status %d.", pid, status);
     }

     // print each element of the line
     for (i = 0; i < *argumentsCount; i++) {
         printf("Argument %i is: %s\n", i, arguments[i]);
     }

     // free all the elements from the memory
     for (i = 0; i < *argumentsCount; i++) {
         free(arguments[i]);
     }

     free(arguments);
     free(origLine);
 }

 return 0;
}

When I enter in the Console : ls > out.txt

I get :

The Command is: ls
execvp: No such file or directory
In son process
ERROR: execvp failed
Process 4047 returned with status 256.Argument 0 is: >
Argument 1 is: out.txt

So I guess that the son process is active , but from some reason the execvp fails .

Why ?

Regards

REMARK :

The ls command is just an example . I need to make this works with any given command .

EDIT 1 :

User input : ls > qq.out Program output :

The Command is: ls

Output after separating the given line from the user
Argument 0 is: >
Argument 1 is: qq.out

In son process
>: cannot access qq.out: No such file or directory
Process 4885 returned with status 512.Argument 0 is: >
Argument 1 is: qq.out

© Stack Overflow or respective owner

Related posts about c

    Related posts about shell