Bell 32V development
[unix-history] / usr / doc / ctour / ios.r
CommitLineData
05841234
TL
1.de sr
2.sp 1
3.ft I
4.ne 2
5\\$1
6.if t .sp .2
7.br
8.ft R
9..
10.de it
11\fI\\$1\fR
12..
13.TL
14A New Input-Output Package
15.AU
16D. M. Ritchie
17.PP
18A new package of IO routines is available under the Unix system.
19It was designed with the following goals in mind.
20.IP 1.
21It should be similar in spirit to the earlier Portable
22Library, and, to the extent possible, be compatible with it.
23At the same time a few dubious design choices
24in the Portable Library will be corrected.
25.IP 2.
26It must be as efficient as possible, both in time and in space,
27so that there will be no hesitation in using it
28no matter how critical the application.
29.IP 3.
30It must be simple to use, and also free of the magic
31numbers and mysterious calls the use
32of which mars the understandability and portability
33of many programs using older packages.
34.IP 4.
35The interface provided should be applicable on all machines,
36whether or not the programs which implement it are directly portable
37to other systems,
38or to machines other than the PDP11 running a version of Unix.
39.PP
40It is intended that this package replace the Portable Library.
41Although it is not directly compatible, as discussed below,
42it is sufficiently similar that
43a set of relatively small, inexpensive adaptor routines
44exist which make it appear identical to the current Portable Library
45except in some very obscure details.
46.PP
47The most crucial difference between this package and the Portable
48Library is that the current offering names streams in terms
49of pointers rather than by
50the integers known as `file descriptors.'
51Thus, for example, the routine which opens a named file
52returns a pointer to a certain structure rather than a number;
53the routine which reads an open file
54takes as an argument the pointer returned from the open call.
55.SH
56General Usage
57.RT
58Each program using the library must have the line
59.DS
60 #include <stdio.h>
61.DE
62which defines certain macros and variables.
63The library containing the routines is `/usr/lib/libS.a,'
64so the command to compile is
65.DS
66 cc . . . \-lS
67.DE
68All names in the include file intended only for internal use begin
69with an underscore `\_' to reduce the possibility
70of collision with a user name.
71The names intended to be visible outside the package are
72.IP stdin 10
73The name of the standard input file
74.IP stdout 10
75The name of the standard output file
76.IP stderr 10
77The name of the standard error file
78.IP EOF 10
79is actually \-1, and is the value returned by
80the read routines on end-of-file or error.
81.IP NULL 10
82is a notation for the null pointer, returned by
83pointer-valued functions
84to indicate an error
85.IP FILE 10
86expands to `struct \_iob' and is a useful
87shorthand when declaring pointers
88to streams.
89.IP BUFSIZ
90is a number (viz. 512)
91of the size suitable for an IO buffer supplied by the user.
92See
93.it setbuf,
94below.
95.IP "getc, getchar, putc, putchar, feof, ferror, fileno" 10
96
97.br
98are defined as macros.
99Their actions are described below;
100they are mentioned here
101to point out that it is not possible to
102redeclare them
103and that they are not actually functions;
104thus, for example, they may not have breakpoints set on them.
105.PP
106The routines in this package, like the current Portable
107Library,
108offer the convenience of automatic buffer allocation
109and output flushing where appropriate.
110Absent, however, is the facility
111of changing the default input and output streams
112by assigning to `cin' and `cout.'
113The names `stdin,' stdout,' and `stderr'
114are in effect constants and may not be assigned to.
115.SH
116Calls
117.RT
118The routines in the library are in nearly one-to-one
119correspondence with those in the Portable Library.
120In several cases the name has been changed.
121This is an attempt to reduce confusion.
122If the attempt is judged to fail the names may be made identical even
123though
124the arguments may be different.
125The order of this list generally follows the order
126used in the Portable Library document.
127.sr "FILE *fopen(filename, type)"
128.it Fopen
129opens the file and, if needed, allocates a buffer for it.
130.it Filename
131is a character string specifying the name.
132.it Type
133is a character string (not a single character).
134It may be `"r",' `"w",' or `"a"' to indicate
135intent to read, write, or append.
136The value returned is a file pointer.
137If it is null the attempt to open failed.
138.sr "int getc(ioptr)"
139returns the next character from the stream named by
140.it ioptr,
141which is a pointer to a file such as returned by
142.it fopen,
143or the name
144.it stdin.
145The integer EOF is returned on end-of-file or when
146an error occurs.
147The null character is a legal character.
148.sr "putc(c, ioptr)"
149.it Putc
150writes the character
151.it c
152on the output stream named by
153.it ioptr,
154which is a value returned from
155.it fopen
156or perhaps
157.it stdout
158or
159.it stderr.
160The character is returned as value,
161but EOF is returned on error.
162.sr fclose(ioptr)
163The file corresponding to
164.it ioptr
165is closed after any buffers are emptied.
166A buffer allocated by the IO system is freed.
167.it Fclose
168is automatic on normal termination of the program.
169.sr fflush(ioptr)
170Any buffered information on the (output) stream named by
171.it ioptr
172is written out.
173Output files are normally buffered
174if and only if they are not directed to the terminal,
175but
176.it stderr
177is unbuffered unless
178.it setbuf
179is used.
180.sr exit(errcode)
181.it Exit
182terminates the process and returns its argument as status
183to the parent.
184This is a special version of the routine
185which calls
186.it fflush
187for each output file.
188To terminate without flushing,
189use
190.it \_exit.
191.sr feof(ioptr)
192returns non-zero when end-of-file
193has occurred on the specified input stream.
194.sr ferror(ioptr)
195returns non-zero when an error has occurred while reading
196or writing the named stream.
197The error indication lasts until the file has been closed.
198.sr "getchar( )"
199is identical to `getc(stdin)'.
200.sr "putchar(c)"
201is identical to `putc(c, stdout)'.
202.sr "char *gets(s)"
203reads characters up to a new-line from the standard input.
204The new-line character is replaced by a null character.
205It is the user's responsibility to make sure that the character array
206.it s
207is large enough.
208.it Gets
209returns its argument, or null if end-of-file or error occurred.
210.sr "char *fgets(s, n, ioptr)"
211reads up to
212.it n
213characters from the stream
214.it ioptr
215into the character pointer
216.it s.
217The read terminates with a new-line character.
218The new-line character is placed in the buffer
219followed by a null pointer.
220The first argument,
221or a null pointer if error or end-of-file occurred,
222is returned.
223.sr puts(s)
224writes the null-terminated string (character array)
225.it s
226on the standard output.
227A new-line is appended.
228No value is returned.
229.sr "fputs(s, ioptr)"
230writes the null-terminated string (character array)
231on the stream
232.it s.
233No new-line is appended.
234No value is returned.
235.sr "ungetc(c, ioptr)"
236The argument character
237.it c
238is pushed back on the input stream named by
239.it ioptr.
240Only one character may be pushed back.
241.sr "printf(format, a1, . . .)"
242.sr "fprintf(ioptr, format, a1, . . .)"
243.sr "sprintf(s, format, a1, . . .)"
244.it Printf
245writes on the standard output.
246.it Fprintf
247writes on the named output stream.
248.it Sprintf
249puts characters in the character array (string)
250named by
251.it s.
252The specifications are as usual.
253.sr "scanf(format, a1, . . .)"
254.sr "fscanf(ioptr, format, a1, . . .)"
255.sr "sscanf(s, format, a1, . . .)"
256.it Scanf
257reads from the standard input.
258.it Fscanf
259reads from the named input stream.
260.it Sscanf
261reads from the character string
262supplied as
263.it s.
264The specifications are identical
265to those of the Portable Library.
266.sr "fread(ptr, sizeof(*ptr), nitems, ioptr)"
267writes
268.it nitems
269of data beginning at
270.it ptr
271on file
272.it ioptr.
273It behaves identically to the Portable Library's
274.it cread.
275No advance notification
276that binary IO is being done is required;
277when, for portability reasons,
278it becomes required, it will be done
279by adding an additional character to the mode-string on the
280fopen call.
281.sr "fwrite(ptr, sizeof(*ptr), nitems, ioptr)"
282Like
283.it fread,
284but in the other direction.
285.sr rewind(ioptr)
286rewinds the stream
287named by
288.it ioptr.
289It is not very useful except on input,
290since a rewound output file is still open only for output.
291.sr system(string)
292.sr atof(s)
293.sr tmpnam(s)
294.sr abort(code)
295.sr "intss( )"
296.sr "cfree(ptr)"
297.sr "wdleng( )"
298are available with specifications identical to those
299described for the Portable Library.
300.sr "char *calloc(n, sizeof(object))"
301returns null when no space is available.
302The space is guaranteed to be 0.
303.sr ftoa
304is not implemented but there are plausible alternatives.
305.sr "nargs( )"
306is not implemented.
307.sr getw(ioptr)
308returns the next word from the input stream named by
309.it ioptr.
310EOF is returned on end-of-file or error,
311but since this a perfectly good
312integer
313.it feof
314and
315.it ferror
316should be used.
317.sr "putw(w, ioptr)"
318writes the integer
319.it w
320on the named output stream.
321.sr "setbuf(ioptr, buf)"
322.it Setbuf
323may be used after a stream has been opened
324but before IO has started.
325If
326.it buf
327is null,
328the stream will be unbuffered.
329Otherwise the buffer supplied will be used.
330It is a character array of sufficient size:
331.DS
332char buf[BUFSIZ];
333.DE
334.sr "fileno(ioptr)"
335returns the integer file descriptor associated with the file.
336.PP
337Several additional routines are available.
338.sr "fseek(ioptr, offset, ptrname)"
339The location of the next byte in the stream
340named by
341.it ioptr
342is adjusted.
343.it Offset
344is a long integer.
345If
346.it ptrname
347is 0, the offset is measured from the beginning of the file;
348if
349.it ptrname
350is 1, the offset is measured from the current read or
351write pointer;
352if
353.it ptrname
354is 2, the offset is measured from the end of the file.
355The routine accounts properly for any buffering.
356.sr "long ftell(iop)"
357The byte offset, measured from the beginning of the file,
358associated with the named stream is returned.
359Any buffering is properly accounted for.
360.sr "getpw(uid, buf)"
361The password file is searched for the given integer user ID.
362If an appropriate line is found, it is copied into
363the character array
364.it buf,
365and 0 is returned.
366If no line is found corresponding to the user ID
367then 1 is returned.
368.sr "strcat(s1, s2)"
369.it S1
370and
371.it s2
372are character pointers.
373The end (null byte)
374of the
375.it s1
376string is found and
377.it s2
378is copied to
379.it s1
380starting there.
381The space pointed to by
382.it s1
383must be large enough.
384.sr "strcmp(s1, s2)"
385The character strings
386.it s1
387and
388.it s2
389are compared.
390The result is positive, zero, or negative according as
391.it s1
392is greater than, equal to, or less than
393.it s2
394in ASCII collating sequence.
395.sr "strcpy(s1, s2)
396The null-terminated character string
397.it s2
398is copied to the location pointed to by
399.it s1.
400.sr "strlen(s)"
401The number of bytes in s up to a null byte
402is returned.
403.it S
404is a character pointer.
405.sr "gcvt(num, ndig, buf)"
406.it Num
407is a floating or double quantity.
408.it Ndig
409significant digits are converted to ASCII and placed
410into the character array
411.it buf.
412The conversion is in Fortran
413.it e
414or
415.it f
416style, whichever yields the shorter string.
417Insignificant trailing zeros are eliminated.