cechew@bruce.OZ (Earl Chew) (09/04/89)
#! /bin/sh # This is a shell archive. Remove anything before this line, then unpack # it by saving it into a file and typing "sh file". To overwrite existing # files, type "sh file -c". You can also feed this as standard input via # unshar, or by typing "sh <file", e.g.. If this archive is complete, you # will see the following message at the end: # "End of archive 5 (of 7)." # Contents: fwrite.c gets.c getw.c printf.c puts.c putw.c rewind.c # scanf.c setbuf.c setvbuf.c sprintf.c sscanf.c stdio.c # Wrapped by cechew@bruce on Mon Sep 4 12:50:18 1989 PATH=/bin:/usr/bin:/usr/ucb ; export PATH if test -f 'fwrite.c' -a "${1}" != "-c" ; then echo shar: Will not clobber existing file \"'fwrite.c'\" else echo shar: Extracting \"'fwrite.c'\" \(3613 characters\) sed "s/^X//" >'fwrite.c' <<'END_OF_FILE' X/* f w r i t e X * X * This function writes a series of records to the output stream. X * The function returns the number of items written to the stream. X * Writing stops when the correct number of items have been written X * or when the function encounters an error. X * X * Patchlevel 1.0 X * X * Edit History: X * 04-Sep-1989 Add code to deal with really large objects. X * 03-Sep-1989 Write to buffer only if it requires at most one call X * to write(), otherwise use write() directly. X * 01-Sep-1989 Use Collyer's idea to speed up by doing writes directly X * from buffer. X */ X X#include "stdiolib.h" X X#if defined(BSD) X# include <values.h> X# define INT_MAX (MAXINT) X#endif X X#if defined(MINIX) || defined(MSDOS) || defined(__MSDOS__) X# include <limits.h> X#endif X X/*LINTLIBRARY*/ X Xint fwrite(p, size, nitems, fp) X Xvoid *p; /* buffer */ Xunsigned int size; /* size of item */ Xunsigned int nitems; /* number of items */ XFILE *fp; /* stream */ X X{ X int wrote; /* bytes written in write call */ X int write(); /* write call */ X X/* Phase 1 --- Amount to write overflows INT_MAX */ X unsigned int burstsize; /* size aligned chunk to write */ X unsigned int itemsperburst; /* items written per burst */ X unsigned int itemsleft; /* items left */ X X/* Phase 2 --- Write remainder */ X unsigned int bytestotal; /* total number of bytes */ X unsigned int bytesleft; /* total number of bytes left */ X unsigned int copybytes; /* bytes to copy */ X unsigned char *q; /* pointer into putc buffer */ X X/* Items left to write */ X if ((itemsleft = nitems) == 0 || size == 0) X return 0; X X/* Very large objects */ X if ((itemsperburst = INT_MAX / size) == 0) { X (void) fflush(fp); X do { X bytesleft = size; X do { X if ((burstsize = INT_MAX) > bytesleft) X burstsize = bytesleft; X if ((wrote = write(fileno(fp), (char *) p, (int) burstsize)) != X burstsize) X return (nitems-itemsleft); X p = (void *) ((unsigned char *) p + burstsize); X } while ((bytesleft -= burstsize) != 0); X } while (--itemsleft); X return nitems; X } X X/* Write large amounts of data for small objects */ X for (burstsize = itemsperburst * size; itemsleft > itemsperburst; ) { X if (itemsleft == nitems) X (void) fflush(fp); X if ((wrote = write(fileno(fp), (char *) p, (int) burstsize)) != burstsize) { X if (wrote == -1) X wrote = 0; X return (nitems-itemsleft) + (wrote/size); X } X p = (void *) ((unsigned char *) p + burstsize); X itemsleft -= itemsperburst; X } X X/* At this point itemsleft contains the number of items left to X * write. The output buffer may not be empty. itemsleft*size X * will not overflow INT_MAX. X */ X X if ((bytesleft = bytestotal = size*itemsleft) != 0) { X X/* Unallocated buffer */ X if (! HASBUFFER(fp)) { X if (_allocbuf(fp) < 0) X return (nitems-itemsleft); X SETCLEANUP(); X } X X/* Partially filled write buffer */ X if (! TESTFLAG(fp, _IONBF) && X bytesleft <= UNUSEDINWRITEBUFFER(fp) + BUFFERSIZE(fp)) { X do { X if ((copybytes = UNUSEDINWRITEBUFFER(fp)) > bytesleft) X copybytes = bytesleft; X wrote = copybytes; X q = GETWRITEPTR(fp); X UNROLL_DO(fwritebytes, copybytes, (*q++ = *((unsigned char *) p)++)); X SETWRITEPTR(fp, q); X if (bytesleft > wrote && fflush(fp) == EOF) X break; X } while ((bytesleft -= wrote) != 0); X X if (TESTFLAG(fp, _IOLBF) && bytesleft == 0) X (void) fflush(fp); X } X X/* Dump rest of object directly to file */ X else if (fflush(fp) != EOF && X (wrote = write(fileno(fp), (char *) p, (int) bytesleft)) != -1) X bytesleft -= wrote; X } X X return (nitems-itemsleft) + (bytestotal-bytesleft)/size; X} END_OF_FILE if test 3613 -ne `wc -c <'fwrite.c'`; then echo shar: \"'fwrite.c'\" unpacked with wrong size! fi # end of 'fwrite.c' fi if test -f 'gets.c' -a "${1}" != "-c" ; then echo shar: Will not clobber existing file \"'gets.c'\" else echo shar: Extracting \"'gets.c'\" \(1341 characters\) sed "s/^X//" >'gets.c' <<'END_OF_FILE' X/* g e t s X * X * Read a line from stdin. The line is read up until the next X * newline character or EOF is encountered. The newline X * character is not inserted into the buffer, but the buffer X * is terminated with a null character. No checks are made X * that the line will fit into the buffer. The function returns X * a pointer to the buffer. If EOF is encountered before any X * characters have been read, the NULL pointer is returned. X * X * Patchlevel 1.0 X * X * Edit History: X * 02-Sep-1989 Speed up by reading directly from buffer. X */ X X#include "stdiolib.h" X X/*LINTLIBRARY*/ X Xchar *gets(buf) X Xchar *buf; /* input buffer */ X X{ X int ch; /* next character */ X unsigned char *q; /* input buffer pointer */ X unsigned char *s; /* user's buffer pointer */ X unsigned int bytesleft; /* bytes left in current load */ X X if (TESTFLAG(stdout, _IOLBF)) X (void) fflush(stdout); X X for (s = (unsigned char *) buf; ;*s++ = ch) { X if ((bytesleft = BYTESINREADBUFFER(stdin)) != 0) { X q = GETREADPTR(stdin); X UNROLL_DO(fgetsbytes, bytesleft, if ((*s++ = *q++) == '\n') break); X SETREADPTR(stdin, q); X } X if (bytesleft != 0) { X *--s = 0; X return buf; X } X *s = 0; X if ((ch = getc(stdin)) == EOF) X return s == (unsigned char *) buf ? NULL : buf; X if (ch == '\n') X return buf; X } X} END_OF_FILE if test 1341 -ne `wc -c <'gets.c'`; then echo shar: \"'gets.c'\" unpacked with wrong size! fi # end of 'gets.c' fi if test -f 'getw.c' -a "${1}" != "-c" ; then echo shar: Will not clobber existing file \"'getw.c'\" else echo shar: Extracting \"'getw.c'\" \(400 characters\) sed "s/^X//" >'getw.c' <<'END_OF_FILE' X/* g e t w X * X * Read a word from a stream. The function will return EOF on error. X * Because EOF is also a valid integer, it is better to check for X * errors using ferror. X * X * Patchlevel 1.0 X * X * Edit History: X */ X X#include "stdiolib.h" X X/*LINTLIBRARY*/ X Xint getw(fp) X XFILE *fp; /* stream */ X X{ X int w; /* word read */ X X return fread((void *) &w, sizeof(int), 1, fp) == 1 ? w : EOF; X} END_OF_FILE if test 400 -ne `wc -c <'getw.c'`; then echo shar: \"'getw.c'\" unpacked with wrong size! fi # end of 'getw.c' fi if test -f 'printf.c' -a "${1}" != "-c" ; then echo shar: Will not clobber existing file \"'printf.c'\" else echo shar: Extracting \"'printf.c'\" \(449 characters\) sed "s/^X//" >'printf.c' <<'END_OF_FILE' X/* p r i n t f X * X * Formatted output to stdout. The function returns the number of bytes X * output on success and EOF on failure. X * X * Patchlevel 1.0 X * X * Edit History: X */ X X#include "stdiolib.h" X X/*LINTLIBRARY*/ X/*VARARGS1*/ X Xint printf(fmt, va_alist) X Xchar *fmt; /* format */ Xva_dcl X X{ X va_list arg; /* argument vector */ X int v; /* return value */ X X va_start(arg); X v = vfprintf(stdout, fmt, arg); X va_end(arg); X return v; X} END_OF_FILE if test 449 -ne `wc -c <'printf.c'`; then echo shar: \"'printf.c'\" unpacked with wrong size! fi # end of 'printf.c' fi if test -f 'puts.c' -a "${1}" != "-c" ; then echo shar: Will not clobber existing file \"'puts.c'\" else echo shar: Extracting \"'puts.c'\" \(624 characters\) sed "s/^X//" >'puts.c' <<'END_OF_FILE' X/* p u t s X * X * Write a string followed by a newline character onto stdout. The X * null character terminating the string is not written. X * X * The function returns the last character written (the newline) X * on success and EOF on error. X * X * Patchlevel 1.0 X * X * Edit History: X * 03-Sep-1989 Call PUTC() for faster processing of files. Explicit X * flush for line buffered streams. X */ X X#include "stdiolib.h" X X/*LINTLIBRARY*/ X Xint puts(s) X Xchar *s; /* string */ X X{ X return fputs(s, stdout) == EOF || X PUTC('\n',stdout) == EOF || X TESTFLAG(stdout, _IOLBF) && fflush(stdout) == EOF X ? EOF : '\n'; X} END_OF_FILE if test 624 -ne `wc -c <'puts.c'`; then echo shar: \"'puts.c'\" unpacked with wrong size! fi # end of 'puts.c' fi if test -f 'putw.c' -a "${1}" != "-c" ; then echo shar: Will not clobber existing file \"'putw.c'\" else echo shar: Extracting \"'putw.c'\" \(433 characters\) sed "s/^X//" >'putw.c' <<'END_OF_FILE' X/* p u t w X * X * Write a word to a stream. The function putw returns the word written X * or EOF on error. Because EOF is a valid word, it is better for the X * caller to check for errors using ferror. X * X * Patchlevel 1.0 X * X * Edit History: X */ X X#include "stdiolib.h" X X/*LINTLIBRARY*/ X Xint putw(w, fp) X Xint w; /* word to write */ XFILE *fp; /* stream */ X X{ X return fwrite((void *) &w, sizeof(int), 1, fp) == 1 ? w : EOF; X} END_OF_FILE if test 433 -ne `wc -c <'putw.c'`; then echo shar: \"'putw.c'\" unpacked with wrong size! fi # end of 'putw.c' fi if test -f 'rewind.c' -a "${1}" != "-c" ; then echo shar: Will not clobber existing file \"'rewind.c'\" else echo shar: Extracting \"'rewind.c'\" \(330 characters\) sed "s/^X//" >'rewind.c' <<'END_OF_FILE' X/* r e w i n d X * X * This function rewinds a stream to the beginning of the file. X * It is equivalent to fseek(fp, 0L, 0) except that no value X * is returned. X * X * Patchlevel 1.0 X * X * Edit History: X */ X X#include "stdiolib.h" X X/*LINTLIBRARY*/ X Xvoid rewind(fp) X XFILE *fp; /* stream */ X X{ X (void) fseek(fp, 0L, SEEK_SET); X} END_OF_FILE if test 330 -ne `wc -c <'rewind.c'`; then echo shar: \"'rewind.c'\" unpacked with wrong size! fi # end of 'rewind.c' fi if test -f 'scanf.c' -a "${1}" != "-c" ; then echo shar: Will not clobber existing file \"'scanf.c'\" else echo shar: Extracting \"'scanf.c'\" \(442 characters\) sed "s/^X//" >'scanf.c' <<'END_OF_FILE' X/* s c a n f X * X * Formatted input from stdin. The function returns the number items X * scanned from stdin and EOF on failure. X * X * Patchlevel 1.0 X * X * Edit History: X */ X X#include "stdiolib.h" X X/*LINTLIBRARY*/ X/*VARARGS1*/ X Xint scanf(fmt, va_alist) X Xchar *fmt; /* format */ Xva_dcl X X{ X va_list arg; /* argument vector */ X int v; /* return value */ X X va_start(arg); X v = vfscanf(stdin, fmt, arg); X va_end(arg); X return v; X} END_OF_FILE if test 442 -ne `wc -c <'scanf.c'`; then echo shar: \"'scanf.c'\" unpacked with wrong size! fi # end of 'scanf.c' fi if test -f 'setbuf.c' -a "${1}" != "-c" ; then echo shar: Will not clobber existing file \"'setbuf.c'\" else echo shar: Extracting \"'setbuf.c'\" \(851 characters\) sed "s/^X//" >'setbuf.c' <<'END_OF_FILE' X/* s e t b u f X * X * This function causes the specified buffer to be used for IO buffering X * instead of an automatically allocated buffer. It is called immediately X * after the stream has been opened, but before it is read from or X * written to. It is legal to call setbuf after making the stream X * unbuffered. X * X * If buf if NULL, IO will be unbuffered, otherwise it will be fully X * buffered. The manifest constant BUFSIZ in <stdio.h> tells how X * big an array is needed. Line buffering will be initiated if the X * output stream is directed to a terminal. X * X * Patchlevel 1.0 X * X * Edit History: X */ X X#include "stdiolib.h" X X/*LINTLIBRARY*/ X Xvoid setbuf(fp, buf) X XFILE *fp; /* stream */ Xchar *buf; /* buffer */ X X{ X if (buf != NULL) X (void) setvbuf(fp, buf, _IOFBF, BUFSIZ); X else X (void) setvbuf(fp, (char *) NULL, _IONBF, 0); X} END_OF_FILE if test 851 -ne `wc -c <'setbuf.c'`; then echo shar: \"'setbuf.c'\" unpacked with wrong size! fi # end of 'setbuf.c' fi if test -f 'setvbuf.c' -a "${1}" != "-c" ; then echo shar: Will not clobber existing file \"'setvbuf.c'\" else echo shar: Extracting \"'setvbuf.c'\" \(1302 characters\) sed "s/^X//" >'setvbuf.c' <<'END_OF_FILE' X/* s e t v b u f X * X * Set the type of buffering to be used on this stream. This X * must be called before any read or write has been done on X * the stream. It is legal to make a stream unbuffered and X * then at some subsequent time, make it buffered. X * X * Input streams cannot be line buffered. Attempts to do so X * will make them fully buffered instead. X * X * Patchlevel 1.0 X * X * Edit History: X */ X X#include "stdiolib.h" X X/*LINTLIBRARY*/ X Xint setvbuf(fp, buf, type, size) X XFILE *fp; /* stream */ Xchar *buf; /* buffer */ Xint type; /* type of buffering */ Xunsigned size; /* size of buffer */ X X{ X void *malloc(); /* memory allocator */ X X if (TESTFLAG(fp, _IONBF)) X fp->_base = NULL; X X if (fp->_base != NULL) X return EOF; X X CLEARFLAG(fp, (_IOFBF | _IONBF | _IOLBF)); X X if (type == _IOFBF || type == _IOLBF) { X if (size == 0) X return EOF; X X if (buf == NULL) { X if ((buf = (char *) malloc(size)) == NULL) X return EOF; X else X SETFLAG(fp, _IOMYBUF); X } X X fp->_base = (unsigned char *) buf; X fp->_bufsiz = size; X X if (TESTFLAG(fp, _IOREAD)) X type = _IOFBF; X } X else if (type == _IONBF) { X fp->_base = &fp->_buf; X fp->_bufsiz = sizeof(fp->_buf); X } X else X return EOF; X X SETFLAG(fp, type); X INITBUFFER(fp); X return 0; X} END_OF_FILE if test 1302 -ne `wc -c <'setvbuf.c'`; then echo shar: \"'setvbuf.c'\" unpacked with wrong size! fi # end of 'setvbuf.c' fi if test -f 'sprintf.c' -a "${1}" != "-c" ; then echo shar: Will not clobber existing file \"'sprintf.c'\" else echo shar: Extracting \"'sprintf.c'\" \(588 characters\) sed "s/^X//" >'sprintf.c' <<'END_OF_FILE' X/* s p r i n t f X * X * Formatted output to a string. A null character will be appended X * to the string to mark the end of string. The function returns X * the number of bytes written to the string (excluding the null X * character). X * X * Patchlevel 1.0 X * X * Edit History: X */ X X#include "stdiolib.h" X X/*LINTLIBRARY*/ X/*VARARGS2*/ X Xint sprintf(buf, fmt, va_alist) X Xchar *buf; /* output buffer */ Xchar *fmt; /* format */ Xva_dcl X X{ X va_list arg; /* argument vector */ X int v; /* return value */ X X va_start(arg); X v = vsprintf(buf, fmt, arg); X va_end(arg); X X return v; X} END_OF_FILE if test 588 -ne `wc -c <'sprintf.c'`; then echo shar: \"'sprintf.c'\" unpacked with wrong size! fi # end of 'sprintf.c' fi if test -f 'sscanf.c' -a "${1}" != "-c" ; then echo shar: Will not clobber existing file \"'sscanf.c'\" else echo shar: Extracting \"'sscanf.c'\" \(491 characters\) sed "s/^X//" >'sscanf.c' <<'END_OF_FILE' X/* s s c a n f X * X * Formatted input from a string. The function returns the number X * items scanned from the string and EOF on failure. X * X * Patchlevel 1.0 X * X * Edit History: X */ X X#include "stdiolib.h" X X/*LINTLIBRARY*/ X/*VARARGS2*/ X Xint sscanf(buf, fmt, va_alist) X Xchar *buf; /* output buffer */ Xchar *fmt; /* format */ Xva_dcl X X{ X va_list arg; /* argument vector */ X int v; /* return value */ X X va_start(arg); X v = vsscanf(buf, fmt, arg); X va_end(arg); X X return v; X} END_OF_FILE if test 491 -ne `wc -c <'sscanf.c'`; then echo shar: \"'sscanf.c'\" unpacked with wrong size! fi # end of 'sscanf.c' fi if test -f 'stdio.c' -a "${1}" != "-c" ; then echo shar: Will not clobber existing file \"'stdio.c'\" else echo shar: Extracting \"'stdio.c'\" \(1523 characters\) sed "s/^X//" >'stdio.c' <<'END_OF_FILE' X/* s t d i o X * X * This provides a stream IO buffering scheme which is provides X * higher level IO functions over the lower level functions. X * A file associated with buffering is called a stream and is X * declared to be a pointer to the type FILE. X * X * Normally there are three open streams with pointers declared X * in <stdio.h>: X * X * stdin standard input file X * stdout standard output file X * stderr standard error file X * X * The constant NULL designates the non-existent pointer. The constant X * EOF is returned on eof and by many functions to designate an X * error condition. X * X * Any module which uses this package must include the header file X * at the beginning of the module: X * X * #include <stdio.h> X * X * The following functions are implemented as macros: X * X * getc X * getchar X * putc X * putchar X * feof X * ferror X * fileno X * clearerr X * X * This file contains basic data structure declarations for the stdio X * package. Runtime cleanup is done by _cleanup. This is inserted into X * exit by _flsbuf since cleanup is only required if streams are X * written to. X * X * Patchlevel 1.0 X * X * Edit History: X */ X X#if defined(MSDOS) X#include <fcntl.h> X#endif X X#include "stdiolib.h" X Xstatic struct _iobuf _stdin = {NULL, NULL, NULL, 0, _IOREAD, 0, 0}; Xstatic struct _iobuf _stdout = {NULL, NULL, NULL, 0, _IOWRITE, 1, 0}; Xstatic struct _iobuf _stderr = {NULL, NULL, NULL, 0, _IOWRITE, 2, 0}; X Xstruct _iobuf *_iop[_NFILE] = {&_stdin, &_stdout, &_stderr}; X X#if defined(MSDOS) Xextern int _fmode = O_BINARY; X#endif END_OF_FILE if test 1523 -ne `wc -c <'stdio.c'`; then echo shar: \"'stdio.c'\" unpacked with wrong size! fi # end of 'stdio.c' fi echo shar: End of archive 5 \(of 7\). cp /dev/null ark5isdone MISSING="" for I in 1 2 3 4 5 6 7 ; do if test ! -f ark${I}isdone ; then MISSING="${MISSING} ${I}" fi done if test "${MISSING}" = "" ; then echo You have unpacked all 7 archives. rm -f ark[1-9]isdone else echo You still need to unpack the following archives: echo " " ${MISSING} fi ## End of shell archive. exit 0