BSD 3 development
authorBill Joy <wnj@ucbvax.Berkeley.EDU>
Tue, 22 Jan 1980 10:32:45 +0000 (02:32 -0800)
committerBill Joy <wnj@ucbvax.Berkeley.EDU>
Tue, 22 Jan 1980 10:32:45 +0000 (02:32 -0800)
Work on file usr/src/lib/libNS/flsbuf.c
Work on file usr/src/lib/libNS/data.c

Synthesized-from: 3bsd

usr/src/lib/libNS/data.c [new file with mode: 0755]
usr/src/lib/libNS/flsbuf.c [new file with mode: 0755]

diff --git a/usr/src/lib/libNS/data.c b/usr/src/lib/libNS/data.c
new file mode 100755 (executable)
index 0000000..155889f
--- /dev/null
@@ -0,0 +1,14 @@
+/* Copyright (c) 1979 Regents of the University of California */
+#include <stdio.h>
+char   _sibuf[BUFSIZ];
+char   _sobuf[BUFSIZ];
+
+struct _iobuf  _iob[_NFILE] = {
+       { 0, _sibuf, _sibuf, _IOREAD, 0, 0},
+       { 0, NULL, NULL, _IOWRT, 1, 0},
+       { 0, NULL, NULL, _IOWRT+_IONBF, 2, 0},
+};
+/*
+ * Ptr to end of buffers
+ */
+struct _iobuf  *_lastbuf = { &_iob[_NFILE] };
diff --git a/usr/src/lib/libNS/flsbuf.c b/usr/src/lib/libNS/flsbuf.c
new file mode 100755 (executable)
index 0000000..28f4d05
--- /dev/null
@@ -0,0 +1,112 @@
+/* Copyright (c) 1979 Regents of the University of California */
+#include       <stdio.h>
+
+_flsbuf(c, iop)
+register struct _iobuf *iop;
+{
+       register n;
+       register char *base;
+       char c1, *malloc();
+       extern char _sobuf[];
+
+       if ((iop->_flag&_IOWRT)==0)
+               _error("writing\n");
+tryagain:
+       if (iop->_flag&_IONBF) {
+               c1 = c;
+               n = write(fileno(iop), &c1, 1);
+               iop->_cnt = 0;
+       } else {
+               if ((base=iop->_base)==NULL) {
+                       if (iop==stdout) {
+                               if (isatty(fileno(stdout)) &&
+                                   ! iop->_flag&_IOREAD) {
+                                       iop->_flag |= _IONBF;
+                                       goto tryagain;
+                               }
+                               iop->_base = _sobuf;
+                               iop->_ptr = _sobuf;
+                               goto tryagain;
+                       }
+                       if ((iop->_base=base=malloc(BUFSIZ)) == NULL) {
+                               iop->_flag |= _IONBF;
+                               goto tryagain;
+                       }
+                       iop->_flag |= _IOMYBUF;
+                       n = 1;
+               } else if ((n = iop->_ptr - base) > 0) {
+                       if (iop->_delta && (iop->_flag&_IOREAD)) {
+                               if(lseek(iop->_file, (long) -iop->_delta, 1)<0)
+                                       _error("Seek error in flsbuf\n");
+                               iop->_delta = 0;
+                       }
+                       n = write(fileno(iop), base, n);
+                       if ((iop->_flag & _IOREAD) &&
+                        (0 >(iop->_delta = read(iop->_file, base, BUFSIZ)))) {
+                               iop->_delta = 0;
+                               iop->_flag |= _IOERR;
+                       }
+               }
+               iop->_cnt = BUFSIZ-1;
+               *base++ = c;
+               iop->_ptr = base;
+       }
+       if (n < 0) {
+               iop->_flag |= _IOERR;
+               return(-1);
+       }
+       return(c);
+}
+
+fflush(iop)
+register struct _iobuf *iop;
+{
+       register char *base;
+       register n;
+
+       if ((iop->_flag&(_IONBF|_IOWRT))==_IOWRT
+        && (base=iop->_base)!=NULL && ((n=iop->_ptr-base)>0) 
+        && (iop->_flag & _IODIRT)) {
+               iop->_ptr = base;
+               iop->_cnt = 0;
+               if(iop->_delta) {
+                       if(0>lseek(fileno(iop), (long) -iop->_delta, 1)) {
+                               _error("Seek error in fflush\n");
+                               iop->_flag |= _IOERR;
+                       }
+                       iop->_delta = 0;
+               }
+               if (write(fileno(iop), base, n)!=n)
+                       iop->_flag |= _IOERR;
+               iop->_flag &= ~_IODIRT;
+       }
+}
+
+/*
+ * Flush buffers on exit
+ */
+
+_cleanup()
+{
+       register struct _iobuf *iop;
+       extern struct _iobuf *_lastbuf;
+
+       for (iop = _iob; iop < _lastbuf; iop++)
+               fclose(iop);
+}
+
+fclose(iop)
+register struct _iobuf *iop;
+{
+       if (iop->_flag&(_IOREAD|_IOWRT)) {
+               fflush(iop);
+               close(fileno(iop));
+               if (iop->_flag&_IOMYBUF)
+                       free(iop->_base);
+       }
+       iop->_base = NULL;
+       iop->_flag &= ~(_IOREAD|_IOWRT|_IONBF|_IOMYBUF|_IOERR|_IOEOF|_IODIRT|_IOSTRG);
+       iop->_cnt = 0;
+       iop->_delta = 0;
+       return(0);
+}