]> git.ipfire.org Git - thirdparty/glibc.git/blame - manual/stdio.texi
Update.
[thirdparty/glibc.git] / manual / stdio.texi
CommitLineData
28f540f4 1@node I/O on Streams, Low-Level I/O, I/O Overview, Top
34992338 2@c %MENU% High-level, portable I/O facilities
28f540f4 3@chapter Input/Output on Streams
838e5ffe
UD
4@c fix an overfull:
5@tex
6\hyphenation{which-ever}
7@end tex
28f540f4
RM
8
9This chapter describes the functions for creating streams and performing
10input and output operations on them. As discussed in @ref{I/O
11Overview}, a stream is a fairly abstract, high-level concept
12representing a communications channel to a file, device, or process.
13
14@menu
15* Streams:: About the data type representing a stream.
19c3f208 16* Standard Streams:: Streams to the standard input and output
28f540f4
RM
17 devices are created for you.
18* Opening Streams:: How to create a stream to talk to a file.
19* Closing Streams:: Close a stream when you are finished with it.
20* Simple Output:: Unformatted output by characters and lines.
21* Character Input:: Unformatted input by characters and words.
22* Line Input:: Reading a line or a record from a stream.
23* Unreading:: Peeking ahead/pushing back input just read.
24* Block Input/Output:: Input and output operations on blocks of data.
25* Formatted Output:: @code{printf} and related functions.
26* Customizing Printf:: You can define new conversion specifiers for
27 @code{printf} and friends.
28* Formatted Input:: @code{scanf} and related functions.
29* EOF and Errors:: How you can tell if an I/O error happens.
30* Binary Streams:: Some systems distinguish between text files
31 and binary files.
32* File Positioning:: About random-access streams.
f65fd747 33* Portable Positioning:: Random access on peculiar ISO C systems.
28f540f4
RM
34* Stream Buffering:: How to control buffering of streams.
35* Other Kinds of Streams:: Streams that do not necessarily correspond
19c3f208 36 to an open file.
0501d603 37* Formatted Messages:: Print strictly formatted messages.
28f540f4
RM
38@end menu
39
40@node Streams
41@section Streams
42
43For historical reasons, the type of the C data structure that represents
44a stream is called @code{FILE} rather than ``stream''. Since most of
45the library functions deal with objects of type @code{FILE *}, sometimes
46the term @dfn{file pointer} is also used to mean ``stream''. This leads
47to unfortunate confusion over terminology in many books on C. This
48manual, however, is careful to use the terms ``file'' and ``stream''
49only in the technical sense.
50@cindex file pointer
51
52@pindex stdio.h
53The @code{FILE} type is declared in the header file @file{stdio.h}.
54
55@comment stdio.h
f65fd747 56@comment ISO
28f540f4
RM
57@deftp {Data Type} FILE
58This is the data type used to represent stream objects. A @code{FILE}
59object holds all of the internal state information about the connection
60to the associated file, including such things as the file position
61indicator and buffering information. Each stream also has error and
62end-of-file status indicators that can be tested with the @code{ferror}
63and @code{feof} functions; see @ref{EOF and Errors}.
64@end deftp
65
66@code{FILE} objects are allocated and managed internally by the
67input/output library functions. Don't try to create your own objects of
68type @code{FILE}; let the library do it. Your programs should
69deal only with pointers to these objects (that is, @code{FILE *} values)
70rather than the objects themselves.
6d52618b 71@c !!! should say that FILE's have "No user-serviceable parts inside."
28f540f4
RM
72
73@node Standard Streams
74@section Standard Streams
75@cindex standard streams
76@cindex streams, standard
77
78When the @code{main} function of your program is invoked, it already has
79three predefined streams open and available for use. These represent
80the ``standard'' input and output channels that have been established
81for the process.
82
83These streams are declared in the header file @file{stdio.h}.
84@pindex stdio.h
85
86@comment stdio.h
f65fd747 87@comment ISO
28f540f4
RM
88@deftypevar {FILE *} stdin
89The @dfn{standard input} stream, which is the normal source of input for the
90program.
91@end deftypevar
92@cindex standard input stream
93
94@comment stdio.h
f65fd747 95@comment ISO
28f540f4
RM
96@deftypevar {FILE *} stdout
97The @dfn{standard output} stream, which is used for normal output from
98the program.
99@end deftypevar
100@cindex standard output stream
101
102@comment stdio.h
f65fd747 103@comment ISO
28f540f4
RM
104@deftypevar {FILE *} stderr
105The @dfn{standard error} stream, which is used for error messages and
106diagnostics issued by the program.
107@end deftypevar
108@cindex standard error stream
109
110In the GNU system, you can specify what files or processes correspond to
111these streams using the pipe and redirection facilities provided by the
112shell. (The primitives shells use to implement these facilities are
113described in @ref{File System Interface}.) Most other operating systems
114provide similar mechanisms, but the details of how to use them can vary.
115
116In the GNU C library, @code{stdin}, @code{stdout}, and @code{stderr} are
117normal variables which you can set just like any others. For example, to redirect
118the standard output to a file, you could do:
119
120@smallexample
121fclose (stdout);
122stdout = fopen ("standard-output-file", "w");
123@end smallexample
124
125Note however, that in other systems @code{stdin}, @code{stdout}, and
126@code{stderr} are macros that you cannot assign to in the normal way.
127But you can use @code{freopen} to get the effect of closing one and
128reopening it. @xref{Opening Streams}.
129
130@node Opening Streams
131@section Opening Streams
132
133@cindex opening a stream
134Opening a file with the @code{fopen} function creates a new stream and
135establishes a connection between the stream and a file. This may
19c3f208 136involve creating a new file.
28f540f4
RM
137
138@pindex stdio.h
139Everything described in this section is declared in the header file
140@file{stdio.h}.
141
142@comment stdio.h
f65fd747 143@comment ISO
28f540f4
RM
144@deftypefun {FILE *} fopen (const char *@var{filename}, const char *@var{opentype})
145The @code{fopen} function opens a stream for I/O to the file
146@var{filename}, and returns a pointer to the stream.
147
148The @var{opentype} argument is a string that controls how the file is
149opened and specifies attributes of the resulting stream. It must begin
150with one of the following sequences of characters:
151
152@table @samp
153@item r
154Open an existing file for reading only.
155
156@item w
157Open the file for writing only. If the file already exists, it is
158truncated to zero length. Otherwise a new file is created.
159
160@item a
161Open a file for append access; that is, writing at the end of file only.
162If the file already exists, its initial contents are unchanged and
163output to the stream is appended to the end of the file.
164Otherwise, a new, empty file is created.
165
166@item r+
167Open an existing file for both reading and writing. The initial contents
168of the file are unchanged and the initial file position is at the
169beginning of the file.
170
171@item w+
172Open a file for both reading and writing. If the file already exists, it
173is truncated to zero length. Otherwise, a new file is created.
174
175@item a+
176Open or create file for both reading and appending. If the file exists,
177its initial contents are unchanged. Otherwise, a new file is created.
178The initial file position for reading is at the beginning of the file,
179but output is always appended to the end of the file.
180@end table
181
182As you can see, @samp{+} requests a stream that can do both input and
f65fd747 183output. The ISO standard says that when using such a stream, you must
28f540f4
RM
184call @code{fflush} (@pxref{Stream Buffering}) or a file positioning
185function such as @code{fseek} (@pxref{File Positioning}) when switching
186from reading to writing or vice versa. Otherwise, internal buffers
187might not be emptied properly. The GNU C library does not have this
188limitation; you can do arbitrary reading and writing operations on a
189stream in whatever order.
190
191Additional characters may appear after these to specify flags for the
192call. Always put the mode (@samp{r}, @samp{w+}, etc.) first; that is
193the only part you are guaranteed will be understood by all systems.
194
195The GNU C library defines one additional character for use in
196@var{opentype}: the character @samp{x} insists on creating a new
197file---if a file @var{filename} already exists, @code{fopen} fails
198rather than opening it. If you use @samp{x} you can are guaranteed that
199you will not clobber an existing file. This is equivalent to the
200@code{O_EXCL} option to the @code{open} function (@pxref{Opening and
201Closing Files}).
202
203The character @samp{b} in @var{opentype} has a standard meaning; it
204requests a binary stream rather than a text stream. But this makes no
205difference in POSIX systems (including the GNU system). If both
206@samp{+} and @samp{b} are specified, they can appear in either order.
207@xref{Binary Streams}.
208
209Any other characters in @var{opentype} are simply ignored. They may be
210meaningful in other systems.
211
212If the open fails, @code{fopen} returns a null pointer.
a3a4a74e
UD
213
214When the sources are compiling with @code{_FILE_OFFSET_BITS == 64} on a
21532 bits machine this function is in fact @code{fopen64} since the LFS
216interface replaces transparently the old interface.
28f540f4
RM
217@end deftypefun
218
219You can have multiple streams (or file descriptors) pointing to the same
220file open at the same time. If you do only input, this works
221straightforwardly, but you must be careful if any output streams are
222included. @xref{Stream/Descriptor Precautions}. This is equally true
223whether the streams are in one program (not usual) or in several
224programs (which can easily happen). It may be advantageous to use the
225file locking facilities to avoid simultaneous access. @xref{File
226Locks}.
227
a3a4a74e
UD
228@comment stdio.h
229@comment Unix98
230@deftypefun {FILE *} fopen64 (const char *@var{filename}, const char *@var{opentype})
231This function is similar to @code{fopen} but the stream it returns a
232pointer for is opened using @code{open64}. Therefore this stream can be
233used even on files larger then @math{2^31} bytes on 32 bits machines.
234
235Please note that the return type is still @code{FILE *}. There is no
236special @code{FILE} type for the LFS interface.
237
238If the sources are compiled with @code{_FILE_OFFSET_BITS == 64} on a 32
239bits machine this function is available under the name @code{fopen}
240and so transparently replaces the old interface.
241@end deftypefun
242
28f540f4 243@comment stdio.h
f65fd747 244@comment ISO
28f540f4
RM
245@deftypevr Macro int FOPEN_MAX
246The value of this macro is an integer constant expression that
247represents the minimum number of streams that the implementation
248guarantees can be open simultaneously. You might be able to open more
249than this many streams, but that is not guaranteed. The value of this
250constant is at least eight, which includes the three standard streams
251@code{stdin}, @code{stdout}, and @code{stderr}. In POSIX.1 systems this
252value is determined by the @code{OPEN_MAX} parameter; @pxref{General
253Limits}. In BSD and GNU, it is controlled by the @code{RLIMIT_NOFILE}
254resource limit; @pxref{Limits on Resources}.
255@end deftypevr
256
257@comment stdio.h
f65fd747 258@comment ISO
28f540f4
RM
259@deftypefun {FILE *} freopen (const char *@var{filename}, const char *@var{opentype}, FILE *@var{stream})
260This function is like a combination of @code{fclose} and @code{fopen}.
261It first closes the stream referred to by @var{stream}, ignoring any
262errors that are detected in the process. (Because errors are ignored,
263you should not use @code{freopen} on an output stream if you have
264actually done any output using the stream.) Then the file named by
265@var{filename} is opened with mode @var{opentype} as for @code{fopen},
266and associated with the same stream object @var{stream}.
267
268If the operation fails, a null pointer is returned; otherwise,
269@code{freopen} returns @var{stream}.
270
271@code{freopen} has traditionally been used to connect a standard stream
272such as @code{stdin} with a file of your own choice. This is useful in
273programs in which use of a standard stream for certain purposes is
274hard-coded. In the GNU C library, you can simply close the standard
275streams and open new ones with @code{fopen}. But other systems lack
276this ability, so using @code{freopen} is more portable.
a3a4a74e
UD
277
278When the sources are compiling with @code{_FILE_OFFSET_BITS == 64} on a
27932 bits machine this function is in fact @code{freopen64} since the LFS
280interface replaces transparently the old interface.
281@end deftypefun
282
283@comment stdio.h
284@comment Unix98
285@deftypefun {FILE *} freopen64 (const char *@var{filename}, const char *@var{opentype}, FILE *@var{stream})
286This function is similar to @code{freopen}. The only difference is that
287on 32 bits machine the stream returned is able to read beyond the
288@math{2^31} bytes limits imposed by the normal interface. It should be
289noted that the stream pointed to by @var{stream} need not be opened
290using @code{fopen64} or @code{freopen64} since its mode is not important
291for this function.
292
293If the sources are compiled with @code{_FILE_OFFSET_BITS == 64} on a 32
294bits machine this function is available under the name @code{freopen}
295and so transparently replaces the old interface.
28f540f4
RM
296@end deftypefun
297
298
299@node Closing Streams
300@section Closing Streams
301
302@cindex closing a stream
303When a stream is closed with @code{fclose}, the connection between the
304stream and the file is cancelled. After you have closed a stream, you
305cannot perform any additional operations on it.
306
307@comment stdio.h
f65fd747 308@comment ISO
28f540f4
RM
309@deftypefun int fclose (FILE *@var{stream})
310This function causes @var{stream} to be closed and the connection to
311the corresponding file to be broken. Any buffered output is written
312and any buffered input is discarded. The @code{fclose} function returns
313a value of @code{0} if the file was closed successfully, and @code{EOF}
19c3f208 314if an error was detected.
28f540f4
RM
315
316It is important to check for errors when you call @code{fclose} to close
317an output stream, because real, everyday errors can be detected at this
318time. For example, when @code{fclose} writes the remaining buffered
319output, it might get an error because the disk is full. Even if you
320know the buffer is empty, errors can still occur when closing a file if
321you are using NFS.
322
323The function @code{fclose} is declared in @file{stdio.h}.
324@end deftypefun
325
6bc31da0
UD
326To close all streams currently available the GNU C Library provides
327another function.
328
329@comment stdio.h
330@comment GNU
331@deftypefun int fcloseall (void)
332This function causes all open streams of the process to be closed and
333the connection to corresponding files to be broken. All buffered data
f2ea0f5b 334is written and any buffered input is discarded. The @code{fcloseall}
6bc31da0
UD
335function returns a value of @code{0} if all the files were closed
336successfully, and @code{EOF} if an error was detected.
337
2d26e9eb 338This function should be used only in special situation, e.g., when an
6bc31da0
UD
339error occurred and the program must be aborted. Normally each single
340stream should be closed separately so that problems with one stream can
2d26e9eb 341be identified. It is also problematic since the standard streams
6bc31da0
UD
342(@pxref{Standard Streams}) will also be closed.
343
344The function @code{fcloseall} is declared in @file{stdio.h}.
345@end deftypefun
346
28f540f4
RM
347If the @code{main} function to your program returns, or if you call the
348@code{exit} function (@pxref{Normal Termination}), all open streams are
349automatically closed properly. If your program terminates in any other
350manner, such as by calling the @code{abort} function (@pxref{Aborting a
351Program}) or from a fatal signal (@pxref{Signal Handling}), open streams
352might not be closed properly. Buffered output might not be flushed and
353files may be incomplete. For more information on buffering of streams,
354see @ref{Stream Buffering}.
355
356@node Simple Output
357@section Simple Output by Characters or Lines
358
359@cindex writing to a stream, by characters
360This section describes functions for performing character- and
361line-oriented output.
362
363These functions are declared in the header file @file{stdio.h}.
364@pindex stdio.h
365
366@comment stdio.h
f65fd747 367@comment ISO
28f540f4
RM
368@deftypefun int fputc (int @var{c}, FILE *@var{stream})
369The @code{fputc} function converts the character @var{c} to type
19c3f208 370@code{unsigned char}, and writes it to the stream @var{stream}.
28f540f4
RM
371@code{EOF} is returned if a write error occurs; otherwise the
372character @var{c} is returned.
373@end deftypefun
374
375@comment stdio.h
f65fd747 376@comment ISO
28f540f4
RM
377@deftypefun int putc (int @var{c}, FILE *@var{stream})
378This is just like @code{fputc}, except that most systems implement it as
379a macro, making it faster. One consequence is that it may evaluate the
380@var{stream} argument more than once, which is an exception to the
381general rule for macros. @code{putc} is usually the best function to
382use for writing a single character.
383@end deftypefun
384
385@comment stdio.h
f65fd747 386@comment ISO
28f540f4
RM
387@deftypefun int putchar (int @var{c})
388The @code{putchar} function is equivalent to @code{putc} with
389@code{stdout} as the value of the @var{stream} argument.
390@end deftypefun
391
392@comment stdio.h
f65fd747 393@comment ISO
28f540f4
RM
394@deftypefun int fputs (const char *@var{s}, FILE *@var{stream})
395The function @code{fputs} writes the string @var{s} to the stream
396@var{stream}. The terminating null character is not written.
397This function does @emph{not} add a newline character, either.
398It outputs only the characters in the string.
399
400This function returns @code{EOF} if a write error occurs, and otherwise
401a non-negative value.
402
403For example:
404
405@smallexample
406fputs ("Are ", stdout);
407fputs ("you ", stdout);
408fputs ("hungry?\n", stdout);
409@end smallexample
410
411@noindent
412outputs the text @samp{Are you hungry?} followed by a newline.
413@end deftypefun
414
415@comment stdio.h
f65fd747 416@comment ISO
28f540f4
RM
417@deftypefun int puts (const char *@var{s})
418The @code{puts} function writes the string @var{s} to the stream
419@code{stdout} followed by a newline. The terminating null character of
420the string is not written. (Note that @code{fputs} does @emph{not}
421write a newline as this function does.)
422
423@code{puts} is the most convenient function for printing simple
424messages. For example:
425
426@smallexample
427puts ("This is a message.");
428@end smallexample
429@end deftypefun
430
431@comment stdio.h
432@comment SVID
433@deftypefun int putw (int @var{w}, FILE *@var{stream})
434This function writes the word @var{w} (that is, an @code{int}) to
435@var{stream}. It is provided for compatibility with SVID, but we
436recommend you use @code{fwrite} instead (@pxref{Block Input/Output}).
437@end deftypefun
438
439@node Character Input
440@section Character Input
441
442@cindex reading from a stream, by characters
443This section describes functions for performing character-oriented input.
444These functions are declared in the header file @file{stdio.h}.
445@pindex stdio.h
446
447These functions return an @code{int} value that is either a character of
448input, or the special value @code{EOF} (usually -1). It is important to
449store the result of these functions in a variable of type @code{int}
450instead of @code{char}, even when you plan to use it only as a
451character. Storing @code{EOF} in a @code{char} variable truncates its
452value to the size of a character, so that it is no longer
453distinguishable from the valid character @samp{(char) -1}. So always
454use an @code{int} for the result of @code{getc} and friends, and check
455for @code{EOF} after the call; once you've verified that the result is
456not @code{EOF}, you can be sure that it will fit in a @samp{char}
457variable without loss of information.
458
459@comment stdio.h
f65fd747 460@comment ISO
28f540f4
RM
461@deftypefun int fgetc (FILE *@var{stream})
462This function reads the next character as an @code{unsigned char} from
463the stream @var{stream} and returns its value, converted to an
464@code{int}. If an end-of-file condition or read error occurs,
19c3f208 465@code{EOF} is returned instead.
28f540f4
RM
466@end deftypefun
467
468@comment stdio.h
f65fd747 469@comment ISO
28f540f4
RM
470@deftypefun int getc (FILE *@var{stream})
471This is just like @code{fgetc}, except that it is permissible (and
472typical) for it to be implemented as a macro that evaluates the
473@var{stream} argument more than once. @code{getc} is often highly
474optimized, so it is usually the best function to use to read a single
475character.
476@end deftypefun
477
478@comment stdio.h
f65fd747 479@comment ISO
28f540f4
RM
480@deftypefun int getchar (void)
481The @code{getchar} function is equivalent to @code{getc} with @code{stdin}
482as the value of the @var{stream} argument.
483@end deftypefun
484
485Here is an example of a function that does input using @code{fgetc}. It
486would work just as well using @code{getc} instead, or using
487@code{getchar ()} instead of @w{@code{fgetc (stdin)}}.
488
489@smallexample
490int
491y_or_n_p (const char *question)
492@{
493 fputs (question, stdout);
494 while (1)
495 @{
496 int c, answer;
497 /* @r{Write a space to separate answer from question.} */
498 fputc (' ', stdout);
499 /* @r{Read the first character of the line.}
500 @r{This should be the answer character, but might not be.} */
501 c = tolower (fgetc (stdin));
502 answer = c;
503 /* @r{Discard rest of input line.} */
504 while (c != '\n' && c != EOF)
505 c = fgetc (stdin);
506 /* @r{Obey the answer if it was valid.} */
507 if (answer == 'y')
508 return 1;
509 if (answer == 'n')
510 return 0;
511 /* @r{Answer was invalid: ask for valid answer.} */
512 fputs ("Please answer y or n:", stdout);
513 @}
514@}
515@end smallexample
516
517@comment stdio.h
518@comment SVID
519@deftypefun int getw (FILE *@var{stream})
520This function reads a word (that is, an @code{int}) from @var{stream}.
521It's provided for compatibility with SVID. We recommend you use
522@code{fread} instead (@pxref{Block Input/Output}). Unlike @code{getc},
523any @code{int} value could be a valid result. @code{getw} returns
524@code{EOF} when it encounters end-of-file or an error, but there is no
525way to distinguish this from an input word with value -1.
526@end deftypefun
527
528@node Line Input
529@section Line-Oriented Input
530
531Since many programs interpret input on the basis of lines, it's
532convenient to have functions to read a line of text from a stream.
533
534Standard C has functions to do this, but they aren't very safe: null
535characters and even (for @code{gets}) long lines can confuse them. So
536the GNU library provides the nonstandard @code{getline} function that
537makes it easy to read lines reliably.
538
539Another GNU extension, @code{getdelim}, generalizes @code{getline}. It
540reads a delimited record, defined as everything through the next
541occurrence of a specified delimiter character.
542
543All these functions are declared in @file{stdio.h}.
544
545@comment stdio.h
546@comment GNU
547@deftypefun ssize_t getline (char **@var{lineptr}, size_t *@var{n}, FILE *@var{stream})
548This function reads an entire line from @var{stream}, storing the text
549(including the newline and a terminating null character) in a buffer
550and storing the buffer address in @code{*@var{lineptr}}.
551
552Before calling @code{getline}, you should place in @code{*@var{lineptr}}
553the address of a buffer @code{*@var{n}} bytes long, allocated with
554@code{malloc}. If this buffer is long enough to hold the line,
555@code{getline} stores the line in this buffer. Otherwise,
556@code{getline} makes the buffer bigger using @code{realloc}, storing the
557new buffer address back in @code{*@var{lineptr}} and the increased size
558back in @code{*@var{n}}.
559@xref{Unconstrained Allocation}.
560
561If you set @code{*@var{lineptr}} to a null pointer, and @code{*@var{n}}
562to zero, before the call, then @code{getline} allocates the initial
563buffer for you by calling @code{malloc}.
564
565In either case, when @code{getline} returns, @code{*@var{lineptr}} is
566a @code{char *} which points to the text of the line.
567
568When @code{getline} is successful, it returns the number of characters
569read (including the newline, but not including the terminating null).
570This value enables you to distinguish null characters that are part of
571the line from the null character inserted as a terminator.
572
573This function is a GNU extension, but it is the recommended way to read
574lines from a stream. The alternative standard functions are unreliable.
575
576If an error occurs or end of file is reached, @code{getline} returns
577@code{-1}.
578@end deftypefun
579
580@comment stdio.h
581@comment GNU
582@deftypefun ssize_t getdelim (char **@var{lineptr}, size_t *@var{n}, int @var{delimiter}, FILE *@var{stream})
583This function is like @code{getline} except that the character which
584tells it to stop reading is not necessarily newline. The argument
585@var{delimiter} specifies the delimiter character; @code{getdelim} keeps
586reading until it sees that character (or end of file).
587
588The text is stored in @var{lineptr}, including the delimiter character
589and a terminating null. Like @code{getline}, @code{getdelim} makes
590@var{lineptr} bigger if it isn't big enough.
591
592@code{getline} is in fact implemented in terms of @code{getdelim}, just
593like this:
594
595@smallexample
596ssize_t
597getline (char **lineptr, size_t *n, FILE *stream)
598@{
599 return getdelim (lineptr, n, '\n', stream);
600@}
601@end smallexample
602@end deftypefun
603
604@comment stdio.h
f65fd747 605@comment ISO
28f540f4
RM
606@deftypefun {char *} fgets (char *@var{s}, int @var{count}, FILE *@var{stream})
607The @code{fgets} function reads characters from the stream @var{stream}
608up to and including a newline character and stores them in the string
609@var{s}, adding a null character to mark the end of the string. You
610must supply @var{count} characters worth of space in @var{s}, but the
611number of characters read is at most @var{count} @minus{} 1. The extra
612character space is used to hold the null character at the end of the
613string.
614
615If the system is already at end of file when you call @code{fgets}, then
616the contents of the array @var{s} are unchanged and a null pointer is
617returned. A null pointer is also returned if a read error occurs.
618Otherwise, the return value is the pointer @var{s}.
619
620@strong{Warning:} If the input data has a null character, you can't tell.
621So don't use @code{fgets} unless you know the data cannot contain a null.
622Don't use it to read files edited by the user because, if the user inserts
623a null character, you should either handle it properly or print a clear
624error message. We recommend using @code{getline} instead of @code{fgets}.
625@end deftypefun
626
627@comment stdio.h
f65fd747 628@comment ISO
28f540f4
RM
629@deftypefn {Deprecated function} {char *} gets (char *@var{s})
630The function @code{gets} reads characters from the stream @code{stdin}
631up to the next newline character, and stores them in the string @var{s}.
632The newline character is discarded (note that this differs from the
633behavior of @code{fgets}, which copies the newline character into the
634string). If @code{gets} encounters a read error or end-of-file, it
635returns a null pointer; otherwise it returns @var{s}.
636
637@strong{Warning:} The @code{gets} function is @strong{very dangerous}
638because it provides no protection against overflowing the string
639@var{s}. The GNU library includes it for compatibility only. You
640should @strong{always} use @code{fgets} or @code{getline} instead. To
641remind you of this, the linker (if using GNU @code{ld}) will issue a
642warning whenever you use @code{gets}.
643@end deftypefn
644
645@node Unreading
646@section Unreading
647@cindex peeking at input
648@cindex unreading characters
649@cindex pushing input back
650
651In parser programs it is often useful to examine the next character in
652the input stream without removing it from the stream. This is called
653``peeking ahead'' at the input because your program gets a glimpse of
654the input it will read next.
655
656Using stream I/O, you can peek ahead at input by first reading it and
19c3f208 657then @dfn{unreading} it (also called @dfn{pushing it back} on the stream).
28f540f4
RM
658Unreading a character makes it available to be input again from the stream,
659by the next call to @code{fgetc} or other input function on that stream.
660
661@menu
662* Unreading Idea:: An explanation of unreading with pictures.
663* How Unread:: How to call @code{ungetc} to do unreading.
664@end menu
665
666@node Unreading Idea
667@subsection What Unreading Means
668
669Here is a pictorial explanation of unreading. Suppose you have a
670stream reading a file that contains just six characters, the letters
671@samp{foobar}. Suppose you have read three characters so far. The
672situation looks like this:
673
674@smallexample
675f o o b a r
676 ^
677@end smallexample
678
679@noindent
680so the next input character will be @samp{b}.
681
682@c @group Invalid outside @example
683If instead of reading @samp{b} you unread the letter @samp{o}, you get a
684situation like this:
685
686@smallexample
687f o o b a r
688 |
689 o--
690 ^
691@end smallexample
692
693@noindent
694so that the next input characters will be @samp{o} and @samp{b}.
695@c @end group
696
697@c @group
698If you unread @samp{9} instead of @samp{o}, you get this situation:
699
700@smallexample
701f o o b a r
702 |
703 9--
704 ^
705@end smallexample
706
707@noindent
708so that the next input characters will be @samp{9} and @samp{b}.
709@c @end group
710
711@node How Unread
712@subsection Using @code{ungetc} To Do Unreading
19c3f208 713
28f540f4
RM
714The function to unread a character is called @code{ungetc}, because it
715reverses the action of @code{getc}.
716
717@comment stdio.h
f65fd747 718@comment ISO
28f540f4
RM
719@deftypefun int ungetc (int @var{c}, FILE *@var{stream})
720The @code{ungetc} function pushes back the character @var{c} onto the
721input stream @var{stream}. So the next input from @var{stream} will
722read @var{c} before anything else.
723
724If @var{c} is @code{EOF}, @code{ungetc} does nothing and just returns
725@code{EOF}. This lets you call @code{ungetc} with the return value of
726@code{getc} without needing to check for an error from @code{getc}.
727
728The character that you push back doesn't have to be the same as the last
729character that was actually read from the stream. In fact, it isn't
730necessary to actually read any characters from the stream before
731unreading them with @code{ungetc}! But that is a strange way to write
732a program; usually @code{ungetc} is used only to unread a character
733that was just read from the same stream.
734
735The GNU C library only supports one character of pushback---in other
736words, it does not work to call @code{ungetc} twice without doing input
737in between. Other systems might let you push back multiple characters;
738then reading from the stream retrieves the characters in the reverse
739order that they were pushed.
740
741Pushing back characters doesn't alter the file; only the internal
742buffering for the stream is affected. If a file positioning function
a5a0310d
UD
743(such as @code{fseek}, @code{fseeko} or @code{rewind}; @pxref{File
744Positioning}) is called, any pending pushed-back characters are
745discarded.
28f540f4
RM
746
747Unreading a character on a stream that is at end of file clears the
748end-of-file indicator for the stream, because it makes the character of
749input available. After you read that character, trying to read again
750will encounter end of file.
751@end deftypefun
752
753Here is an example showing the use of @code{getc} and @code{ungetc} to
754skip over whitespace characters. When this function reaches a
755non-whitespace character, it unreads that character to be seen again on
756the next read operation on the stream.
757
758@smallexample
759#include <stdio.h>
760#include <ctype.h>
761
762void
763skip_whitespace (FILE *stream)
764@{
765 int c;
766 do
767 /* @r{No need to check for @code{EOF} because it is not}
768 @r{@code{isspace}, and @code{ungetc} ignores @code{EOF}.} */
769 c = getc (stream);
770 while (isspace (c));
771 ungetc (c, stream);
772@}
773@end smallexample
774
775@node Block Input/Output
776@section Block Input/Output
777
778This section describes how to do input and output operations on blocks
779of data. You can use these functions to read and write binary data, as
780well as to read and write text in fixed-size blocks instead of by
781characters or lines.
782@cindex binary I/O to a stream
783@cindex block I/O to a stream
784@cindex reading from a stream, by blocks
785@cindex writing to a stream, by blocks
786
787Binary files are typically used to read and write blocks of data in the
788same format as is used to represent the data in a running program. In
789other words, arbitrary blocks of memory---not just character or string
790objects---can be written to a binary file, and meaningfully read in
791again by the same program.
792
793Storing data in binary form is often considerably more efficient than
794using the formatted I/O functions. Also, for floating-point numbers,
795the binary form avoids possible loss of precision in the conversion
796process. On the other hand, binary files can't be examined or modified
797easily using many standard file utilities (such as text editors), and
798are not portable between different implementations of the language, or
799different kinds of computers.
800
801These functions are declared in @file{stdio.h}.
802@pindex stdio.h
803
804@comment stdio.h
f65fd747 805@comment ISO
28f540f4
RM
806@deftypefun size_t fread (void *@var{data}, size_t @var{size}, size_t @var{count}, FILE *@var{stream})
807This function reads up to @var{count} objects of size @var{size} into
808the array @var{data}, from the stream @var{stream}. It returns the
809number of objects actually read, which might be less than @var{count} if
810a read error occurs or the end of the file is reached. This function
811returns a value of zero (and doesn't read anything) if either @var{size}
812or @var{count} is zero.
813
814If @code{fread} encounters end of file in the middle of an object, it
815returns the number of complete objects read, and discards the partial
816object. Therefore, the stream remains at the actual end of the file.
817@end deftypefun
818
819@comment stdio.h
f65fd747 820@comment ISO
28f540f4
RM
821@deftypefun size_t fwrite (const void *@var{data}, size_t @var{size}, size_t @var{count}, FILE *@var{stream})
822This function writes up to @var{count} objects of size @var{size} from
823the array @var{data}, to the stream @var{stream}. The return value is
824normally @var{count}, if the call succeeds. Any other value indicates
825some sort of error, such as running out of space.
826@end deftypefun
827
828@node Formatted Output
829@section Formatted Output
830
831@cindex format string, for @code{printf}
832@cindex template, for @code{printf}
833@cindex formatted output to a stream
834@cindex writing to a stream, formatted
835The functions described in this section (@code{printf} and related
836functions) provide a convenient way to perform formatted output. You
837call @code{printf} with a @dfn{format string} or @dfn{template string}
838that specifies how to format the values of the remaining arguments.
839
840Unless your program is a filter that specifically performs line- or
841character-oriented processing, using @code{printf} or one of the other
842related functions described in this section is usually the easiest and
843most concise way to perform output. These functions are especially
844useful for printing error messages, tables of data, and the like.
845
846@menu
847* Formatted Output Basics:: Some examples to get you started.
848* Output Conversion Syntax:: General syntax of conversion
849 specifications.
850* Table of Output Conversions:: Summary of output conversions and
851 what they do.
852* Integer Conversions:: Details about formatting of integers.
853* Floating-Point Conversions:: Details about formatting of
854 floating-point numbers.
855* Other Output Conversions:: Details about formatting of strings,
856 characters, pointers, and the like.
857* Formatted Output Functions:: Descriptions of the actual functions.
858* Dynamic Output:: Functions that allocate memory for the output.
859* Variable Arguments Output:: @code{vprintf} and friends.
860* Parsing a Template String:: What kinds of args does a given template
19c3f208 861 call for?
28f540f4
RM
862* Example of Parsing:: Sample program using @code{parse_printf_format}.
863@end menu
864
865@node Formatted Output Basics
866@subsection Formatted Output Basics
867
868The @code{printf} function can be used to print any number of arguments.
869The template string argument you supply in a call provides
870information not only about the number of additional arguments, but also
871about their types and what style should be used for printing them.
872
873Ordinary characters in the template string are simply written to the
874output stream as-is, while @dfn{conversion specifications} introduced by
875a @samp{%} character in the template cause subsequent arguments to be
876formatted and written to the output stream. For example,
877@cindex conversion specifications (@code{printf})
878
879@smallexample
880int pct = 37;
881char filename[] = "foo.txt";
882printf ("Processing of `%s' is %d%% finished.\nPlease be patient.\n",
883 filename, pct);
884@end smallexample
885
886@noindent
887produces output like
888
889@smallexample
890Processing of `foo.txt' is 37% finished.
891Please be patient.
892@end smallexample
893
894This example shows the use of the @samp{%d} conversion to specify that
895an @code{int} argument should be printed in decimal notation, the
896@samp{%s} conversion to specify printing of a string argument, and
897the @samp{%%} conversion to print a literal @samp{%} character.
898
899There are also conversions for printing an integer argument as an
900unsigned value in octal, decimal, or hexadecimal radix (@samp{%o},
901@samp{%u}, or @samp{%x}, respectively); or as a character value
902(@samp{%c}).
903
904Floating-point numbers can be printed in normal, fixed-point notation
905using the @samp{%f} conversion or in exponential notation using the
906@samp{%e} conversion. The @samp{%g} conversion uses either @samp{%e}
907or @samp{%f} format, depending on what is more appropriate for the
908magnitude of the particular number.
909
910You can control formatting more precisely by writing @dfn{modifiers}
911between the @samp{%} and the character that indicates which conversion
912to apply. These slightly alter the ordinary behavior of the conversion.
913For example, most conversion specifications permit you to specify a
914minimum field width and a flag indicating whether you want the result
915left- or right-justified within the field.
916
917The specific flags and modifiers that are permitted and their
918interpretation vary depending on the particular conversion. They're all
919described in more detail in the following sections. Don't worry if this
920all seems excessively complicated at first; you can almost always get
921reasonable free-format output without using any of the modifiers at all.
922The modifiers are mostly used to make the output look ``prettier'' in
923tables.
924
925@node Output Conversion Syntax
926@subsection Output Conversion Syntax
927
928This section provides details about the precise syntax of conversion
929specifications that can appear in a @code{printf} template
930string.
931
390955cb
UD
932Characters in the template string that are not part of a conversion
933specification are printed as-is to the output stream. Multibyte
934character sequences (@pxref{Character Set Handling}) are permitted in a
935template string.
28f540f4
RM
936
937The conversion specifications in a @code{printf} template string have
938the general form:
939
940@example
dfd2257a 941% @r{[} @var{param-no} @r{$]} @var{flags} @var{width} @r{[} . @var{precision} @r{]} @var{type} @var{conversion}
28f540f4
RM
942@end example
943
944For example, in the conversion specifier @samp{%-10.8ld}, the @samp{-}
945is a flag, @samp{10} specifies the field width, the precision is
946@samp{8}, the letter @samp{l} is a type modifier, and @samp{d} specifies
947the conversion style. (This particular type specifier says to
948print a @code{long int} argument in decimal notation, with a minimum of
9498 digits left-justified in a field at least 10 characters wide.)
950
951In more detail, output conversion specifications consist of an
952initial @samp{%} character followed in sequence by:
953
954@itemize @bullet
dfd2257a
UD
955@item
956An optional specification of the parameter used for this format.
957Normally the parameters to the @code{printf} function a assigned to the
f2ea0f5b 958formats in the order of appearance in the format string. But in some
dfd2257a
UD
959situations (such as message translation) this is not desirable and this
960extension allows to specify and explicit parameter to be used.
961
962The @var{param-no} part of the format must be an integer in the range of
9631 to the maximum number of arguments present to the function call. Some
964implementations limit this number to a certainly upper bound. The exact
965limit can be retrieved by the following constant.
966
967@defvr Macro NL_ARGMAX
968The value of @code{ARGMAX} is the maximum value allowed for the
969specification of an positional parameter in a @code{printf} call. The
970actual value in effect at runtime can be retrieved by using
971@code{sysconf} using the @code{_SC_NL_ARGMAX} parameter @pxref{Sysconf
972Definition}.
973
974Some system have a quite low limit such as @math{9} for @w{System V}
975systems. The GNU C library has no real limit.
976@end defvr
977
978If any of the formats has a specification for the parameter position all
979of them in the format string shall have one. Otherwise the behaviour is
980undefined.
981
19c3f208 982@item
28f540f4
RM
983Zero or more @dfn{flag characters} that modify the normal behavior of
984the conversion specification.
985@cindex flag character (@code{printf})
986
19c3f208 987@item
28f540f4
RM
988An optional decimal integer specifying the @dfn{minimum field width}.
989If the normal conversion produces fewer characters than this, the field
990is padded with spaces to the specified width. This is a @emph{minimum}
991value; if the normal conversion produces more characters than this, the
992field is @emph{not} truncated. Normally, the output is right-justified
993within the field.
994@cindex minimum field width (@code{printf})
995
996You can also specify a field width of @samp{*}. This means that the
997next argument in the argument list (before the actual value to be
998printed) is used as the field width. The value must be an @code{int}.
999If the value is negative, this means to set the @samp{-} flag (see
1000below) and to use the absolute value as the field width.
1001
19c3f208 1002@item
28f540f4
RM
1003An optional @dfn{precision} to specify the number of digits to be
1004written for the numeric conversions. If the precision is specified, it
1005consists of a period (@samp{.}) followed optionally by a decimal integer
1006(which defaults to zero if omitted).
1007@cindex precision (@code{printf})
1008
1009You can also specify a precision of @samp{*}. This means that the next
1010argument in the argument list (before the actual value to be printed) is
1011used as the precision. The value must be an @code{int}, and is ignored
1012if it is negative. If you specify @samp{*} for both the field width and
1013precision, the field width argument precedes the precision argument.
1014Other C library versions may not recognize this syntax.
1015
1016@item
1017An optional @dfn{type modifier character}, which is used to specify the
1018data type of the corresponding argument if it differs from the default
1019type. (For example, the integer conversions assume a type of @code{int},
1020but you can specify @samp{h}, @samp{l}, or @samp{L} for other integer
1021types.)
1022@cindex type modifier character (@code{printf})
1023
1024@item
1025A character that specifies the conversion to be applied.
1026@end itemize
1027
19c3f208 1028The exact options that are permitted and how they are interpreted vary
28f540f4
RM
1029between the different conversion specifiers. See the descriptions of the
1030individual conversions for information about the particular options that
1031they use.
1032
1033With the @samp{-Wformat} option, the GNU C compiler checks calls to
1034@code{printf} and related functions. It examines the format string and
1035verifies that the correct number and types of arguments are supplied.
1036There is also a GNU C syntax to tell the compiler that a function you
19c3f208 1037write uses a @code{printf}-style format string.
28f540f4
RM
1038@xref{Function Attributes, , Declaring Attributes of Functions,
1039gcc.info, Using GNU CC}, for more information.
1040
1041@node Table of Output Conversions
1042@subsection Table of Output Conversions
1043@cindex output conversions, for @code{printf}
1044
1045Here is a table summarizing what all the different conversions do:
1046
1047@table @asis
1048@item @samp{%d}, @samp{%i}
1049Print an integer as a signed decimal number. @xref{Integer
1050Conversions}, for details. @samp{%d} and @samp{%i} are synonymous for
1051output, but are different when used with @code{scanf} for input
1052(@pxref{Table of Input Conversions}).
1053
1054@item @samp{%o}
1055Print an integer as an unsigned octal number. @xref{Integer
1056Conversions}, for details.
1057
1058@item @samp{%u}
1059Print an integer as an unsigned decimal number. @xref{Integer
1060Conversions}, for details.
1061
1062@item @samp{%x}, @samp{%X}
1063Print an integer as an unsigned hexadecimal number. @samp{%x} uses
1064lower-case letters and @samp{%X} uses upper-case. @xref{Integer
1065Conversions}, for details.
1066
1067@item @samp{%f}
1068Print a floating-point number in normal (fixed-point) notation.
1069@xref{Floating-Point Conversions}, for details.
1070
1071@item @samp{%e}, @samp{%E}
1072Print a floating-point number in exponential notation. @samp{%e} uses
1073lower-case letters and @samp{%E} uses upper-case. @xref{Floating-Point
1074Conversions}, for details.
1075
1076@item @samp{%g}, @samp{%G}
1077Print a floating-point number in either normal or exponential notation,
1078whichever is more appropriate for its magnitude. @samp{%g} uses
1079lower-case letters and @samp{%G} uses upper-case. @xref{Floating-Point
1080Conversions}, for details.
1081
2f6d1f1b
UD
1082@item @samp{%a}, @samp{%A}
1083Print a floating-point number in a hexadecimal fractional notation which
1084the exponent to base 2 represented in decimal digits. @samp{%a} uses
1085lower-case letters and @samp{%A} uses upper-case. @xref{Floating-Point
1086Conversions}, for details.
1087
28f540f4
RM
1088@item @samp{%c}
1089Print a single character. @xref{Other Output Conversions}.
1090
1091@item @samp{%s}
1092Print a string. @xref{Other Output Conversions}.
1093
1094@item @samp{%p}
1095Print the value of a pointer. @xref{Other Output Conversions}.
1096
1097@item @samp{%n}
1098Get the number of characters printed so far. @xref{Other Output Conversions}.
1099Note that this conversion specification never produces any output.
1100
1101@item @samp{%m}
1102Print the string corresponding to the value of @code{errno}.
1103(This is a GNU extension.)
1104@xref{Other Output Conversions}.
1105
1106@item @samp{%%}
1107Print a literal @samp{%} character. @xref{Other Output Conversions}.
1108@end table
1109
1110If the syntax of a conversion specification is invalid, unpredictable
1111things will happen, so don't do this. If there aren't enough function
1112arguments provided to supply values for all the conversion
1113specifications in the template string, or if the arguments are not of
1114the correct types, the results are unpredictable. If you supply more
1115arguments than conversion specifications, the extra argument values are
1116simply ignored; this is sometimes useful.
1117
1118@node Integer Conversions
1119@subsection Integer Conversions
1120
1121This section describes the options for the @samp{%d}, @samp{%i},
1122@samp{%o}, @samp{%u}, @samp{%x}, and @samp{%X} conversion
1123specifications. These conversions print integers in various formats.
1124
1125The @samp{%d} and @samp{%i} conversion specifications both print an
1126@code{int} argument as a signed decimal number; while @samp{%o},
1127@samp{%u}, and @samp{%x} print the argument as an unsigned octal,
1128decimal, or hexadecimal number (respectively). The @samp{%X} conversion
1129specification is just like @samp{%x} except that it uses the characters
1130@samp{ABCDEF} as digits instead of @samp{abcdef}.
1131
1132The following flags are meaningful:
1133
1134@table @asis
1135@item @samp{-}
1136Left-justify the result in the field (instead of the normal
1137right-justification).
1138
1139@item @samp{+}
1140For the signed @samp{%d} and @samp{%i} conversions, print a
1141plus sign if the value is positive.
1142
1143@item @samp{ }
1144For the signed @samp{%d} and @samp{%i} conversions, if the result
1145doesn't start with a plus or minus sign, prefix it with a space
1146character instead. Since the @samp{+} flag ensures that the result
1147includes a sign, this flag is ignored if you supply both of them.
1148
1149@item @samp{#}
1150For the @samp{%o} conversion, this forces the leading digit to be
1151@samp{0}, as if by increasing the precision. For @samp{%x} or
1152@samp{%X}, this prefixes a leading @samp{0x} or @samp{0X} (respectively)
1153to the result. This doesn't do anything useful for the @samp{%d},
1154@samp{%i}, or @samp{%u} conversions. Using this flag produces output
1155which can be parsed by the @code{strtoul} function (@pxref{Parsing of
1156Integers}) and @code{scanf} with the @samp{%i} conversion
1157(@pxref{Numeric Input Conversions}).
1158
1159@item @samp{'}
1160Separate the digits into groups as specified by the locale specified for
1161the @code{LC_NUMERIC} category; @pxref{General Numeric}. This flag is a
1162GNU extension.
1163
1164@item @samp{0}
1165Pad the field with zeros instead of spaces. The zeros are placed after
1166any indication of sign or base. This flag is ignored if the @samp{-}
1167flag is also specified, or if a precision is specified.
1168@end table
1169
1170If a precision is supplied, it specifies the minimum number of digits to
1171appear; leading zeros are produced if necessary. If you don't specify a
1172precision, the number is printed with as many digits as it needs. If
1173you convert a value of zero with an explicit precision of zero, then no
1174characters at all are produced.
1175
1176Without a type modifier, the corresponding argument is treated as an
1177@code{int} (for the signed conversions @samp{%i} and @samp{%d}) or
1178@code{unsigned int} (for the unsigned conversions @samp{%o}, @samp{%u},
1179@samp{%x}, and @samp{%X}). Recall that since @code{printf} and friends
1180are variadic, any @code{char} and @code{short} arguments are
1181automatically converted to @code{int} by the default argument
1182promotions. For arguments of other integer types, you can use these
1183modifiers:
1184
1185@table @samp
cc3fa755
UD
1186@item hh
1187Specifies that the argument is a @code{signed char} or @code{unsigned
1188char}, as appropriate. A @code{char} argument is converted to an
1189@code{int} or @code{unsigned int} by the default argument promotions
1190anyway, but the @samp{h} modifier says to convert it back to a
1191@code{char} again.
1192
e852e889
UD
1193This modifier was introduced in @w{ISO C 9x}.
1194
28f540f4
RM
1195@item h
1196Specifies that the argument is a @code{short int} or @code{unsigned
1197short int}, as appropriate. A @code{short} argument is converted to an
1198@code{int} or @code{unsigned int} by the default argument promotions
1199anyway, but the @samp{h} modifier says to convert it back to a
1200@code{short} again.
1201
e852e889
UD
1202@item j
1203Specifies that the argument is a @code{intmax_t} or @code{uintmax_t}, as
1204appropriate.
1205
1206This modifier was introduced in @w{ISO C 9x}.
1207
28f540f4
RM
1208@item l
1209Specifies that the argument is a @code{long int} or @code{unsigned long
1210int}, as appropriate. Two @samp{l} characters is like the @samp{L}
1211modifier, below.
1212
1213@item L
1214@itemx ll
1215@itemx q
1216Specifies that the argument is a @code{long long int}. (This type is
1217an extension supported by the GNU C compiler. On systems that don't
1218support extra-long integers, this is the same as @code{long int}.)
1219
1220The @samp{q} modifier is another name for the same thing, which comes
1221from 4.4 BSD; a @w{@code{long long int}} is sometimes called a ``quad''
1222@code{int}.
1223
e852e889
UD
1224@item t
1225Specifies that the argument is a @code{ptrdiff_t}.
1226
1227This modifier was introduced in @w{ISO C 9x}.
1228
1229@item z
1230@itemx Z
1231Specifies that the argument is a @code{size_t}.
1232
1233@samp{z} was introduced in @w{ISO C 9x}. @samp{Z} is a GNU extension
1234predating this addition and should not be used anymore in new code.
28f540f4
RM
1235@end table
1236
1237Here is an example. Using the template string:
1238
1239@smallexample
1240"|%5d|%-5d|%+5d|%+-5d|% 5d|%05d|%5.0d|%5.2d|%d|\n"
1241@end smallexample
1242
1243@noindent
1244to print numbers using the different options for the @samp{%d}
1245conversion gives results like:
1246
1247@smallexample
1248| 0|0 | +0|+0 | 0|00000| | 00|0|
1249| 1|1 | +1|+1 | 1|00001| 1| 01|1|
1250| -1|-1 | -1|-1 | -1|-0001| -1| -01|-1|
1251|100000|100000|+100000| 100000|100000|100000|100000|100000|
1252@end smallexample
1253
1254In particular, notice what happens in the last case where the number
1255is too large to fit in the minimum field width specified.
1256
1257Here are some more examples showing how unsigned integers print under
1258various format options, using the template string:
1259
1260@smallexample
1261"|%5u|%5o|%5x|%5X|%#5o|%#5x|%#5X|%#10.8x|\n"
1262@end smallexample
1263
1264@smallexample
1265| 0| 0| 0| 0| 0| 0x0| 0X0|0x00000000|
1266| 1| 1| 1| 1| 01| 0x1| 0X1|0x00000001|
1267|100000|303240|186a0|186A0|0303240|0x186a0|0X186A0|0x000186a0|
1268@end smallexample
1269
1270
1271@node Floating-Point Conversions
1272@subsection Floating-Point Conversions
1273
1274This section discusses the conversion specifications for floating-point
1275numbers: the @samp{%f}, @samp{%e}, @samp{%E}, @samp{%g}, and @samp{%G}
1276conversions.
1277
1278The @samp{%f} conversion prints its argument in fixed-point notation,
1279producing output of the form
1280@w{[@code{-}]@var{ddd}@code{.}@var{ddd}},
1281where the number of digits following the decimal point is controlled
1282by the precision you specify.
1283
1284The @samp{%e} conversion prints its argument in exponential notation,
1285producing output of the form
1286@w{[@code{-}]@var{d}@code{.}@var{ddd}@code{e}[@code{+}|@code{-}]@var{dd}}.
1287Again, the number of digits following the decimal point is controlled by
1288the precision. The exponent always contains at least two digits. The
1289@samp{%E} conversion is similar but the exponent is marked with the letter
1290@samp{E} instead of @samp{e}.
1291
1292The @samp{%g} and @samp{%G} conversions print the argument in the style
1293of @samp{%e} or @samp{%E} (respectively) if the exponent would be less
1294than -4 or greater than or equal to the precision; otherwise they use the
1295@samp{%f} style. Trailing zeros are removed from the fractional portion
1296of the result and a decimal-point character appears only if it is
1297followed by a digit.
1298
2f6d1f1b
UD
1299The @samp{%a} and @samp{%A} conversions are meant for representing
1300floating-point number exactly in textual form so that they can be
1301exchanged as texts between different programs and/or machines. The
1302numbers are represented is the form
1303@w{[@code{-}]@code{0x}@var{h}@code{.}@var{hhh}@code{p}[@code{+}|@code{-}]@var{dd}}.
1304At the left of the decimal-point character exactly one digit is print.
91ea72b7 1305This character is only @code{0} if the number is denormalized.
f2ea0f5b 1306Otherwise the value is unspecified; it is implemention dependent how many
2f6d1f1b
UD
1307bits are used. The number of hexadecimal digits on the right side of
1308the decimal-point character is equal to the precision. If the precision
1309is zero it is determined to be large enough to provide an exact
1310representation of the number (or it is large enough to distinguish two
1311adjacent values if the @code{FLT_RADIX} is not a power of 2,
91ea72b7 1312@pxref{Floating Point Parameters}). For the @samp{%a} conversion
2f6d1f1b
UD
1313lower-case characters are used to represent the hexadecimal number and
1314the prefix and exponent sign are printed as @code{0x} and @code{p}
1315respectively. Otherwise upper-case characters are used and @code{0X}
1316and @code{P} are used for the representation of prefix and exponent
1317string. The exponent to the base of two is printed as a decimal number
1318using at least one digit but at most as many digits as necessary to
1319represent the value exactly.
1320
1321If the value to be printed represents infinity or a NaN, the output is
1322@w{[@code{-}]@code{inf}} or @code{nan} respectively if the conversion
1323specifier is @samp{%a}, @samp{%e}, @samp{%f}, or @samp{%g} and it is
1324@w{[@code{-}]@code{INF}} or @code{NAN} respectively if the conversion is
1325@samp{%A}, @samp{%E}, or @samp{%G}.
1326
28f540f4
RM
1327The following flags can be used to modify the behavior:
1328
1329@comment We use @asis instead of @samp so we can have ` ' as an item.
1330@table @asis
1331@item @samp{-}
1332Left-justify the result in the field. Normally the result is
1333right-justified.
1334
1335@item @samp{+}
1336Always include a plus or minus sign in the result.
1337
1338@item @samp{ }
1339If the result doesn't start with a plus or minus sign, prefix it with a
1340space instead. Since the @samp{+} flag ensures that the result includes
1341a sign, this flag is ignored if you supply both of them.
1342
1343@item @samp{#}
1344Specifies that the result should always include a decimal point, even
1345if no digits follow it. For the @samp{%g} and @samp{%G} conversions,
1346this also forces trailing zeros after the decimal point to be left
1347in place where they would otherwise be removed.
1348
1349@item @samp{'}
1350Separate the digits of the integer part of the result into groups as
1351specified by the locale specified for the @code{LC_NUMERIC} category;
1352@pxref{General Numeric}. This flag is a GNU extension.
1353
1354@item @samp{0}
1355Pad the field with zeros instead of spaces; the zeros are placed
1356after any sign. This flag is ignored if the @samp{-} flag is also
1357specified.
1358@end table
1359
1360The precision specifies how many digits follow the decimal-point
1361character for the @samp{%f}, @samp{%e}, and @samp{%E} conversions. For
1362these conversions, the default precision is @code{6}. If the precision
1363is explicitly @code{0}, this suppresses the decimal point character
1364entirely. For the @samp{%g} and @samp{%G} conversions, the precision
1365specifies how many significant digits to print. Significant digits are
1366the first digit before the decimal point, and all the digits after it.
91ea72b7 1367If the precision is @code{0} or not specified for @samp{%g} or @samp{%G},
28f540f4
RM
1368it is treated like a value of @code{1}. If the value being printed
1369cannot be expressed accurately in the specified number of digits, the
1370value is rounded to the nearest number that fits.
1371
1372Without a type modifier, the floating-point conversions use an argument
1373of type @code{double}. (By the default argument promotions, any
1374@code{float} arguments are automatically converted to @code{double}.)
1375The following type modifier is supported:
1376
1377@table @samp
1378@item L
1379An uppercase @samp{L} specifies that the argument is a @code{long
1380double}.
1381@end table
1382
1383Here are some examples showing how numbers print using the various
1384floating-point conversions. All of the numbers were printed using
1385this template string:
1386
1387@smallexample
2f6d1f1b 1388"|%13.4a|%13.4f|%13.4e|%13.4g|\n"
28f540f4
RM
1389@end smallexample
1390
1391Here is the output:
1392
1393@smallexample
2f6d1f1b
UD
1394| 0x0.0000p+0| 0.0000| 0.0000e+00| 0|
1395| 0x1.0000p-1| 0.5000| 5.0000e-01| 0.5|
1396| 0x1.0000p+0| 1.0000| 1.0000e+00| 1|
1397| -0x1.0000p+0| -1.0000| -1.0000e+00| -1|
1398| 0x1.9000p+6| 100.0000| 1.0000e+02| 100|
1399| 0x1.f400p+9| 1000.0000| 1.0000e+03| 1000|
1400| 0x1.3880p+13| 10000.0000| 1.0000e+04| 1e+04|
1401| 0x1.81c8p+13| 12345.0000| 1.2345e+04| 1.234e+04|
1402| 0x1.86a0p+16| 100000.0000| 1.0000e+05| 1e+05|
1403| 0x1.e240p+16| 123456.0000| 1.2346e+05| 1.235e+05|
28f540f4
RM
1404@end smallexample
1405
1406Notice how the @samp{%g} conversion drops trailing zeros.
1407
1408@node Other Output Conversions
1409@subsection Other Output Conversions
1410
1411This section describes miscellaneous conversions for @code{printf}.
1412
1413The @samp{%c} conversion prints a single character. The @code{int}
1414argument is first converted to an @code{unsigned char}. The @samp{-}
1415flag can be used to specify left-justification in the field, but no
1416other flags are defined, and no precision or type modifier can be given.
1417For example:
1418
1419@smallexample
1420printf ("%c%c%c%c%c", 'h', 'e', 'l', 'l', 'o');
1421@end smallexample
1422
1423@noindent
1424prints @samp{hello}.
1425
1426The @samp{%s} conversion prints a string. The corresponding argument
1427must be of type @code{char *} (or @code{const char *}). A precision can
1428be specified to indicate the maximum number of characters to write;
1429otherwise characters in the string up to but not including the
1430terminating null character are written to the output stream. The
1431@samp{-} flag can be used to specify left-justification in the field,
1432but no other flags or type modifiers are defined for this conversion.
1433For example:
1434
1435@smallexample
1436printf ("%3s%-6s", "no", "where");
1437@end smallexample
1438
1439@noindent
1440prints @samp{ nowhere }.
1441
1442If you accidentally pass a null pointer as the argument for a @samp{%s}
1443conversion, the GNU library prints it as @samp{(null)}. We think this
1444is more useful than crashing. But it's not good practice to pass a null
1445argument intentionally.
1446
1447The @samp{%m} conversion prints the string corresponding to the error
1448code in @code{errno}. @xref{Error Messages}. Thus:
1449
1450@smallexample
1451fprintf (stderr, "can't open `%s': %m\n", filename);
1452@end smallexample
1453
1454@noindent
1455is equivalent to:
1456
1457@smallexample
1458fprintf (stderr, "can't open `%s': %s\n", filename, strerror (errno));
1459@end smallexample
1460
1461@noindent
1462The @samp{%m} conversion is a GNU C library extension.
1463
1464The @samp{%p} conversion prints a pointer value. The corresponding
1465argument must be of type @code{void *}. In practice, you can use any
1466type of pointer.
1467
1468In the GNU system, non-null pointers are printed as unsigned integers,
1469as if a @samp{%#x} conversion were used. Null pointers print as
1470@samp{(nil)}. (Pointers might print differently in other systems.)
1471
1472For example:
1473
1474@smallexample
1475printf ("%p", "testing");
1476@end smallexample
1477
1478@noindent
1479prints @samp{0x} followed by a hexadecimal number---the address of the
1480string constant @code{"testing"}. It does not print the word
1481@samp{testing}.
1482
1483You can supply the @samp{-} flag with the @samp{%p} conversion to
1484specify left-justification, but no other flags, precision, or type
1485modifiers are defined.
1486
1487The @samp{%n} conversion is unlike any of the other output conversions.
1488It uses an argument which must be a pointer to an @code{int}, but
1489instead of printing anything it stores the number of characters printed
1490so far by this call at that location. The @samp{h} and @samp{l} type
1491modifiers are permitted to specify that the argument is of type
1492@code{short int *} or @code{long int *} instead of @code{int *}, but no
1493flags, field width, or precision are permitted.
1494
1495For example,
1496
1497@smallexample
1498int nchar;
1499printf ("%d %s%n\n", 3, "bears", &nchar);
1500@end smallexample
1501
1502@noindent
1503prints:
1504
1505@smallexample
15063 bears
1507@end smallexample
1508
1509@noindent
19c3f208 1510and sets @code{nchar} to @code{7}, because @samp{3 bears} is seven
28f540f4
RM
1511characters.
1512
1513
1514The @samp{%%} conversion prints a literal @samp{%} character. This
1515conversion doesn't use an argument, and no flags, field width,
1516precision, or type modifiers are permitted.
1517
1518
1519@node Formatted Output Functions
1520@subsection Formatted Output Functions
1521
1522This section describes how to call @code{printf} and related functions.
1523Prototypes for these functions are in the header file @file{stdio.h}.
1524Because these functions take a variable number of arguments, you
1525@emph{must} declare prototypes for them before using them. Of course,
1526the easiest way to make sure you have all the right prototypes is to
1527just include @file{stdio.h}.
1528@pindex stdio.h
1529
1530@comment stdio.h
f65fd747 1531@comment ISO
28f540f4
RM
1532@deftypefun int printf (const char *@var{template}, @dots{})
1533The @code{printf} function prints the optional arguments under the
1534control of the template string @var{template} to the stream
1535@code{stdout}. It returns the number of characters printed, or a
1536negative value if there was an output error.
1537@end deftypefun
1538
1539@comment stdio.h
f65fd747 1540@comment ISO
28f540f4
RM
1541@deftypefun int fprintf (FILE *@var{stream}, const char *@var{template}, @dots{})
1542This function is just like @code{printf}, except that the output is
1543written to the stream @var{stream} instead of @code{stdout}.
1544@end deftypefun
1545
1546@comment stdio.h
f65fd747 1547@comment ISO
28f540f4
RM
1548@deftypefun int sprintf (char *@var{s}, const char *@var{template}, @dots{})
1549This is like @code{printf}, except that the output is stored in the character
1550array @var{s} instead of written to a stream. A null character is written
1551to mark the end of the string.
1552
1553The @code{sprintf} function returns the number of characters stored in
1554the array @var{s}, not including the terminating null character.
1555
1556The behavior of this function is undefined if copying takes place
1557between objects that overlap---for example, if @var{s} is also given
1558as an argument to be printed under control of the @samp{%s} conversion.
1559@xref{Copying and Concatenation}.
1560
1561@strong{Warning:} The @code{sprintf} function can be @strong{dangerous}
1562because it can potentially output more characters than can fit in the
1563allocation size of the string @var{s}. Remember that the field width
1564given in a conversion specification is only a @emph{minimum} value.
1565
1566To avoid this problem, you can use @code{snprintf} or @code{asprintf},
1567described below.
1568@end deftypefun
1569
1570@comment stdio.h
1571@comment GNU
1572@deftypefun int snprintf (char *@var{s}, size_t @var{size}, const char *@var{template}, @dots{})
1573The @code{snprintf} function is similar to @code{sprintf}, except that
1574the @var{size} argument specifies the maximum number of characters to
1575produce. The trailing null character is counted towards this limit, so
1576you should allocate at least @var{size} characters for the string @var{s}.
1577
fe7bdd63 1578The return value is the number of characters which would be generated
da2d1bc5
UD
1579for the given input, excluding the trailing null. If this value is
1580greater or equal to @var{size}, not all characters from the result have
1581been stored in @var{s}. You should try again with a bigger output
1582string. Here is an example of doing this:
28f540f4
RM
1583
1584@smallexample
1585@group
1586/* @r{Construct a message describing the value of a variable}
1587 @r{whose name is @var{name} and whose value is @var{value}.} */
1588char *
1589make_message (char *name, char *value)
1590@{
1591 /* @r{Guess we need no more than 100 chars of space.} */
1592 int size = 100;
1593 char *buffer = (char *) xmalloc (size);
4cca6b86 1594 int nchars;
28f540f4
RM
1595@end group
1596@group
4cca6b86
UD
1597 /* @r{Try to print in the allocated space.} */
1598 nchars = snprintf (buffer, size, "value of %s is %s",
1599 name, value);
1600@end group
1601@group
fe7bdd63 1602 if (nchars >= size)
28f540f4 1603 @{
4b8f94d3 1604 /* @r{Reallocate buffer now that we know
838e5ffe 1605 how much space is needed.} */
4cca6b86
UD
1606 buffer = (char *) xrealloc (buffer, nchars + 1);
1607
1608 /* @r{Try again.} */
4b8f94d3 1609 snprintf (buffer, size, "value of %s is %s",
838e5ffe 1610 name, value);
28f540f4 1611 @}
4cca6b86
UD
1612 /* @r{The last call worked, return the string.} */
1613 return buffer;
28f540f4
RM
1614@}
1615@end group
1616@end smallexample
1617
1618In practice, it is often easier just to use @code{asprintf}, below.
fb971363
UD
1619
1620@strong{Attention:} In the GNU C library version 2.0 the return value
1621is the number of characters stored, not including the terminating null.
1622If this value equals @code{@var{size} - 1}, then there was not enough
0ea554bf 1623space in @var{s} for all the output. This change was necessary with
fb971363 1624the adoption of snprintf by ISO C9x.
28f540f4
RM
1625@end deftypefun
1626
1627@node Dynamic Output
1628@subsection Dynamically Allocating Formatted Output
1629
1630The functions in this section do formatted output and place the results
1631in dynamically allocated memory.
1632
1633@comment stdio.h
1634@comment GNU
1635@deftypefun int asprintf (char **@var{ptr}, const char *@var{template}, @dots{})
1636This function is similar to @code{sprintf}, except that it dynamically
1637allocates a string (as with @code{malloc}; @pxref{Unconstrained
1638Allocation}) to hold the output, instead of putting the output in a
1639buffer you allocate in advance. The @var{ptr} argument should be the
1640address of a @code{char *} object, and @code{asprintf} stores a pointer
1641to the newly allocated string at that location.
1642
1643Here is how to use @code{asprintf} to get the same result as the
1644@code{snprintf} example, but more easily:
1645
1646@smallexample
1647/* @r{Construct a message describing the value of a variable}
1648 @r{whose name is @var{name} and whose value is @var{value}.} */
1649char *
1650make_message (char *name, char *value)
1651@{
1652 char *result;
1653 asprintf (&result, "value of %s is %s", name, value);
1654 return result;
1655@}
1656@end smallexample
1657@end deftypefun
1658
1659@comment stdio.h
1660@comment GNU
1661@deftypefun int obstack_printf (struct obstack *@var{obstack}, const char *@var{template}, @dots{})
1662This function is similar to @code{asprintf}, except that it uses the
1663obstack @var{obstack} to allocate the space. @xref{Obstacks}.
1664
1665The characters are written onto the end of the current object.
1666To get at them, you must finish the object with @code{obstack_finish}
1667(@pxref{Growing Objects}).@refill
1668@end deftypefun
1669
1670@node Variable Arguments Output
1671@subsection Variable Arguments Output Functions
1672
1673The functions @code{vprintf} and friends are provided so that you can
1674define your own variadic @code{printf}-like functions that make use of
1675the same internals as the built-in formatted output functions.
1676
1677The most natural way to define such functions would be to use a language
1678construct to say, ``Call @code{printf} and pass this template plus all
1679of my arguments after the first five.'' But there is no way to do this
1680in C, and it would be hard to provide a way, since at the C language
1681level there is no way to tell how many arguments your function received.
1682
1683Since that method is impossible, we provide alternative functions, the
1684@code{vprintf} series, which lets you pass a @code{va_list} to describe
1685``all of my arguments after the first five.''
1686
19c3f208 1687When it is sufficient to define a macro rather than a real function,
28f540f4
RM
1688the GNU C compiler provides a way to do this much more easily with macros.
1689For example:
1690
1691@smallexample
838e5ffe
UD
1692#define myprintf(a, b, c, d, e, rest...) \
1693 printf (mytemplate , ## rest...)
28f540f4
RM
1694@end smallexample
1695
1696@noindent
1697@xref{Macro Varargs, , Macros with Variable Numbers of Arguments,
1698gcc.info, Using GNU CC}, for details. But this is limited to macros,
1699and does not apply to real functions at all.
1700
1701Before calling @code{vprintf} or the other functions listed in this
1702section, you @emph{must} call @code{va_start} (@pxref{Variadic
1703Functions}) to initialize a pointer to the variable arguments. Then you
1704can call @code{va_arg} to fetch the arguments that you want to handle
1705yourself. This advances the pointer past those arguments.
1706
1707Once your @code{va_list} pointer is pointing at the argument of your
1708choice, you are ready to call @code{vprintf}. That argument and all
1709subsequent arguments that were passed to your function are used by
1710@code{vprintf} along with the template that you specified separately.
1711
1712In some other systems, the @code{va_list} pointer may become invalid
1713after the call to @code{vprintf}, so you must not use @code{va_arg}
1714after you call @code{vprintf}. Instead, you should call @code{va_end}
1715to retire the pointer from service. However, you can safely call
1716@code{va_start} on another pointer variable and begin fetching the
1717arguments again through that pointer. Calling @code{vprintf} does not
1718destroy the argument list of your function, merely the particular
1719pointer that you passed to it.
1720
1721GNU C does not have such restrictions. You can safely continue to fetch
1722arguments from a @code{va_list} pointer after passing it to
1723@code{vprintf}, and @code{va_end} is a no-op. (Note, however, that
1724subsequent @code{va_arg} calls will fetch the same arguments which
1725@code{vprintf} previously used.)
1726
1727Prototypes for these functions are declared in @file{stdio.h}.
1728@pindex stdio.h
1729
1730@comment stdio.h
f65fd747 1731@comment ISO
28f540f4
RM
1732@deftypefun int vprintf (const char *@var{template}, va_list @var{ap})
1733This function is similar to @code{printf} except that, instead of taking
1734a variable number of arguments directly, it takes an argument list
1735pointer @var{ap}.
1736@end deftypefun
1737
1738@comment stdio.h
f65fd747 1739@comment ISO
28f540f4
RM
1740@deftypefun int vfprintf (FILE *@var{stream}, const char *@var{template}, va_list @var{ap})
1741This is the equivalent of @code{fprintf} with the variable argument list
1742specified directly as for @code{vprintf}.
1743@end deftypefun
1744
1745@comment stdio.h
f65fd747 1746@comment ISO
28f540f4
RM
1747@deftypefun int vsprintf (char *@var{s}, const char *@var{template}, va_list @var{ap})
1748This is the equivalent of @code{sprintf} with the variable argument list
1749specified directly as for @code{vprintf}.
1750@end deftypefun
1751
1752@comment stdio.h
1753@comment GNU
1754@deftypefun int vsnprintf (char *@var{s}, size_t @var{size}, const char *@var{template}, va_list @var{ap})
1755This is the equivalent of @code{snprintf} with the variable argument list
1756specified directly as for @code{vprintf}.
1757@end deftypefun
1758
1759@comment stdio.h
1760@comment GNU
1761@deftypefun int vasprintf (char **@var{ptr}, const char *@var{template}, va_list @var{ap})
1762The @code{vasprintf} function is the equivalent of @code{asprintf} with the
1763variable argument list specified directly as for @code{vprintf}.
1764@end deftypefun
1765
1766@comment stdio.h
1767@comment GNU
1768@deftypefun int obstack_vprintf (struct obstack *@var{obstack}, const char *@var{template}, va_list @var{ap})
1769The @code{obstack_vprintf} function is the equivalent of
1770@code{obstack_printf} with the variable argument list specified directly
1771as for @code{vprintf}.@refill
1772@end deftypefun
1773
1774Here's an example showing how you might use @code{vfprintf}. This is a
1775function that prints error messages to the stream @code{stderr}, along
1776with a prefix indicating the name of the program
19c3f208 1777(@pxref{Error Messages}, for a description of
28f540f4
RM
1778@code{program_invocation_short_name}).
1779
1780@smallexample
1781@group
1782#include <stdio.h>
1783#include <stdarg.h>
1784
1785void
1786eprintf (const char *template, ...)
1787@{
1788 va_list ap;
1789 extern char *program_invocation_short_name;
1790
1791 fprintf (stderr, "%s: ", program_invocation_short_name);
4b8f94d3 1792 va_start (ap, template);
28f540f4
RM
1793 vfprintf (stderr, template, ap);
1794 va_end (ap);
1795@}
1796@end group
1797@end smallexample
1798
1799@noindent
1800You could call @code{eprintf} like this:
1801
1802@smallexample
1803eprintf ("file `%s' does not exist\n", filename);
1804@end smallexample
1805
1806In GNU C, there is a special construct you can use to let the compiler
1807know that a function uses a @code{printf}-style format string. Then it
1808can check the number and types of arguments in each call to the
1809function, and warn you when they do not match the format string.
1810For example, take this declaration of @code{eprintf}:
1811
1812@smallexample
1813void eprintf (const char *template, ...)
1814 __attribute__ ((format (printf, 1, 2)));
1815@end smallexample
1816
1817@noindent
1818This tells the compiler that @code{eprintf} uses a format string like
1819@code{printf} (as opposed to @code{scanf}; @pxref{Formatted Input});
1820the format string appears as the first argument;
1821and the arguments to satisfy the format begin with the second.
1822@xref{Function Attributes, , Declaring Attributes of Functions,
1823gcc.info, Using GNU CC}, for more information.
1824
1825@node Parsing a Template String
1826@subsection Parsing a Template String
1827@cindex parsing a template string
1828
1829You can use the function @code{parse_printf_format} to obtain
1830information about the number and types of arguments that are expected by
1831a given template string. This function permits interpreters that
1832provide interfaces to @code{printf} to avoid passing along invalid
1833arguments from the user's program, which could cause a crash.
1834
1835All the symbols described in this section are declared in the header
1836file @file{printf.h}.
1837
1838@comment printf.h
1839@comment GNU
1840@deftypefun size_t parse_printf_format (const char *@var{template}, size_t @var{n}, int *@var{argtypes})
1841This function returns information about the number and types of
1842arguments expected by the @code{printf} template string @var{template}.
1843The information is stored in the array @var{argtypes}; each element of
1844this array describes one argument. This information is encoded using
1845the various @samp{PA_} macros, listed below.
1846
1847The @var{n} argument specifies the number of elements in the array
1848@var{argtypes}. This is the most elements that
1849@code{parse_printf_format} will try to write.
1850
1851@code{parse_printf_format} returns the total number of arguments required
1852by @var{template}. If this number is greater than @var{n}, then the
1853information returned describes only the first @var{n} arguments. If you
1854want information about more than that many arguments, allocate a bigger
1855array and call @code{parse_printf_format} again.
1856@end deftypefun
1857
1858The argument types are encoded as a combination of a basic type and
1859modifier flag bits.
1860
1861@comment printf.h
1862@comment GNU
1863@deftypevr Macro int PA_FLAG_MASK
1864This macro is a bitmask for the type modifier flag bits. You can write
1865the expression @code{(argtypes[i] & PA_FLAG_MASK)} to extract just the
1866flag bits for an argument, or @code{(argtypes[i] & ~PA_FLAG_MASK)} to
1867extract just the basic type code.
1868@end deftypevr
1869
1870Here are symbolic constants that represent the basic types; they stand
1871for integer values.
1872
779ae82e 1873@vtable @code
28f540f4
RM
1874@comment printf.h
1875@comment GNU
1876@item PA_INT
28f540f4
RM
1877This specifies that the base type is @code{int}.
1878
1879@comment printf.h
1880@comment GNU
1881@item PA_CHAR
28f540f4
RM
1882This specifies that the base type is @code{int}, cast to @code{char}.
1883
1884@comment printf.h
1885@comment GNU
1886@item PA_STRING
28f540f4
RM
1887This specifies that the base type is @code{char *}, a null-terminated string.
1888
1889@comment printf.h
1890@comment GNU
1891@item PA_POINTER
28f540f4
RM
1892This specifies that the base type is @code{void *}, an arbitrary pointer.
1893
1894@comment printf.h
1895@comment GNU
1896@item PA_FLOAT
28f540f4
RM
1897This specifies that the base type is @code{float}.
1898
1899@comment printf.h
1900@comment GNU
1901@item PA_DOUBLE
28f540f4
RM
1902This specifies that the base type is @code{double}.
1903
1904@comment printf.h
1905@comment GNU
1906@item PA_LAST
28f540f4
RM
1907You can define additional base types for your own programs as offsets
1908from @code{PA_LAST}. For example, if you have data types @samp{foo}
1909and @samp{bar} with their own specialized @code{printf} conversions,
1910you could define encodings for these types as:
1911
1912@smallexample
1913#define PA_FOO PA_LAST
1914#define PA_BAR (PA_LAST + 1)
1915@end smallexample
779ae82e 1916@end vtable
28f540f4
RM
1917
1918Here are the flag bits that modify a basic type. They are combined with
1919the code for the basic type using inclusive-or.
1920
779ae82e 1921@vtable @code
28f540f4
RM
1922@comment printf.h
1923@comment GNU
1924@item PA_FLAG_PTR
28f540f4
RM
1925If this bit is set, it indicates that the encoded type is a pointer to
1926the base type, rather than an immediate value.
1927For example, @samp{PA_INT|PA_FLAG_PTR} represents the type @samp{int *}.
1928
1929@comment printf.h
1930@comment GNU
1931@item PA_FLAG_SHORT
28f540f4
RM
1932If this bit is set, it indicates that the base type is modified with
1933@code{short}. (This corresponds to the @samp{h} type modifier.)
1934
1935@comment printf.h
1936@comment GNU
1937@item PA_FLAG_LONG
28f540f4
RM
1938If this bit is set, it indicates that the base type is modified with
1939@code{long}. (This corresponds to the @samp{l} type modifier.)
1940
1941@comment printf.h
1942@comment GNU
1943@item PA_FLAG_LONG_LONG
28f540f4
RM
1944If this bit is set, it indicates that the base type is modified with
1945@code{long long}. (This corresponds to the @samp{L} type modifier.)
1946
1947@comment printf.h
1948@comment GNU
1949@item PA_FLAG_LONG_DOUBLE
28f540f4
RM
1950This is a synonym for @code{PA_FLAG_LONG_LONG}, used by convention with
1951a base type of @code{PA_DOUBLE} to indicate a type of @code{long double}.
779ae82e 1952@end vtable
28f540f4
RM
1953
1954@ifinfo
6d52618b 1955For an example of using these facilities, see @ref{Example of Parsing}.
28f540f4
RM
1956@end ifinfo
1957
1958@node Example of Parsing
1959@subsection Example of Parsing a Template String
1960
1961Here is an example of decoding argument types for a format string. We
1962assume this is part of an interpreter which contains arguments of type
1963@code{NUMBER}, @code{CHAR}, @code{STRING} and @code{STRUCTURE} (and
1964perhaps others which are not valid here).
1965
1966@smallexample
1967/* @r{Test whether the @var{nargs} specified objects}
1968 @r{in the vector @var{args} are valid}
1969 @r{for the format string @var{format}:}
1970 @r{if so, return 1.}
1971 @r{If not, return 0 after printing an error message.} */
1972
1973int
1974validate_args (char *format, int nargs, OBJECT *args)
1975@{
1976 int *argtypes;
1977 int nwanted;
1978
1979 /* @r{Get the information about the arguments.}
1980 @r{Each conversion specification must be at least two characters}
1981 @r{long, so there cannot be more specifications than half the}
1982 @r{length of the string.} */
1983
1984 argtypes = (int *) alloca (strlen (format) / 2 * sizeof (int));
1985 nwanted = parse_printf_format (string, nelts, argtypes);
1986
1987 /* @r{Check the number of arguments.} */
1988 if (nwanted > nargs)
1989 @{
1990 error ("too few arguments (at least %d required)", nwanted);
1991 return 0;
1992 @}
19c3f208 1993
28f540f4
RM
1994 /* @r{Check the C type wanted for each argument}
1995 @r{and see if the object given is suitable.} */
1996 for (i = 0; i < nwanted; i++)
1997 @{
1998 int wanted;
1999
2000 if (argtypes[i] & PA_FLAG_PTR)
2001 wanted = STRUCTURE;
2002 else
2003 switch (argtypes[i] & ~PA_FLAG_MASK)
2004 @{
2005 case PA_INT:
2006 case PA_FLOAT:
2007 case PA_DOUBLE:
2008 wanted = NUMBER;
2009 break;
2010 case PA_CHAR:
2011 wanted = CHAR;
2012 break;
2013 case PA_STRING:
2014 wanted = STRING;
2015 break;
2016 case PA_POINTER:
2017 wanted = STRUCTURE;
2018 break;
2019 @}
2020 if (TYPE (args[i]) != wanted)
2021 @{
2022 error ("type mismatch for arg number %d", i);
2023 return 0;
2024 @}
2025 @}
2026 return 1;
2027@}
2028@end smallexample
2029
2030@node Customizing Printf
2031@section Customizing @code{printf}
2032@cindex customizing @code{printf}
2033@cindex defining new @code{printf} conversions
2034@cindex extending @code{printf}
2035
2036The GNU C library lets you define your own custom conversion specifiers
2037for @code{printf} template strings, to teach @code{printf} clever ways
2038to print the important data structures of your program.
2039
2040The way you do this is by registering the conversion with the function
2041@code{register_printf_function}; see @ref{Registering New Conversions}.
2042One of the arguments you pass to this function is a pointer to a handler
2043function that produces the actual output; see @ref{Defining the Output
2044Handler}, for information on how to write this function.
2045
2046You can also install a function that just returns information about the
2047number and type of arguments expected by the conversion specifier.
2048@xref{Parsing a Template String}, for information about this.
2049
2050The facilities of this section are declared in the header file
2051@file{printf.h}.
2052
2053@menu
19c3f208 2054* Registering New Conversions:: Using @code{register_printf_function}
28f540f4
RM
2055 to register a new output conversion.
2056* Conversion Specifier Options:: The handler must be able to get
2057 the options specified in the
19c3f208 2058 template when it is called.
28f540f4
RM
2059* Defining the Output Handler:: Defining the handler and arginfo
2060 functions that are passed as arguments
19c3f208 2061 to @code{register_printf_function}.
28f540f4 2062* Printf Extension Example:: How to define a @code{printf}
19c3f208 2063 handler function.
29bb8719 2064* Predefined Printf Handlers:: Predefined @code{printf} handlers.
28f540f4
RM
2065@end menu
2066
2067@strong{Portability Note:} The ability to extend the syntax of
f65fd747 2068@code{printf} template strings is a GNU extension. ISO standard C has
28f540f4
RM
2069nothing similar.
2070
2071@node Registering New Conversions
2072@subsection Registering New Conversions
2073
2074The function to register a new output conversion is
2075@code{register_printf_function}, declared in @file{printf.h}.
2076@pindex printf.h
2077
2078@comment printf.h
2079@comment GNU
2080@deftypefun int register_printf_function (int @var{spec}, printf_function @var{handler-function}, printf_arginfo_function @var{arginfo-function})
2081This function defines the conversion specifier character @var{spec}.
2082Thus, if @var{spec} is @code{'z'}, it defines the conversion @samp{%z}.
2083You can redefine the built-in conversions like @samp{%s}, but flag
2084characters like @samp{#} and type modifiers like @samp{l} can never be
2085used as conversions; calling @code{register_printf_function} for those
2086characters has no effect.
2087
2088The @var{handler-function} is the function called by @code{printf} and
2089friends when this conversion appears in a template string.
2090@xref{Defining the Output Handler}, for information about how to define
2091a function to pass as this argument. If you specify a null pointer, any
2092existing handler function for @var{spec} is removed.
2093
2094The @var{arginfo-function} is the function called by
2095@code{parse_printf_format} when this conversion appears in a
2096template string. @xref{Parsing a Template String}, for information
2097about this.
2098
54d79e99
UD
2099@c The following is not true anymore. The `parse_printf_format' function
2100@c is now also called from `vfprintf' via `parse_one_spec'.
2101@c --drepper@gnu, 1996/11/14
2102@c
2103@c Normally, you install both functions for a conversion at the same time,
2104@c but if you are never going to call @code{parse_printf_format}, you do
2105@c not need to define an arginfo function.
2106
2107@strong{Attention:} In the GNU C library version before 2.0 the
2108@var{arginfo-function} function did not need to be installed unless
2109the user uses the @code{parse_printf_format} function. This changed.
2110Now a call to any of the @code{printf} functions will call this
2111function when this format specifier appears in the format string.
28f540f4
RM
2112
2113The return value is @code{0} on success, and @code{-1} on failure
2114(which occurs if @var{spec} is out of range).
2115
2116You can redefine the standard output conversions, but this is probably
2117not a good idea because of the potential for confusion. Library routines
2118written by other people could break if you do this.
2119@end deftypefun
2120
2121@node Conversion Specifier Options
2122@subsection Conversion Specifier Options
2123
40deae08
RM
2124If you define a meaning for @samp{%A}, what if the template contains
2125@samp{%+23A} or @samp{%-#A}? To implement a sensible meaning for these,
28f540f4
RM
2126the handler when called needs to be able to get the options specified in
2127the template.
2128
838e5ffe
UD
2129Both the @var{handler-function} and @var{arginfo-function} accept an
2130argument that points to a @code{struct printf_info}, which contains
2131information about the options appearing in an instance of the conversion
2132specifier. This data type is declared in the header file
2133@file{printf.h}.
28f540f4
RM
2134@pindex printf.h
2135
2136@comment printf.h
2137@comment GNU
2138@deftp {Type} {struct printf_info}
2139This structure is used to pass information about the options appearing
2140in an instance of a conversion specifier in a @code{printf} template
2141string to the handler and arginfo functions for that specifier. It
2142contains the following members:
2143
2144@table @code
2145@item int prec
2146This is the precision specified. The value is @code{-1} if no precision
2147was specified. If the precision was given as @samp{*}, the
2148@code{printf_info} structure passed to the handler function contains the
2149actual value retrieved from the argument list. But the structure passed
2150to the arginfo function contains a value of @code{INT_MIN}, since the
2151actual value is not known.
2152
2153@item int width
2154This is the minimum field width specified. The value is @code{0} if no
2155width was specified. If the field width was given as @samp{*}, the
2156@code{printf_info} structure passed to the handler function contains the
2157actual value retrieved from the argument list. But the structure passed
2158to the arginfo function contains a value of @code{INT_MIN}, since the
2159actual value is not known.
2160
54d79e99 2161@item wchar_t spec
28f540f4
RM
2162This is the conversion specifier character specified. It's stored in
2163the structure so that you can register the same handler function for
2164multiple characters, but still have a way to tell them apart when the
2165handler function is called.
2166
2167@item unsigned int is_long_double
2168This is a boolean that is true if the @samp{L}, @samp{ll}, or @samp{q}
2169type modifier was specified. For integer conversions, this indicates
2170@code{long long int}, as opposed to @code{long double} for floating
2171point conversions.
2172
d64b6ad0
UD
2173@item unsigned int is_char
2174This is a boolean that is true if the @samp{hh} type modifier was specified.
2175
28f540f4
RM
2176@item unsigned int is_short
2177This is a boolean that is true if the @samp{h} type modifier was specified.
2178
2179@item unsigned int is_long
2180This is a boolean that is true if the @samp{l} type modifier was specified.
2181
2182@item unsigned int alt
2183This is a boolean that is true if the @samp{#} flag was specified.
2184
2185@item unsigned int space
2186This is a boolean that is true if the @samp{ } flag was specified.
2187
2188@item unsigned int left
2189This is a boolean that is true if the @samp{-} flag was specified.
2190
2191@item unsigned int showsign
2192This is a boolean that is true if the @samp{+} flag was specified.
2193
2194@item unsigned int group
2195This is a boolean that is true if the @samp{'} flag was specified.
2196
54d79e99
UD
2197@item unsigned int extra
2198This flag has a special meaning depending on the context. It could
2199be used freely by the user-defined handlers but when called from
2200the @code{printf} function this variable always contains the value
2201@code{0}.
2202
d64b6ad0
UD
2203@item unsigned int wide
2204This flag is set if the stream is wide oriented.
2205
54d79e99 2206@item wchar_t pad
28f540f4
RM
2207This is the character to use for padding the output to the minimum field
2208width. The value is @code{'0'} if the @samp{0} flag was specified, and
2209@code{' '} otherwise.
2210@end table
2211@end deftp
2212
2213
2214@node Defining the Output Handler
2215@subsection Defining the Output Handler
2216
2217Now let's look at how to define the handler and arginfo functions
2218which are passed as arguments to @code{register_printf_function}.
2219
0ea554bf 2220@strong{Compatibility Note:} The interface changed in the GNU libc
54d79e99
UD
2221version 2.0. Previously the third argument was of type
2222@code{va_list *}.
2223
28f540f4
RM
2224You should define your handler functions with a prototype like:
2225
2226@smallexample
2227int @var{function} (FILE *stream, const struct printf_info *info,
54d79e99 2228 const void *const *args)
28f540f4
RM
2229@end smallexample
2230
54d79e99 2231The @var{stream} argument passed to the handler function is the stream to
28f540f4
RM
2232which it should write output.
2233
54d79e99 2234The @var{info} argument is a pointer to a structure that contains
28f540f4
RM
2235information about the various options that were included with the
2236conversion in the template string. You should not modify this structure
2237inside your handler function. @xref{Conversion Specifier Options}, for
2238a description of this data structure.
2239
54d79e99
UD
2240@c The following changes some time back. --drepper@gnu, 1996/11/14
2241@c
2242@c The @code{ap_pointer} argument is used to pass the tail of the variable
2243@c argument list containing the values to be printed to your handler.
2244@c Unlike most other functions that can be passed an explicit variable
2245@c argument list, this is a @emph{pointer} to a @code{va_list}, rather than
2246@c the @code{va_list} itself. Thus, you should fetch arguments by
2247@c means of @code{va_arg (*ap_pointer, @var{type})}.
2248@c
2249@c (Passing a pointer here allows the function that calls your handler
2250@c function to update its own @code{va_list} variable to account for the
2251@c arguments that your handler processes. @xref{Variadic Functions}.)
2252
2253The @var{args} is a vector of pointers to the arguments data.
2254The number of arguments were determined by calling the argument
2255information function provided by the user.
28f540f4
RM
2256
2257Your handler function should return a value just like @code{printf}
2258does: it should return the number of characters it has written, or a
2259negative value to indicate an error.
2260
2261@comment printf.h
2262@comment GNU
2263@deftp {Data Type} printf_function
2264This is the data type that a handler function should have.
2265@end deftp
2266
2267If you are going to use @w{@code{parse_printf_format}} in your
54d79e99 2268application, you must also define a function to pass as the
28f540f4 2269@var{arginfo-function} argument for each new conversion you install with
19c3f208 2270@code{register_printf_function}.
28f540f4 2271
54d79e99 2272You have to define these functions with a prototype like:
28f540f4
RM
2273
2274@smallexample
2275int @var{function} (const struct printf_info *info,
2276 size_t n, int *argtypes)
2277@end smallexample
2278
2279The return value from the function should be the number of arguments the
2280conversion expects. The function should also fill in no more than
2281@var{n} elements of the @var{argtypes} array with information about the
2282types of each of these arguments. This information is encoded using the
2283various @samp{PA_} macros. (You will notice that this is the same
2284calling convention @code{parse_printf_format} itself uses.)
2285
2286@comment printf.h
2287@comment GNU
2288@deftp {Data Type} printf_arginfo_function
2289This type is used to describe functions that return information about
2290the number and type of arguments used by a conversion specifier.
2291@end deftp
2292
2293@node Printf Extension Example
2294@subsection @code{printf} Extension Example
2295
2296Here is an example showing how to define a @code{printf} handler function.
19c3f208 2297This program defines a data structure called a @code{Widget} and
28f540f4
RM
2298defines the @samp{%W} conversion to print information about @w{@code{Widget *}}
2299arguments, including the pointer value and the name stored in the data
2300structure. The @samp{%W} conversion supports the minimum field width and
2301left-justification options, but ignores everything else.
2302
2303@smallexample
2304@include rprintf.c.texi
2305@end smallexample
2306
2307The output produced by this program looks like:
2308
2309@smallexample
2310|<Widget 0xffeffb7c: mywidget>|
2311| <Widget 0xffeffb7c: mywidget>|
2312|<Widget 0xffeffb7c: mywidget> |
2313@end smallexample
2314
29bb8719
UD
2315@node Predefined Printf Handlers
2316@subsection Predefined @code{printf} Handlers
2317
2318The GNU libc also contains a concrete and useful application of the
2319@code{printf} handler extension. There are two functions available
2320which implement a special way to print floating-point numbers.
2321
2322@comment printf.h
2323@comment GNU
2324@deftypefun int printf_size (FILE *@var{fp}, const struct printf_info *@var{info}, const void *const *@var{args})
2325Print a given floating point number as for the format @code{%f} except
2326that there is a postfix character indicating the divisor for the
2327number to make this less than 1000. There are two possible divisors:
2328powers of 1024 or powers to 1000. Which one is used depends on the
2329format character specified while registered this handler. If the
2330character is of lower case, 1024 is used. For upper case characters,
23311000 is used.
2332
2333The postfix tag corresponds to bytes, kilobytes, megabytes, gigabytes,
2334etc. The full table is:
2335
779ae82e
UD
2336@ifinfo
2337@multitable @hsep @vsep {' '} {2^10 (1024)} {zetta} {Upper} {10^24 (1000)}
29bb8719
UD
2338@item low @tab Multiplier @tab From @tab Upper @tab Multiplier
2339@item ' ' @tab 1 @tab @tab ' ' @tab 1
2340@item k @tab 2^10 (1024) @tab kilo @tab K @tab 10^3 (1000)
2341@item m @tab 2^20 @tab mega @tab M @tab 10^6
2342@item g @tab 2^30 @tab giga @tab G @tab 10^9
2343@item t @tab 2^40 @tab tera @tab T @tab 10^12
2344@item p @tab 2^50 @tab peta @tab P @tab 10^15
2345@item e @tab 2^60 @tab exa @tab E @tab 10^18
2346@item z @tab 2^70 @tab zetta @tab Z @tab 10^21
2347@item y @tab 2^80 @tab yotta @tab Y @tab 10^24
2348@end multitable
779ae82e
UD
2349@end ifinfo
2350@iftex
2351@tex
2352\hbox to\hsize{\hfil\vbox{\offinterlineskip
2353\hrule
2354\halign{\strut#& \vrule#\tabskip=1em plus2em& {\tt#}\hfil& \vrule#& #\hfil& \vrule#& #\hfil& \vrule#& {\tt#}\hfil& \vrule#& #\hfil& \vrule#\tabskip=0pt\cr
2355\noalign{\hrule}
2356\omit&height2pt&\omit&&\omit&&\omit&&\omit&&\omit&\cr
2357&& \omit low && Multiplier && From && \omit Upper && Multiplier &\cr
2358\omit&height2pt&\omit&&\omit&&\omit&&\omit&&\omit&\cr
2359\noalign{\hrule}
2360&& {\tt\char32} && 1 && && {\tt\char32} && 1 &\cr
2361&& k && $2^{10} = 1024$ && kilo && K && $10^3 = 1000$ &\cr
2362&& m && $2^{20}$ && mega && M && $10^6$ &\cr
2363&& g && $2^{30}$ && giga && G && $10^9$ &\cr
2364&& t && $2^{40}$ && tera && T && $10^{12}$ &\cr
2365&& p && $2^{50}$ && peta && P && $10^{15}$ &\cr
2366&& e && $2^{60}$ && exa && E && $10^{18}$ &\cr
2367&& z && $2^{70}$ && zetta && Z && $10^{21}$ &\cr
2368&& y && $2^{80}$ && yotta && Y && $10^{24}$ &\cr
2369\noalign{\hrule}}}\hfil}
2370@end tex
2371@end iftex
29bb8719
UD
2372
2373The default precision is 3, i.e., 1024 is printed with a lower-case
2374format character as if it were @code{%.3fk} and will yield @code{1.000k}.
2375@end deftypefun
2376
2377Due to the requirements of @code{register_printf_function} we must also
2378provide the function which return information about the arguments.
2379
2380@comment printf.h
2381@comment GNU
2382@deftypefun int printf_size_info (const struct printf_info *@var{info}, size_t @var{n}, int *@var{argtypes})
2383This function will return in @var{argtypes} the information about the
2384used parameters in the way the @code{vfprintf} implementation expects
2385it. The format always takes one argument.
2386@end deftypefun
2387
2388To use these functions both functions must be registered with a call like
2389
2390@smallexample
2391register_printf_function ('B', printf_size, printf_size_info);
2392@end smallexample
2393
2394Here we register the functions to print numbers as powers of 1000 since
f2ea0f5b 2395the format character @code{'B'} is an upper-case character. If we
29bb8719
UD
2396would additionally use @code{'b'} in a line like
2397
2398@smallexample
2399register_printf_function ('b', printf_size, printf_size_info);
2400@end smallexample
2401
2402@noindent
2403we could also print using power of 1024. Please note that all what is
2404different in these both lines in the format specifier. The
2405@code{printf_size} function knows about the difference of low and upper
2406case format specifiers.
2407
2408The use of @code{'B'} and @code{'b'} is no coincidence. Rather it is
2409the preferred way to use this functionality since it is available on
2410some other systems also available using the format specifiers.
2411
28f540f4
RM
2412@node Formatted Input
2413@section Formatted Input
2414
2415@cindex formatted input from a stream
2416@cindex reading from a stream, formatted
2417@cindex format string, for @code{scanf}
2418@cindex template, for @code{scanf}
2419The functions described in this section (@code{scanf} and related
2420functions) provide facilities for formatted input analogous to the
2421formatted output facilities. These functions provide a mechanism for
2422reading arbitrary values under the control of a @dfn{format string} or
2423@dfn{template string}.
2424
2425@menu
2426* Formatted Input Basics:: Some basics to get you started.
2427* Input Conversion Syntax:: Syntax of conversion specifications.
2428* Table of Input Conversions:: Summary of input conversions and what they do.
2429* Numeric Input Conversions:: Details of conversions for reading numbers.
2430* String Input Conversions:: Details of conversions for reading strings.
2431* Dynamic String Input:: String conversions that @code{malloc} the buffer.
2432* Other Input Conversions:: Details of miscellaneous other conversions.
2433* Formatted Input Functions:: Descriptions of the actual functions.
2434* Variable Arguments Input:: @code{vscanf} and friends.
2435@end menu
2436
2437@node Formatted Input Basics
2438@subsection Formatted Input Basics
2439
2440Calls to @code{scanf} are superficially similar to calls to
2441@code{printf} in that arbitrary arguments are read under the control of
2442a template string. While the syntax of the conversion specifications in
2443the template is very similar to that for @code{printf}, the
2444interpretation of the template is oriented more towards free-format
2445input and simple pattern matching, rather than fixed-field formatting.
2446For example, most @code{scanf} conversions skip over any amount of
2447``white space'' (including spaces, tabs, and newlines) in the input
2448file, and there is no concept of precision for the numeric input
2449conversions as there is for the corresponding output conversions.
2450Ordinarily, non-whitespace characters in the template are expected to
2451match characters in the input stream exactly, but a matching failure is
2452distinct from an input error on the stream.
2453@cindex conversion specifications (@code{scanf})
2454
2455Another area of difference between @code{scanf} and @code{printf} is
2456that you must remember to supply pointers rather than immediate values
2457as the optional arguments to @code{scanf}; the values that are read are
2458stored in the objects that the pointers point to. Even experienced
2459programmers tend to forget this occasionally, so if your program is
2460getting strange errors that seem to be related to @code{scanf}, you
2461might want to double-check this.
2462
2463When a @dfn{matching failure} occurs, @code{scanf} returns immediately,
2464leaving the first non-matching character as the next character to be
2465read from the stream. The normal return value from @code{scanf} is the
2466number of values that were assigned, so you can use this to determine if
2467a matching error happened before all the expected values were read.
2468@cindex matching failure, in @code{scanf}
2469
2470The @code{scanf} function is typically used for things like reading in
2471the contents of tables. For example, here is a function that uses
2472@code{scanf} to initialize an array of @code{double}:
2473
2474@smallexample
2475void
2476readarray (double *array, int n)
2477@{
2478 int i;
2479 for (i=0; i<n; i++)
2480 if (scanf (" %lf", &(array[i])) != 1)
2481 invalid_input_error ();
2482@}
2483@end smallexample
2484
2485The formatted input functions are not used as frequently as the
2486formatted output functions. Partly, this is because it takes some care
2487to use them properly. Another reason is that it is difficult to recover
2488from a matching error.
2489
2490If you are trying to read input that doesn't match a single, fixed
2491pattern, you may be better off using a tool such as Flex to generate a
2492lexical scanner, or Bison to generate a parser, rather than using
2493@code{scanf}. For more information about these tools, see @ref{, , ,
2494flex.info, Flex: The Lexical Scanner Generator}, and @ref{, , ,
2495bison.info, The Bison Reference Manual}.
2496
2497@node Input Conversion Syntax
2498@subsection Input Conversion Syntax
2499
2500A @code{scanf} template string is a string that contains ordinary
2501multibyte characters interspersed with conversion specifications that
2502start with @samp{%}.
2503
2504Any whitespace character (as defined by the @code{isspace} function;
2505@pxref{Classification of Characters}) in the template causes any number
2506of whitespace characters in the input stream to be read and discarded.
2507The whitespace characters that are matched need not be exactly the same
2508whitespace characters that appear in the template string. For example,
2509write @samp{ , } in the template to recognize a comma with optional
2510whitespace before and after.
2511
2512Other characters in the template string that are not part of conversion
2513specifications must match characters in the input stream exactly; if
2514this is not the case, a matching failure occurs.
2515
2516The conversion specifications in a @code{scanf} template string
2517have the general form:
2518
2519@smallexample
2520% @var{flags} @var{width} @var{type} @var{conversion}
2521@end smallexample
2522
2523In more detail, an input conversion specification consists of an initial
2524@samp{%} character followed in sequence by:
2525
2526@itemize @bullet
2527@item
2528An optional @dfn{flag character} @samp{*}, which says to ignore the text
2529read for this specification. When @code{scanf} finds a conversion
2530specification that uses this flag, it reads input as directed by the
2531rest of the conversion specification, but it discards this input, does
2532not use a pointer argument, and does not increment the count of
2533successful assignments.
2534@cindex flag character (@code{scanf})
2535
2536@item
2537An optional flag character @samp{a} (valid with string conversions only)
2538which requests allocation of a buffer long enough to store the string in.
2539(This is a GNU extension.)
2540@xref{Dynamic String Input}.
2541
2542@item
2543An optional decimal integer that specifies the @dfn{maximum field
2544width}. Reading of characters from the input stream stops either when
2545this maximum is reached or when a non-matching character is found,
2546whichever happens first. Most conversions discard initial whitespace
2547characters (those that don't are explicitly documented), and these
2548discarded characters don't count towards the maximum field width.
2549String input conversions store a null character to mark the end of the
2550input; the maximum field width does not include this terminator.
2551@cindex maximum field width (@code{scanf})
2552
2553@item
2554An optional @dfn{type modifier character}. For example, you can
2555specify a type modifier of @samp{l} with integer conversions such as
2556@samp{%d} to specify that the argument is a pointer to a @code{long int}
2557rather than a pointer to an @code{int}.
2558@cindex type modifier character (@code{scanf})
2559
2560@item
2561A character that specifies the conversion to be applied.
2562@end itemize
2563
19c3f208 2564The exact options that are permitted and how they are interpreted vary
28f540f4
RM
2565between the different conversion specifiers. See the descriptions of the
2566individual conversions for information about the particular options that
2567they allow.
2568
2569With the @samp{-Wformat} option, the GNU C compiler checks calls to
2570@code{scanf} and related functions. It examines the format string and
2571verifies that the correct number and types of arguments are supplied.
2572There is also a GNU C syntax to tell the compiler that a function you
19c3f208 2573write uses a @code{scanf}-style format string.
28f540f4
RM
2574@xref{Function Attributes, , Declaring Attributes of Functions,
2575gcc.info, Using GNU CC}, for more information.
2576
2577@node Table of Input Conversions
2578@subsection Table of Input Conversions
2579@cindex input conversions, for @code{scanf}
2580
2581Here is a table that summarizes the various conversion specifications:
2582
2583@table @asis
2584@item @samp{%d}
2585Matches an optionally signed integer written in decimal. @xref{Numeric
2586Input Conversions}.
2587
2588@item @samp{%i}
2589Matches an optionally signed integer in any of the formats that the C
2590language defines for specifying an integer constant. @xref{Numeric
2591Input Conversions}.
2592
2593@item @samp{%o}
2594Matches an unsigned integer written in octal radix.
2595@xref{Numeric Input Conversions}.
2596
2597@item @samp{%u}
2598Matches an unsigned integer written in decimal radix.
2599@xref{Numeric Input Conversions}.
2600
2601@item @samp{%x}, @samp{%X}
2602Matches an unsigned integer written in hexadecimal radix.
2603@xref{Numeric Input Conversions}.
2604
2605@item @samp{%e}, @samp{%f}, @samp{%g}, @samp{%E}, @samp{%G}
2606Matches an optionally signed floating-point number. @xref{Numeric Input
2607Conversions}.
2608
2609@item @samp{%s}
2610Matches a string containing only non-whitespace characters.
2611@xref{String Input Conversions}.
2612
2613@item @samp{%[}
2614Matches a string of characters that belong to a specified set.
2615@xref{String Input Conversions}.
2616
2617@item @samp{%c}
2618Matches a string of one or more characters; the number of characters
2619read is controlled by the maximum field width given for the conversion.
2620@xref{String Input Conversions}.
2621
2622@item @samp{%p}
2623Matches a pointer value in the same implementation-defined format used
2624by the @samp{%p} output conversion for @code{printf}. @xref{Other Input
2625Conversions}.
2626
2627@item @samp{%n}
2628This conversion doesn't read any characters; it records the number of
2629characters read so far by this call. @xref{Other Input Conversions}.
2630
2631@item @samp{%%}
2632This matches a literal @samp{%} character in the input stream. No
2633corresponding argument is used. @xref{Other Input Conversions}.
2634@end table
2635
2636If the syntax of a conversion specification is invalid, the behavior is
2637undefined. If there aren't enough function arguments provided to supply
2638addresses for all the conversion specifications in the template strings
2639that perform assignments, or if the arguments are not of the correct
2640types, the behavior is also undefined. On the other hand, extra
2641arguments are simply ignored.
2642
2643@node Numeric Input Conversions
2644@subsection Numeric Input Conversions
2645
2646This section describes the @code{scanf} conversions for reading numeric
2647values.
2648
2649The @samp{%d} conversion matches an optionally signed integer in decimal
2650radix. The syntax that is recognized is the same as that for the
2651@code{strtol} function (@pxref{Parsing of Integers}) with the value
2652@code{10} for the @var{base} argument.
2653
2654The @samp{%i} conversion matches an optionally signed integer in any of
2655the formats that the C language defines for specifying an integer
2656constant. The syntax that is recognized is the same as that for the
2657@code{strtol} function (@pxref{Parsing of Integers}) with the value
2658@code{0} for the @var{base} argument. (You can print integers in this
2659syntax with @code{printf} by using the @samp{#} flag character with the
2660@samp{%x}, @samp{%o}, or @samp{%d} conversion. @xref{Integer Conversions}.)
2661
2662For example, any of the strings @samp{10}, @samp{0xa}, or @samp{012}
2663could be read in as integers under the @samp{%i} conversion. Each of
2664these specifies a number with decimal value @code{10}.
2665
2666The @samp{%o}, @samp{%u}, and @samp{%x} conversions match unsigned
2667integers in octal, decimal, and hexadecimal radices, respectively. The
2668syntax that is recognized is the same as that for the @code{strtoul}
2669function (@pxref{Parsing of Integers}) with the appropriate value
2670(@code{8}, @code{10}, or @code{16}) for the @var{base} argument.
2671
2672The @samp{%X} conversion is identical to the @samp{%x} conversion. They
2673both permit either uppercase or lowercase letters to be used as digits.
2674
2675The default type of the corresponding argument for the @code{%d} and
2676@code{%i} conversions is @code{int *}, and @code{unsigned int *} for the
2677other integer conversions. You can use the following type modifiers to
2678specify other sizes of integer:
2679
2680@table @samp
cc3fa755
UD
2681@item hh
2682Specifies that the argument is a @code{signed char *} or @code{unsigned
2683char *}.
2684
e852e889
UD
2685This modifier was introduced in @w{ISO C 9x}.
2686
28f540f4
RM
2687@item h
2688Specifies that the argument is a @code{short int *} or @code{unsigned
2689short int *}.
2690
e852e889
UD
2691@item j
2692Specifies that the argument is a @code{intmax_t *} or @code{uintmax_t *}.
2693
2694This modifier was introduced in @w{ISO C 9x}.
2695
28f540f4
RM
2696@item l
2697Specifies that the argument is a @code{long int *} or @code{unsigned
2698long int *}. Two @samp{l} characters is like the @samp{L} modifier, below.
2699
2700@need 100
2701@item ll
2702@itemx L
2703@itemx q
2704Specifies that the argument is a @code{long long int *} or @code{unsigned long long int *}. (The @code{long long} type is an extension supported by the
2705GNU C compiler. For systems that don't provide extra-long integers, this
2706is the same as @code{long int}.)
2707
2708The @samp{q} modifier is another name for the same thing, which comes
2709from 4.4 BSD; a @w{@code{long long int}} is sometimes called a ``quad''
2710@code{int}.
e852e889
UD
2711
2712@item t
2713Specifies that the argument is a @code{ptrdiff_t *}.
2714
2715This modifier was introduced in @w{ISO C 9x}.
2716
2717@item z
2718Specifies that the argument is a @code{size_t *}.
2719
2720This modifier was introduced in @w{ISO C 9x}.
28f540f4
RM
2721@end table
2722
2723All of the @samp{%e}, @samp{%f}, @samp{%g}, @samp{%E}, and @samp{%G}
2724input conversions are interchangeable. They all match an optionally
2725signed floating point number, in the same syntax as for the
2726@code{strtod} function (@pxref{Parsing of Floats}).
2727
2728For the floating-point input conversions, the default argument type is
2729@code{float *}. (This is different from the corresponding output
2730conversions, where the default type is @code{double}; remember that
2731@code{float} arguments to @code{printf} are converted to @code{double}
2732by the default argument promotions, but @code{float *} arguments are
2733not promoted to @code{double *}.) You can specify other sizes of float
2734using these type modifiers:
2735
2736@table @samp
2737@item l
2738Specifies that the argument is of type @code{double *}.
2739
2740@item L
2741Specifies that the argument is of type @code{long double *}.
2742@end table
2743
2c6fe0bd
UD
2744For all the above number parsing formats there is an additional optional
2745flag @samp{'}. When this flag is given the @code{scanf} function
2746expects the number represented in the input string to be formatted
2747according to the grouping rules of the currently selected locale
2748(@pxref{General Numeric}).
2749
2750If the @code{"C"} or @code{"POSIX"} locale is selected there is no
2751difference. But for a locale which specifies values for the appropriate
2752fields in the locale the input must have the correct form in the input.
2753Otherwise the longest prefix with a correct form is processed.
2754
28f540f4
RM
2755@node String Input Conversions
2756@subsection String Input Conversions
2757
2758This section describes the @code{scanf} input conversions for reading
19c3f208 2759string and character values: @samp{%s}, @samp{%[}, and @samp{%c}.
28f540f4
RM
2760
2761You have two options for how to receive the input from these
2762conversions:
2763
2764@itemize @bullet
2765@item
2766Provide a buffer to store it in. This is the default. You
2767should provide an argument of type @code{char *}.
2768
2769@strong{Warning:} To make a robust program, you must make sure that the
2770input (plus its terminating null) cannot possibly exceed the size of the
2771buffer you provide. In general, the only way to do this is to specify a
2772maximum field width one less than the buffer size. @strong{If you
2773provide the buffer, always specify a maximum field width to prevent
2774overflow.}
2775
2776@item
2777Ask @code{scanf} to allocate a big enough buffer, by specifying the
2778@samp{a} flag character. This is a GNU extension. You should provide
2779an argument of type @code{char **} for the buffer address to be stored
2780in. @xref{Dynamic String Input}.
2781@end itemize
2782
2783The @samp{%c} conversion is the simplest: it matches a fixed number of
2784characters, always. The maximum field with says how many characters to
2785read; if you don't specify the maximum, the default is 1. This
2786conversion doesn't append a null character to the end of the text it
2787reads. It also does not skip over initial whitespace characters. It
2788reads precisely the next @var{n} characters, and fails if it cannot get
2789that many. Since there is always a maximum field width with @samp{%c}
2790(whether specified, or 1 by default), you can always prevent overflow by
2791making the buffer long enough.
2792
2793The @samp{%s} conversion matches a string of non-whitespace characters.
2794It skips and discards initial whitespace, but stops when it encounters
2795more whitespace after having read something. It stores a null character
2796at the end of the text that it reads.
2797
2798For example, reading the input:
2799
2800@smallexample
2801 hello, world
2802@end smallexample
2803
2804@noindent
2805with the conversion @samp{%10c} produces @code{" hello, wo"}, but
2806reading the same input with the conversion @samp{%10s} produces
2807@code{"hello,"}.
2808
2809@strong{Warning:} If you do not specify a field width for @samp{%s},
2810then the number of characters read is limited only by where the next
2811whitespace character appears. This almost certainly means that invalid
2812input can make your program crash---which is a bug.
2813
2814To read in characters that belong to an arbitrary set of your choice,
2815use the @samp{%[} conversion. You specify the set between the @samp{[}
2816character and a following @samp{]} character, using the same syntax used
2817in regular expressions. As special cases:
2818
2819@itemize @bullet
19c3f208 2820@item
28f540f4
RM
2821A literal @samp{]} character can be specified as the first character
2822of the set.
2823
19c3f208 2824@item
28f540f4
RM
2825An embedded @samp{-} character (that is, one that is not the first or
2826last character of the set) is used to specify a range of characters.
2827
19c3f208 2828@item
28f540f4
RM
2829If a caret character @samp{^} immediately follows the initial @samp{[},
2830then the set of allowed input characters is the everything @emph{except}
2831the characters listed.
2832@end itemize
2833
2834The @samp{%[} conversion does not skip over initial whitespace
2835characters.
2836
2837Here are some examples of @samp{%[} conversions and what they mean:
2838
2839@table @samp
2840@item %25[1234567890]
2841Matches a string of up to 25 digits.
2842
2843@item %25[][]
2844Matches a string of up to 25 square brackets.
2845
2846@item %25[^ \f\n\r\t\v]
2847Matches a string up to 25 characters long that doesn't contain any of
2848the standard whitespace characters. This is slightly different from
2849@samp{%s}, because if the input begins with a whitespace character,
2850@samp{%[} reports a matching failure while @samp{%s} simply discards the
2851initial whitespace.
2852
19c3f208 2853@item %25[a-z]
28f540f4
RM
2854Matches up to 25 lowercase characters.
2855@end table
2856
2857One more reminder: the @samp{%s} and @samp{%[} conversions are
2858@strong{dangerous} if you don't specify a maximum width or use the
2859@samp{a} flag, because input too long would overflow whatever buffer you
2860have provided for it. No matter how long your buffer is, a user could
2861supply input that is longer. A well-written program reports invalid
2862input with a comprehensible error message, not with a crash.
2863
2864@node Dynamic String Input
2865@subsection Dynamically Allocating String Conversions
2866
2867A GNU extension to formatted input lets you safely read a string with no
2868maximum size. Using this feature, you don't supply a buffer; instead,
2869@code{scanf} allocates a buffer big enough to hold the data and gives
2870you its address. To use this feature, write @samp{a} as a flag
2871character, as in @samp{%as} or @samp{%a[0-9a-z]}.
2872
2873The pointer argument you supply for where to store the input should have
2874type @code{char **}. The @code{scanf} function allocates a buffer and
2875stores its address in the word that the argument points to. You should
2876free the buffer with @code{free} when you no longer need it.
2877
2878Here is an example of using the @samp{a} flag with the @samp{%[@dots{}]}
2879conversion specification to read a ``variable assignment'' of the form
2880@samp{@var{variable} = @var{value}}.
2881
2882@smallexample
2883@{
2884 char *variable, *value;
2885
2886 if (2 > scanf ("%a[a-zA-Z0-9] = %a[^\n]\n",
2887 &variable, &value))
2888 @{
2889 invalid_input_error ();
2890 return 0;
2891 @}
2892
2893 @dots{}
2894@}
2895@end smallexample
2896
2897@node Other Input Conversions
2898@subsection Other Input Conversions
2899
2900This section describes the miscellaneous input conversions.
2901
2902The @samp{%p} conversion is used to read a pointer value. It recognizes
2903the same syntax as is used by the @samp{%p} output conversion for
2904@code{printf} (@pxref{Other Output Conversions}); that is, a hexadecimal
2905number just as the @samp{%x} conversion accepts. The corresponding
2906argument should be of type @code{void **}; that is, the address of a
2907place to store a pointer.
2908
2909The resulting pointer value is not guaranteed to be valid if it was not
2910originally written during the same program execution that reads it in.
2911
2912The @samp{%n} conversion produces the number of characters read so far
2913by this call. The corresponding argument should be of type @code{int *}.
2914This conversion works in the same way as the @samp{%n} conversion for
2915@code{printf}; see @ref{Other Output Conversions}, for an example.
2916
2917The @samp{%n} conversion is the only mechanism for determining the
2918success of literal matches or conversions with suppressed assignments.
2919If the @samp{%n} follows the locus of a matching failure, then no value
2920is stored for it since @code{scanf} returns before processing the
2921@samp{%n}. If you store @code{-1} in that argument slot before calling
2922@code{scanf}, the presence of @code{-1} after @code{scanf} indicates an
2923error occurred before the @samp{%n} was reached.
2924
2925Finally, the @samp{%%} conversion matches a literal @samp{%} character
2926in the input stream, without using an argument. This conversion does
2927not permit any flags, field width, or type modifier to be specified.
2928
2929@node Formatted Input Functions
2930@subsection Formatted Input Functions
2931
2932Here are the descriptions of the functions for performing formatted
2933input.
2934Prototypes for these functions are in the header file @file{stdio.h}.
2935@pindex stdio.h
2936
2937@comment stdio.h
f65fd747 2938@comment ISO
28f540f4
RM
2939@deftypefun int scanf (const char *@var{template}, @dots{})
2940The @code{scanf} function reads formatted input from the stream
2941@code{stdin} under the control of the template string @var{template}.
2942The optional arguments are pointers to the places which receive the
2943resulting values.
2944
2945The return value is normally the number of successful assignments. If
2946an end-of-file condition is detected before any matches are performed
2947(including matches against whitespace and literal characters in the
2948template), then @code{EOF} is returned.
2949@end deftypefun
2950
2951@comment stdio.h
f65fd747 2952@comment ISO
28f540f4
RM
2953@deftypefun int fscanf (FILE *@var{stream}, const char *@var{template}, @dots{})
2954This function is just like @code{scanf}, except that the input is read
2955from the stream @var{stream} instead of @code{stdin}.
2956@end deftypefun
2957
2958@comment stdio.h
f65fd747 2959@comment ISO
28f540f4
RM
2960@deftypefun int sscanf (const char *@var{s}, const char *@var{template}, @dots{})
2961This is like @code{scanf}, except that the characters are taken from the
2962null-terminated string @var{s} instead of from a stream. Reaching the
2963end of the string is treated as an end-of-file condition.
2964
2965The behavior of this function is undefined if copying takes place
2966between objects that overlap---for example, if @var{s} is also given
2967as an argument to receive a string read under control of the @samp{%s}
2968conversion.
2969@end deftypefun
2970
2971@node Variable Arguments Input
2972@subsection Variable Arguments Input Functions
2973
2974The functions @code{vscanf} and friends are provided so that you can
2975define your own variadic @code{scanf}-like functions that make use of
2976the same internals as the built-in formatted output functions.
2977These functions are analogous to the @code{vprintf} series of output
2978functions. @xref{Variable Arguments Output}, for important
2979information on how to use them.
2980
2981@strong{Portability Note:} The functions listed in this section are GNU
2982extensions.
2983
2984@comment stdio.h
2985@comment GNU
2986@deftypefun int vscanf (const char *@var{template}, va_list @var{ap})
2987This function is similar to @code{scanf} except that, instead of taking
2988a variable number of arguments directly, it takes an argument list
2989pointer @var{ap} of type @code{va_list} (@pxref{Variadic Functions}).
2990@end deftypefun
2991
2992@comment stdio.h
2993@comment GNU
2994@deftypefun int vfscanf (FILE *@var{stream}, const char *@var{template}, va_list @var{ap})
2995This is the equivalent of @code{fscanf} with the variable argument list
2996specified directly as for @code{vscanf}.
2997@end deftypefun
2998
2999@comment stdio.h
3000@comment GNU
3001@deftypefun int vsscanf (const char *@var{s}, const char *@var{template}, va_list @var{ap})
3002This is the equivalent of @code{sscanf} with the variable argument list
3003specified directly as for @code{vscanf}.
3004@end deftypefun
3005
3006In GNU C, there is a special construct you can use to let the compiler
3007know that a function uses a @code{scanf}-style format string. Then it
3008can check the number and types of arguments in each call to the
3009function, and warn you when they do not match the format string.
3010@xref{Function Attributes, , Declaring Attributes of Functions,
3011gcc.info, Using GNU CC}, for details.
3012
3013@node EOF and Errors
3014@section End-Of-File and Errors
3015
3016@cindex end of file, on a stream
3017Many of the functions described in this chapter return the value of the
3018macro @code{EOF} to indicate unsuccessful completion of the operation.
3019Since @code{EOF} is used to report both end of file and random errors,
3020it's often better to use the @code{feof} function to check explicitly
3021for end of file and @code{ferror} to check for errors. These functions
3022check indicators that are part of the internal state of the stream
3023object, indicators set if the appropriate condition was detected by a
3024previous I/O operation on that stream.
3025
3026These symbols are declared in the header file @file{stdio.h}.
3027@pindex stdio.h
3028
3029@comment stdio.h
f65fd747 3030@comment ISO
28f540f4
RM
3031@deftypevr Macro int EOF
3032This macro is an integer value that is returned by a number of functions
3033to indicate an end-of-file condition, or some other error situation.
3034With the GNU library, @code{EOF} is @code{-1}. In other libraries, its
3035value may be some other negative number.
3036@end deftypevr
3037
3038@comment stdio.h
f65fd747 3039@comment ISO
28f540f4
RM
3040@deftypefun void clearerr (FILE *@var{stream})
3041This function clears the end-of-file and error indicators for the
3042stream @var{stream}.
3043
3044The file positioning functions (@pxref{File Positioning}) also clear the
3045end-of-file indicator for the stream.
3046@end deftypefun
3047
3048@comment stdio.h
f65fd747 3049@comment ISO
28f540f4
RM
3050@deftypefun int feof (FILE *@var{stream})
3051The @code{feof} function returns nonzero if and only if the end-of-file
3052indicator for the stream @var{stream} is set.
3053@end deftypefun
3054
3055@comment stdio.h
f65fd747 3056@comment ISO
28f540f4
RM
3057@deftypefun int ferror (FILE *@var{stream})
3058The @code{ferror} function returns nonzero if and only if the error
3059indicator for the stream @var{stream} is set, indicating that an error
3060has occurred on a previous operation on the stream.
3061@end deftypefun
3062
3063In addition to setting the error indicator associated with the stream,
3064the functions that operate on streams also set @code{errno} in the same
3065way as the corresponding low-level functions that operate on file
3066descriptors. For example, all of the functions that perform output to a
3067stream---such as @code{fputc}, @code{printf}, and @code{fflush}---are
3068implemented in terms of @code{write}, and all of the @code{errno} error
3069conditions defined for @code{write} are meaningful for these functions.
3070For more information about the descriptor-level I/O functions, see
3071@ref{Low-Level I/O}.
3072
3073@node Binary Streams
3074@section Text and Binary Streams
3075
3076The GNU system and other POSIX-compatible operating systems organize all
3077files as uniform sequences of characters. However, some other systems
3078make a distinction between files containing text and files containing
f65fd747 3079binary data, and the input and output facilities of @w{ISO C} provide for
28f540f4
RM
3080this distinction. This section tells you how to write programs portable
3081to such systems.
3082
3083@cindex text stream
3084@cindex binary stream
3085When you open a stream, you can specify either a @dfn{text stream} or a
3086@dfn{binary stream}. You indicate that you want a binary stream by
3087specifying the @samp{b} modifier in the @var{opentype} argument to
3088@code{fopen}; see @ref{Opening Streams}. Without this
3089option, @code{fopen} opens the file as a text stream.
3090
3091Text and binary streams differ in several ways:
3092
3093@itemize @bullet
3094@item
3095The data read from a text stream is divided into @dfn{lines} which are
3096terminated by newline (@code{'\n'}) characters, while a binary stream is
3097simply a long series of characters. A text stream might on some systems
3098fail to handle lines more than 254 characters long (including the
3099terminating newline character).
3100@cindex lines (in a text file)
3101
3102@item
3103On some systems, text files can contain only printing characters,
3104horizontal tab characters, and newlines, and so text streams may not
3105support other characters. However, binary streams can handle any
3106character value.
3107
3108@item
3109Space characters that are written immediately preceding a newline
3110character in a text stream may disappear when the file is read in again.
3111
3112@item
3113More generally, there need not be a one-to-one mapping between
3114characters that are read from or written to a text stream, and the
3115characters in the actual file.
3116@end itemize
3117
3118Since a binary stream is always more capable and more predictable than a
3119text stream, you might wonder what purpose text streams serve. Why not
3120simply always use binary streams? The answer is that on these operating
3121systems, text and binary streams use different file formats, and the
3122only way to read or write ``an ordinary file of text'' that can work
3123with other text-oriented programs is through a text stream.
3124
3125In the GNU library, and on all POSIX systems, there is no difference
3126between text streams and binary streams. When you open a stream, you
3127get the same kind of stream regardless of whether you ask for binary.
3128This stream can handle any file content, and has none of the
3129restrictions that text streams sometimes have.
3130
3131@node File Positioning
3132@section File Positioning
3133@cindex file positioning on a stream
3134@cindex positioning a stream
3135@cindex seeking on a stream
3136
3137The @dfn{file position} of a stream describes where in the file the
3138stream is currently reading or writing. I/O on the stream advances the
3139file position through the file. In the GNU system, the file position is
3140represented as an integer, which counts the number of bytes from the
3141beginning of the file. @xref{File Position}.
3142
3143During I/O to an ordinary disk file, you can change the file position
3144whenever you wish, so as to read or write any portion of the file. Some
3145other kinds of files may also permit this. Files which support changing
3146the file position are sometimes referred to as @dfn{random-access}
3147files.
3148
3149You can use the functions in this section to examine or modify the file
3150position indicator associated with a stream. The symbols listed below
3151are declared in the header file @file{stdio.h}.
3152@pindex stdio.h
3153
3154@comment stdio.h
f65fd747 3155@comment ISO
28f540f4
RM
3156@deftypefun {long int} ftell (FILE *@var{stream})
3157This function returns the current file position of the stream
3158@var{stream}.
3159
3160This function can fail if the stream doesn't support file positioning,
3161or if the file position can't be represented in a @code{long int}, and
3162possibly for other reasons as well. If a failure occurs, a value of
3163@code{-1} is returned.
3164@end deftypefun
3165
a5a0310d
UD
3166@comment stdio.h
3167@comment Unix98
3168@deftypefun off_t ftello (FILE *@var{stream})
3169The @code{ftello} function is similar to @code{ftell} only it corrects a
3170problem which the POSIX type system. In this type system all file
3171positions are described using values of type @code{off_t} which is not
3172necessarily of the same size as @code{long int}. Therefore using
3173@code{ftell} can lead to problems if the implementation is written on
3174top of a POSIX compliant lowlevel I/O implementation.
3175
3176Therefore it is a good idea to prefer @code{ftello} whenever it is
3177available since its functionality is (if different at all) closer the
3178underlying definition.
3179
3180If this function fails it return @code{(off_t) -1}. This can happen due
3181to missing support for file positioning or internal errors. Otherwise
3182the return value is the current file position.
3183
3184The function is an extension defined in the Unix Single Specification
3185version 2.
a3a4a74e
UD
3186
3187When the sources are compiled with @code{_FILE_OFFSET_BITS == 64} on a
318832 bits system this function is in fact @code{ftello64}. I.e., the
3189LFS interface transparently replaces the old interface.
3190@end deftypefun
3191
3192@comment stdio.h
3193@comment Unix98
3194@deftypefun off64_t ftello64 (FILE *@var{stream})
3195This function is similar to @code{ftello} with the only difference that
3196the return value is of type @code{off64_t}. This also requires that the
3197stream @var{stream} was opened using either @code{fopen64},
3198@code{freopen64}, or @code{tmpfile64} since otherwise the underlying
3199file operations to position the file pointer beyond the @math{2^31}
3200bytes limit might fail.
3201
3202If the sources are compiled with @code{_FILE_OFFSET_BITS == 64} on a 32
3203bits machine this function is available under the name @code{ftello}
3204and so transparently replaces the old interface.
a5a0310d
UD
3205@end deftypefun
3206
28f540f4 3207@comment stdio.h
f65fd747 3208@comment ISO
28f540f4
RM
3209@deftypefun int fseek (FILE *@var{stream}, long int @var{offset}, int @var{whence})
3210The @code{fseek} function is used to change the file position of the
3211stream @var{stream}. The value of @var{whence} must be one of the
3212constants @code{SEEK_SET}, @code{SEEK_CUR}, or @code{SEEK_END}, to
3213indicate whether the @var{offset} is relative to the beginning of the
3214file, the current file position, or the end of the file, respectively.
3215
3216This function returns a value of zero if the operation was successful,
3217and a nonzero value to indicate failure. A successful call also clears
3218the end-of-file indicator of @var{stream} and discards any characters
3219that were ``pushed back'' by the use of @code{ungetc}.
3220
3221@code{fseek} either flushes any buffered output before setting the file
3222position or else remembers it so it will be written later in its proper
3223place in the file.
3224@end deftypefun
3225
a5a0310d
UD
3226@comment stdio.h
3227@comment Unix98
3228@deftypefun int fseeko (FILE *@var{stream}, off_t @var{offset}, int @var{whence})
3229This function is similar to @code{fseek} but it corrects a problem with
3230@code{fseek} in a system with POSIX types. Using a value of type
3231@code{long int} for the offset is not compatible with POSIX.
3232@code{fseeko} uses the correct type @code{off_t} for the @var{offset}
3233parameter.
3234
f2ea0f5b 3235For this reason it is a good idea to prefer @code{ftello} whenever it is
a5a0310d
UD
3236available since its functionality is (if different at all) closer the
3237underlying definition.
3238
3239The functionality and return value is the same as for @code{fseek}.
3240
3241The function is an extension defined in the Unix Single Specification
3242version 2.
a3a4a74e
UD
3243
3244When the sources are compiled with @code{_FILE_OFFSET_BITS == 64} on a
324532 bits system this function is in fact @code{fseeko64}. I.e., the
3246LFS interface transparently replaces the old interface.
3247@end deftypefun
3248
3249@comment stdio.h
3250@comment Unix98
3251@deftypefun int fseeko64 (FILE *@var{stream}, off64_t @var{offset}, int @var{whence})
3252This function is similar to @code{fseeko} with the only difference that
3253the @var{offset} parameter is of type @code{off64_t}. This also
3254requires that the stream @var{stream} was opened using either
3255@code{fopen64}, @code{freopen64}, or @code{tmpfile64} since otherwise
3256the underlying file operations to position the file pointer beyond the
3257@math{2^31} bytes limit might fail.
3258
3259If the sources are compiled with @code{_FILE_OFFSET_BITS == 64} on a 32
3260bits machine this function is available under the name @code{fseeko}
3261and so transparently replaces the old interface.
a5a0310d
UD
3262@end deftypefun
3263
3264@strong{Portability Note:} In non-POSIX systems, @code{ftell},
3265@code{ftello}, @code{fseek} and @code{fseeko} might work reliably only
3266on binary streams. @xref{Binary Streams}.
28f540f4
RM
3267
3268The following symbolic constants are defined for use as the @var{whence}
3269argument to @code{fseek}. They are also used with the @code{lseek}
3270function (@pxref{I/O Primitives}) and to specify offsets for file locks
3271(@pxref{Control Operations}).
3272
3273@comment stdio.h
f65fd747 3274@comment ISO
28f540f4
RM
3275@deftypevr Macro int SEEK_SET
3276This is an integer constant which, when used as the @var{whence}
a5a0310d
UD
3277argument to the @code{fseek} or @code{fseeko} function, specifies that
3278the offset provided is relative to the beginning of the file.
28f540f4
RM
3279@end deftypevr
3280
3281@comment stdio.h
f65fd747 3282@comment ISO
28f540f4
RM
3283@deftypevr Macro int SEEK_CUR
3284This is an integer constant which, when used as the @var{whence}
a5a0310d
UD
3285argument to the @code{fseek} or @code{fseeko} function, specifies that
3286the offset provided is relative to the current file position.
28f540f4
RM
3287@end deftypevr
3288
3289@comment stdio.h
f65fd747 3290@comment ISO
28f540f4
RM
3291@deftypevr Macro int SEEK_END
3292This is an integer constant which, when used as the @var{whence}
a5a0310d
UD
3293argument to the @code{fseek} or @code{fseeko} function, specifies that
3294the offset provided is relative to the end of the file.
28f540f4
RM
3295@end deftypevr
3296
3297@comment stdio.h
f65fd747 3298@comment ISO
28f540f4
RM
3299@deftypefun void rewind (FILE *@var{stream})
3300The @code{rewind} function positions the stream @var{stream} at the
f2ea0f5b 3301beginning of the file. It is equivalent to calling @code{fseek} or
a5a0310d
UD
3302@code{fseeko} on the @var{stream} with an @var{offset} argument of
3303@code{0L} and a @var{whence} argument of @code{SEEK_SET}, except that
3304the return value is discarded and the error indicator for the stream is
3305reset.
28f540f4
RM
3306@end deftypefun
3307
3308These three aliases for the @samp{SEEK_@dots{}} constants exist for the
3309sake of compatibility with older BSD systems. They are defined in two
3310different header files: @file{fcntl.h} and @file{sys/file.h}.
3311
3312@table @code
3313@comment sys/file.h
3314@comment BSD
3315@item L_SET
3316@vindex L_SET
3317An alias for @code{SEEK_SET}.
3318
3319@comment sys/file.h
3320@comment BSD
3321@item L_INCR
3322@vindex L_INCR
3323An alias for @code{SEEK_CUR}.
3324
3325@comment sys/file.h
3326@comment BSD
3327@item L_XTND
3328@vindex L_XTND
3329An alias for @code{SEEK_END}.
3330@end table
3331
3332@node Portable Positioning
3333@section Portable File-Position Functions
3334
3335On the GNU system, the file position is truly a character count. You
a5a0310d
UD
3336can specify any character count value as an argument to @code{fseek} or
3337@code{fseeko} and get reliable results for any random access file.
3338However, some @w{ISO C} systems do not represent file positions in this
3339way.
28f540f4
RM
3340
3341On some systems where text streams truly differ from binary streams, it
3342is impossible to represent the file position of a text stream as a count
3343of characters from the beginning of the file. For example, the file
3344position on some systems must encode both a record offset within the
3345file, and a character offset within the record.
3346
3347As a consequence, if you want your programs to be portable to these
3348systems, you must observe certain rules:
3349
3350@itemize @bullet
3351@item
3352The value returned from @code{ftell} on a text stream has no predictable
3353relationship to the number of characters you have read so far. The only
3354thing you can rely on is that you can use it subsequently as the
a5a0310d
UD
3355@var{offset} argument to @code{fseek} or @code{fseeko} to move back to
3356the same file position.
28f540f4 3357
19c3f208 3358@item
a5a0310d
UD
3359In a call to @code{fseek} or @code{fseeko} on a text stream, either the
3360@var{offset} must either be zero; or @var{whence} must be
3361@code{SEEK_SET} and the @var{offset} must be the result of an earlier
3362call to @code{ftell} on the same stream.
28f540f4
RM
3363
3364@item
3365The value of the file position indicator of a text stream is undefined
3366while there are characters that have been pushed back with @code{ungetc}
3367that haven't been read or discarded. @xref{Unreading}.
3368@end itemize
3369
3370But even if you observe these rules, you may still have trouble for long
3371files, because @code{ftell} and @code{fseek} use a @code{long int} value
3372to represent the file position. This type may not have room to encode
a5a0310d
UD
3373all the file positions in a large file. Using the @code{ftello} and
3374@code{fseeko} functions might help here since the @code{off_t} type is
3375expected to be able to hold all file position values but this still does
3376not help to handle additional information which must be associated with
3377a file position.
28f540f4
RM
3378
3379So if you do want to support systems with peculiar encodings for the
3380file positions, it is better to use the functions @code{fgetpos} and
3381@code{fsetpos} instead. These functions represent the file position
3382using the data type @code{fpos_t}, whose internal representation varies
3383from system to system.
3384
3385These symbols are declared in the header file @file{stdio.h}.
3386@pindex stdio.h
3387
3388@comment stdio.h
f65fd747 3389@comment ISO
28f540f4
RM
3390@deftp {Data Type} fpos_t
3391This is the type of an object that can encode information about the
3392file position of a stream, for use by the functions @code{fgetpos} and
3393@code{fsetpos}.
3394
3395In the GNU system, @code{fpos_t} is equivalent to @code{off_t} or
3396@code{long int}. In other systems, it might have a different internal
3397representation.
a3a4a74e
UD
3398
3399When compiling with @code{_FILE_OFFSET_BITS == 64} on a 32 bits machine
3400this type is in fact equivalent to @code{off64_t} since the LFS
3401interface transparently replaced the old interface.
3402@end deftp
3403
3404@comment stdio.h
3405@comment Unix98
3406@deftp {Data Type} fpos64_t
3407This is the type of an object that can encode information about the
3408file position of a stream, for use by the functions @code{fgetpos64} and
3409@code{fsetpos64}.
3410
3411In the GNU system, @code{fpos64_t} is equivalent to @code{off64_t} or
3412@code{long long int}. In other systems, it might have a different internal
3413representation.
28f540f4
RM
3414@end deftp
3415
3416@comment stdio.h
f65fd747 3417@comment ISO
28f540f4
RM
3418@deftypefun int fgetpos (FILE *@var{stream}, fpos_t *@var{position})
3419This function stores the value of the file position indicator for the
3420stream @var{stream} in the @code{fpos_t} object pointed to by
3421@var{position}. If successful, @code{fgetpos} returns zero; otherwise
3422it returns a nonzero value and stores an implementation-defined positive
3423value in @code{errno}.
a3a4a74e
UD
3424
3425When the sources are compiled with @code{_FILE_OFFSET_BITS == 64} on a
342632 bits system the function is in fact @code{fgetpos64}. I.e., the LFS
3427interface transparently replaced the old interface.
3428@end deftypefun
3429
3430@comment stdio.h
3431@comment Unix98
3432@deftypefun int fgetpos64 (FILE *@var{stream}, fpos64_t *@var{position})
3433This function is similar to @code{fgetpos} but the file position is
3434returned in a variable of type @code{fpos64_t} to which @var{position}
3435points.
3436
3437If the sources are compiled with @code{_FILE_OFFSET_BITS == 64} on a 32
3438bits machine this function is available under the name @code{fgetpos}
3439and so transparently replaces the old interface.
28f540f4
RM
3440@end deftypefun
3441
3442@comment stdio.h
f65fd747 3443@comment ISO
a3a4a74e 3444@deftypefun int fsetpos (FILE *@var{stream}, const fpos_t *@var{position})
28f540f4
RM
3445This function sets the file position indicator for the stream @var{stream}
3446to the position @var{position}, which must have been set by a previous
3447call to @code{fgetpos} on the same stream. If successful, @code{fsetpos}
3448clears the end-of-file indicator on the stream, discards any characters
3449that were ``pushed back'' by the use of @code{ungetc}, and returns a value
3450of zero. Otherwise, @code{fsetpos} returns a nonzero value and stores
3451an implementation-defined positive value in @code{errno}.
a3a4a74e
UD
3452
3453When the sources are compiled with @code{_FILE_OFFSET_BITS == 64} on a
345432 bits system the function is in fact @code{fsetpos64}. I.e., the LFS
3455interface transparently replaced the old interface.
3456@end deftypefun
3457
3458@comment stdio.h
3459@comment Unix98
3460@deftypefun int fsetpos64 (FILE *@var{stream}, const fpos64_t *@var{position})
3461This function is similar to @code{fsetpos} but the file position used
3462for positioning is provided in a variable of type @code{fpos64_t} to
3463which @var{position} points.
3464
3465If the sources are compiled with @code{_FILE_OFFSET_BITS == 64} on a 32
3466bits machine this function is available under the name @code{fsetpos}
3467and so transparently replaces the old interface.
28f540f4
RM
3468@end deftypefun
3469
3470@node Stream Buffering
3471@section Stream Buffering
3472
3473@cindex buffering of streams
3474Characters that are written to a stream are normally accumulated and
3475transmitted asynchronously to the file in a block, instead of appearing
3476as soon as they are output by the application program. Similarly,
3477streams often retrieve input from the host environment in blocks rather
3478than on a character-by-character basis. This is called @dfn{buffering}.
3479
3480If you are writing programs that do interactive input and output using
3481streams, you need to understand how buffering works when you design the
3482user interface to your program. Otherwise, you might find that output
3483(such as progress or prompt messages) doesn't appear when you intended
3484it to, or other unexpected behavior.
3485
3486This section deals only with controlling when characters are transmitted
3487between the stream and the file or device, and @emph{not} with how
3488things like echoing, flow control, and the like are handled on specific
3489classes of devices. For information on common control operations on
3490terminal devices, see @ref{Low-Level Terminal Interface}.
3491
3492You can bypass the stream buffering facilities altogether by using the
3493low-level input and output functions that operate on file descriptors
3494instead. @xref{Low-Level I/O}.
3495
3496@menu
3497* Buffering Concepts:: Terminology is defined here.
3498* Flushing Buffers:: How to ensure that output buffers are flushed.
3499* Controlling Buffering:: How to specify what kind of buffering to use.
3500@end menu
3501
3502@node Buffering Concepts
3503@subsection Buffering Concepts
3504
3505There are three different kinds of buffering strategies:
3506
3507@itemize @bullet
3508@item
3509Characters written to or read from an @dfn{unbuffered} stream are
3510transmitted individually to or from the file as soon as possible.
3511@cindex unbuffered stream
3512
3513@item
3514Characters written to a @dfn{line buffered} stream are transmitted to
3515the file in blocks when a newline character is encountered.
3516@cindex line buffered stream
3517
3518@item
3519Characters written to or read from a @dfn{fully buffered} stream are
3520transmitted to or from the file in blocks of arbitrary size.
3521@cindex fully buffered stream
3522@end itemize
3523
3524Newly opened streams are normally fully buffered, with one exception: a
3525stream connected to an interactive device such as a terminal is
3526initially line buffered. @xref{Controlling Buffering}, for information
3527on how to select a different kind of buffering. Usually the automatic
3528selection gives you the most convenient kind of buffering for the file
3529or device you open.
3530
3531The use of line buffering for interactive devices implies that output
3532messages ending in a newline will appear immediately---which is usually
3533what you want. Output that doesn't end in a newline might or might not
3534show up immediately, so if you want them to appear immediately, you
3535should flush buffered output explicitly with @code{fflush}, as described
3536in @ref{Flushing Buffers}.
3537
3538@node Flushing Buffers
3539@subsection Flushing Buffers
3540
3541@cindex flushing a stream
3542@dfn{Flushing} output on a buffered stream means transmitting all
3543accumulated characters to the file. There are many circumstances when
3544buffered output on a stream is flushed automatically:
3545
3546@itemize @bullet
3547@item
3548When you try to do output and the output buffer is full.
3549
3550@item
3551When the stream is closed. @xref{Closing Streams}.
3552
19c3f208 3553@item
28f540f4
RM
3554When the program terminates by calling @code{exit}.
3555@xref{Normal Termination}.
3556
3557@item
3558When a newline is written, if the stream is line buffered.
3559
3560@item
3561Whenever an input operation on @emph{any} stream actually reads data
3562from its file.
3563@end itemize
3564
3565If you want to flush the buffered output at another time, call
3566@code{fflush}, which is declared in the header file @file{stdio.h}.
3567@pindex stdio.h
3568
3569@comment stdio.h
f65fd747 3570@comment ISO
28f540f4
RM
3571@deftypefun int fflush (FILE *@var{stream})
3572This function causes any buffered output on @var{stream} to be delivered
3573to the file. If @var{stream} is a null pointer, then
3574@code{fflush} causes buffered output on @emph{all} open output streams
3575to be flushed.
3576
3577This function returns @code{EOF} if a write error occurs, or zero
3578otherwise.
3579@end deftypefun
3580
3581@strong{Compatibility Note:} Some brain-damaged operating systems have
3582been known to be so thoroughly fixated on line-oriented input and output
3583that flushing a line buffered stream causes a newline to be written!
3584Fortunately, this ``feature'' seems to be becoming less common. You do
3585not need to worry about this in the GNU system.
3586
3587
3588@node Controlling Buffering
3589@subsection Controlling Which Kind of Buffering
3590
3591After opening a stream (but before any other operations have been
3592performed on it), you can explicitly specify what kind of buffering you
3593want it to have using the @code{setvbuf} function.
3594@cindex buffering, controlling
3595
3596The facilities listed in this section are declared in the header
3597file @file{stdio.h}.
3598@pindex stdio.h
3599
3600@comment stdio.h
f65fd747 3601@comment ISO
28f540f4
RM
3602@deftypefun int setvbuf (FILE *@var{stream}, char *@var{buf}, int @var{mode}, size_t @var{size})
3603This function is used to specify that the stream @var{stream} should
3604have the buffering mode @var{mode}, which can be either @code{_IOFBF}
3605(for full buffering), @code{_IOLBF} (for line buffering), or
3606@code{_IONBF} (for unbuffered input/output).
3607
3608If you specify a null pointer as the @var{buf} argument, then @code{setvbuf}
3609allocates a buffer itself using @code{malloc}. This buffer will be freed
3610when you close the stream.
3611
3612Otherwise, @var{buf} should be a character array that can hold at least
3613@var{size} characters. You should not free the space for this array as
3614long as the stream remains open and this array remains its buffer. You
3615should usually either allocate it statically, or @code{malloc}
3616(@pxref{Unconstrained Allocation}) the buffer. Using an automatic array
3617is not a good idea unless you close the file before exiting the block
3618that declares the array.
3619
3620While the array remains a stream buffer, the stream I/O functions will
3621use the buffer for their internal purposes. You shouldn't try to access
3622the values in the array directly while the stream is using it for
3623buffering.
3624
3625The @code{setvbuf} function returns zero on success, or a nonzero value
3626if the value of @var{mode} is not valid or if the request could not
3627be honored.
3628@end deftypefun
3629
3630@comment stdio.h
f65fd747 3631@comment ISO
28f540f4
RM
3632@deftypevr Macro int _IOFBF
3633The value of this macro is an integer constant expression that can be
3634used as the @var{mode} argument to the @code{setvbuf} function to
3635specify that the stream should be fully buffered.
3636@end deftypevr
3637
3638@comment stdio.h
f65fd747 3639@comment ISO
28f540f4
RM
3640@deftypevr Macro int _IOLBF
3641The value of this macro is an integer constant expression that can be
3642used as the @var{mode} argument to the @code{setvbuf} function to
3643specify that the stream should be line buffered.
3644@end deftypevr
3645
3646@comment stdio.h
f65fd747 3647@comment ISO
28f540f4
RM
3648@deftypevr Macro int _IONBF
3649The value of this macro is an integer constant expression that can be
3650used as the @var{mode} argument to the @code{setvbuf} function to
3651specify that the stream should be unbuffered.
3652@end deftypevr
3653
3654@comment stdio.h
f65fd747 3655@comment ISO
28f540f4
RM
3656@deftypevr Macro int BUFSIZ
3657The value of this macro is an integer constant expression that is good
3658to use for the @var{size} argument to @code{setvbuf}. This value is
3659guaranteed to be at least @code{256}.
3660
3661The value of @code{BUFSIZ} is chosen on each system so as to make stream
19c3f208 3662I/O efficient. So it is a good idea to use @code{BUFSIZ} as the size
28f540f4
RM
3663for the buffer when you call @code{setvbuf}.
3664
3665Actually, you can get an even better value to use for the buffer size
3666by means of the @code{fstat} system call: it is found in the
3667@code{st_blksize} field of the file attributes. @xref{Attribute Meanings}.
3668
3669Sometimes people also use @code{BUFSIZ} as the allocation size of
3670buffers used for related purposes, such as strings used to receive a
3671line of input with @code{fgets} (@pxref{Character Input}). There is no
3672particular reason to use @code{BUFSIZ} for this instead of any other
3673integer, except that it might lead to doing I/O in chunks of an
3674efficient size.
3675@end deftypevr
3676
3677@comment stdio.h
f65fd747 3678@comment ISO
28f540f4
RM
3679@deftypefun void setbuf (FILE *@var{stream}, char *@var{buf})
3680If @var{buf} is a null pointer, the effect of this function is
3681equivalent to calling @code{setvbuf} with a @var{mode} argument of
3682@code{_IONBF}. Otherwise, it is equivalent to calling @code{setvbuf}
3683with @var{buf}, and a @var{mode} of @code{_IOFBF} and a @var{size}
3684argument of @code{BUFSIZ}.
3685
3686The @code{setbuf} function is provided for compatibility with old code;
3687use @code{setvbuf} in all new programs.
3688@end deftypefun
3689
3690@comment stdio.h
3691@comment BSD
3692@deftypefun void setbuffer (FILE *@var{stream}, char *@var{buf}, size_t @var{size})
3693If @var{buf} is a null pointer, this function makes @var{stream} unbuffered.
3694Otherwise, it makes @var{stream} fully buffered using @var{buf} as the
3695buffer. The @var{size} argument specifies the length of @var{buf}.
3696
3697This function is provided for compatibility with old BSD code. Use
3698@code{setvbuf} instead.
3699@end deftypefun
3700
3701@comment stdio.h
3702@comment BSD
3703@deftypefun void setlinebuf (FILE *@var{stream})
3704This function makes @var{stream} be line buffered, and allocates the
3705buffer for you.
3706
3707This function is provided for compatibility with old BSD code. Use
3708@code{setvbuf} instead.
3709@end deftypefun
3710
3711@node Other Kinds of Streams
3712@section Other Kinds of Streams
3713
3714The GNU library provides ways for you to define additional kinds of
3715streams that do not necessarily correspond to an open file.
3716
3717One such type of stream takes input from or writes output to a string.
3718These kinds of streams are used internally to implement the
3719@code{sprintf} and @code{sscanf} functions. You can also create such a
3720stream explicitly, using the functions described in @ref{String Streams}.
3721
3722More generally, you can define streams that do input/output to arbitrary
3723objects using functions supplied by your program. This protocol is
3724discussed in @ref{Custom Streams}.
3725
3726@strong{Portability Note:} The facilities described in this section are
3727specific to GNU. Other systems or C implementations might or might not
3728provide equivalent functionality.
3729
3730@menu
19c3f208 3731* String Streams:: Streams that get data from or put data in
28f540f4
RM
3732 a string or memory buffer.
3733* Obstack Streams:: Streams that store data in an obstack.
3734* Custom Streams:: Defining your own streams with an arbitrary
3735 input data source and/or output data sink.
3736@end menu
3737
3738@node String Streams
3739@subsection String Streams
3740
3741@cindex stream, for I/O to a string
3742@cindex string stream
3743The @code{fmemopen} and @code{open_memstream} functions allow you to do
3744I/O to a string or memory buffer. These facilities are declared in
3745@file{stdio.h}.
3746@pindex stdio.h
3747
3748@comment stdio.h
3749@comment GNU
3750@deftypefun {FILE *} fmemopen (void *@var{buf}, size_t @var{size}, const char *@var{opentype})
3751This function opens a stream that allows the access specified by the
3752@var{opentype} argument, that reads from or writes to the buffer specified
3753by the argument @var{buf}. This array must be at least @var{size} bytes long.
3754
3755If you specify a null pointer as the @var{buf} argument, @code{fmemopen}
3756dynamically allocates (as with @code{malloc}; @pxref{Unconstrained
3757Allocation}) an array @var{size} bytes long. This is really only useful
3758if you are going to write things to the buffer and then read them back
3759in again, because you have no way of actually getting a pointer to the
3760buffer (for this, try @code{open_memstream}, below). The buffer is
3761freed when the stream is open.
3762
3763The argument @var{opentype} is the same as in @code{fopen}
8b7fb588 3764(@pxref{Opening Streams}). If the @var{opentype} specifies
28f540f4
RM
3765append mode, then the initial file position is set to the first null
3766character in the buffer. Otherwise the initial file position is at the
3767beginning of the buffer.
3768
3769When a stream open for writing is flushed or closed, a null character
3770(zero byte) is written at the end of the buffer if it fits. You
3771should add an extra byte to the @var{size} argument to account for this.
3772Attempts to write more than @var{size} bytes to the buffer result
3773in an error.
3774
3775For a stream open for reading, null characters (zero bytes) in the
3776buffer do not count as ``end of file''. Read operations indicate end of
3777file only when the file position advances past @var{size} bytes. So, if
3778you want to read characters from a null-terminated string, you should
3779supply the length of the string as the @var{size} argument.
3780@end deftypefun
3781
3782Here is an example of using @code{fmemopen} to create a stream for
3783reading from a string:
3784
3785@smallexample
3786@include memopen.c.texi
3787@end smallexample
3788
3789This program produces the following output:
3790
3791@smallexample
3792Got f
3793Got o
3794Got o
3795Got b
3796Got a
3797Got r
3798@end smallexample
3799
3800@comment stdio.h
3801@comment GNU
3802@deftypefun {FILE *} open_memstream (char **@var{ptr}, size_t *@var{sizeloc})
3803This function opens a stream for writing to a buffer. The buffer is
3804allocated dynamically (as with @code{malloc}; @pxref{Unconstrained
3805Allocation}) and grown as necessary.
3806
3807When the stream is closed with @code{fclose} or flushed with
3808@code{fflush}, the locations @var{ptr} and @var{sizeloc} are updated to
3809contain the pointer to the buffer and its size. The values thus stored
3810remain valid only as long as no further output on the stream takes
3811place. If you do more output, you must flush the stream again to store
3812new values before you use them again.
3813
3814A null character is written at the end of the buffer. This null character
3815is @emph{not} included in the size value stored at @var{sizeloc}.
3816
a5a0310d
UD
3817You can move the stream's file position with @code{fseek} or
3818@code{fseeko} (@pxref{File Positioning}). Moving the file position past
3819the end of the data already written fills the intervening space with
3820zeroes.
28f540f4
RM
3821@end deftypefun
3822
3823Here is an example of using @code{open_memstream}:
3824
3825@smallexample
3826@include memstrm.c.texi
3827@end smallexample
3828
3829This program produces the following output:
3830
3831@smallexample
3832buf = `hello', size = 5
3833buf = `hello, world', size = 12
3834@end smallexample
3835
3836@c @group Invalid outside @example.
3837@node Obstack Streams
3838@subsection Obstack Streams
3839
3840You can open an output stream that puts it data in an obstack.
3841@xref{Obstacks}.
3842
3843@comment stdio.h
3844@comment GNU
3845@deftypefun {FILE *} open_obstack_stream (struct obstack *@var{obstack})
3846This function opens a stream for writing data into the obstack @var{obstack}.
3847This starts an object in the obstack and makes it grow as data is
3848written (@pxref{Growing Objects}).
3849@c @end group Doubly invalid because not nested right.
3850
3851Calling @code{fflush} on this stream updates the current size of the
3852object to match the amount of data that has been written. After a call
3853to @code{fflush}, you can examine the object temporarily.
3854
a5a0310d
UD
3855You can move the file position of an obstack stream with @code{fseek} or
3856@code{fseeko} (@pxref{File Positioning}). Moving the file position past
3857the end of the data written fills the intervening space with zeros.
28f540f4
RM
3858
3859To make the object permanent, update the obstack with @code{fflush}, and
3860then use @code{obstack_finish} to finalize the object and get its address.
3861The following write to the stream starts a new object in the obstack,
3862and later writes add to that object until you do another @code{fflush}
3863and @code{obstack_finish}.
3864
3865But how do you find out how long the object is? You can get the length
3866in bytes by calling @code{obstack_object_size} (@pxref{Status of an
3867Obstack}), or you can null-terminate the object like this:
3868
3869@smallexample
3870obstack_1grow (@var{obstack}, 0);
3871@end smallexample
3872
3873Whichever one you do, you must do it @emph{before} calling
3874@code{obstack_finish}. (You can do both if you wish.)
3875@end deftypefun
3876
3877Here is a sample function that uses @code{open_obstack_stream}:
3878
3879@smallexample
3880char *
3881make_message_string (const char *a, int b)
3882@{
3883 FILE *stream = open_obstack_stream (&message_obstack);
3884 output_task (stream);
3885 fprintf (stream, ": ");
3886 fprintf (stream, a, b);
3887 fprintf (stream, "\n");
3888 fclose (stream);
3889 obstack_1grow (&message_obstack, 0);
3890 return obstack_finish (&message_obstack);
3891@}
3892@end smallexample
3893
3894@node Custom Streams
3895@subsection Programming Your Own Custom Streams
3896@cindex custom streams
3897@cindex programming your own streams
3898
3899This section describes how you can make a stream that gets input from an
3900arbitrary data source or writes output to an arbitrary data sink
3901programmed by you. We call these @dfn{custom streams}.
3902
3903@c !!! this does not talk at all about the higher-level hooks
3904
3905@menu
3906* Streams and Cookies:: The @dfn{cookie} records where to fetch or
19c3f208 3907 store data that is read or written.
28f540f4 3908* Hook Functions:: How you should define the four @dfn{hook
19c3f208 3909 functions} that a custom stream needs.
28f540f4
RM
3910@end menu
3911
3912@node Streams and Cookies
3913@subsubsection Custom Streams and Cookies
3914@cindex cookie, for custom stream
3915
3916Inside every custom stream is a special object called the @dfn{cookie}.
3917This is an object supplied by you which records where to fetch or store
3918the data read or written. It is up to you to define a data type to use
3919for the cookie. The stream functions in the library never refer
3920directly to its contents, and they don't even know what the type is;
3921they record its address with type @code{void *}.
3922
3923To implement a custom stream, you must specify @emph{how} to fetch or
3924store the data in the specified place. You do this by defining
3925@dfn{hook functions} to read, write, change ``file position'', and close
3926the stream. All four of these functions will be passed the stream's
3927cookie so they can tell where to fetch or store the data. The library
3928functions don't know what's inside the cookie, but your functions will
3929know.
3930
3931When you create a custom stream, you must specify the cookie pointer,
19c3f208 3932and also the four hook functions stored in a structure of type
28f540f4
RM
3933@code{cookie_io_functions_t}.
3934
3935These facilities are declared in @file{stdio.h}.
3936@pindex stdio.h
3937
3938@comment stdio.h
3939@comment GNU
3940@deftp {Data Type} {cookie_io_functions_t}
19c3f208 3941This is a structure type that holds the functions that define the
28f540f4
RM
3942communications protocol between the stream and its cookie. It has
3943the following members:
3944
3945@table @code
3946@item cookie_read_function_t *read
3947This is the function that reads data from the cookie. If the value is a
f2ea0f5b 3948null pointer instead of a function, then read operations on this stream
28f540f4
RM
3949always return @code{EOF}.
3950
3951@item cookie_write_function_t *write
3952This is the function that writes data to the cookie. If the value is a
3953null pointer instead of a function, then data written to the stream is
3954discarded.
3955
3956@item cookie_seek_function_t *seek
3957This is the function that performs the equivalent of file positioning on
3958the cookie. If the value is a null pointer instead of a function, calls
a5a0310d
UD
3959to @code{fseek} or @code{fseeko} on this stream can only seek to
3960locations within the buffer; any attempt to seek outside the buffer will
3961return an @code{ESPIPE} error.
28f540f4
RM
3962
3963@item cookie_close_function_t *close
3964This function performs any appropriate cleanup on the cookie when
3965closing the stream. If the value is a null pointer instead of a
3966function, nothing special is done to close the cookie when the stream is
3967closed.
3968@end table
3969@end deftp
3970
3971@comment stdio.h
3972@comment GNU
3973@deftypefun {FILE *} fopencookie (void *@var{cookie}, const char *@var{opentype}, cookie_io_functions_t @var{io-functions})
3974This function actually creates the stream for communicating with the
3975@var{cookie} using the functions in the @var{io-functions} argument.
3976The @var{opentype} argument is interpreted as for @code{fopen};
3977see @ref{Opening Streams}. (But note that the ``truncate on
3978open'' option is ignored.) The new stream is fully buffered.
3979
3980The @code{fopencookie} function returns the newly created stream, or a null
3981pointer in case of an error.
3982@end deftypefun
3983
3984@node Hook Functions
3985@subsubsection Custom Stream Hook Functions
3986@cindex hook functions (of custom streams)
3987
3988Here are more details on how you should define the four hook functions
3989that a custom stream needs.
3990
3991You should define the function to read data from the cookie as:
3992
3993@smallexample
3994ssize_t @var{reader} (void *@var{cookie}, void *@var{buffer}, size_t @var{size})
3995@end smallexample
3996
3997This is very similar to the @code{read} function; see @ref{I/O
3998Primitives}. Your function should transfer up to @var{size} bytes into
3999the @var{buffer}, and return the number of bytes read, or zero to
4000indicate end-of-file. You can return a value of @code{-1} to indicate
4001an error.
4002
4003You should define the function to write data to the cookie as:
4004
4005@smallexample
4006ssize_t @var{writer} (void *@var{cookie}, const void *@var{buffer}, size_t @var{size})
4007@end smallexample
4008
4009This is very similar to the @code{write} function; see @ref{I/O
4010Primitives}. Your function should transfer up to @var{size} bytes from
4011the buffer, and return the number of bytes written. You can return a
4012value of @code{-1} to indicate an error.
4013
4014You should define the function to perform seek operations on the cookie
4015as:
4016
4017@smallexample
4018int @var{seeker} (void *@var{cookie}, fpos_t *@var{position}, int @var{whence})
4019@end smallexample
4020
4021For this function, the @var{position} and @var{whence} arguments are
4022interpreted as for @code{fgetpos}; see @ref{Portable Positioning}. In
4023the GNU library, @code{fpos_t} is equivalent to @code{off_t} or
4024@code{long int}, and simply represents the number of bytes from the
4025beginning of the file.
4026
19c3f208 4027After doing the seek operation, your function should store the resulting
28f540f4
RM
4028file position relative to the beginning of the file in @var{position}.
4029Your function should return a value of @code{0} on success and @code{-1}
4030to indicate an error.
4031
4032You should define the function to do cleanup operations on the cookie
4033appropriate for closing the stream as:
4034
4035@smallexample
4036int @var{cleaner} (void *@var{cookie})
4037@end smallexample
4038
4039Your function should return @code{-1} to indicate an error, and @code{0}
4040otherwise.
4041
4042@comment stdio.h
4043@comment GNU
4044@deftp {Data Type} cookie_read_function
4045This is the data type that the read function for a custom stream should have.
4046If you declare the function as shown above, this is the type it will have.
4047@end deftp
4048
4049@comment stdio.h
4050@comment GNU
4051@deftp {Data Type} cookie_write_function
4052The data type of the write function for a custom stream.
4053@end deftp
4054
4055@comment stdio.h
4056@comment GNU
4057@deftp {Data Type} cookie_seek_function
4058The data type of the seek function for a custom stream.
4059@end deftp
4060
4061@comment stdio.h
4062@comment GNU
4063@deftp {Data Type} cookie_close_function
4064The data type of the close function for a custom stream.
4065@end deftp
4066
4067@ignore
4068Roland says:
4069
4070@quotation
4071There is another set of functions one can give a stream, the
4072input-room and output-room functions. These functions must
4073understand stdio internals. To describe how to use these
4074functions, you also need to document lots of how stdio works
4075internally (which isn't relevant for other uses of stdio).
4076Perhaps I can write an interface spec from which you can write
4077good documentation. But it's pretty complex and deals with lots
4078of nitty-gritty details. I think it might be better to let this
4079wait until the rest of the manual is more done and polished.
4080@end quotation
4081@end ignore
4082
4083@c ??? This section could use an example.
0501d603
UD
4084
4085
4086@node Formatted Messages
4087@section Formatted Messages
4088@cindex formatted messages
4089
4090On systems which are based on System V messages of programs (especially
4091the system tools) are printed in a strict form using the @code{fmtmsg}
4092function. The uniformity sometimes helps the user to interpret messages
cf29ffbe 4093and the strictness tests of the @code{fmtmsg} function ensure that the
0501d603
UD
4094programmer follows some minimal requirements.
4095
4096@menu
4097* Printing Formatted Messages:: The @code{fmtmsg} function.
4098* Adding Severity Classes:: Add more severity classes.
4099* Example:: How to use @code{fmtmsg} and @code{addseverity}.
4100@end menu
4101
4102
4103@node Printing Formatted Messages
4104@subsection Printing Formatted Messages
4105
4106Messages can be printed to standard error and/or to the console. To
cf29ffbe 4107select the destination the programmer can use the following two values,
0501d603
UD
4108bitwise OR combined if wanted, for the @var{classification} parameter of
4109@code{fmtmsg}:
4110
4111@vtable @code
4112@item MM_PRINT
4113Display the message in standard error.
4114@item MM_CONSOLE
4115Display the message on the system console.
4116@end vtable
4117
f2ea0f5b 4118The erroneous piece of the system can be signalled by exactly one of the
0501d603
UD
4119following values which also is bitwise ORed with the
4120@var{classification} parameter to @code{fmtmsg}:
4121
4122@vtable @code
4123@item MM_HARD
4124The source of the condition is some hardware.
4125@item MM_SOFT
4126The source of the condition is some software.
4127@item MM_FIRM
4128The source of the condition is some firmware.
4129@end vtable
4130
4131A third component of the @var{classification} parameter to @code{fmtmsg}
4132can describe the part of the system which detects the problem. This is
4133done by using exactly one of the following values:
4134
4135@vtable @code
4136@item MM_APPL
f2ea0f5b 4137The erroneous condition is detected by the application.
0501d603 4138@item MM_UTIL
f2ea0f5b 4139The erroneous condition is detected by a utility.
0501d603 4140@item MM_OPSYS
f2ea0f5b 4141The erroneous condition is detected by the operating system.
0501d603
UD
4142@end vtable
4143
4144A last component of @var{classification} can signal the results of this
4145message. Exactly one of the following values can be used:
4146
4147@vtable @code
4148@item MM_RECOVER
4149It is a recoverable error.
4150@item MM_NRECOV
4151It is a non-recoverable error.
4152@end vtable
4153
4154@comment fmtmsg.h
4155@comment XPG
4156@deftypefun int fmtmsg (long int @var{classification}, const char *@var{label}, int @var{severity}, const char *@var{text}, const char *@var{action}, const char *@var{tag})
4157Display a message described by its parameters on the device(s) specified
4158in the @var{classification} parameter. The @var{label} parameter
4159identifies the source of the message. The string should consist of two
4160colon separated parts where the first part has not more than 10 and the
4161second part not more the 14 characters. The @var{text} parameter
f2ea0f5b 4162describes the condition of the error, the @var{action} parameter possible
0501d603
UD
4163steps to recover from the error and the @var{tag} parameter is a
4164reference to the online documentation where more information can be
4165found. It should contain the @var{label} value and a unique
4166identification number.
4167
cf29ffbe 4168Each of the parameters can be a special value which means this value
0501d603
UD
4169is to be omitted. The symbolic names for these values are:
4170
4171@vtable @code
4172@item MM_NULLLBL
4173Ignore @var{label} parameter.
4174@item MM_NULLSEV
4175Ignore @var{severity} parameter.
4176@item MM_NULLMC
4177Ignore @var{classification} parameter. This implies that nothing is
4178actually printed.
4179@item MM_NULLTXT
4180Ignore @var{text} parameter.
4181@item MM_NULLACT
4182Ignore @var{action} parameter.
4183@item MM_NULLTAG
4184Ignore @var{tag} parameter.
4185@end vtable
4186
cf29ffbe 4187There is another way certain fields can be omitted from the output to
0501d603
UD
4188standard error. This is described below in the description of
4189environment variables influencing the behaviour.
4190
4191The @var{severity} parameter can have one of the values in the following
4192table:
4193@cindex severity class
4194
4195@vtable @code
4196@item MM_NOSEV
4197Nothing is printed, this value is the same as @code{MM_NULLSEV}.
4198@item MM_HALT
4199This value is printed as @code{HALT}.
4200@item MM_ERROR
4201This value is printed as @code{ERROR}.
4202@item MM_WARNING
4203This value is printed as @code{WARNING}.
4204@item MM_INFO
4205This value is printed as @code{INFO}.
4206@end vtable
4207
4208The numeric value of these five macros are between @code{0} and
4209@code{4}. Using the environment variable @code{SEV_LEVEL} or using the
4210@code{addseverity} function one can add more severity levels with their
4211corresponding string to print. This is described below
4212(@pxref{Adding Severity Classes}).
4213
4214@noindent
4215If no parameter is ignored the output looks like this:
4216
4217@smallexample
4218@var{label}: @var{severity-string}: @var{text}
4219TO FIX: @var{action} @var{tag}
4220@end smallexample
4221
4222The colons, new line characters and the @code{TO FIX} string are
4223inserted if necessary, i.e., if the corresponding parameter is not
4224ignored.
4225
4226This function is specified in the X/Open Portability Guide. It is also
4227available on all system derived from System V.
4228
cf29ffbe 4229The function returns the value @code{MM_OK} if no error occurred. If
0501d603
UD
4230only the printing to standard error failed, it returns @code{MM_NOMSG}.
4231If printing to the console fails, it returns @code{MM_NOCON}. If
cf29ffbe 4232nothing is printed @code{MM_NOTOK} is returned. Among situations where
0501d603
UD
4233all outputs fail this last value is also returned if a parameter value
4234is incorrect.
4235@end deftypefun
4236
4237There are two environment variables which influence the behaviour of
4238@code{fmtmsg}. The first is @code{MSGVERB}. It is used to control the
4239output actually happening on standard error (@emph{not} the console
4240output). Each of the five fields can explicitely be enabled. To do
4241this the user has to put the @code{MSGVERB} variable with a format like
cf29ffbe 4242the following in the environment before calling the @code{fmtmsg} function
0501d603
UD
4243the first time:
4244
4245@smallexample
4246MSGVERB=@var{keyword}[:@var{keyword}[:...]]
4247@end smallexample
4248
4249Valid @var{keyword}s are @code{label}, @code{severity}, @code{text},
4250@code{action}, and @code{tag}. If the environment variable is not given
4251or is the empty string, a not supported keyword is given or the value is
4252somehow else invalid, no part of the message is masked out.
4253
4254The second environment variable which influences the behaviour of
4255@code{fmtmsg} is @code{SEV_LEVEL}. This variable and the change in the
4256behaviour of @code{fmtmsg} is not specified in the X/Open Portability
4257Guide. It is available in System V systems, though. It can be used to
cf29ffbe 4258introduce new severity levels. By default, only the five severity levels
0501d603
UD
4259described above are available. Any other numeric value would make
4260@code{fmtmsg} print nothing.
4261
4262If the user puts @code{SEV_LEVEL} with a format like
4263
4264@smallexample
4265SEV_LEVEL=[@var{description}[:@var{description}[:...]]]
4266@end smallexample
4267
4268@noindent
4269in the environment of the process before the first call to
4270@code{fmtmsg}, where @var{description} has a value of the form
4271
4272@smallexample
4273@var{severity-keyword},@var{level},@var{printstring}
4274@end smallexample
4275
4276The @var{severity-keyword} part is not used by @code{fmtmsg} but it has
4277to be present. The @var{level} part is a string representation of a
4278number. The numeric value must be a number greater than 4. This value
4279must be used in the @var{severity} parameter of @code{fmtmsg} to select
4280this class. It is not possible to overwrite any of the predefined
4281classes. The @var{printstring} is the string printed when a message of
4282this class is processed by @code{fmtmsg} (see above, @code{fmtsmg} does
4283not print the numeric value but instead the string representation).
4284
4285
4286@node Adding Severity Classes
4287@subsection Adding Severity Classes
4288@cindex severity class
4289
4290There is another possibility to introduce severity classes beside using
4291the environment variable @code{SEV_LEVEL}. This simplifies the task of
4292introducing new classes in a running program. One could use the
4293@code{setenv} or @code{putenv} function to set the environment variable,
cf29ffbe 4294but this is toilsome.
0501d603
UD
4295
4296@deftypefun int addseverity (int @var{severity}, const char *@var{string})
4297This function allows to introduce new severity classes which can be
4298addressed by the @var{severity} parameter of the @code{fmtmsg} function.
4299The @var{severity} parameter of @code{addseverity} must match the value
4300for the parameter with the same name of @code{fmtmsg} and @var{string}
4301is the string printed in the actual messages instead of the numeric
4302value.
4303
4304If @var{string} is @code{NULL} the severity class with the numeric value
4305according to @var{severity} is removed.
4306
cf29ffbe
UD
4307It is not possible to overwrite or remove one of the default severity
4308classes. All calls to @code{addseverity} with @var{severity} set to one
4309of the values for the default classes will fail.
4310
0501d603
UD
4311The return value is @code{MM_OK} if the task was successfully performed.
4312If the return value is @code{MM_NOTOK} something went wrong. This could
4313mean that no more memory is available or a class is not available when
4314it has to be removed.
4315
4316This function is not specified in the X/Open Portability Guide although
cf29ffbe 4317the @code{fmtsmg} function is. It is available on System V systems.
0501d603
UD
4318@end deftypefun
4319
4320
4321@node Example
4322@subsection How to use @code{fmtmsg} and @code{addseverity}
4323
4324Here is a simple example program to illustrate the use of the both
4325functions described in this section.
4326
4327@smallexample
4328@include fmtmsgexpl.c.texi
4329@end smallexample
4330
4331The second call to @code{fmtmsg} illustrates a use of this function how
4332it usually happens on System V systems which heavily use this function.
4333It might be worth a thought to follow the scheme used in System V
4334systems so we give a short explanation here. The value of the
4335@var{label} field (@code{UX:cat}) says that the error occured in the
4336Unix program @code{cat}. The explanation of the error follows and the
4337value for the @var{action} parameter is @code{"refer to manual"}. One
4338could me more specific here, if needed. The @var{tag} field contains,
4339as proposed above, the value of the string given for the @var{label}
4340parameter, and additionally a unique ID (@code{001} in this case). For
4341a GNU environment this string could contain a reference to the
4342corresponding node in the Info page for the program.
4343
4344@noindent
4345Running this program without specifying the @code{MSGVERB} and
4346@code{SEV_LEVEL} function produces the following output:
4347
4348@smallexample
4349UX:cat: NOTE2: invalid syntax
4350TO FIX: refer to manual UX:cat:001
4351@end smallexample
4352
4353We see the different fields of the message and how the extra glue (the
4354colons and the @code{TO FIX} string) are printed. But only one of the
4355three calls to @code{fmtmsg} produced output. The first call does not
4356print anything because the @var{label} parameter is not in the correct
8b7fb588
UD
4357form. The string must contain two fields, separated by a colon
4358(@pxref{Printing Formatted Messages}). The third @code{fmtmsg} call
0501d603
UD
4359produced no output since the class with the numeric value @code{6} is
4360not defined. Although a class with numeric value @code{5} is also not
4361defined by default, the call the @code{addseverity} introduces it and
0ea554bf 4362the second call to @code{fmtmsg} produces the above output.
0501d603
UD
4363
4364When we change the environment of the program to contain
4365@code{SEV_LEVEL=XXX,6,NOTE} when running it we get a different result:
4366
4367@smallexample
4368UX:cat: NOTE2: invalid syntax
4369TO FIX: refer to manual UX:cat:001
4370label:foo: NOTE: text
4371TO FIX: action tag
4372@end smallexample
4373
4374Now the third call the @code{fmtmsg} produced some output and we see how
4375the string @code{NOTE} from the environment variable appears in the
4376message.
4377
4378Now we can reduce the output by specifying in which fields we are
4379interested in. If we additionally set the environment variable
4380@code{MSGVERB} to the value @code{severity:label:action} we get the
4381following output:
4382
4383@smallexample
4384UX:cat: NOTE2
4385TO FIX: refer to manual
4386label:foo: NOTE
4387TO FIX: action
4388@end smallexample
4389
4390@noindent
4391I.e., the output produced by the @var{text} and the @var{tag} parameters
cf29ffbe 4392to @code{fmtmsg} vanished. Please also note that now there is no colon
0501d603
UD
4393after the @code{NOTE} and @code{NOTE2} strings in the output. This is
4394not necessary since there is no more output on this line since the text
4395is missing.