R0drom
R0drom

Reputation: 51

Unix commands in C code

I need to implement ps -auxj, grep "user id", and wc. I already have word count, but I'm not sure how to do the others while they have parameters. This is what I have so far.

int main() {
int pfd[2];
int pid;

if (pipe(pfd) == -1) {
    perror("pipe failed");
    exit(-1);
}
if ((pid = fork()) < 0) {
    perror("fork failed");
    exit(-2);
}
if (pid == 0) {
    close(pfd[1]);
    dup2(pfd[0], 0);
    close(pfd[0]);
    execlp("wc", "wc", (char *) 0);
    perror("wc failed");
    exit(-3);
} 
else {
    close(pfd[0]);
    dup2(pfd[1], 1);
    close(pfd[1]);
    execlp("ls", "ls", (char *) 0);
    perror("ls failed");
    exit(-4);
}
exit(0);

}

Any help in the right direction would be great.

Upvotes: 0

Views: 110

Answers (1)

sraok
sraok

Reputation: 615

exec gives details about how to pass arguments to the exec family of functions. e.g.

execl("ls", "ls", "-l",(char *) 0);

you can choose from there whatever suits you.

Upvotes: 3

Related Questions