add ftw
[unix-history] / usr / src / lib / libc / stdio / fwrite.c
index cf27ef9..886c265 100644 (file)
@@ -1,4 +1,13 @@
-/* @(#)fwrite.c        4.1 (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[] = "@(#)fwrite.c   5.2 (Berkeley) %G%";
+#endif LIBC_SCCS and not lint
+
 #include       <stdio.h>
 
 fwrite(ptr, size, count, iop)
 #include       <stdio.h>
 
 fwrite(ptr, size, count, iop)
@@ -9,7 +18,15 @@ fwrite(ptr, size, count, iop)
        register int s;
 
        s = size * count;
        register int s;
 
        s = size * count;
-       while (s > 0) {
+       if (iop->_flag & _IOLBF)
+               while (s > 0) {
+                       if (--iop->_cnt > -iop->_bufsiz && *ptr != '\n')
+                               *iop->_ptr++ = *ptr++;
+                       else if (_flsbuf(*(unsigned char *)ptr++, iop) == EOF)
+                               break;
+                       s--;
+               }
+       else while (s > 0) {
                if (iop->_cnt < s) {
                        if (iop->_cnt > 0) {
                                bcopy(ptr, iop->_ptr, iop->_cnt);
                if (iop->_cnt < s) {
                        if (iop->_cnt > 0) {
                                bcopy(ptr, iop->_ptr, iop->_cnt);
@@ -17,7 +34,7 @@ fwrite(ptr, size, count, iop)
                                iop->_ptr += iop->_cnt;
                                s -= iop->_cnt;
                        }
                                iop->_ptr += iop->_cnt;
                                s -= iop->_cnt;
                        }
-                       if (_flsbuf((unsigned)*ptr++, iop) == EOF)
+                       if (_flsbuf(*(unsigned char *)ptr++, iop) == EOF)
                                break;
                        s--;
                }
                                break;
                        s--;
                }
@@ -28,5 +45,5 @@ fwrite(ptr, size, count, iop)
                        return (count);
                }
        }
                        return (count);
                }
        }
-       return (count - ((s + size - 1) / size));
+       return (size != 0 ? count - ((s + size - 1) / size) : 0);
 }
 }