-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuf.c
More file actions
121 lines (98 loc) · 1.72 KB
/
buf.c
File metadata and controls
121 lines (98 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include "apue.h"
void pr_stdio(const char *, FILE *);
int is_unbuffered(FILE *);
int is_linebuffered(FILE *);
int buffer_size(FILE *);
int
main(void)
{
FILE *fp;
fputs("enter any character\n", stdout);
if (getchar() == EOF)
err_sys("getchar error");
fputs("one line to standard error\n", stderr);
pr_stdio("stdin", stdin);
pr_stdio("stdout", stdout);
pr_stdio("stderr", stderr);
if ((fp = fopen("/etc/passwd", "r")) == NULL)
err_sys("fopen error");
if (getc(fp) == EOF)
err_sys("getc error");
pr_stdio("/etc/passwd", fp);
exit(0);
}
void
pr_stdio(const char *name, FILE *fp)
{
printf("stream = %s, ", name);
if (is_unbuffered(fp))
printf("unbuffered");
else if (is_linebuffered(fp))
printf("line buffered");
else /* if neither of above */
printf("fully buffered");
printf(", buffer size = %d\n", buffer_size(fp));
}
/*
* The following is nonportable.
*/
#if defined(_IO_UNBUFFERED)
int
is_unbuffered(FILE *fp)
{
return(fp->_flags & _IO_UNBUFFERED);
}
int
is_linebuffered(FILE *fp)
{
return(fp->_flags & _IO_LINE_BUF);
}
int
buffer_size(FILE *fp)
{
return(fp->_IO_buf_end - fp->_IO_buf_base);
}
#elif defined(__SNBF)
int
is_unbuffered(FILE *fp)
{
return(fp->_flags & __SNBF);
}
int
is_linebuffered(FILE *fp)
{
return(fp->_flags & __SLBF);
}
int
buffer_size(FILE *fp)
{
return(fp->_bf._size);
}
#elif defined(_IONBF)
#ifdef _LP64
#define _flag __pad[4]
#define _ptr __pad[1]
#define _base __pad[2]
#endif
int
is_unbuffered(FILE *fp)
{
return(fp->_flag & _IONBF);
}
int
is_linebuffered(FILE *fp)
{
return(fp->_flag & _IOLBF);
}
int
buffer_size(FILE *fp)
{
#ifdef _LP64
return(fp->_base - fp->_ptr);
#else
return(BUFSIZ); /* just a guess */
#endif
}
#else
#error unknown stdio implementation!
#endif