1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| #include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
extern char **environ;
char *get_next_line(int fd);
int main()
{
int tube [2];
int pid;
char *line;
if ((pipe(tube) == -1) || ((pid = fork ()) == -1))
printf("error\n");
if (pid > 0)
{
wait(NULL);
close (0);
dup(tube[0]);
while (line = get_next_line(0))
printf("%s\n", line);
execl("/usr/bin/wc", "wc", "-l", NULL);
}
else
{
close (1);
dup(tube[1]);
execl("/bin/ls", "ls", "-la", NULL);
}
return (0);
} |