chet@cwns1.CWRU.EDU (Chet Ramey) (11/10/89)
In article <8911080748.AA14217@skinfaxe.diku.dk> seindal@DIKU.DK (Rene' Seindal) writes: >Bash 1.04 doesn't compile on 4.3BSD (and 4.2BSD), because these systems >doesn't have setvbuf(3), but use setlinebuf(3) as a partial substitute. On the other hand... /* * An ANSI-compatible setvbuf() for those sites that need it (usually * 4.2BSD-derived systems, up to and including 4.3-tahoe). * * Bugs to chet@po.CWRU.Edu */ #ifndef _IOFBF #define _IOFBF 0 #endif int setvbuf(iop, buf, type, size) FILE *iop; char *buf; int type, size; { /* * Do a little sanity checking on type, and fail if an invalid * type is given. */ if ((type != _IOFBF) && (type != _IOLBF) && (type != _IONBF)) return EOF; /* * If we already allocated something for this iop, clean up. */ if ((iop->_base != NULL) && (iop->_flag & _IOMYBUF)) free(iop->_base); /* * Clear out all previous settings before we start */ iop->_flag &= ~(_IOMYBUF | _IOFBF | _IONBF | _IOLBF); /* * If we're specifying no buffering, all we need to do is set the * appropriate flag and return, since buf and size are ignored. */ if (type == _IONBF) { iop->_flag |= _IONBF; iop->_ptr = iop->_base = (char *) NULL; iop->_bufsiz = iop->_cnt = 0; return 0; } /* * If we specify full buffering or line buffering, but with a size of * 0, then fail and return. */ if ((type == _IOFBF || type == _IOLBF) && (size == 0)) return EOF; /* * If we are not given a buffer, but we have a non-zero size, we must * allocate one ourselves and set the corresponding flag. Note that * by the time we get here, we are assured that we have line or full * buffering, so a buffer MUST be allocated if none is given. If we * can't allocate a buffer, fail and return. */ if (buf == NULL) if ((buf = (char *) malloc(size)) == NULL) return EOF; else iop->_flag |= _IOMYBUF; /* * OK, we're almost home. Set up the buffer and other stuff and * return success. */ iop->_ptr = iop->_base = buf; iop->_cnt = 0; iop->_bufsiz = size; iop->_flag |= type; return 0; } -- Chet Ramey "How terrible is wisdom when it Network Services Group brings no profit to the wise." Case Western Reserve University -- Mephistopheles chet@ins.CWRU.Edu