Implementando shell en C y necesita ayuda para manejar la redirección de entrada / salida

La ronda 2

Después de leer algunas de las respuestas, mi código revisado es:

int pid = fork();

if (pid == -1) {
    perror("fork");
} else if (pid == 0) {   

    if (in) { //if '<' char was found in string inputted by user
        int fd0 = open(input, O_RDONLY, 0);
        dup2(fd0, STDIN_FILENO);
        close(fd0);
        in = 0;
    }

    if (out) { //if '>' was found in string inputted by user
        int fd1 = creat(output, 0644);
        dup2(fd1, STDOUT_FILENO);
        close(fd1);
        out = 0;
    }   

    execvp(res[0], res);
    perror("execvp");
    _exit(1);
} else {
    waitpid(pid, 0, 0);
    free(res);
}

Funciona, pero parece que la salida estándar no se está reconectando o algo así. Aquí está la ejecución:

SHELL$ cat > file
hello, world
this is a test
SHELL$ cat < file //no output
SHELL$ ls //no output

'<' y '>' ambos funcionan, pero después de ejecutarse no hay salida.

La ronda 1

He estado trabajando en un shell relativamente simple en C durante un tiempo, pero tengo problemas para implementar la redirección de entrada (<) y salida (>). Ayúdame a encontrar los problemas en el siguiente código:

int fd;
int pid = fork();
int current_out;

if (in) { //if '<' char was found in string inputted by user
    fd = open(input, O_RDONLY, 0);
    dup2(fd, STDIN_FILENO);
    in = 0;
    current_out = dup(0);
}

if (out) { //if '>' was found in string inputted by user
    fd = creat(output, 0644);
    dup2(fd, STDOUT_FILENO);
    out = 0;
    current_out = dup(1);
}

if (pid == -1) {
    perror("fork");
} else if (pid == 0) {       
    execvp(res[0], res);
    perror("execvp");
    _exit(1);
} else {
    waitpid(pid, 0, 0);
    dup2(current_out, 1);
    free(res);
}

Puede que tenga algo de material innecesario allí porque he estado intentando diferentes cosas para que funcione. No estoy seguro de qué está mal.

Respuestas a la pregunta(3)

Su respuesta a la pregunta