Commit | Line | Data |
---|---|---|
63b1c981 | 1 | /* @(#)wbuf.c 4.7 (Berkeley) %G% */ |
62e7287b | 2 | #include <stdio.h> |
f4c06a32 KM |
3 | #include <sys/types.h> |
4 | #include <sys/stat.h> | |
62e7287b BJ |
5 | |
6 | char *malloc(); | |
7 | ||
8 | _flsbuf(c, iop) | |
9 | register FILE *iop; | |
10 | { | |
11 | register char *base; | |
12 | register n, rn; | |
13 | char c1; | |
f4c06a32 KM |
14 | int size; |
15 | struct stat stbuf; | |
62e7287b | 16 | |
d8af6b8b MT |
17 | if (iop->_flag & _IORW) { |
18 | iop->_flag |= _IOWRT; | |
ec6ddbc1 | 19 | iop->_flag &= ~(_IOEOF|_IOREAD); |
d8af6b8b MT |
20 | } |
21 | ||
62e7287b BJ |
22 | if ((iop->_flag&_IOWRT)==0) |
23 | return(EOF); | |
24 | tryagain: | |
25 | if (iop->_flag&_IOLBF) { | |
26 | base = iop->_base; | |
27 | *iop->_ptr++ = c; | |
f4c06a32 | 28 | if (iop->_ptr >= base+iop->_bufsiz || c == '\n') { |
62e7287b BJ |
29 | n = write(fileno(iop), base, rn = iop->_ptr - base); |
30 | iop->_ptr = base; | |
31 | } else | |
32 | rn = n = 0; | |
33 | iop->_cnt = 0; | |
34 | } else if (iop->_flag&_IONBF) { | |
35 | c1 = c; | |
36 | rn = 1; | |
37 | n = write(fileno(iop), &c1, rn); | |
38 | iop->_cnt = 0; | |
39 | } else { | |
40 | if ((base=iop->_base)==NULL) { | |
f4c06a32 KM |
41 | if (fstat(fileno(iop), &stbuf) < 0 || |
42 | stbuf.st_blksize <= NULL) | |
43 | size = BUFSIZ; | |
44 | else | |
45 | size = stbuf.st_blksize; | |
f4c06a32 | 46 | if ((iop->_base=base=malloc(size)) == NULL) { |
62e7287b BJ |
47 | iop->_flag |= _IONBF; |
48 | goto tryagain; | |
49 | } | |
50 | iop->_flag |= _IOMYBUF; | |
f4c06a32 | 51 | iop->_bufsiz = size; |
c528f54e RC |
52 | if (iop==stdout && isatty(fileno(stdout))) { |
53 | iop->_flag |= _IOLBF; | |
54 | iop->_ptr = base; | |
55 | goto tryagain; | |
56 | } | |
62e7287b BJ |
57 | rn = n = 0; |
58 | } else if ((rn = n = iop->_ptr - base) > 0) { | |
59 | iop->_ptr = base; | |
60 | n = write(fileno(iop), base, n); | |
61 | } | |
f4c06a32 | 62 | iop->_cnt = iop->_bufsiz-1; |
62e7287b BJ |
63 | *base++ = c; |
64 | iop->_ptr = base; | |
65 | } | |
66 | if (rn != n) { | |
67 | iop->_flag |= _IOERR; | |
68 | return(EOF); | |
69 | } | |
70 | return(c); | |
71 | } | |
72 | ||
73 | fflush(iop) | |
74 | register struct _iobuf *iop; | |
75 | { | |
76 | register char *base; | |
77 | register n; | |
78 | ||
79 | if ((iop->_flag&(_IONBF|_IOWRT))==_IOWRT | |
80 | && (base=iop->_base)!=NULL && (n=iop->_ptr-base)>0) { | |
81 | iop->_ptr = base; | |
f4c06a32 | 82 | iop->_cnt = (iop->_flag&(_IOLBF|_IONBF)) ? 0 : iop->_bufsiz; |
62e7287b BJ |
83 | if (write(fileno(iop), base, n)!=n) { |
84 | iop->_flag |= _IOERR; | |
85 | return(EOF); | |
86 | } | |
87 | } | |
88 | return(0); | |
89 | } | |
90 | ||
91 | /* | |
92 | * Flush buffers on exit | |
93 | */ | |
94 | ||
95 | _cleanup() | |
96 | { | |
97 | register struct _iobuf *iop; | |
98 | extern struct _iobuf *_lastbuf; | |
99 | ||
100 | for (iop = _iob; iop < _lastbuf; iop++) | |
101 | fclose(iop); | |
102 | } | |
103 | ||
104 | fclose(iop) | |
193b9a78 | 105 | register struct _iobuf *iop; |
62e7287b | 106 | { |
c13cf752 | 107 | register int r; |
62e7287b BJ |
108 | |
109 | r = EOF; | |
d8af6b8b | 110 | if (iop->_flag&(_IOREAD|_IOWRT|_IORW) && (iop->_flag&_IOSTRG)==0) { |
62e7287b BJ |
111 | r = fflush(iop); |
112 | if (close(fileno(iop)) < 0) | |
113 | r = EOF; | |
114 | if (iop->_flag&_IOMYBUF) | |
115 | free(iop->_base); | |
62e7287b | 116 | } |
62e7287b | 117 | iop->_cnt = 0; |
c13cf752 CC |
118 | iop->_base = (char *)NULL; |
119 | iop->_ptr = (char *)NULL; | |
120 | iop->_bufsiz = 0; | |
121 | iop->_flag = 0; | |
122 | iop->_file = 0; | |
62e7287b BJ |
123 | return(r); |
124 | } |