pager.c
public c v1 · immutable /* get the $PAGER env variable */ pager = get_pager(); /* first we create a pipe this will pass data between two processes */ if (pipe(file_pipes) != 0) { perror("pipe error"); exit(EXIT_FAILURE); } /* next the new process is created with fork() */ fork_result = fork(); /* check if fork() works */ if (fork_result == (pid_t)-1) { perror("fork error"); exit(EXIT_FAILURE); } /* now we have both parent and child processes */ switch (fork_result) { case -1: perror("fork failed"); exit(1); case 0: /* we are in the childs thread of execution */ /* set stdin to a copy of the readable fd of the pipe */ /* close the pipes read and write fd's because we made a copy */ /* call the external program */ break; default: /* we are in the parents thread of execution */ /* open the file for reading */ if ((file_ptr = gzopen(filename, "r")) == NULL) { perror("Cannot open file for reading"); return 1; } /* read the file line by line or until EOF */ while ((gzeof(file_ptr)) != 1) { /* exit loop if noting to read */ if ((gzgets(file_ptr, gzbuffer, 256)) == Z_NULL) { break; } /* send all the data to the child */ data_processed = write(file_pipes[1], gzbuffer, strlen(gzbuffer)); if (data_processed == -1) { perror("write error"); exit(EXIT_FAILURE); } /* close the file */ /* wait for the child to finish */ /* continue with the main program */ break; } getch(); /* any key press returns you to the menu */ gzclose(file_ptr); return 0;