|
|
1.1 root 1: /* Copyright (c) 1979 Regents of the University of California */
2: #include <stdio.h>
3:
4: _flsbuf(c, iop)
5: register struct _iobuf *iop;
6: {
7: register n;
8: register char *base;
9: char c1, *malloc();
10: extern char _sobuf[];
11:
12: if ((iop->_flag&_IOWRT)==0)
13: _error("writing\n");
14: tryagain:
15: if (iop->_flag&_IONBF) {
16: c1 = c;
17: n = write(fileno(iop), &c1, 1);
18: iop->_cnt = 0;
19: } else {
20: if ((base=iop->_base)==NULL) {
21: if (iop==stdout) {
22: if (isatty(fileno(stdout)) &&
23: ! iop->_flag&_IOREAD) {
24: iop->_flag |= _IONBF;
25: goto tryagain;
26: }
27: iop->_base = _sobuf;
28: iop->_ptr = _sobuf;
29: goto tryagain;
30: }
31: if ((iop->_base=base=malloc(BUFSIZ)) == NULL) {
32: iop->_flag |= _IONBF;
33: goto tryagain;
34: }
35: iop->_flag |= _IOMYBUF;
36: n = 1;
37: } else if ((n = iop->_ptr - base) > 0) {
38: if (iop->_delta && (iop->_flag&_IOREAD)) {
39: if(lseek(iop->_file, (long) -iop->_delta, 1)<0)
40: _error("Seek error in flsbuf\n");
41: iop->_delta = 0;
42: }
43: n = write(fileno(iop), base, n);
44: if ((iop->_flag & _IOREAD) &&
45: (0 >(iop->_delta = read(iop->_file, base, BUFSIZ)))) {
46: iop->_delta = 0;
47: iop->_flag |= _IOERR;
48: }
49: }
50: iop->_cnt = BUFSIZ-1;
51: *base++ = c;
52: iop->_ptr = base;
53: }
54: if (n < 0) {
55: iop->_flag |= _IOERR;
56: return(-1);
57: }
58: return(c);
59: }
60:
61: fflush(iop)
62: register struct _iobuf *iop;
63: {
64: register char *base;
65: register n;
66:
67: if ((iop->_flag&(_IONBF|_IOWRT))==_IOWRT
68: && (base=iop->_base)!=NULL && ((n=iop->_ptr-base)>0)
69: && (iop->_flag & _IODIRT)) {
70: iop->_ptr = base;
71: iop->_cnt = 0;
72: if(iop->_delta) {
73: if(0>lseek(fileno(iop), (long) -iop->_delta, 1)) {
74: _error("Seek error in fflush\n");
75: iop->_flag |= _IOERR;
76: }
77: iop->_delta = 0;
78: }
79: if (write(fileno(iop), base, n)!=n)
80: iop->_flag |= _IOERR;
81: iop->_flag &= ~_IODIRT;
82: }
83: }
84:
85: /*
86: * Flush buffers on exit
87: */
88:
89: _cleanup()
90: {
91: register struct _iobuf *iop;
92: extern struct _iobuf *_lastbuf;
93:
94: for (iop = _iob; iop < _lastbuf; iop++)
95: fclose(iop);
96: }
97:
98: fclose(iop)
99: register struct _iobuf *iop;
100: {
101: if (iop->_flag&(_IOREAD|_IOWRT)) {
102: fflush(iop);
103: close(fileno(iop));
104: if (iop->_flag&_IOMYBUF)
105: free(iop->_base);
106: }
107: iop->_base = NULL;
108: iop->_flag &= ~(_IOREAD|_IOWRT|_IONBF|_IOMYBUF|_IOERR|_IOEOF|_IODIRT|_IOSTRG);
109: iop->_cnt = 0;
110: iop->_delta = 0;
111: return(0);
112: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.