BSD 4_2 release
[unix-history] / usr / src / lib / libc / stdio / filbuf.c
index 43054ff..6ed01ed 100644 (file)
@@ -1,15 +1,23 @@
-/* @(#)filbuf.c        4.1 (Berkeley) %G% */
+/* @(#)filbuf.c        4.6 (Berkeley) 6/30/83 */
 #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;
 {
+       int size;
+       struct stat stbuf;
        static char smallbuf[_NFILE];
        static char smallbuf[_NFILE];
+       extern char _sibuf[];
+
+       if (iop->_flag & _IORW)
+               iop->_flag |= _IOREAD;
 
        if ((iop->_flag&_IOREAD) == 0)
                return(EOF);
 
        if ((iop->_flag&_IOREAD) == 0)
                return(EOF);
-       if (iop->_flag&_IOSTRG)
+       if (iop->_flag&(_IOSTRG|_IOEOF))
                return(EOF);
 tryagain:
        if (iop->_base==NULL) {
                return(EOF);
 tryagain:
        if (iop->_base==NULL) {
@@ -17,20 +25,36 @@ tryagain:
                        iop->_base = &smallbuf[fileno(iop)];
                        goto tryagain;
                }
                        iop->_base = &smallbuf[fileno(iop)];
                        goto tryagain;
                }
-               if ((iop->_base = malloc(BUFSIZ)) == NULL) {
-                       iop->_flag |= _IONBF;
-                       goto tryagain;
+               if (fstat(fileno(iop), &stbuf) < 0 || stbuf.st_blksize <= NULL)
+                       size = BUFSIZ;
+               else
+                       size = stbuf.st_blksize;
+               if (iop == stdin)
+                       iop->_base = _sibuf;
+               else {
+                       if ((iop->_base = malloc(size)) == NULL) {
+                               iop->_flag |= _IONBF;
+                               goto tryagain;
+                       }
+                       iop->_flag |= _IOMYBUF;
                }
                }
-               iop->_flag |= _IOMYBUF;
+               iop->_bufsiz = size;
        }
        }
+       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 == stdin && (stdout->_flag&_IOLBF))
-               fflush(stdout);
-       iop->_cnt = read(fileno(iop), iop->_ptr, iop->_flag&_IONBF?1:BUFSIZ);
        if (--iop->_cnt < 0) {
        if (--iop->_cnt < 0) {
-               if (iop->_cnt == -1)
+               if (iop->_cnt == -1) {
                        iop->_flag |= _IOEOF;
                        iop->_flag |= _IOEOF;
-               else
+                       if (iop->_flag & _IORW)
+                               iop->_flag &= ~_IOREAD;
+               } else
                        iop->_flag |= _IOERR;
                iop->_cnt = 0;
                return(-1);
                        iop->_flag |= _IOERR;
                iop->_cnt = 0;
                return(-1);