I can barely understand the man page for pipe, so I kinda need help understanding how to take a piped input in an external executable.
I have 2 programs: main.o & log.o
I written main.o to fork. Here is what it is doing:
- Parent fork will pipe data to the child
- Child fork will exec log.o
I need the child fork for main to pipe to STDIN of log.o
log.o simply takes STDIN & logs with time stamp to a file.
My code is composed of some code from various StackOverflow pages I dont remember & the man page for pipe:
printf("\n> "); while(fgets(input, MAXINPUTLINE, stdin)){ char buf; int fd[2], num, status; if(pipe(fd)){ perror("Pipe broke, dood"); return 111; } switch(fork()){ case -1: perror("Fork is sad fais"); return 111; case 0: // Child close(fd[1]); // Close unused write end while (read(fd[0], &buf, 1) > 0) write(STDOUT_FILENO, &buf, 1); write(STDOUT_FILENO, "\n", 1); close(fd[0]); execlp("./log", "log", "log.txt", 0); // This is where I am confused _exit(EXIT_FAILURE); default: // Parent data=stuff_happens_here(); close(fd[0]); // Close unused read end write(fd[1], data, strlen(data)); close(fd[1]); // Reader will see EOF wait(NULL); // Wait for child } printf("\n> "); }