BSD 4_3 release
[unix-history] / usr / doc / ps2 / 03.uprog / p9
CommitLineData
95f51977
C
1.\" @(#)p9 6.1 (Berkeley) 4/25/86
2.\"
c9528a00
C
3.sp 100
4.TL
5.ft R
6Appendix \(em The Standard I/O Library
7.AU
8D. M. Ritchie
9.AI
10.MH
11.PP
12The standard I/O library
13was designed with the following goals in mind.
14.IP 1.
15It must be as efficient as possible, both in time and in space,
16so that there will be no hesitation in using it
17no matter how critical the application.
18.IP 2.
19It must be simple to use, and also free of the magic
20numbers and mysterious calls
21whose use mars the understandability and portability
22of many programs using older packages.
23.IP 3.
24The interface provided should be applicable on all machines,
25whether or not the programs which implement it are directly portable
26to other systems,
27or to machines other than the PDP-11 running a version of
28.UC UNIX .
29.SH
301. General Usage
31.PP
32Each program using the library must have the line
33.P1
34 #include <stdio.h>
35.P2
36which defines certain macros and variables.
37The routines are in the normal C library,
38so no special library argument is needed for loading.
39All names in the include file intended only for internal use begin
40with an underscore
41.UL _
42to reduce the possibility
43of collision with a user name.
44The names intended to be visible outside the package are
45.IP \f3stdin\f1 10
46The name of the standard input file
47.IP \f3stdout\f1 10
48The name of the standard output file
49.IP \f3stderr\f1 10
50The name of the standard error file
51.IP \f3EOF\f1 10
52is actually \-1, and is the value returned by
53the read routines on end-of-file or error.
54.IP \f3NULL\f1 10
55is a notation for the null pointer, returned by
56pointer-valued functions
57to indicate an error
58.IP \f3FILE\f1 10
59expands to
60.UL struct
61.UL _iob
62and is a useful
63shorthand when declaring pointers
64to streams.
65.IP \f3BUFSIZ\f1 10
66is a number (viz. 512)
67of the size suitable for an I/O buffer supplied by the user.
68See
69.UL setbuf ,
70below.
71.IP \f3getc,\ getchar,\ putc,\ putchar,\ feof,\ ferror,\ f\&ileno\f1 10
72.br
73are defined as macros.
74Their actions are described below;
75they are mentioned here
76to point out that it is not possible to
77redeclare them
78and that they are not actually functions;
79thus, for example, they may not have breakpoints set on them.
80.PP
81The routines in this package
82offer the convenience of automatic buffer allocation
83and output flushing where appropriate.
84The names
85.UL stdin ,
86.UL stdout ,
87and
88.UL stderr
89are in effect constants and may not be assigned to.
90.SH
912. Calls
92.nr PD .4v
93.LP
94.UL FILE\ *fopen(filename,\ type)\ char\ *filename,\ *type;
95.nr PD 0
96.IP
97.br
98opens the file and, if needed, allocates a buffer for it.
99.UL filename
100is a character string specifying the name.
101.UL type
102is a character string (not a single character).
103It may be
104.UL \&"r" ,
105.UL \&"w" ,
106or
107.UL \&"a"
108to indicate
109intent to read, write, or append.
110The value returned is a file pointer.
111If it is
112.UL NULL
113the attempt to open failed.
114.ne 3
115.nr PD .4v
116.LP
117.UL FILE\ *freopen(filename,\ type,\ ioptr)\ char\ *filename,\ *type;\ FILE\ *ioptr;
118.nr PD 0
119.IP
120.br
121The stream named by
122.UL ioptr
123is closed, if necessary, and then reopened
124as if by
125.UL fopen .
126If the attempt to open fails,
127.UL NULL
128is returned,
129otherwise
130.UL ioptr ,
131which will now refer to the new file.
132Often the reopened stream is
133.UL stdin
134or
135.UL stdout .
136.nr PD .4v
137.LP
138.UL int\ getc(ioptr)\ FILE\ *ioptr;
139.nr PD 0
140.IP
141returns the next character from the stream named by
142.UL ioptr ,
143which is a pointer to a file such as returned by
144.UL fopen ,
145or the name
146.UL stdin .
147The integer
148.UL EOF
149is returned on end-of-file or when
150an error occurs.
151The null character
152.UL \e0
153is a legal character.
154.nr PD .4v
155.LP
156.UL int\ fgetc(ioptr)\ FILE\ *ioptr;
157.nr PD 0
158.IP
159.br
160acts like
161.UL getc
162but is a genuine function,
163not a macro,
164so it can be pointed to, passed as an argument, etc.
165.nr PD .4v
166.LP
167.UL putc(c,\ ioptr)\ FILE\ *ioptr;
168.nr PD 0
169.IP
170.br
171.UL putc
172writes the character
173.UL c
174on the output stream named by
175.UL ioptr ,
176which is a value returned from
177.UL fopen
178or perhaps
179.UL stdout
180or
181.UL stderr .
182The character is returned as value,
183but
184.UL EOF
185is returned on error.
186.nr PD .4v
187.LP
188.UL fputc(c,\ ioptr)\ FILE\ *ioptr;
189.nr PD 0
190.IP
191.br
192acts like
193.UL putc
194but is a genuine
195function, not a macro.
196.nr PD .4v
197.LP
198.UL fclose(ioptr)\ FILE\ *ioptr;
199.nr PD 0
200.IP
201.br
202The file corresponding to
203.UL ioptr
204is closed after any buffers are emptied.
205A buffer allocated by the I/O system is freed.
206.UL fclose
207is automatic on normal termination of the program.
208.nr PD .4v
209.LP
210.UL fflush(ioptr)\ FILE\ *ioptr;
211.nr PD 0
212.IP
213.br
214Any buffered information on the (output) stream named by
215.UL ioptr
216is written out.
217Output files are normally buffered
218if and only if they are not directed to the terminal;
219however,
220.UL stderr
221always starts off unbuffered and remains so unless
222.UL setbuf
223is used, or unless it is reopened.
224.nr PD .4v
225.LP
226.UL exit(errcode);
227.nr PD 0
228.IP
229.br
230terminates the process and returns its argument as status
231to the parent.
232This is a special version of the routine
233which calls
234.UL fflush
235for each output file.
236To terminate without flushing,
237use
238.UL _exit .
239.nr PD .4v
240.LP
241.UL feof(ioptr)\ FILE\ *ioptr;
242.nr PD 0
243.IP
244.br
245returns non-zero when end-of-file
246has occurred on the specified input stream.
247.nr PD .4v
248.LP
249.UL ferror(ioptr)\ FILE\ *ioptr;
250.nr PD 0
251.IP
252.br
253returns non-zero when an error has occurred while reading
254or writing the named stream.
255The error indication lasts until the file has been closed.
256.nr PD .4v
257.LP
258.UL getchar();
259.nr PD 0
260.IP
261.br
262is identical to
263.UL getc(stdin) .
264.nr PD .4v
265.LP
266.UL putchar(c);
267.nr PD 0
268.IP
269.br
270is identical to
271.UL putc(c,\ stdout) .
272.nr PD .4v
273.nr PD .4v
274.ne 2
275.LP
276.UL char\ *fgets(s,\ n,\ ioptr)\ char\ *s;\ FILE\ *ioptr;
277.nr PD 0
278.IP
279.br
280reads up to
281.UL n-1
282characters from the stream
283.UL ioptr
284into the character pointer
285.UL s .
286The read terminates with a newline character.
287The newline character is placed in the buffer
288followed by a null character.
289.UL fgets
290returns the first argument,
291or
292.UL NULL
293if error or end-of-file occurred.
294.nr PD .4v
295.nr PD .4v
296.LP
297.UL fputs(s,\ ioptr)\ char\ *s;\ FILE\ *ioptr;
298.nr PD 0
299.IP
300.br
301writes the null-terminated string (character array)
302.UL s
303on the stream
304.UL ioptr .
305No newline is appended.
306No value is returned.
307.nr PD .4v
308.LP
309.UL ungetc(c,\ ioptr)\ FILE\ *ioptr;
310.nr PD 0
311.IP
312.br
313The argument character
314.UL c
315is pushed back on the input stream named by
316.UL ioptr .
317Only one character may be pushed back.
318.ne 5
319.nr PD .4v
320.LP
321.UL printf(format,\ a1,\ ...)\ char\ *format;
322.br
323.UL fprintf(ioptr,\ format,\ a1,\ ...)\ FILE\ *ioptr;\ char\ *format;
324.br
325.UL sprintf(s,\ format,\ a1,\ ...)char\ *s,\ *format;
326.br
327.nr PD 0
328.IP
329.UL printf
330writes on the standard output.
331.UL fprintf
332writes on the named output stream.
333.UL sprintf
334puts characters in the character array (string)
335named by
336.UL s .
337The specifications are as described in section
338.UL printf (3)
339of the
340.ul
341.UC UNIX
342.ul
343Programmer's Manual.
344.nr PD .4v
345.LP
346.UL scanf(format,\ a1,\ ...)\ char\ *format;
347.br
348.UL fscanf(ioptr,\ format,\ a1,\ ...)\ FILE\ *ioptr;\ char\ *format;
349.br
350.UL sscanf(s,\ format,\ a1,\ ...)\ char\ *s,\ *format;
351.nr PD 0
352.IP
353.br
354.UL scanf
355reads from the standard input.
356.UL fscanf
357reads from the named input stream.
358.UL sscanf
359reads from the character string
360supplied as
361.UL s .
362.UL scanf
363reads characters, interprets
364them according to a format, and stores the results in its arguments.
365Each routine expects as arguments
366a control string
367.UL format ,
368and a set of arguments,
369.I
370each of which must be a pointer,
371.R
372indicating where the converted input should be stored.
373.if t .sp .4v
374.UL scanf
375returns as its value the number of successfully matched and assigned input
376items.
377This can be used to decide how many input items were found.
378On end of file,
379.UL EOF
380is returned; note that this is different
381from 0, which means that the next input character does not
382match what was called for in the control string.
383.RE
384.nr PD .4v
385.LP
386.UL fread(ptr,\ sizeof(*ptr),\ nitems,\ ioptr)\ FILE\ *ioptr;
387.nr PD 0
388.IP
389.br
390reads
391.UL nitems
392of data beginning at
393.UL ptr
394from file
395.UL ioptr .
396No advance notification
397that binary I/O is being done is required;
398when, for portability reasons,
399it becomes required, it will be done
400by adding an additional character to the mode-string on the
401.UL fopen
402call.
403.nr PD .4v
404.LP
405.UL fwrite(ptr,\ sizeof(*ptr),\ nitems,\ ioptr)\ FILE\ *ioptr;
406.nr PD 0
407.IP
408.br
409Like
410.UL fread ,
411but in the other direction.
412.nr PD .4v
413.LP
414.UL rewind(ioptr)\ FILE\ *ioptr;
415.nr PD 0
416.IP
417.br
418rewinds the stream
419named by
420.UL ioptr .
421It is not very useful except on input,
422since a rewound output file is still open only for output.
423.nr PD .4v
424.LP
425.UL system(string)\ char\ *string;
426.nr PD 0
427.IP
428.br
429The
430.UL string
431is executed by the shell as if typed at the terminal.
432.nr PD .4v
433.LP
434.UL getw(ioptr)\ FILE\ *ioptr;
435.nr PD 0
436.IP
437.br
438returns the next word from the input stream named by
439.UL ioptr .
440.UL EOF
441is returned on end-of-file or error,
442but since this a perfectly good
443integer
444.UL feof
445and
446.UL ferror
447should be used.
448A ``word'' is 16 bits on the
449.UC PDP-11.
450.nr PD .4v
451.LP
452.UL putw(w,\ ioptr)\ FILE\ *ioptr;
453.nr PD 0
454.IP
455.br
456writes the integer
457.UL w
458on the named output stream.
459.nr PD .4v
460.LP
461.UL setbuf(ioptr,\ buf)\ FILE\ *ioptr;\ char\ *buf;
462.nr PD 0
463.IP
464.br
465.UL setbuf
466may be used after a stream has been opened
467but before I/O has started.
468If
469.UL buf
470is
471.UL NULL ,
472the stream will be unbuffered.
473Otherwise the buffer supplied will be used.
474It must be a character array of sufficient size:
475.P1
476char buf[BUFSIZ];
477.P2
478.nr PD .4v
479.LP
480.UL fileno(ioptr)\ FILE\ *ioptr;
481.nr PD 0
482.IP
483.br
484returns the integer file descriptor associated with the file.
485.nr PD .4v
486.LP
487.UL fseek(ioptr,\ offset,\ ptrname)\ FILE\ *ioptr;\ long\ offset;
488.nr PD 0
489.IP
490.br
491The location of the next byte in the stream
492named by
493.UL ioptr
494is adjusted.
495.UL offset
496is a long integer.
497If
498.UL ptrname
499is 0, the offset is measured from the beginning of the file;
500if
501.UL ptrname
502is 1, the offset is measured from the current read or
503write pointer;
504if
505.UL ptrname
506is 2, the offset is measured from the end of the file.
507The routine accounts properly for any buffering.
508(When this routine is used on
509.UC UNIX \& non-
510systems,
511the offset must be a value returned from
512.UL ftell
513and the ptrname must be 0).
514.ne 3
515.nr PD .4v
516.LP
517.UL long\ ftell(ioptr)\ FILE\ *ioptr;
518.nr PD 0
519.IP
520.br
521The byte offset, measured from the beginning of the file,
522associated with the named stream is returned.
523Any buffering is properly accounted for.
524(On
525.UC UNIX \& non-
526systems the value of this call is useful only
527for handing to
528.UL fseek ,
529so as to position the file to the same place it was when
530.UL ftell
531was called.)
532.nr PD .4v
533.LP
534.UL getpw(uid,\ buf)\ char\ *buf;
535.nr PD 0
536.IP
537.br
538The password file is searched for the given integer user ID.
539If an appropriate line is found, it is copied into
540the character array
541.UL buf ,
542and 0 is returned.
543If no line is found corresponding to the user ID
544then 1 is returned.
545.nr PD .4v
546.LP
547.UL char\ *malloc(num);
548.nr PD 0
549.IP
550.br
551allocates
552.UL num
553bytes.
554The pointer returned is sufficiently well aligned to be usable for any purpose.
555.UL NULL
556is returned if no space is available.
557.nr PD .4v
558.LP
559.UL char\ *calloc(num,\ size);
560.nr PD 0
561.IP
562.br
563allocates space for
564.UL num
565items each of size
566.UL size .
567The space is guaranteed to be set to 0 and the pointer is
568sufficiently well aligned to be usable for any purpose.
569.UL NULL
570is returned if no space is available .
571.nr PD .4v
572.LP
573.UL cfree(ptr)\ char\ *ptr;
574.nr PD 0
575.IP
576.br
577Space is returned to the pool used by
578.UL calloc .
579Disorder can be expected if the pointer was not obtained
580from
581.UL calloc .
582.nr PD .4v
583.LP
584The following are macros whose definitions may be obtained by including
585.UL <ctype.h> .
586.nr PD .4v
587.LP
588.UL isalpha(c)
589returns non-zero if the argument is alphabetic.
590.nr PD .4v
591.LP
592.UL isupper(c)
593returns non-zero if the argument is upper-case alphabetic.
594.nr PD .4v
595.LP
596.UL islower(c)
597returns non-zero if the argument is lower-case alphabetic.
598.nr PD .4v
599.LP
600.UL isdigit(c)
601returns non-zero if the argument is a digit.
602.nr PD .4v
603.LP
604.UL isspace(c)
605returns non-zero if the argument is a spacing character:
606tab, newline, carriage return, vertical tab,
607form feed, space.
608.nr PD .4v
609.LP
610.UL ispunct(c)
611returns non-zero if the argument is
612any punctuation character, i.e., not a space, letter,
613digit or control character.
614.nr PD .4v
615.LP
616.UL isalnum(c)
617returns non-zero if the argument is a letter or a digit.
618.nr PD .4v
619.LP
620.UL isprint(c)
621returns non-zero if the argument is printable \(em
622a letter, digit, or punctuation character.
623.nr PD .4v
624.LP
625.UL iscntrl(c)
626returns non-zero if the argument is a control character.
627.nr PD .4v
628.LP
629.UL isascii(c)
630returns non-zero if the argument is an ascii character, i.e., less than octal 0200.
631.nr PD .4v
632.LP
633.UL toupper(c)
634returns the upper-case character corresponding to the lower-case
635letter
636.UL c.
637.nr PD .4v
638.LP
639.UL tolower(c)
640returns the lower-case character corresponding to the upper-case
641letter
642.UL c .