Research V7 development
authorDennis Ritchie <dmr@research.uucp>
Sat, 5 May 1979 05:57:03 +0000 (00:57 -0500)
committerDennis Ritchie <dmr@research.uucp>
Sat, 5 May 1979 05:57:03 +0000 (00:57 -0500)
Work on file usr/src/libc/stdio/endopen.c
Work on file usr/src/libc/stdio/fdopen.c
Work on file usr/src/libc/stdio/filbuf.c
Work on file usr/src/libc/stdio/flsbuf.c
Work on file usr/src/libc/stdio/findiop.c
Work on file usr/src/libc/stdio/fseek.c
Work on file usr/src/libc/stdio/fopen.c
Work on file usr/src/libc/stdio/freopen.c
Work on file usr/src/libc/stdio/ftell.c
Work on file usr/src/libc/stdio/rew.c

Synthesized-from: v7

usr/src/libc/stdio/endopen.c [new file with mode: 0644]
usr/src/libc/stdio/fdopen.c [new file with mode: 0644]
usr/src/libc/stdio/filbuf.c [new file with mode: 0644]
usr/src/libc/stdio/findiop.c [new file with mode: 0644]
usr/src/libc/stdio/flsbuf.c [new file with mode: 0644]
usr/src/libc/stdio/fopen.c [new file with mode: 0644]
usr/src/libc/stdio/freopen.c [new file with mode: 0644]
usr/src/libc/stdio/fseek.c [new file with mode: 0644]
usr/src/libc/stdio/ftell.c [new file with mode: 0644]
usr/src/libc/stdio/rew.c [new file with mode: 0644]

diff --git a/usr/src/libc/stdio/endopen.c b/usr/src/libc/stdio/endopen.c
new file mode 100644 (file)
index 0000000..5e91de0
--- /dev/null
@@ -0,0 +1,68 @@
+#include       <stdio.h>
+#include       <errno.h>
+
+FILE *
+_endopen(file, mode, iop)
+       char *file, *mode;
+       register FILE *iop;
+{
+       extern int errno;
+       register int rw, f;
+
+       if (iop == NULL)
+               return(NULL);
+
+       rw = mode[1] == '+';
+
+       switch (*mode) {
+
+       case 'w':
+               f = create(file, rw);
+               break;
+
+       case 'a':
+               if ((f = open(file, rw? 2: 1)) < 0) {
+                       if (errno == ENOENT)
+                               f = create(file, rw);
+               }
+               lseek(f, 0L, 2);
+               break;
+
+       case 'r':
+               f = open(file, rw? 2: 0);
+               break;
+
+       default:
+               return(NULL);
+       }
+
+       if (f < 0)
+               return(NULL);
+
+       iop->_cnt = 0;
+       iop->_file = f;
+
+       if (rw)
+               iop->_flag |= _IORW;
+       else if(*mode == 'r')
+               iop->_flag |= _IOREAD;
+       else
+               iop->_flag |= _IOWRT;
+
+       return(iop);
+}
+
+static int
+create(file, rw)
+       register char *file;
+       int rw;
+{
+       register int f;
+
+       f = creat(file, 0666);
+       if (rw && f>=0) {
+               close(f);
+               f = open(file, 2);
+       }
+       return(f);
+}
diff --git a/usr/src/libc/stdio/fdopen.c b/usr/src/libc/stdio/fdopen.c
new file mode 100644 (file)
index 0000000..5ca8195
--- /dev/null
@@ -0,0 +1,45 @@
+/*
+ * Unix routine to do an "fopen" on file descriptor
+ * The mode has to be repeated because you can't query its
+ * status
+ */
+
+#include       <stdio.h>
+#include       <errno.h>
+
+FILE *
+fdopen(fd, mode)
+       register char *mode;
+{
+       register FILE *iop;
+       FILE *_findiop();
+
+       if ((iop = _findiop()) == NULL)
+               return(NULL);
+
+       iop->_cnt = 0;
+       iop->_file = fd;
+       switch (*mode) {
+
+       case 'r':
+               iop->_flag |= _IOREAD;
+               break;
+
+       case 'a':
+               lseek(fd, 0L, 2);
+               /* No break */
+       case 'w':
+               iop->_flag |= _IOWRT;
+               break;
+
+       default:
+               return(NULL);
+       }
+
+       if (mode[1] == '+') {
+               iop->_flag &= ~(_IOREAD|_IOWRT);
+               iop->_flag |= _IORW;
+       }
+
+       return(iop);
+}
diff --git a/usr/src/libc/stdio/filbuf.c b/usr/src/libc/stdio/filbuf.c
new file mode 100644 (file)
index 0000000..3cb9555
--- /dev/null
@@ -0,0 +1,43 @@
+#include       <stdio.h>
+
+char   *malloc();
+
+
+int
+_filbuf(iop)
+       register FILE *iop;
+{
+       static char smallbuf[_NFILE];
+
+       if (iop->_flag & _IORW)
+               iop->_flag |= _IOREAD;
+
+       if ((iop->_flag & _IOREAD) == 0 || iop->_flag & _IOSTRG)
+               return(EOF);
+
+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;
+       }
+       iop->_ptr = iop->_base;
+       iop->_cnt = read(fileno(iop), iop->_ptr, iop->_flag&_IONBF?1:BUFSIZ);
+       if (--iop->_cnt < 0) {
+               if (iop->_cnt == -1) {
+                       iop->_flag |= _IOEOF;
+                       if (iop->_flag & _IORW)
+                               iop->_flag &= ~_IOREAD;
+               } else
+                       iop->_flag |= _IOERR;
+               iop->_cnt = 0;
+               return(EOF);
+       }
+       return(*iop->_ptr++ & 0377);
+}
diff --git a/usr/src/libc/stdio/findiop.c b/usr/src/libc/stdio/findiop.c
new file mode 100644 (file)
index 0000000..752f820
--- /dev/null
@@ -0,0 +1,14 @@
+#include <stdio.h>
+
+FILE *
+_findiop()
+{
+       extern FILE *_lastbuf;
+       register FILE *iop;
+
+       for(iop = _iob; iop->_flag & (_IOREAD|_IOWRT|_IORW); iop++)
+               if (iop >= _lastbuf)
+                       return(NULL);
+
+       return(iop);
+}
diff --git a/usr/src/libc/stdio/flsbuf.c b/usr/src/libc/stdio/flsbuf.c
new file mode 100644 (file)
index 0000000..b381bb2
--- /dev/null
@@ -0,0 +1,111 @@
+#include       <stdio.h>
+
+char   *malloc();
+
+int
+_flsbuf(c, iop)
+       int c;
+       register FILE *iop;
+{
+       register char *base;
+       register n, rn;
+       char c1;
+       extern char _sobuf[];
+
+       if (iop->_flag & _IORW) {
+               iop->_flag |= _IOWRT;
+               iop->_flag &= ~_IOEOF;
+       }
+
+tryagain:
+       if (iop->_flag & _IONBF) {
+               c1 = c;
+               rn = 1;
+               n = write(fileno(iop), &c1, rn);
+               iop->_cnt = 0;
+       } else {
+               if ((base = iop->_base) == NULL) {
+                       if (iop == stdout) {
+                               if (isatty(fileno(stdout))) {
+                                       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;
+                       rn = n = 0;
+               } else if((rn = n = iop->_ptr - base) > 0) {
+                       iop->_ptr = base;
+                       n = write(fileno(iop), base, n);
+               }
+               iop->_cnt = BUFSIZ - 1;
+               *base++ = c;
+               iop->_ptr = base;
+       }
+       if (rn != n) {
+               iop->_flag |= _IOERR;
+               return(EOF);
+       }
+       return(c);
+}
+
+int
+fflush(iop)
+       register FILE *iop;
+{
+       register char *base;
+       register n;
+
+       if ((iop->_flag & (_IONBF|_IOWRT)) == _IOWRT
+        && (base = iop->_base) != NULL && (n = iop->_ptr - base) > 0) {
+               iop->_ptr = base;
+               iop->_cnt = BUFSIZ;
+               if (write(fileno(iop), base, n) != n) {
+                       iop->_flag |= _IOERR;
+                       return(EOF);
+               }
+       }
+       return(0);
+}
+
+/*
+ * Flush buffers on exit
+ */
+
+_cleanup()
+{
+       register FILE *iop;
+       extern FILE *_lastbuf;
+
+       for(iop = _iob; iop < _lastbuf; iop++)
+               fclose(iop);
+}
+
+int
+fclose(iop)
+       register FILE *iop;
+{
+       register r;
+
+       r = EOF;
+       if (iop->_flag & (_IOREAD|_IOWRT|_IORW)
+           && (iop->_flag & _IOSTRG) == 0) {
+               r = fflush(iop);
+               if (close(fileno(iop)) < 0)
+                       r = EOF;
+               if (iop->_flag & _IOMYBUF)
+                       free(iop->_base);
+               if (iop->_flag & (_IOMYBUF|_IONBF))
+                       iop->_base = NULL;
+       }
+       iop->_flag &=
+               ~(_IOREAD|_IOWRT|_IONBF|_IOMYBUF|_IOERR|_IOEOF|_IOSTRG|_IORW);
+       iop->_cnt = 0;
+       return(r);
+}
diff --git a/usr/src/libc/stdio/fopen.c b/usr/src/libc/stdio/fopen.c
new file mode 100644 (file)
index 0000000..a5a04e5
--- /dev/null
@@ -0,0 +1,10 @@
+#include       <stdio.h>
+
+FILE *
+fopen(file, mode)
+       char *file, *mode;
+{
+       FILE *_findiop(), *_endopen();
+
+       return(_endopen(file, mode, _findiop()));
+}
diff --git a/usr/src/libc/stdio/freopen.c b/usr/src/libc/stdio/freopen.c
new file mode 100644 (file)
index 0000000..016c0b2
--- /dev/null
@@ -0,0 +1,12 @@
+#include <stdio.h>
+
+FILE *
+freopen(file, mode, iop)
+       char *file, *mode;
+       register FILE *iop;
+{
+       FILE *_endopen();
+
+       fclose(iop);
+       return(_endopen(file, mode, iop));
+}
diff --git a/usr/src/libc/stdio/fseek.c b/usr/src/libc/stdio/fseek.c
new file mode 100644 (file)
index 0000000..ecbda88
--- /dev/null
@@ -0,0 +1,48 @@
+/*
+ * Seek for standard library.  Coordinates with buffering.
+ */
+
+#include       <stdio.h>
+
+long lseek();
+
+fseek(iop, offset, ptrname)
+       register FILE *iop;
+       long offset;
+{
+       register int c;
+       long p;
+
+       iop->_flag &= ~_IOEOF;
+       if (iop->_flag & _IOREAD) {
+               if (ptrname < 2 && iop->_base && !(iop->_flag&_IONBF)) {
+                       c = iop->_cnt;
+                       p = offset;
+                       if (ptrname == 0)
+                               p += c - lseek(fileno(iop), 0L, 1);
+                       else
+                               offset -= c;
+                       if (!(iop->_flag&_IORW) && c > 0 && p <= c
+                           && p >= iop->_base - iop->_ptr){
+                               iop->_ptr += (int) p;
+                               iop->_cnt -= (int) p;
+                               return(0);
+                       }
+               }
+               if (iop->_flag & _IORW) {
+                       iop->_ptr = iop->_base;
+                       iop->_flag &= ~_IOREAD;
+               }
+               p = lseek(fileno(iop), offset, ptrname);
+               iop->_cnt = 0;
+       } else if(iop->_flag & (_IOWRT|_IORW)) {
+               fflush(iop);
+               if (iop->_flag & _IORW) {
+                       iop->_cnt = 0;
+                       iop->_flag &= ~_IOWRT;
+                       iop->_ptr = iop->_base;
+               }
+               p = lseek(fileno(iop), offset, ptrname);
+       }
+       return(p==-1? -1: 0);
+}
diff --git a/usr/src/libc/stdio/ftell.c b/usr/src/libc/stdio/ftell.c
new file mode 100644 (file)
index 0000000..1f328a4
--- /dev/null
@@ -0,0 +1,31 @@
+/*
+ * Return file offset.
+ * Coordinates with buffering.
+ */
+
+#include       <stdio.h>
+long   lseek();
+
+
+long ftell(iop)
+FILE *iop;
+{
+       long tres;
+       register adjust;
+
+       if (iop->_cnt < 0)
+               iop->_cnt = 0;
+       if (iop->_flag&_IOREAD)
+               adjust = - iop->_cnt;
+       else if(iop->_flag&(_IOWRT|_IORW)) {
+               adjust = 0;
+               if (iop->_flag&_IOWRT && iop->_base && (iop->_flag&_IONBF)==0)
+                       adjust = iop->_ptr - iop->_base;
+       } else
+               return(-1);
+       tres = lseek(fileno(iop), 0L, 1);
+       if (tres<0)
+               return(tres);
+       tres += adjust;
+       return(tres);
+}
diff --git a/usr/src/libc/stdio/rew.c b/usr/src/libc/stdio/rew.c
new file mode 100644 (file)
index 0000000..2184a4f
--- /dev/null
@@ -0,0 +1,13 @@
+#include       <stdio.h>
+
+rewind(iop)
+       register struct _iobuf *iop;
+{
+       fflush(iop);
+       lseek(fileno(iop), 0L, 0);
+       iop->_cnt = 0;
+       iop->_ptr = iop->_base;
+       iop->_flag &= ~(_IOERR|_IOEOF);
+       if (iop->_flag & _IORW)
+               iop->_flag &= ~(_IOREAD|_IOWRT);
+}