Hi, I'm trying to implement the 3 stage pipe 'who | cut -d" " -f 1 | sort -u' as a C program (basically i want returned a list of users logged into the system but i want to use pipes.
I've worked out how to implement a two stage pipe (i think but it may be wrong)...
Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(void)
{
int pfds[2];
pipe(pfds);
if (!fork()) {
close(1); /* close normal stdout */
dup(pfds[1]); /* make stdout same as pfds[1] */
close(pfds[0]); /* we don't need this */
execlp("who, "who", NULL);
} else {
close(0); /* close normal stdin */
dup(pfds[0]); /* make stdin same as pfds[0] */
close(pfds[1]); /* we don't need this */
execlp(cut", "cut", "-d", "-f 1");
}
return 0;
}
Can someone tell me how I'd convert that to a three stage? And if what i've done is right? thanks