Annotation of 41BSD/libc/stdio/flsbuf.c, revision 1.1.1.1

1.1       root        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&_IOLBF) {
                     17:                base = iop->_base;
                     18:                *iop->_ptr++ = c;
                     19:                if (iop->_ptr >= base+BUFSIZ || c == '\n') {
                     20:                        n = write(fileno(iop), base, rn = iop->_ptr - base);
                     21:                        iop->_ptr = base;
                     22:                } else
                     23:                        rn = n = 0;
                     24:                iop->_cnt = 0;
                     25:        } else if (iop->_flag&_IONBF) {
                     26:                c1 = c;
                     27:                rn = 1;
                     28:                n = write(fileno(iop), &c1, rn);
                     29:                iop->_cnt = 0;
                     30:        } else {
                     31:                if ((base=iop->_base)==NULL) {
                     32:                        if (iop==stdout) {
                     33:                                if (isatty(fileno(stdout)))
                     34:                                        iop->_flag |= _IOLBF;
                     35:                                iop->_base = _sobuf;
                     36:                                iop->_ptr = _sobuf;
                     37:                                goto tryagain;
                     38:                        }
                     39:                        if ((iop->_base=base=malloc(BUFSIZ)) == NULL) {
                     40:                                iop->_flag |= _IONBF;
                     41:                                goto tryagain;
                     42:                        }
                     43:                        iop->_flag |= _IOMYBUF;
                     44:                        rn = n = 0;
                     45:                } else if ((rn = n = iop->_ptr - base) > 0) {
                     46:                        iop->_ptr = base;
                     47:                        n = write(fileno(iop), base, n);
                     48:                }
                     49:                iop->_cnt = BUFSIZ-1;
                     50:                *base++ = c;
                     51:                iop->_ptr = base;
                     52:        }
                     53:        if (rn != n) {
                     54:                iop->_flag |= _IOERR;
                     55:                return(EOF);
                     56:        }
                     57:        return(c);
                     58: }
                     59: 
                     60: fflush(iop)
                     61: register struct _iobuf *iop;
                     62: {
                     63:        register char *base;
                     64:        register n;
                     65: 
                     66:        if ((iop->_flag&(_IONBF|_IOWRT))==_IOWRT
                     67:         && (base=iop->_base)!=NULL && (n=iop->_ptr-base)>0) {
                     68:                iop->_ptr = base;
                     69:                iop->_cnt = (iop->_flag&(_IOLBF|_IONBF)) ? 0 : BUFSIZ;
                     70:                if (write(fileno(iop), base, n)!=n) {
                     71:                        iop->_flag |= _IOERR;
                     72:                        return(EOF);
                     73:                }
                     74:        }
                     75:        return(0);
                     76: }
                     77: 
                     78: /*
                     79:  * Flush buffers on exit
                     80:  */
                     81: 
                     82: _cleanup()
                     83: {
                     84:        register struct _iobuf *iop;
                     85:        extern struct _iobuf *_lastbuf;
                     86: 
                     87:        for (iop = _iob; iop < _lastbuf; iop++)
                     88:                fclose(iop);
                     89: }
                     90: 
                     91: fclose(iop)
                     92: register struct _iobuf *iop;
                     93: {
                     94:        register r;
                     95: 
                     96:        r = EOF;
                     97:        if (iop->_flag&(_IOREAD|_IOWRT) && (iop->_flag&_IOSTRG)==0) {
                     98:                r = fflush(iop);
                     99:                if (close(fileno(iop)) < 0)
                    100:                        r = EOF;
                    101:                if (iop->_flag&_IOMYBUF)
                    102:                        free(iop->_base);
                    103:                if (iop->_flag&(_IOMYBUF|_IONBF|_IOLBF))
                    104:                        iop->_base = NULL;
                    105:        }
                    106:        iop->_flag &= ~(_IOREAD|_IOWRT|_IOLBF|_IONBF|_IOMYBUF|_IOERR|_IOEOF|_IOSTRG);
                    107:        iop->_cnt = 0;
                    108:        return(r);
                    109: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.