From 1bd0b2144e23c7cd7b0d25a9a3849b08871db3ac Mon Sep 17 00:00:00 2001 From: Bill Joy Date: Sun, 18 Nov 1979 09:43:59 -0800 Subject: [PATCH] BSD 3 development Work on file usr/src/lib/libNS/READ_ME Work on file usr/src/lib/libNS/filbuf.c Work on file usr/src/lib/libNS/fseek.c Work on file usr/src/lib/libNS/freopen.c Work on file usr/src/lib/libNS/getw.c Work on file usr/src/lib/libNS/ftell.c Work on file usr/src/lib/libNS/putw.c Synthesized-from: 3bsd --- usr/src/lib/libNS/READ_ME | 24 +++++++++++++++++ usr/src/lib/libNS/filbuf.c | 49 +++++++++++++++++++++++++++++++++++ usr/src/lib/libNS/freopen.c | 51 +++++++++++++++++++++++++++++++++++++ usr/src/lib/libNS/fseek.c | 41 +++++++++++++++++++++++++++++ usr/src/lib/libNS/ftell.c | 25 ++++++++++++++++++ usr/src/lib/libNS/getw.c | 23 +++++++++++++++++ usr/src/lib/libNS/putw.c | 19 ++++++++++++++ 7 files changed, 232 insertions(+) create mode 100755 usr/src/lib/libNS/READ_ME create mode 100755 usr/src/lib/libNS/filbuf.c create mode 100755 usr/src/lib/libNS/freopen.c create mode 100755 usr/src/lib/libNS/fseek.c create mode 100755 usr/src/lib/libNS/ftell.c create mode 100644 usr/src/lib/libNS/getw.c create mode 100644 usr/src/lib/libNS/putw.c diff --git a/usr/src/lib/libNS/READ_ME b/usr/src/lib/libNS/READ_ME new file mode 100755 index 0000000000..90e9c4b48f --- /dev/null +++ b/usr/src/lib/libNS/READ_ME @@ -0,0 +1,24 @@ +Thu Nov 16 20:07:42 PST 1978 + +This represents an otherwise invisble extension to the standard i/o +library to allow streams to be open with concurrent read/write +capabilities. To incorporate the changes, recompile all C routines +in the library with the new header. + +Ammended manual pages are included. + + Keith Sklower + +[Note by Bill Joy: This is a modification of an (old) pdp-11 version +of the standard i/o library, but should go easily into the new stuff. +It has been used in a Modula Compiler for the PDP/11 version of UNIX.] + +Fri Feb 23 10:15:27 PST 1979 + +It should be noted that the structure _iobuf should be exactly like that +of the old structure, except for the addition of _delta, at the end. This +is because _doprint is usually coded in assembly language and need not be +changed if the field _delta (which it does not need to reference) is kept +at the end. The structures vary from version 6 to VAX unix. + + Keith Sklower. diff --git a/usr/src/lib/libNS/filbuf.c b/usr/src/lib/libNS/filbuf.c new file mode 100755 index 0000000000..524b9276d1 --- /dev/null +++ b/usr/src/lib/libNS/filbuf.c @@ -0,0 +1,49 @@ +/* Copyright (c) 1979 Regents of the University of California */ +#include +_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/usr/src/lib/libNS/freopen.c b/usr/src/lib/libNS/freopen.c new file mode 100755 index 0000000000..bae025cd07 --- /dev/null +++ b/usr/src/lib/libNS/freopen.c @@ -0,0 +1,51 @@ +/* Copyright (c) 1979 Regents of the University of California */ +#include + +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/usr/src/lib/libNS/fseek.c b/usr/src/lib/libNS/fseek.c new file mode 100755 index 0000000000..d1d8109db1 --- /dev/null +++ b/usr/src/lib/libNS/fseek.c @@ -0,0 +1,41 @@ +/* Copyright (c) 1979 Regents of the University of California */ +#include + +/* + * 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/usr/src/lib/libNS/ftell.c b/usr/src/lib/libNS/ftell.c new file mode 100755 index 0000000000..6b8d5b93b3 --- /dev/null +++ b/usr/src/lib/libNS/ftell.c @@ -0,0 +1,25 @@ +/* Copyright (c) 1979 Regents of the University of California */ +#include + +/* + * 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/usr/src/lib/libNS/getw.c b/usr/src/lib/libNS/getw.c new file mode 100644 index 0000000000..7f91ef5abe --- /dev/null +++ b/usr/src/lib/libNS/getw.c @@ -0,0 +1,23 @@ +/* Copyright (c) 1979 Regents of the University of California */ +#include + +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/usr/src/lib/libNS/putw.c b/usr/src/lib/libNS/putw.c new file mode 100644 index 0000000000..610214c0e7 --- /dev/null +++ b/usr/src/lib/libNS/putw.c @@ -0,0 +1,19 @@ +/* Copyright (c) 1979 Regents of the University of California */ +#include + +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); +} -- 2.20.1