BSD 2 development
authorKeith Sklower <sklower@ucbvax.Berkeley.EDU>
Thu, 10 May 1979 02:45:25 +0000 (18:45 -0800)
committerKeith Sklower <sklower@ucbvax.Berkeley.EDU>
Thu, 10 May 1979 02:45:25 +0000 (18:45 -0800)
Work on file src/libNS/data.c
Work on file src/libNS/filbuf.c
Work on file src/libNS/flsbuf.c
Work on file src/libNS/fopen.c
Work on file src/libNS/freopen.c
Work on file src/libNS/fseek.c
Work on file src/libNS/ftell.c
Work on file src/libNS/getw.c
Work on file src/libNS/putw.c
Work on file src/libNS/stdio.h

Synthesized-from: 2bsd

src/libNS/data.c [new file with mode: 0755]
src/libNS/filbuf.c [new file with mode: 0755]
src/libNS/flsbuf.c [new file with mode: 0755]
src/libNS/fopen.c [new file with mode: 0755]
src/libNS/freopen.c [new file with mode: 0755]
src/libNS/fseek.c [new file with mode: 0755]
src/libNS/ftell.c [new file with mode: 0755]
src/libNS/getw.c [new file with mode: 0644]
src/libNS/putw.c [new file with mode: 0644]
src/libNS/stdio.h [new file with mode: 0755]

diff --git a/src/libNS/data.c b/src/libNS/data.c
new file mode 100755 (executable)
index 0000000..98c8875
--- /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] = {
+       { _sibuf, 0, _sibuf, _IOREAD, 0, 0},
+       { NULL, 0, NULL, _IOWRT, 1, 0},
+       { NULL, 0, NULL, _IOWRT+_IONBF, 2, 0},
+};
+/*
+ * Ptr to end of buffers
+ */
+struct _iobuf  *_lastbuf = { &_iob[_NFILE] };
diff --git a/src/libNS/filbuf.c b/src/libNS/filbuf.c
new file mode 100755 (executable)
index 0000000..524b927
--- /dev/null
@@ -0,0 +1,49 @@
+/* Copyright (c) 1979 Regents of the University of California */
+#include       <stdio.h>
+_filbuf(iop)
+register struct _iobuf *iop;
+{
+       static char smallbuf[_NFILE];
+       register n;
+       char *malloc();
+
+       if ((iop->_flag&_IOREAD) == 0)
+               _error("Reading bad file\n");
+       if (iop->_flag&_IOSTRG)
+               return(-1);
+tryagain:
+       if (iop->_base==NULL) {
+               if (iop->_flag&_IONBF) {
+                       iop->_base = &smallbuf[fileno(iop)];
+                       goto tryagain;
+               }
+               if ((iop->_base = malloc(BUFSIZ)) == NULL) {
+                       iop->_flag |= _IONBF;
+                       goto tryagain;
+               }
+               iop->_flag |= _IOMYBUF;
+       }
+       if((iop->_flag & _IODIRT) && !(iop->_flag & _IONBF) && (iop->_flag & _IOWRT)) {
+               if (iop->_delta)
+                       if (lseek(iop->_file,(long)  -iop->_delta, 1) < 0) {
+                               _error("Seek error in filbuf\n");
+                               iop->_flag |= _IOERR;
+                       }
+               if( 0 < (n = iop->_ptr - iop->_base))
+                       if( n != write(iop->_file, iop->_base, n) )
+                               iop->_flag |= _IOERR;
+               iop->_flag &= ~_IODIRT;
+       }
+       iop->_ptr = iop->_base;
+       iop->_cnt = read(fileno(iop), iop->_ptr, iop->_flag&_IONBF?1:BUFSIZ);
+       iop->_delta = iop->_cnt;
+       if (--iop->_cnt < 0) {
+               if (iop->_cnt == -1)
+                       iop->_flag |= _IOEOF;
+               else
+                       iop->_flag |= _IOERR;
+               iop->_cnt = 0;
+               return(-1);
+       }
+       return(*iop->_ptr++&0377);
+}
diff --git a/src/libNS/flsbuf.c b/src/libNS/flsbuf.c
new file mode 100755 (executable)
index 0000000..542dd46
--- /dev/null
@@ -0,0 +1,107 @@
+/* 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) {
+                               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);
+}
diff --git a/src/libNS/fopen.c b/src/libNS/fopen.c
new file mode 100755 (executable)
index 0000000..6bd48ce
--- /dev/null
@@ -0,0 +1,57 @@
+/* Copyright (c) 1979 Regents of the University of California */
+#include       <stdio.h>
+
+struct _iobuf *fopen(file, mode)
+register char *mode;
+{
+       register f;
+       register struct _iobuf *iop;
+       int unixmode, a_flag;
+
+       for (iop = _iob; iop->_flag&(_IOREAD|_IOWRT); iop++)
+               if (iop >= _iob + _NFILE)
+                       return(NULL);
+       iop->_flag = 0;
+       iop->_delta = 0;
+       a_flag = 0;
+       for(;*mode;mode++) {
+               switch(*mode) {
+
+               case 'w':
+                       iop->_flag |= _IOWRT;
+                       break;
+
+               case 'r':
+                       iop->_flag |= _IOREAD;
+                       break;
+
+               case 'a':
+                       a_flag = 1;
+                       iop->_flag |= _IOWRT ;
+               }
+       }
+       if((unixmode = (iop->_flag & 3) - 1) < 0) {
+               unixmode = 0;
+               iop->_flag = 1;
+       }
+       if ((iop->_flag & _IOWRT) && a_flag==0 ) {
+               f = creat(file, 0644);
+               if((iop->_flag &  _IOREAD) && (f>0)) {
+                       close(f);
+                       f = open(file,2);
+               }
+       }
+       else
+               if ((0 >(f = open(file, unixmode))) && (a_flag || !(iop->_flag&_IOREAD))) {
+                       f = creat(file, 0644);
+                       close(f);
+                       f = open(file,unixmode);
+               }
+
+       if (f < 0)
+               return(NULL);
+       if (a_flag)
+               lseek(f, (long) 0, 2);
+       iop->_file = f;
+       return(iop);
+}
diff --git a/src/libNS/freopen.c b/src/libNS/freopen.c
new file mode 100755 (executable)
index 0000000..bae025c
--- /dev/null
@@ -0,0 +1,51 @@
+/* Copyright (c) 1979 Regents of the University of California */
+#include       <stdio.h>
+
+struct _iobuf *freopen(file, mode, iop)
+register char *mode;
+register struct _iobuf *iop;
+{
+       register f;
+       int unixmode, a_flag;
+
+       fclose(iop);
+       a_flag = 0;
+       for(;*mode;mode++) {
+               switch(*mode) {
+
+               case 'w':
+                       iop->_flag |= _IOWRT;
+                       break;
+
+               case 'r':
+                       iop->_flag |= _IOREAD;
+                       break;
+
+               case 'a':
+                       a_flag = 1;
+                       iop->_flag |= _IOWRT ;
+               }
+       }
+       if((unixmode = (iop->_flag & 3) - 1) < 0) {
+               unixmode = 0;
+               iop->_flag = 1;
+       }
+       if ((iop->_flag & _IOWRT) && a_flag==0 ) {
+               f = creat(file, 0644);
+               if((iop->_flag & _IOREAD) && (f > 0)) {
+                       close(f);
+                       f = open(file,2);
+               }
+       } else if ((0>(f = open(file, unixmode))) && (a_flag || !(iop->_flag&_IOREAD))){
+                       f = creat(file, 0644);
+                       close(f);
+                       f = open(file,unixmode);
+               }
+
+       if (f < 0)
+               return(NULL);
+       if (a_flag)
+               lseek(f, (long) 0, 2);
+       iop->_file = f;
+       return(iop);
+}
diff --git a/src/libNS/fseek.c b/src/libNS/fseek.c
new file mode 100755 (executable)
index 0000000..d1d8109
--- /dev/null
@@ -0,0 +1,41 @@
+/* Copyright (c) 1979 Regents of the University of California */
+#include       <stdio.h>
+
+/*
+ * Seek for standard library.  Coordinates with buffering.
+ */
+
+long fseek(iop, offset, ptrname)
+register FILE *iop;
+long offset;
+{
+       register n, resync;
+
+       if (iop->_flag&_IODIRT) {
+               fflush(iop);
+               return(lseek(fileno(iop), offset, ptrname));
+       }
+       if (iop->_flag&(_IOREAD|_IOWRT)) {
+               resync = 0;
+               if (ptrname==1) {       /* relative */
+                       n = iop->_cnt;
+                       if (n<0)
+                               n = 0;
+               } else {
+                       n = offset&01;
+                       resync = n;
+               }
+               n = lseek(fileno(iop), offset - n, ptrname);
+               iop->_cnt = 0;
+               iop->_ptr = iop->_base ;
+               iop->_delta = 0;
+               if (resync)
+                       getc(iop);
+               return(n);
+       }
+       _error("fseek\n");
+}
+
+/*  The current character is always iop->_cnt characters behind the current 
+position in the file, except when a file is open and in use for pure writing
+in which case it is _IODIRT(y), and will be correctly positioned by fflush */
diff --git a/src/libNS/ftell.c b/src/libNS/ftell.c
new file mode 100755 (executable)
index 0000000..6b8d5b9
--- /dev/null
@@ -0,0 +1,25 @@
+/* Copyright (c) 1979 Regents of the University of California */
+#include       <stdio.h>
+
+/*
+ * Return file offset.
+ * Coordinates with buffering.
+ */
+
+long   tell();
+
+long ftell(iop)
+register FILE *iop;
+{
+       long tres;
+       register adjust;
+
+       if (iop->_cnt < 0)
+               iop->_cnt = 0;
+       if (!(iop->_flag & (_IOREAD|_IOWRT)))
+               _error("ftell\n");
+       tres = tell(fileno(iop));
+       if (tres<0)
+               return(tres);
+       return(tres - iop->_delta + iop->_ptr - iop->_base);
+}
diff --git a/src/libNS/getw.c b/src/libNS/getw.c
new file mode 100644 (file)
index 0000000..7f91ef5
--- /dev/null
@@ -0,0 +1,23 @@
+/* Copyright (c) 1979 Regents of the University of California */
+#include       <stdio.h>
+
+short getsh(iop)
+register struct _iobuf *iop;
+{
+       register i;
+
+       i = getc(iop);
+       if (iop->_flag&_IOEOF)
+               return(-1);
+       return(i | (getc(iop)<<8));
+}
+getw(iop)
+register struct _iobuf *iop;
+{
+       register i;
+
+       i = getsh(iop);
+       if (iop->_flag&_IOEOF)
+               return(-1);
+       return(i | (getsh(iop)<<16));
+}
diff --git a/src/libNS/putw.c b/src/libNS/putw.c
new file mode 100644 (file)
index 0000000..610214c
--- /dev/null
@@ -0,0 +1,19 @@
+/* Copyright (c) 1979 Regents of the University of California */
+#include       <stdio.h>
+
+putw(i, iop)
+register i;
+register struct _iobuf *iop;
+{
+       putc(i, iop);
+       putc(i>>=8, iop);
+       putc(i>>=8, iop);
+       putc(i>>=8, iop);
+}
+short putsh(i, iop)
+register i;
+register struct _iobuf *iop;
+{
+       putc(i, iop);
+       putc(i>>8, iop);
+}
diff --git a/src/libNS/stdio.h b/src/libNS/stdio.h
new file mode 100755 (executable)
index 0000000..40b3de5
--- /dev/null
@@ -0,0 +1,46 @@
+/* Copyright (c) 1979 Regents of the University of California */
+#define        BUFSIZ  512
+#define        _NFILE  20
+# ifndef FILE
+extern struct  _iobuf {
+       int     _cnt;
+       char    *_ptr;
+       char    *_base;
+       char    _flag;
+       char    _file;
+short  int     _delta;
+} _iob[_NFILE];
+# endif
+
+#define        _IOREAD 01
+#define        _IOWRT  02
+#define        _IONBF  04
+#define        _IOMYBUF        010
+#define        _IOEOF  020
+#define        _IOERR  040
+#define        _IOSTRG 0100
+#define _IODIRT        0200
+#define        NULL    0
+#define        FILE    struct _iobuf
+#define        EOF     (-1)
+
+#define        stdin   (&_iob[0])
+#define        stdout  (&_iob[1])
+#define        stderr  (&_iob[2])
+#define getc(p)                (--(p)->_cnt>=0? *(p)->_ptr++&0377:_filbuf(p))
+#define        getchar()       getc(stdin)
+#define        peekc(p)        (p->_cnt>0? *(p)->_ptr&0377:_filbuf(p)==-1?-1:((p)->_cnt++,*--(p)->_ptr&0377))
+#define        peekchar()      peekc(stdin)
+#define        putc(x,p)       (((p)->_flag |= _IODIRT,--(p)->_cnt)>=0? ((int)(*(p)->_ptr++ = (unsigned)(x))):_flsbuf( (unsigned)(x),p))
+#define        putchar(x)      putc(x,stdout)
+#define        feof(p)         (((p)->_flag&_IOEOF)!=0)
+#define        ferror(p)       (((p)->_flag&_IOERR)!=0)
+#define        fileno(p)       p->_file
+
+FILE   *fopen();
+FILE   *fdopen();
+FILE   *freopen();
+long   ftell();
+char   *fgets();
+short  getsh();
+short  putsh();