Introduce old redir program
[lcapit-junk-code.git] / books / apue / buf-files.c
blob4c0a56efd1d1e577cd56d861e213b6b0b5f54d8d
1 /*
2 * Print buffering for various standard I/O streams
3 */
5 #include <stdio.h>
6 #include <stdlib.h>
8 void pr_stdio(const char *name, FILE *fp)
10 printf("stream = %s, ", name);
12 if (fp->_flags & _IONBF)
13 printf("unbuffered");
14 else if (fp->_flags & _IOLBF)
15 printf("line buffered");
16 else
17 printf("fully buffered");
19 printf("\n");
22 int main(void)
24 FILE *fp;
26 fputs("enter any character\n", stdout);
27 if (getchar() == EOF) {
28 perror("getchar()");
29 exit(1);
31 fputs("one line to standard error\n", stderr);
33 pr_stdio("stdin", stdin);
34 pr_stdio("stdout", stdout);
35 pr_stdio("stderr", stderr);
37 if ((fp = fopen("/etc/passwd", "r")) == NULL) {
38 fprintf(stderr, "fopen() error");
39 exit(1);
41 if (getc(fp) == EOF) {
42 fprintf(stderr, "getc() error");
43 exit(1);
46 pr_stdio("/etc/passwd", fp);
47 fclose(fp);
49 return 0;