Bell 32V development
[unix-history] / usr / src / libc / stdio / ios.r
CommitLineData
9a8b3379
TL
1.de s
2.sp .5v
3..
4.de sr
5.ft I
6.ne 2
7\\$1
8.if t .sp .2
9.br
10.ft R
11..
12.de it
13\fI\\$1\fR
14..
15.TL
16A New Input-Output Package
17.AU
18D. M. Ritchie
19.AI
20.MH
21.PP
22A new package of IO routines is available.
23It was designed with the following goals in mind.
24.IP 1.
25It should be similar in spirit to the earlier Portable
26Library, and, to the extent possible, be compatible with it.
27At the same time a few dubious design choices
28in the Portable Library will be corrected.
29.IP 2.
30It must be as efficient as possible, both in time and in space,
31so that there will be no hesitation in using it
32no matter how critical the application.
33.IP 3.
34It must be simple to use, and also free of the magic
35numbers and mysterious calls the use
36of which mars the understandability and portability
37of many programs using older packages.
38.IP 4.
39The interface provided should be applicable on all machines,
40whether or not the programs which implement it are directly portable
41to other systems,
42or to machines other than the PDP11 running a version of Unix.
43.PP
44It is intended that this package replace the Portable Library.
45Although it is not directly compatible, as discussed below,
46it is sufficiently similar that
47modifying programs to use it should be a simple exercise.
48.PP
49The most crucial difference between this package and the Portable
50Library is that the current offering names streams in terms
51of pointers rather than by
52the integers known as `file descriptors.'
53Thus, for example, the routine which opens a named file
54returns a pointer to a certain structure rather than a number;
55the routine which reads an open file
56takes as an argument the pointer returned from the open call.
57.SH
58General Usage
59.PP
60Each program using the library must have the line
61.DS
62 #include <stdio.h>
63.DE
64which defines certain macros and variables.
65The library containing the routines is `/usr/lib/libS.a,'
66so the command to compile is
67.DS
68 cc . . . \-lS
69.DE
70All names in the include file intended only for internal use begin
71with an underscore `\_' to reduce the possibility
72of collision with a user name.
73The names intended to be visible outside the package are
74.IP stdin 10
75The name of the standard input file
76.IP stdout 10
77The name of the standard output file
78.IP stderr 10
79The name of the standard error file
80.IP EOF 10
81is actually \-1, and is the value returned by
82the read routines on end-of-file or error.
83.IP NULL 10
84is a notation for the null pointer, returned by
85pointer-valued functions
86to indicate an error
87.IP FILE 10
88expands to `struct \_iob' and is a useful
89shorthand when declaring pointers
90to streams.
91.IP BUFSIZ
92is a number (viz. 512)
93of the size suitable for an IO buffer supplied by the user.
94See
95.it setbuf,
96below.
97.IP "getc, getchar, putc, putchar, feof, ferror, fileno" 10
98
99.br
100are defined as macros.
101Their actions are described below;
102they are mentioned here
103to point out that it is not possible to
104redeclare them
105and that they are not actually functions;
106thus, for example, they may not have breakpoints set on them.
107.PP
108The routines in this package, like the Portable
109Library,
110offer the convenience of automatic buffer allocation
111and output flushing where appropriate.
112Absent, however, is the facility
113of changing the default input and output streams
114by assigning to `cin' and `cout.'
115The names `stdin,' stdout,' and `stderr'
116are in effect constants and may not be assigned to.
117.SH
118Calls
119.PP
120The routines in the library are in nearly one-to-one
121correspondence with those in the Portable Library.
122In several cases the name has been changed.
123This is an attempt to reduce confusion.
124.s
125.sr "FILE *fopen(filename, type) char *filename, *type"
126.it Fopen
127opens the file and, if needed, allocates a buffer for it.
128.it Filename
129is a character string specifying the name.
130.it Type
131is a character string (not a single character).
132It may be `"r",' `"w",' or `"a"' to indicate
133intent to read, write, or append.
134The value returned is a file pointer.
135If it is NULL the attempt to open failed.
136.s
137.sr "FILE *freopen(filename, type, ioptr) char *filename, *type; FILE *ioptr
138The stream named by
139.it ioptr
140is closed, if necessary, and then reopened
141as if by
142.it fopen.
143If the attempt to open fails, NULL is returned,
144otherwise
145.it ioptr,
146which will now refer to the new file.
147Often the reopened stream is
148.it stdin
149or
150.it stdout.
151.s
152.sr "int getc(ioptr) FILE *ioptr
153returns the next character from the stream named by
154.it ioptr,
155which is a pointer to a file such as returned by
156.it fopen,
157or the name
158.it stdin.
159The integer EOF is returned on end-of-file or when
160an error occurs.
161The null character \(aa\e0\(aa is a legal character.
162.s
163.sr "int fgetc(ioptr) FILE *ioptr
164acts like
165.it getc
166but is a genuine function,
167not a macro.
168.s
169.sr "putc(c, ioptr) FILE *ioptr
170.it Putc
171writes the character
172.it c
173on the output stream named by
174.it ioptr,
175which is a value returned from
176.it fopen
177or perhaps
178.it stdout
179or
180.it stderr.
181The character is returned as value,
182but EOF is returned on error.
183.s
184.sr "fputc(c, ioptr) FILE *ioptr
185.it Fputc
186acts like
187.it putc
188but is a genuine
189function, not a macro.
190.s
191.sr "fclose(ioptr) FILE *ioptr
192The file corresponding to
193.it ioptr
194is closed after any buffers are emptied.
195A buffer allocated by the IO system is freed.
196.it Fclose
197is automatic on normal termination of the program.
198.s
199.sr "fflush(ioptr) FILE *ioptr
200Any buffered information on the (output) stream named by
201.it ioptr
202is written out.
203Output files are normally buffered
204if and only if they are not directed to the terminal,
205but
206.it stderr
207is unbuffered unless
208.it setbuf
209is used.
210.s
211.sr exit(errcode)
212.it Exit
213terminates the process and returns its argument as status
214to the parent.
215This is a special version of the routine
216which calls
217.it fflush
218for each output file.
219To terminate without flushing,
220use
221.it \_exit.
222.s
223.sr "feof(ioptr) FILE *ioptr
224returns non-zero when end-of-file
225has occurred on the specified input stream.
226.s
227.sr "ferror(ioptr) FILE *ioptr
228returns non-zero when an error has occurred while reading
229or writing the named stream.
230The error indication lasts until the file has been closed.
231.s
232.sr "getchar( )"
233is identical to
234.it "getc(stdin).
235.s
236.sr "putchar(c)"
237is identical to
238.it "putc(c, stdout).
239.s
240.sr "char *gets(s) char *s
241reads characters up to a new-line from the standard input.
242The new-line character is replaced by a null character.
243It is the user's responsibility to make sure that the character array
244.it s
245is large enough.
246.it Gets
247returns its argument, or NULL if end-of-file or error occurred.
248Note that this routine is not compatible with
249.it fgets;
250it is included for downward compatibility.
251.s
252.sr "char *fgets(s, n, ioptr) char *s; FILE *ioptr
253reads up to
254.it n
255characters from the stream
256.it ioptr
257into the character pointer
258.it s.
259The read terminates with a new-line character.
260The new-line character is placed in the buffer
261followed by a null character.
262The first argument,
263or NULL if error or end-of-file occurred,
264is returned.
265.s
266.sr "puts(s) char *s
267writes the null-terminated string (character array)
268.it s
269on the standard output.
270A new-line is appended.
271No value is returned.
272Note that this routine
273is not compatible with
274.it fputs;
275it is included for downward compatibility.
276.s
277.sr "*fputs(s, ioptr) char *s; FILE *ioptr
278writes the null-terminated string (character array)
279.it s
280on the stream
281.it ioptr.
282No new-line is appended.
283No value is returned.
284.s
285.sr "ungetc(c, ioptr) FILE *ioptr
286The argument character
287.it c
288is pushed back on the input stream named by
289.it ioptr.
290Only one character may be pushed back.
291.s
292.sr "printf(format, a1, . . .) char *format
293.sr "fprintf(ioptr, format, a1, . . .) FILE *ioptr; char *format
294.sr "sprintf(s, format, a1, . . .)char *s, *format
295.it Printf
296writes on the standard output.
297.it Fprintf
298writes on the named output stream.
299.it Sprintf
300puts characters in the character array (string)
301named by
302.it s.
303The specifications are as described in section
304.it "printf
305(III)
306of the Unix Programmer's Manual.
307There is a new conversion:
308.it %m.n\fB\|g\fI
309converts a double argument in the style of
310.it e
311or
312.it f
313as most appropriate.
314.s
315.sr "scanf(format, a1, . . .) char *format
316.sr "fscanf(ioptr, format, a1, . . .) FILE *ioptr; char *format
317.sr "sscanf(s, format, a1, . . .) char *s, *format
318.it Scanf
319reads from the standard input.
320.it Fscanf
321reads from the named input stream.
322.it Sscanf
323reads from the character string
324supplied as
325.it s.
326The specifications are identical
327to those of the Portable Library.
328.it Scanf
329reads characters, interprets
330them according to a format, and stores the results in its arguments.
331It expects as arguments
332a control string
333.it format,
334described below,
335and a set of arguments,
336.I
337each of which must be a pointer,
338.R
339indicating where the converted input should be stored.
340.PP
341The
342control string
343usually contains
344conversion specifications, which are used to direct interpretation
345of input sequences.
346The control string may contain:
347.IP 1.
348Blanks, tabs or newlines, which are ignored.
349.IP 2.
350Ordinary characters (not %) which are expected to match
351the next non-space character of the input stream
352(where space characters are defined as blank, tab or newline).
353.IP 3.
354Conversion specifications, consisting of the
355character %, an optional assignment suppressing character \**,
356an optional numerical maximum field width, and a conversion
357character.
358.PP
359A conversion specification is used to direct the conversion of the
360next input field; the result
361is placed in the variable pointed to by the corresponding argument,
362unless assignment suppression was
363indicated by the \** character.
364An input field is defined as a string of non-space characters;
365it extends either to the next space character or until the field
366width, if specified, is exhausted.
367.PP
368The conversion character indicates the interpretation of the
369input field; the corresponding pointer argument must
370usually be of a restricted type.
371The following conversion characters are legal:
372.IP %
373indicates that a single % character is expected
374in the input stream at this point;
375no assignment is done.
376.IP d
377indicates that a decimal integer is expected
378in the input stream;
379the corresponding argument should be an integer pointer.
380.IP o
381indicates that an octal integer is expected in the
382input stream; the corresponding argument should be a integer pointer.
383.IP x
384indicates that a hexadecimal integer is expected in the input stream;
385the corresponding argument should be an integer pointer.
386.ti -0.2i
387.IP s
388indicates that a character string is expected;
389the corresponding argument should be a character pointer
390pointing to an array of characters large enough to accept the
391string and a terminating `\e0', which will be added.
392The input field is terminated by a space character
393or a newline.
394.IP c
395indicates that a character is expected; the
396corresponding argument should be a character pointer;
397the next input character is placed at the indicated spot.
398The normal skip over space characters is suppressed
399in this case;
400to read the next non-space character, try
401.I
402%1s.
403.R
404If a field width is given, the corresponding argument
405should refer to a character array, and the
406indicated number of characters is read.
407.IP e
408(or
409.I f\|\fR)
410.R
411indicates that a floating point number is expected in the input stream;
412the next field is converted accordingly and stored through the
413corresponding argument, which should be a pointer to a
414.it float.
415The input format for
416floating point numbers is
417an optionally signed
418string of digits
419possibly containing a decimal point, followed by an optional
420exponent field beginning with an E or e followed by an optionally signed integer.
421.IP [
422indicates a string not to be delimited by space characters.
423The left bracket is followed by a set of characters and a right
424bracket; the characters between the brackets define a set
425of characters making up the string.
426If the first character
427is not circumflex (\|^\|), the input field
428is all characters until the first character not in the set between
429the brackets; if the first character
430after the left bracket is ^, the input field is all characters
431until the first character which is in the remaining set of characters
432between the brackets.
433The corresponding argument must point to a character array.
434.PP
435The conversion characters
436.I
437d, o
438.R
439and
440.I
441x
442.R
443may be capitalized or preceded
444by
445.I
446l
447.R
448to indicate that a pointer to
449.I
450long
451.R
452rather than
453.I
454int
455.R
456is expected.
457Similarly, the conversion characters
458.I
459e
460.R
461or
462.I
463f
464.R
465may be capitalized or
466preceded by
467.I
468l
469.R
470to indicate that a pointer to
471.I
472double
473.R
474rather than
475.I
476float
477.R
478is in the argument list.
479The character
480.I
481h
482.R
483will function similarly in the future to indicate
484.I
485short
486.R
487data items.
488.PP
489For example, the call
490.DS
491int i; float x; char name[50];
492scanf( "%d%f%s", &i, &x, name);
493.DE
494with the input line
495.DS
49625 54.32E\(mi1 thompson
497.DE
498will assign to
499.it i
500the value
50125,
502.it x
503the value 5.432, and
504.it name
505will contain
506.it ``thompson\e0''.
507Or,
508.DS
509int i; float x; char name[50];
510scanf("%2d%f%\**d%[1234567890]", &i, &x, name);
511.DE
512with input
513.DS
51456789 0123 56a72
515.DE
516will assign 56 to
517.it i,
518789.0 to
519.it x,
520skip ``0123'',
521and place the string ``56\e0'' in
522.it name.
523The next call to
524.it getchar
525will return `a'.
526.PP
527.it Scanf
528returns as its value the number of successfully matched and assigned input
529items.
530This can be used to decide how many input items were found.
531On end of file, EOF is returned; note that this is different
532from 0, which means that the next input character does not
533match what was called for in the control string.
534.s
535.sr "fread(ptr, sizeof(*ptr), nitems, ioptr) FILE *ioptr
536reads
537.it nitems
538of data beginning at
539.it ptr
540from file
541.it ioptr.
542It behaves identically to the Portable Library's
543.it cread.
544No advance notification
545that binary IO is being done is required;
546when, for portability reasons,
547it becomes required, it will be done
548by adding an additional character to the mode-string on the
549fopen call.
550.s
551.sr "fwrite(ptr, sizeof(*ptr), nitems, ioptr) FILE *ioptr
552Like
553.it fread,
554but in the other direction.
555.s
556.sr "rewind(ioptr) FILE *ioptr
557rewinds the stream
558named by
559.it ioptr.
560It is not very useful except on input,
561since a rewound output file is still open only for output.
562.s
563.sr "system(string) char *string
564The
565.it string
566is executed by the shell as if typed at the terminal.
567.s
568.sr "getw(ioptr) FILE *ioptr
569returns the next word from the input stream named by
570.it ioptr.
571EOF is returned on end-of-file or error,
572but since this a perfectly good
573integer
574.it feof
575and
576.it ferror
577should be used.
578.s
579.sr "putw(w, ioptr) FILE *ioptr
580writes the integer
581.it w
582on the named output stream.
583.s
584.sr "setbuf(ioptr, buf) FILE *ioptr; char *buf
585.it Setbuf
586may be used after a stream has been opened
587but before IO has started.
588If
589.it buf
590is NULL,
591the stream will be unbuffered.
592Otherwise the buffer supplied will be used.
593It is a character array of sufficient size:
594.DS
595char buf[BUFSIZ];
596.DE
597.s
598.sr "fileno(ioptr) FILE *ioptr
599returns the integer file descriptor associated with the file.
600.s
601.sr "fseek(ioptr, offset, ptrname) FILE *ioptr; long offset
602The location of the next byte in the stream
603named by
604.it ioptr
605is adjusted.
606.it Offset
607is a long integer.
608If
609.it ptrname
610is 0, the offset is measured from the beginning of the file;
611if
612.it ptrname
613is 1, the offset is measured from the current read or
614write pointer;
615if
616.it ptrname
617is 2, the offset is measured from the end of the file.
618The routine accounts properly for any buffering.
619(When this routine is used on non-Unix systems,
620the offset must be a value returned from
621.it ftell
622and the ptrname must be 0).
623.s
624.sr "long ftell(ioptr) FILE *ioptr
625The byte offset, measured from the beginning of the file,
626associated with the named stream is returned.
627Any buffering is properly accounted for.
628(On non-Unix systems the value of this call is useful only
629for handing to
630.it fseek,
631so as to position the file to the same place it was when
632.it ftell
633was called.)
634.s
635.sr "getpw(uid, buf) char *buf
636The password file is searched for the given integer user ID.
637If an appropriate line is found, it is copied into
638the character array
639.it buf,
640and 0 is returned.
641If no line is found corresponding to the user ID
642then 1 is returned.
643.s
644.sr "char *calloc(num, size)
645allocates space for
646.it num
647items each of size
648.it size.
649The space is guaranteed to be set to 0 and the pointer is
650sufficiently well aligned to be usable for any purpose.
651NULL is returned if no space is available.
652.s
653.sr "cfree(ptr) char *ptr
654Space is returned to the pool used by
655.it calloc.
656Disorder can be expected if the pointer was not obtained
657from
658.it calloc.
659.LP
660The following are macros defined by stdio.h.
661.s
662.sr isalpha(c)
663returns non-zero if the argument is alphabetic.
664.s
665.sr isupper(c)
666returns non-zero if the argument is upper-case alphabetic.
667.s
668.sr islower(c)
669returns non-zero if the argument is lower-case alphabetic.
670.s
671.sr isdigit(c)
672returns non-zero if the argument is a digit.
673.s
674.sr isspace(c)
675returns non-zero if the argument is a spacing character:
676tab, new-line, carriage return, vertical tab,
677form feed, space.
678.s
679.sr toupper(c)
680returns the upper-case character corresponding to the lower-case
681letter c.
682.s
683.sr tolower(c)
684returns the lower-case character corresponding to the upper-case
685letter c.