csci4061/notes/09-pipes-fifos-code/pager_demo.c
Michael Zhang 041f660ccd
f
2018-01-29 17:28:37 -06:00

51 lines
1.1 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Outputs contents of file and connects a pipe to a child process
// which runs the "less" pager program.
//
// Adapted from Stevens/Rago Figure 15.11 "Copy file to pager program
// using popen"
#include <stdio.h>
#include <limits.h>
#include <stdlib.h>
#include <sys/wait.h>
#define MAXLINE 1024
#define PAGER "less" // environment variable, or default
// #define PAGER "${PAGER:-more}" // environment variable, or default
int main(int argc, char *argv[]) {
char line[MAXLINE];
FILE *fpin, *fpout;
if (argc != 2){
printf("usage: a.out <file>\n");
exit(1);
}
if ((fpin = fopen(argv[1], "r")) == NULL){
perror("cant open input file");
exit(1);
}
if ((fpout = popen(PAGER, "w")) == NULL){
perror("popen error");
exit(1);
}
// copy argv[1] to pager
while (fgets(line, MAXLINE, fpin) != NULL) {
if (fputs(line, fpout) == EOF){
perror("fputs error to pipe");
exit(1);
}
}
if (ferror(fpin)){
perror("fgets error");
exit(1);
}
if (pclose(fpout) == -1){
perror("pclose error");
exit(1);
}
return 0;
}