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