post
poster: psYchotic
description: Piping: why not2?
language: C
[download]
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

#define IO_SIZE 4096

int main(int argc, char **argv) {
    int pipes[2];
    int pid;
    int file;
    int io;
    void *buf;
    int reverse = 0;
    char opt;

    while ((opt = getopt(argc, argv, "r")) != -1) {
        if (opt == 'r') reverse = 1;
        else {
            printf("Usage: %s [-f] FILENAME\n", argv[0]);
            return 0;
        }
    }

    if (pipe(pipes) == -1) {
        perror("Could not open pipes");
        return 1;
    }
    
    if ((pid = fork()) == -1) {
        perror("Could not fork");
        return 1;
    }

    if (pid == 0) {
        close(pipes[1]);
        if (dup2(pipes[0], fileno(stdin)) == -1) {
            perror("Could not set standard input of the child");
            return 1;
        }
        close(pipes[0]);
        if (reverse) {
            execlp("cut", "cut", "--complement", "-f1", (char *) NULL);
        } else {
            execlp("nl", "nl", "-ba", (char *) NULL);
        }
    } else {
        int status;
        buf = malloc(sizeof(char) * IO_SIZE);
        file = open(argv[1], O_RDONLY);
        while ((io = read(file, buf, IO_SIZE)) > 0) {
            if ((io = write(pipes[1], buf, io)) == -1) {
                perror("Couldn't write to pipe");
                break;
            }
        }

        close(file);
        free(buf);
        close(pipes[1]);
        do {
            waitpid(pid, &status, 0);
        } while (!WIFEXITED(status));
    }

    return 0;
}