i am trying to use pipes/forks to imitate the system command in bash shell, using C programming
so far i have
The output is sorta correct, i gave the input command to be ls, the output wasCode:int sys_new(char *comm) { char pipeout[512]; int fd[2], status; pipe(fd); pid_t pid = fork(); if (pid == 0) { dup2(fd[1], fileno(stdout)); close(fd[0]); close(fd[1]); execl("/bin/sh", "/bin/sh", "-c", comm, NULL); } else if (pid > 0) { wait(&status); dup2(fd[0], fileno(stdin)); read(fd[0], pipeout, 512); close(fd[0]); close(fd[1]); printf(pipeout); printf("\n"); printf("%d\n", WEXITSTATUS(status)); } else { perror("fork"); exit(1); } return 0; }
my_prog.c
my_prog
abc
ñà·
EÉ÷·8É÷·*à·
0
The zero come up since ls was successfull, but i dont understand why the files in the directory came up line by line and not all in one line like how normally doing ls would result in, also theres some gibberish lines after the ls command results, why did that show up? Am I missing a \0 anywhere or something?
Also i tried giving a command that would fail like cd ffg, and the WEXITSTATUS number came out to be 2, so what really are the possible outcomes for WEXITSTATUS? i only know so far 0 and 2, shouldnt it be -1 instead of 2?



LinkBack URL
About LinkBacks


