Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stddef.h>
- #include <unistd.h>
- static void readwrite_pipe(int, int);
- int main(void) {
- int fd1[2];
- int fd2[2];
- pipe(fd1);
- pipe(fd2);
- if (fork() == 0) {
- dup2(fd1[0], STDIN_FILENO);
- close(fd1[0]);
- close(fd1[1]);
- dup2(fd2[1], STDOUT_FILENO);
- close(fd2[0]);
- close(fd2[1]);
- execl("/usr/bin/uniq", "uniq", NULL);
- } else {
- close(fd1[0]);
- readwrite_pipe(STDIN_FILENO, fd1[1]);
- close(fd1[1]);
- dup2(fd2[0],STDIN_FILENO);
- close(fd2[0]);
- close(fd2[1]);
- execl("/usr/bin/wc", "wc", "-l", NULL);
- }
- }
- static void readwrite_pipe(int in, int out) {
- size_t line_max = sysconf(_SC_LINE_MAX);
- char *buf[line_max];
- for (ssize_t size = read(in, buf, line_max); size > 0; size = read(in, buf, line_max)) {
- write(out, buf, size);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement