#include #include #include #include #include #include #include #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; }