Commit | Line | Data |
---|---|---|
18e5fa7e BJ |
1 | #include <stdio.h> |
2 | ||
3 | char *malloc(); | |
4 | ||
5 | _flsbuf(c, iop) | |
6 | register FILE *iop; | |
7 | { | |
8 | register char *base; | |
9 | register n, rn; | |
10 | char c1; | |
11 | extern char _sobuf[]; | |
12 | ||
13 | if ((iop->_flag&_IOWRT)==0) | |
14 | return(EOF); | |
15 | tryagain: | |
16 | if (iop->_flag&_IONBF) { | |
17 | c1 = c; | |
18 | rn = 1; | |
19 | n = write(fileno(iop), &c1, rn); | |
20 | iop->_cnt = 0; | |
21 | } else { | |
22 | if ((base=iop->_base)==NULL) { | |
23 | if (iop==stdout) { | |
24 | if (isatty(fileno(stdout))) { | |
25 | iop->_flag |= _IONBF; | |
26 | goto tryagain; | |
27 | } | |
28 | iop->_base = _sobuf; | |
29 | iop->_ptr = _sobuf; | |
30 | goto tryagain; | |
31 | } | |
32 | if ((iop->_base=base=malloc(BUFSIZ)) == NULL) { | |
33 | iop->_flag |= _IONBF; | |
34 | goto tryagain; | |
35 | } | |
36 | iop->_flag |= _IOMYBUF; | |
37 | rn = n = 0; | |
38 | } else if ((rn = n = iop->_ptr - base) > 0) { | |
39 | iop->_ptr = base; | |
40 | n = write(fileno(iop), base, n); | |
41 | } | |
42 | iop->_cnt = BUFSIZ-1; | |
43 | *base++ = c; | |
44 | iop->_ptr = base; | |
45 | } | |
46 | if (rn != n) { | |
47 | iop->_flag |= _IOERR; | |
48 | return(EOF); | |
49 | } | |
50 | return(c); | |
51 | } | |
52 | ||
53 | fflush(iop) | |
54 | register struct _iobuf *iop; | |
55 | { | |
56 | register char *base; | |
57 | register n; | |
58 | ||
59 | if ((iop->_flag&(_IONBF|_IOWRT))==_IOWRT | |
60 | && (base=iop->_base)!=NULL && (n=iop->_ptr-base)>0) { | |
61 | iop->_ptr = base; | |
62 | iop->_cnt = BUFSIZ; | |
63 | if (write(fileno(iop), base, n)!=n) { | |
64 | iop->_flag |= _IOERR; | |
65 | return(EOF); | |
66 | } | |
67 | } | |
68 | return(0); | |
69 | } | |
70 | ||
71 | /* | |
72 | * Flush buffers on exit | |
73 | */ | |
74 | ||
75 | _cleanup() | |
76 | { | |
77 | register struct _iobuf *iop; | |
78 | extern struct _iobuf *_lastbuf; | |
79 | ||
80 | for (iop = _iob; iop < _lastbuf; iop++) | |
81 | fclose(iop); | |
82 | } | |
83 | ||
84 | fclose(iop) | |
85 | register struct _iobuf *iop; | |
86 | { | |
87 | register r; | |
88 | ||
89 | r = EOF; | |
90 | if (iop->_flag&(_IOREAD|_IOWRT) && (iop->_flag&_IOSTRG)==0) { | |
91 | r = fflush(iop); | |
92 | if (close(fileno(iop)) < 0) | |
93 | r = EOF; | |
94 | if (iop->_flag&_IOMYBUF) | |
95 | free(iop->_base); | |
96 | if (iop->_flag&(_IOMYBUF|_IONBF)) | |
97 | iop->_base = NULL; | |
98 | } | |
99 | iop->_flag &= ~(_IOREAD|_IOWRT|_IONBF|_IOMYBUF|_IOERR|_IOEOF|_IOSTRG); | |
100 | iop->_cnt = 0; | |
101 | return(r); | |
102 | } |