add strdup for S5
[unix-history] / usr / src / lib / libc / stdio / filbuf.c
index 9a64aeb..e45f8c7 100644 (file)
@@ -1,35 +1,61 @@
-/* @(#)filbuf.c        4.3 (Berkeley) %G% */
+/*
+ * Copyright (c) 1980 Regents of the University of California.
+ * All rights reserved.  The Berkeley software License Agreement
+ * specifies the terms and conditions for redistribution.
+ */
+
+#if defined(LIBC_SCCS) && !defined(lint)
+static char sccsid[] = "@(#)filbuf.c   5.3 (Berkeley) %G%";
+#endif LIBC_SCCS and not lint
+
 #include       <stdio.h>
 #include       <stdio.h>
+#include       <sys/types.h>
+#include       <sys/stat.h>
 char   *malloc();
 
 _filbuf(iop)
 register FILE *iop;
 {
 char   *malloc();
 
 _filbuf(iop)
 register FILE *iop;
 {
-       static char smallbuf[_NFILE];
+       int size;
+       struct stat stbuf;
+       extern char *_smallbuf;
+       char c;
 
        if (iop->_flag & _IORW)
                iop->_flag |= _IOREAD;
 
        if ((iop->_flag&_IOREAD) == 0)
                return(EOF);
 
        if (iop->_flag & _IORW)
                iop->_flag |= _IOREAD;
 
        if ((iop->_flag&_IOREAD) == 0)
                return(EOF);
-       if (iop->_flag&_IOSTRG)
+       if (iop->_flag&(_IOSTRG|_IOEOF))
                return(EOF);
 tryagain:
        if (iop->_base==NULL) {
                if (iop->_flag&_IONBF) {
                return(EOF);
 tryagain:
        if (iop->_base==NULL) {
                if (iop->_flag&_IONBF) {
-                       iop->_base = &smallbuf[fileno(iop)];
+                       iop->_base = _smallbuf ? &_smallbuf[fileno(iop)] : &c;
                        goto tryagain;
                }
                        goto tryagain;
                }
-               if ((iop->_base = malloc(BUFSIZ)) == NULL) {
+               if (fstat(fileno(iop), &stbuf) < 0 || stbuf.st_blksize <= NULL)
+                       size = BUFSIZ;
+               else
+                       size = stbuf.st_blksize;
+               if ((iop->_base = malloc(size)) == NULL) {
                        iop->_flag |= _IONBF;
                        goto tryagain;
                }
                iop->_flag |= _IOMYBUF;
                        iop->_flag |= _IONBF;
                        goto tryagain;
                }
                iop->_flag |= _IOMYBUF;
+               iop->_bufsiz = size;
        }
        }
-       if (iop == stdin && (stdout->_flag&_IOLBF))
-               fflush(stdout);
-       iop->_cnt = read(fileno(iop), iop->_base, iop->_flag&_IONBF?1:BUFSIZ);
+       if (iop == stdin) {
+               if (stdout->_flag&_IOLBF)
+                       fflush(stdout);
+               if (stderr->_flag&_IOLBF)
+                       fflush(stderr);
+       }
+       iop->_cnt = read(fileno(iop), iop->_base,
+               iop->_flag & _IONBF ? 1 : iop->_bufsiz);
        iop->_ptr = iop->_base;
        iop->_ptr = iop->_base;
+       if (iop->_flag & _IONBF && iop->_base == &c)
+               iop->_base = NULL;
        if (--iop->_cnt < 0) {
                if (iop->_cnt == -1) {
                        iop->_flag |= _IOEOF;
        if (--iop->_cnt < 0) {
                if (iop->_cnt == -1) {
                        iop->_flag |= _IOEOF;
@@ -38,7 +64,7 @@ tryagain:
                } else
                        iop->_flag |= _IOERR;
                iop->_cnt = 0;
                } else
                        iop->_flag |= _IOERR;
                iop->_cnt = 0;
-               return(-1);
+               return(EOF);
        }
        return(*iop->_ptr++&0377);
 }
        }
        return(*iop->_ptr++&0377);
 }