Question

I'm trying to implement a simple shell. The piping is not working, in that, the second execve (in case you're wondering, I MUST use execve, NOT execvp), states the command is not found or it just fails and exits. This is my pipe code.

switch(fork()) {
case -1:
        fatal("1st Fork Failed In Pipe!");
case 0:
        break;
default:
        wait(&status);
        return(status);
}

if(pipe(pipe_line) == -1) {
    fatal("Pipe Failed!");
}

switch(fork()) {
case -1:
        fatal("2nd Fork Failed In Pipe!");
case 0:
        dup2(pipe_line[1], 1);
        close(pipe_line[0]);
        close(pipe_line[1]);
        execv(cmd[0].name, cmd[0].argv);
        fatal("1st Execv Failed!");
default:
        dup2(pipe_line[0], 0);
        close(pipe_line[0]);
        close(pipe_line[1]);
        execv(cmd[1].name, cmd[1].argv);
        fatal("2nd Execv Failed!");
}

fflush(stdout);

NOTE: 1. I've tested to make sure cmd[1].name is correct ... such that it's the right directory. And that cmd[1].argv is not null...and actually contains something. I've tried everything...but I can't figure out why it's not working.

This is the result:

ls | wc
wc: invalid zero-length file name
wc: ���|")�:ʊЪ8�m���1��a�������K�!���*"ks8���$: No such file or directory
wc: �]S�: No such file or directory
wc: invalid zero-length file name
wc: &�be: No such file or directory
wc: ����: No such file or directory
wc: T: No such file or directory
0 0 0 total

EDIT!!! I just realized that for some reason, it never goes into the case 0. Fork doesn't fail..but it just never goes into case 0. Why could that be?

Your help is greatly appreciated!

Was it helpful?

Solution

It really looks like your name and argv data are corrupted. Consider running a simple test: replace execve calls with printf to print the command and the argv for each child (separate them by calling sleep() in one of the children).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top