0

I'm working on a shell program for c and tried figuring out why it kept breaking out of the loop after prompting the user for a response. It runs the command correctly, however it breaks out for some reason. I couldn't figure out why and I think it has to do something with the way I'm doing the pipes.

This is what I have for an example, it's supposed to run the piped command, and ask the user to continue to run the command again and again until the user enters something other than "yes". Could it be the execvp that's causing the break? How could I have it so it continues the with the loop? Edit with forking update.

#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <fcntl.h>


int main()
{
    char str[3];
    do{
        char* leftSide[] = {"ls", NULL};
        char* rightSide[] = {"wc", NULL};

        pid_t id, id2;
        int pipe_fd[2];

        pipe(pipe_fd);
        id = fork();

        if(id == 0){
            dup2(pipe_fd[0],0);
            close(pipe_fd[1]);
            close(pipe_fd[0]);
            if(execvp(rightSide[0], rightSide) == -1){
                perror("error running pipe right command");
            }
        }
        else{
            id2 = fork();
            if(id2 == 0){
                dup2(pipe_fd[1],1);
                close(pipe_fd[1]);
                close(pipe_fd[0]);
                if(execvp(leftSide[0],leftSide) == -1){
                    perror("error running pipe left command");
                }
            }
            else{
                wait(NULL); 
                wait(NULL); 
            }
        }

        printf("Continue?");
        fgets(str, 3, stdin);
        str[3] = '\0';
    }while(strcmp(str, "yes") == 0);

    return 0;
}

2 Answers 2

2

You are terminating your program by

    if(execvp(leftSide[0],leftSide) == -1){

You have to fork() twice; once for rightSide and once for leftSide.

1
  • I added a fork to the parent and had it wait twice, but it still exits. Did I write it correctly? I played around with the waits, waiting once, twice and even removed both waits. Mar 21, 2019 at 23:01
1

There are two problems here:

  1. As @ensc pointed out, in his answer, your program terminates when you call execvp. You would have to make two children, the parent would stay your program, asking the user for more input while the children would execute leftside and rightside.

  2. The second problem is with fgets

According to the man page:

fgets() reads in at most one less than size characters from stream and stores them into the buffer pointed to by s. Reading stops after an EOF or a newline. If a newline is read, it is stored into the buffer.

So, the string input from the user would be "yes\n" rather than "yes" and strcmp would always fail.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.