A New Input-Output Package
A new package of IO routines is available under the Unix system.
It was designed with the following goals in mind.
It should be similar in spirit to the earlier Portable
Library, and, to the extent possible, be compatible with it.
At the same time a few dubious design choices
in the Portable Library will be corrected.
It must be as efficient as possible, both in time and in space,
so that there will be no hesitation in using it
no matter how critical the application.
It must be simple to use, and also free of the magic
numbers and mysterious calls the use
of which mars the understandability and portability
of many programs using older packages.
The interface provided should be applicable on all machines,
whether or not the programs which implement it are directly portable
or to machines other than the PDP11 running a version of Unix.
It is intended that this package replace the Portable Library.
Although it is not directly compatible, as discussed below,
it is sufficiently similar that
a set of relatively small, inexpensive adaptor routines
exist which make it appear identical to the current Portable Library
except in some very obscure details.
The most crucial difference between this package and the Portable
Library is that the current offering names streams in terms
of pointers rather than by
the integers known as `file descriptors.'
Thus, for example, the routine which opens a named file
returns a pointer to a certain structure rather than a number;
the routine which reads an open file
takes as an argument the pointer returned from the open call.
Each program using the library must have the line
which defines certain macros and variables.
The library containing the routines is `/usr/lib/libS.a,'
so the command to compile is
All names in the include file intended only for internal use begin
with an underscore `\_' to reduce the possibility
of collision with a user name.
The names intended to be visible outside the package are
The name of the standard input file
The name of the standard output file
The name of the standard error file
is actually \-1, and is the value returned by
the read routines on end-of-file or error.
is a notation for the null pointer, returned by
expands to `struct \_iob' and is a useful
shorthand when declaring pointers
of the size suitable for an IO buffer supplied by the user.
.IP "getc, getchar, putc, putchar, feof, ferror, fileno" 10
Their actions are described below;
to point out that it is not possible to
and that they are not actually functions;
thus, for example, they may not have breakpoints set on them.
The routines in this package, like the current Portable
offer the convenience of automatic buffer allocation
and output flushing where appropriate.
Absent, however, is the facility
of changing the default input and output streams
by assigning to `cin' and `cout.'
The names `stdin,' stdout,' and `stderr'
are in effect constants and may not be assigned to.
The routines in the library are in nearly one-to-one
correspondence with those in the Portable Library.
In several cases the name has been changed.
This is an attempt to reduce confusion.
If the attempt is judged to fail the names may be made identical even
the arguments may be different.
The order of this list generally follows the order
used in the Portable Library document.
.sr "FILE *fopen(filename, type)"
opens the file and, if needed, allocates a buffer for it.
is a character string specifying the name.
is a character string (not a single character).
It may be `"r",' `"w",' or `"a"' to indicate
intent to read, write, or append.
The value returned is a file pointer.
If it is null the attempt to open failed.
returns the next character from the stream named by
which is a pointer to a file such as returned by
The integer EOF is returned on end-of-file or when
The null character is a legal character.
on the output stream named by
which is a value returned from
The character is returned as value,
but EOF is returned on error.
The file corresponding to
is closed after any buffers are emptied.
A buffer allocated by the IO system is freed.
is automatic on normal termination of the program.
Any buffered information on the (output) stream named by
Output files are normally buffered
if and only if they are not directed to the terminal,
terminates the process and returns its argument as status
This is a special version of the routine
To terminate without flushing,
returns non-zero when end-of-file
has occurred on the specified input stream.
returns non-zero when an error has occurred while reading
or writing the named stream.
The error indication lasts until the file has been closed.
is identical to `getc(stdin)'.
is identical to `putc(c, stdout)'.
reads characters up to a new-line from the standard input.
The new-line character is replaced by a null character.
It is the user's responsibility to make sure that the character array
returns its argument, or null if end-of-file or error occurred.
.sr "char *fgets(s, n, ioptr)"
characters from the stream
into the character pointer
The read terminates with a new-line character.
The new-line character is placed in the buffer
followed by a null pointer.
or a null pointer if error or end-of-file occurred,
writes the null-terminated string (character array)
writes the null-terminated string (character array)
is pushed back on the input stream named by
Only one character may be pushed back.
.sr "printf(format, a1, . . .)"
.sr "fprintf(ioptr, format, a1, . . .)"
.sr "sprintf(s, format, a1, . . .)"
writes on the standard output.
writes on the named output stream.
puts characters in the character array (string)
The specifications are as usual.
.sr "scanf(format, a1, . . .)"
.sr "fscanf(ioptr, format, a1, . . .)"
.sr "sscanf(s, format, a1, . . .)"
reads from the standard input.
reads from the named input stream.
reads from the character string
The specifications are identical
to those of the Portable Library.
.sr "fread(ptr, sizeof(*ptr), nitems, ioptr)"
It behaves identically to the Portable Library's
that binary IO is being done is required;
when, for portability reasons,
it becomes required, it will be done
by adding an additional character to the mode-string on the
.sr "fwrite(ptr, sizeof(*ptr), nitems, ioptr)"
but in the other direction.
It is not very useful except on input,
since a rewound output file is still open only for output.
are available with specifications identical to those
described for the Portable Library.
.sr "char *calloc(n, sizeof(object))"
returns null when no space is available.
The space is guaranteed to be 0.
is not implemented but there are plausible alternatives.
returns the next word from the input stream named by
EOF is returned on end-of-file or error,
but since this a perfectly good
on the named output stream.
may be used after a stream has been opened
but before IO has started.
the stream will be unbuffered.
Otherwise the buffer supplied will be used.
It is a character array of sufficient size:
returns the integer file descriptor associated with the file.
Several additional routines are available.
.sr "fseek(ioptr, offset, ptrname)"
The location of the next byte in the stream
is 0, the offset is measured from the beginning of the file;
is 1, the offset is measured from the current read or
is 2, the offset is measured from the end of the file.
The routine accounts properly for any buffering.
The byte offset, measured from the beginning of the file,
associated with the named stream is returned.
Any buffering is properly accounted for.
The password file is searched for the given integer user ID.
If an appropriate line is found, it is copied into
If no line is found corresponding to the user ID
The result is positive, zero, or negative according as
is greater than, equal to, or less than
in ASCII collating sequence.
The null-terminated character string
is copied to the location pointed to by
The number of bytes in s up to a null byte
.sr "gcvt(num, ndig, buf)"
is a floating or double quantity.
significant digits are converted to ASCII and placed
The conversion is in Fortran
style, whichever yields the shorter string.
Insignificant trailing zeros are eliminated.