Annotation of 43BSDTahoe/ucb/Mail/flsbuf.c, revision 1.1.1.1

1.1       root        1: /*
                      2:  * Copyright (c) 1980 Regents of the University of California.
                      3:  * All rights reserved.  The Berkeley software License Agreement
                      4:  * specifies the terms and conditions for redistribution.
                      5:  */
                      6: 
                      7: #if defined(LIBC_SCCS) && !defined(lint)
                      8: static char sccsid[] = "@(#)flsbuf.c   5.2 (Berkeley) 3/9/86";
                      9: #endif LIBC_SCCS and not lint
                     10: 
                     11: #include       <stdio.h>
                     12: #include       <sys/types.h>
                     13: #include       <sys/stat.h>
                     14: 
                     15: char   *malloc();
                     16: 
                     17: _flsbuf(c, iop)
                     18: unsigned char c;
                     19: register FILE *iop;
                     20: {
                     21:        register char *base;
                     22:        register n, rn;
                     23:        char c1;
                     24:        int size;
                     25:        struct stat stbuf;
                     26: 
                     27:        if (iop->_flag & _IORW) {
                     28:                iop->_flag |= _IOWRT;
                     29:                iop->_flag &= ~(_IOEOF|_IOREAD);
                     30:        }
                     31: 
                     32:        if ((iop->_flag&_IOWRT)==0)
                     33:                return(EOF);
                     34: tryagain:
                     35:        if (iop->_flag&_IOLBF) {
                     36:                base = iop->_base;
                     37:                *iop->_ptr++ = c;
                     38:                if ((rn = iop->_ptr - base) >= iop->_bufsiz || c == '\n') {
                     39:                        iop->_ptr = base;
                     40:                        n = write(fileno(iop), base, rn);
                     41:                        iop->_cnt = 0;
                     42:                } else {
                     43:                        /* we got here because _cnt is wrong, so fix it */
                     44:                        iop->_cnt = -rn;
                     45:                        rn = n = 0;
                     46:                }
                     47:        } else if (iop->_flag&_IONBF) {
                     48:                c1 = c;
                     49:                rn = 1;
                     50:                n = write(fileno(iop), &c1, rn);
                     51:                iop->_cnt = 0;
                     52:        } else {
                     53:                if ((base=iop->_base)==NULL) {
                     54:                        if (fstat(fileno(iop), &stbuf) < 0 ||
                     55:                            stbuf.st_blksize <= NULL)
                     56:                                size = BUFSIZ;
                     57:                        else
                     58:                                size = stbuf.st_blksize;
                     59:                        if ((iop->_base=base=malloc(size)) == NULL) {
                     60:                                iop->_flag |= _IONBF;
                     61:                                goto tryagain;
                     62:                        }
                     63:                        iop->_flag |= _IOMYBUF;
                     64:                        iop->_bufsiz = size;
                     65:                        if (iop==stdout && isatty(fileno(stdout))) {
                     66:                                iop->_flag |= _IOLBF;
                     67:                                iop->_ptr = base;
                     68:                                goto tryagain;
                     69:                        }
                     70:                        rn = n = 0;
                     71:                } else if ((rn = n = iop->_ptr - base) > 0) {
                     72:                        iop->_ptr = base;
                     73:                        n = write(fileno(iop), base, n);
                     74:                }
                     75:                iop->_cnt = iop->_bufsiz-1;
                     76:                *base++ = c;
                     77:                iop->_ptr = base;
                     78:        }
                     79:        if (rn != n) {
                     80:                iop->_flag |= _IOERR;
                     81:                return(EOF);
                     82:        }
                     83:        return(c);
                     84: }
                     85: 
                     86: fflush(iop)
                     87: register FILE *iop;
                     88: {
                     89:        register char *base;
                     90:        register n;
                     91: 
                     92:        if ((iop->_flag&(_IONBF|_IOWRT))==_IOWRT
                     93:         && (base=iop->_base)!=NULL && (n=iop->_ptr-base)>0) {
                     94:                iop->_ptr = base;
                     95:                iop->_cnt = (iop->_flag&(_IOLBF|_IONBF)) ? 0 : iop->_bufsiz;
                     96:                if (write(fileno(iop), base, n)!=n) {
                     97:                        iop->_flag |= _IOERR;
                     98:                        return(EOF);
                     99:                }
                    100:        }
                    101:        return(0);
                    102: }
                    103: 
                    104: fclose(iop)
                    105:        register FILE *iop;
                    106: {
                    107:        register int r;
                    108: 
                    109:        r = EOF;
                    110:        if (iop->_flag&(_IOREAD|_IOWRT|_IORW) && (iop->_flag&_IOSTRG)==0) {
                    111:                r = fflush(iop);
                    112:                if (close(fileno(iop)) < 0)
                    113:                        r = EOF;
                    114:                if (iop->_flag&_IOMYBUF)
                    115:                        free(iop->_base);
                    116:        }
                    117:        iop->_cnt = 0;
                    118:        iop->_base = (char *)NULL;
                    119:        iop->_ptr = (char *)NULL;
                    120:        iop->_bufsiz = 0;
                    121:        iop->_flag = 0;
                    122:        iop->_file = 0;
                    123:        return(r);
                    124: }

unix.superglobalmegacorp.com

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