]> git.ipfire.org Git - thirdparty/glibc.git/blame - manual/stdio.texi
manual: Create empty placeholder macros for @standards.
[thirdparty/glibc.git] / manual / stdio.texi
CommitLineData
28f540f4 1@node I/O on Streams, Low-Level I/O, I/O Overview, Top
34992338 2@c %MENU% High-level, portable I/O facilities
28f540f4 3@chapter Input/Output on Streams
838e5ffe
UD
4@c fix an overfull:
5@tex
6\hyphenation{which-ever}
7@end tex
28f540f4
RM
8
9This chapter describes the functions for creating streams and performing
10input and output operations on them. As discussed in @ref{I/O
11Overview}, a stream is a fairly abstract, high-level concept
12representing a communications channel to a file, device, or process.
13
14@menu
15* Streams:: About the data type representing a stream.
19c3f208 16* Standard Streams:: Streams to the standard input and output
162ba701 17 devices are created for you.
28f540f4
RM
18* Opening Streams:: How to create a stream to talk to a file.
19* Closing Streams:: Close a stream when you are finished with it.
7b4161bb 20* Streams and Threads:: Issues with streams in threaded programs.
b5e73f56 21* Streams and I18N:: Streams in internationalized applications.
28f540f4
RM
22* Simple Output:: Unformatted output by characters and lines.
23* Character Input:: Unformatted input by characters and words.
24* Line Input:: Reading a line or a record from a stream.
25* Unreading:: Peeking ahead/pushing back input just read.
26* Block Input/Output:: Input and output operations on blocks of data.
27* Formatted Output:: @code{printf} and related functions.
28* Customizing Printf:: You can define new conversion specifiers for
162ba701 29 @code{printf} and friends.
28f540f4
RM
30* Formatted Input:: @code{scanf} and related functions.
31* EOF and Errors:: How you can tell if an I/O error happens.
460e040a 32* Error Recovery:: What you can do about errors.
28f540f4 33* Binary Streams:: Some systems distinguish between text files
162ba701 34 and binary files.
28f540f4 35* File Positioning:: About random-access streams.
f65fd747 36* Portable Positioning:: Random access on peculiar ISO C systems.
28f540f4
RM
37* Stream Buffering:: How to control buffering of streams.
38* Other Kinds of Streams:: Streams that do not necessarily correspond
162ba701 39 to an open file.
0501d603 40* Formatted Messages:: Print strictly formatted messages.
28f540f4
RM
41@end menu
42
43@node Streams
44@section Streams
45
46For historical reasons, the type of the C data structure that represents
47a stream is called @code{FILE} rather than ``stream''. Since most of
48the library functions deal with objects of type @code{FILE *}, sometimes
49the term @dfn{file pointer} is also used to mean ``stream''. This leads
50to unfortunate confusion over terminology in many books on C. This
51manual, however, is careful to use the terms ``file'' and ``stream''
52only in the technical sense.
53@cindex file pointer
54
55@pindex stdio.h
56The @code{FILE} type is declared in the header file @file{stdio.h}.
57
58@comment stdio.h
f65fd747 59@comment ISO
28f540f4
RM
60@deftp {Data Type} FILE
61This is the data type used to represent stream objects. A @code{FILE}
62object holds all of the internal state information about the connection
63to the associated file, including such things as the file position
64indicator and buffering information. Each stream also has error and
65end-of-file status indicators that can be tested with the @code{ferror}
66and @code{feof} functions; see @ref{EOF and Errors}.
67@end deftp
68
69@code{FILE} objects are allocated and managed internally by the
70input/output library functions. Don't try to create your own objects of
71type @code{FILE}; let the library do it. Your programs should
72deal only with pointers to these objects (that is, @code{FILE *} values)
73rather than the objects themselves.
6d52618b 74@c !!! should say that FILE's have "No user-serviceable parts inside."
28f540f4
RM
75
76@node Standard Streams
77@section Standard Streams
78@cindex standard streams
79@cindex streams, standard
80
81When the @code{main} function of your program is invoked, it already has
82three predefined streams open and available for use. These represent
83the ``standard'' input and output channels that have been established
84for the process.
85
86These streams are declared in the header file @file{stdio.h}.
87@pindex stdio.h
88
89@comment stdio.h
f65fd747 90@comment ISO
28f540f4
RM
91@deftypevar {FILE *} stdin
92The @dfn{standard input} stream, which is the normal source of input for the
93program.
94@end deftypevar
95@cindex standard input stream
96
97@comment stdio.h
f65fd747 98@comment ISO
28f540f4
RM
99@deftypevar {FILE *} stdout
100The @dfn{standard output} stream, which is used for normal output from
101the program.
102@end deftypevar
103@cindex standard output stream
104
105@comment stdio.h
f65fd747 106@comment ISO
28f540f4
RM
107@deftypevar {FILE *} stderr
108The @dfn{standard error} stream, which is used for error messages and
109diagnostics issued by the program.
110@end deftypevar
111@cindex standard error stream
112
a7a93d50 113On @gnusystems{}, you can specify what files or processes correspond to
28f540f4
RM
114these streams using the pipe and redirection facilities provided by the
115shell. (The primitives shells use to implement these facilities are
116described in @ref{File System Interface}.) Most other operating systems
117provide similar mechanisms, but the details of how to use them can vary.
118
1f77f049 119In @theglibc{}, @code{stdin}, @code{stdout}, and @code{stderr} are
b5e73f56
UD
120normal variables which you can set just like any others. For example,
121to redirect the standard output to a file, you could do:
28f540f4
RM
122
123@smallexample
124fclose (stdout);
125stdout = fopen ("standard-output-file", "w");
126@end smallexample
127
128Note however, that in other systems @code{stdin}, @code{stdout}, and
129@code{stderr} are macros that you cannot assign to in the normal way.
130But you can use @code{freopen} to get the effect of closing one and
131reopening it. @xref{Opening Streams}.
132
b5e73f56
UD
133The three streams @code{stdin}, @code{stdout}, and @code{stderr} are not
134unoriented at program start (@pxref{Streams and I18N}).
135
28f540f4
RM
136@node Opening Streams
137@section Opening Streams
138
139@cindex opening a stream
140Opening a file with the @code{fopen} function creates a new stream and
141establishes a connection between the stream and a file. This may
19c3f208 142involve creating a new file.
28f540f4
RM
143
144@pindex stdio.h
145Everything described in this section is declared in the header file
146@file{stdio.h}.
147
148@comment stdio.h
f65fd747 149@comment ISO
28f540f4 150@deftypefun {FILE *} fopen (const char *@var{filename}, const char *@var{opentype})
171e9210
AO
151@safety{@prelim{}@mtsafe{}@asunsafe{@ascuheap{} @asulock{}}@acunsafe{@acsmem{} @acsfd{} @aculock{}}}
152@c fopen may leak the list lock if cancelled within _IO_link_in.
28f540f4
RM
153The @code{fopen} function opens a stream for I/O to the file
154@var{filename}, and returns a pointer to the stream.
155
156The @var{opentype} argument is a string that controls how the file is
157opened and specifies attributes of the resulting stream. It must begin
158with one of the following sequences of characters:
159
160@table @samp
161@item r
162Open an existing file for reading only.
163
164@item w
165Open the file for writing only. If the file already exists, it is
166truncated to zero length. Otherwise a new file is created.
167
168@item a
169Open a file for append access; that is, writing at the end of file only.
170If the file already exists, its initial contents are unchanged and
171output to the stream is appended to the end of the file.
172Otherwise, a new, empty file is created.
173
174@item r+
175Open an existing file for both reading and writing. The initial contents
176of the file are unchanged and the initial file position is at the
177beginning of the file.
178
179@item w+
180Open a file for both reading and writing. If the file already exists, it
181is truncated to zero length. Otherwise, a new file is created.
182
183@item a+
184Open or create file for both reading and appending. If the file exists,
185its initial contents are unchanged. Otherwise, a new file is created.
186The initial file position for reading is at the beginning of the file,
187but output is always appended to the end of the file.
188@end table
189
190As you can see, @samp{+} requests a stream that can do both input and
162ba701
PIM
191output. When using such a stream, you must call @code{fflush}
192(@pxref{Stream Buffering}) or a file positioning function such as
193@code{fseek} (@pxref{File Positioning}) when switching from reading
194to writing or vice versa. Otherwise, internal buffers might not be
195emptied properly.
28f540f4
RM
196
197Additional characters may appear after these to specify flags for the
198call. Always put the mode (@samp{r}, @samp{w+}, etc.) first; that is
199the only part you are guaranteed will be understood by all systems.
200
823fbbb4
AJ
201@Theglibc{} defines additional characters for use in @var{opentype}:
202
203@table @samp
204@item c
205The file is opened with cancellation in the I/O functions disabled.
206
207@item e
208The underlying file descriptor will be closed if you use any of the
209@code{exec@dots{}} functions (@pxref{Executing a File}). (This is
210equivalent to having set @code{FD_CLOEXEC} on that descriptor.
211@xref{Descriptor Flags}.)
212
213@item m
214The file is opened and accessed using @code{mmap}. This is only
215supported with files opened for reading.
216
217@item x
218Insist on creating a new file---if a file @var{filename} already
219exists, @code{fopen} fails rather than opening it. If you use
220@samp{x} you are guaranteed that you will not clobber an existing
221file. This is equivalent to the @code{O_EXCL} option to the
222@code{open} function (@pxref{Opening and Closing Files}).
223
224The @samp{x} modifier is part of @w{ISO C11}.
225@end table
28f540f4
RM
226
227The character @samp{b} in @var{opentype} has a standard meaning; it
228requests a binary stream rather than a text stream. But this makes no
a7a93d50 229difference in POSIX systems (including @gnusystems{}). If both
28f540f4
RM
230@samp{+} and @samp{b} are specified, they can appear in either order.
231@xref{Binary Streams}.
232
4e2e9999
UD
233@cindex stream orientation
234@cindex orientation, stream
235If the @var{opentype} string contains the sequence
236@code{,ccs=@var{STRING}} then @var{STRING} is taken as the name of a
237coded character set and @code{fopen} will mark the stream as
5b23062f
JM
238wide-oriented with appropriate conversion functions in place to convert
239from and to the character set @var{STRING}. Any other stream
4e2e9999
UD
240is opened initially unoriented and the orientation is decided with the
241first file operation. If the first operation is a wide character
242operation, the stream is not only marked as wide-oriented, also the
243conversion functions to convert to the coded character set used for the
244current locale are loaded. This will not change anymore from this point
245on even if the locale selected for the @code{LC_CTYPE} category is
246changed.
247
28f540f4
RM
248Any other characters in @var{opentype} are simply ignored. They may be
249meaningful in other systems.
250
251If the open fails, @code{fopen} returns a null pointer.
a3a4a74e 252
c703cd7a 253When the sources are compiled with @code{_FILE_OFFSET_BITS == 64} on a
0be8752b 25432 bit machine this function is in fact @code{fopen64} since the LFS
a3a4a74e 255interface replaces transparently the old interface.
28f540f4
RM
256@end deftypefun
257
258You can have multiple streams (or file descriptors) pointing to the same
259file open at the same time. If you do only input, this works
260straightforwardly, but you must be careful if any output streams are
261included. @xref{Stream/Descriptor Precautions}. This is equally true
262whether the streams are in one program (not usual) or in several
263programs (which can easily happen). It may be advantageous to use the
264file locking facilities to avoid simultaneous access. @xref{File
265Locks}.
266
a3a4a74e
UD
267@comment stdio.h
268@comment Unix98
269@deftypefun {FILE *} fopen64 (const char *@var{filename}, const char *@var{opentype})
171e9210 270@safety{@prelim{}@mtsafe{}@asunsafe{@ascuheap{} @asulock{}}@acunsafe{@acsmem{} @acsfd{} @aculock{}}}
a3a4a74e
UD
271This function is similar to @code{fopen} but the stream it returns a
272pointer for is opened using @code{open64}. Therefore this stream can be
9ceeb279 273used even on files larger than @twoexp{31} bytes on 32 bit machines.
a3a4a74e
UD
274
275Please note that the return type is still @code{FILE *}. There is no
276special @code{FILE} type for the LFS interface.
277
278If the sources are compiled with @code{_FILE_OFFSET_BITS == 64} on a 32
279bits machine this function is available under the name @code{fopen}
280and so transparently replaces the old interface.
281@end deftypefun
282
28f540f4 283@comment stdio.h
f65fd747 284@comment ISO
28f540f4
RM
285@deftypevr Macro int FOPEN_MAX
286The value of this macro is an integer constant expression that
287represents the minimum number of streams that the implementation
288guarantees can be open simultaneously. You might be able to open more
289than this many streams, but that is not guaranteed. The value of this
290constant is at least eight, which includes the three standard streams
291@code{stdin}, @code{stdout}, and @code{stderr}. In POSIX.1 systems this
292value is determined by the @code{OPEN_MAX} parameter; @pxref{General
293Limits}. In BSD and GNU, it is controlled by the @code{RLIMIT_NOFILE}
294resource limit; @pxref{Limits on Resources}.
295@end deftypevr
296
297@comment stdio.h
f65fd747 298@comment ISO
28f540f4 299@deftypefun {FILE *} freopen (const char *@var{filename}, const char *@var{opentype}, FILE *@var{stream})
171e9210
AO
300@safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{}}@acunsafe{@acucorrupt{} @acsfd{}}}
301@c Like most I/O operations, this one is guarded by a recursive lock,
302@c released even upon cancellation, but cancellation may leak file
303@c descriptors and leave the stream in an inconsistent state (e.g.,
304@c still bound to the closed descriptor). Also, if the stream is
305@c part-way through a significant update (say running freopen) when a
306@c signal handler calls freopen again on the same stream, the result is
307@c likely to be an inconsistent stream, and the possibility of closing
308@c twice file descriptor number that the stream used to use, the second
309@c time when it might have already been reused by another thread.
28f540f4
RM
310This function is like a combination of @code{fclose} and @code{fopen}.
311It first closes the stream referred to by @var{stream}, ignoring any
312errors that are detected in the process. (Because errors are ignored,
313you should not use @code{freopen} on an output stream if you have
314actually done any output using the stream.) Then the file named by
315@var{filename} is opened with mode @var{opentype} as for @code{fopen},
316and associated with the same stream object @var{stream}.
317
318If the operation fails, a null pointer is returned; otherwise,
f1a67a2c
AZ
319@code{freopen} returns @var{stream}. On Linux, @code{freopen} may also
320fail and set @code{errno} to @code{EBUSY} when the kernel structure for
321the old file descriptor was not initialized completely before @code{freopen}
322was called. This can only happen in multi-threaded programs, when two
323threads race to allocate the same file descriptor number. To avoid the
324possibility of this race, do not use @code{close} to close the underlying
325file descriptor for a @code{FILE}; either use @code{freopen} while the
326file is still open, or use @code{open} and then @code{dup2} to install
327the new file descriptor.
28f540f4
RM
328
329@code{freopen} has traditionally been used to connect a standard stream
330such as @code{stdin} with a file of your own choice. This is useful in
331programs in which use of a standard stream for certain purposes is
1f77f049 332hard-coded. In @theglibc{}, you can simply close the standard
28f540f4
RM
333streams and open new ones with @code{fopen}. But other systems lack
334this ability, so using @code{freopen} is more portable.
a3a4a74e 335
c703cd7a 336When the sources are compiled with @code{_FILE_OFFSET_BITS == 64} on a
0be8752b 33732 bit machine this function is in fact @code{freopen64} since the LFS
a3a4a74e
UD
338interface replaces transparently the old interface.
339@end deftypefun
340
341@comment stdio.h
342@comment Unix98
343@deftypefun {FILE *} freopen64 (const char *@var{filename}, const char *@var{opentype}, FILE *@var{stream})
171e9210 344@safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{}}@acunsafe{@acucorrupt{} @acsfd{}}}
a3a4a74e 345This function is similar to @code{freopen}. The only difference is that
0be8752b 346on 32 bit machine the stream returned is able to read beyond the
9ceeb279 347@twoexp{31} bytes limits imposed by the normal interface. It should be
a3a4a74e
UD
348noted that the stream pointed to by @var{stream} need not be opened
349using @code{fopen64} or @code{freopen64} since its mode is not important
350for this function.
351
352If the sources are compiled with @code{_FILE_OFFSET_BITS == 64} on a 32
353bits machine this function is available under the name @code{freopen}
354and so transparently replaces the old interface.
28f540f4
RM
355@end deftypefun
356
f8b23b32
UD
357In some situations it is useful to know whether a given stream is
358available for reading or writing. This information is normally not
359available and would have to be remembered separately. Solaris
360introduced a few functions to get this information from the stream
1f77f049 361descriptor and these functions are also available in @theglibc{}.
f8b23b32
UD
362
363@comment stdio_ext.h
364@comment GNU
365@deftypefun int __freadable (FILE *@var{stream})
171e9210 366@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
f8b23b32
UD
367The @code{__freadable} function determines whether the stream
368@var{stream} was opened to allow reading. In this case the return value
369is nonzero. For write-only streams the function returns zero.
370
371This function is declared in @file{stdio_ext.h}.
372@end deftypefun
373
374@comment stdio_ext.h
375@comment GNU
376@deftypefun int __fwritable (FILE *@var{stream})
171e9210 377@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
f8b23b32
UD
378The @code{__fwritable} function determines whether the stream
379@var{stream} was opened to allow writing. In this case the return value
380is nonzero. For read-only streams the function returns zero.
381
382This function is declared in @file{stdio_ext.h}.
383@end deftypefun
384
c703cd7a 385For slightly different kinds of problems there are two more functions.
f8b23b32
UD
386They provide even finer-grained information.
387
388@comment stdio_ext.h
389@comment GNU
390@deftypefun int __freading (FILE *@var{stream})
171e9210 391@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
f8b23b32
UD
392The @code{__freading} function determines whether the stream
393@var{stream} was last read from or whether it is opened read-only. In
394this case the return value is nonzero, otherwise it is zero.
395Determining whether a stream opened for reading and writing was last
396used for writing allows to draw conclusions about the content about the
397buffer, among other things.
398
399This function is declared in @file{stdio_ext.h}.
400@end deftypefun
401
402@comment stdio_ext.h
403@comment GNU
404@deftypefun int __fwriting (FILE *@var{stream})
171e9210 405@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
f8b23b32
UD
406The @code{__fwriting} function determines whether the stream
407@var{stream} was last written to or whether it is opened write-only. In
408this case the return value is nonzero, otherwise it is zero.
409
410This function is declared in @file{stdio_ext.h}.
411@end deftypefun
412
28f540f4
RM
413
414@node Closing Streams
415@section Closing Streams
416
417@cindex closing a stream
418When a stream is closed with @code{fclose}, the connection between the
0bc93a2f 419stream and the file is canceled. After you have closed a stream, you
28f540f4
RM
420cannot perform any additional operations on it.
421
422@comment stdio.h
f65fd747 423@comment ISO
28f540f4 424@deftypefun int fclose (FILE *@var{stream})
171e9210
AO
425@safety{@prelim{}@mtsafe{}@asunsafe{@ascuheap{} @asulock{}}@acunsafe{@aculock{} @acsmem{} @acsfd{}}}
426@c After fclose, it is undefined behavior to use the stream it points
427@c to. Therefore, one must only call fclose when the stream is
428@c otherwise unused. Concurrent uses started before will complete
429@c successfully because of the lock, which makes it MT-Safe. Calling it
430@c from a signal handler is perfectly safe if the stream is known to be
431@c no longer used, which is a precondition for fclose to be safe in the
432@c first place; since this is no further requirement, fclose is safe for
433@c use in async signals too. After calling fclose, you can no longer
434@c use the stream, not even to fclose it again, so its memory and file
435@c descriptor may leak if fclose is canceled before @c releasing them.
436@c That the stream must be unused and it becomes unused after the call
437@c is what would enable fclose to be AS- and AC-Safe while freopen
438@c isn't. However, because of the possibility of leaving __gconv_lock
439@c taken upon cancellation, AC-Safety is lost.
28f540f4
RM
440This function causes @var{stream} to be closed and the connection to
441the corresponding file to be broken. Any buffered output is written
442and any buffered input is discarded. The @code{fclose} function returns
443a value of @code{0} if the file was closed successfully, and @code{EOF}
19c3f208 444if an error was detected.
28f540f4
RM
445
446It is important to check for errors when you call @code{fclose} to close
447an output stream, because real, everyday errors can be detected at this
448time. For example, when @code{fclose} writes the remaining buffered
449output, it might get an error because the disk is full. Even if you
450know the buffer is empty, errors can still occur when closing a file if
451you are using NFS.
452
453The function @code{fclose} is declared in @file{stdio.h}.
454@end deftypefun
455
1f77f049 456To close all streams currently available @theglibc{} provides
6bc31da0
UD
457another function.
458
459@comment stdio.h
460@comment GNU
461@deftypefun int fcloseall (void)
171e9210
AO
462@safety{@prelim{}@mtunsafe{@mtasurace{:streams}}@asunsafe{}@acsafe{}}
463@c Like fclose, using any previously-opened streams after fcloseall is
464@c undefined. However, the implementation of fcloseall isn't equivalent
465@c to calling fclose for all streams: it just flushes and unbuffers all
466@c streams, without any locking. It's the flushing without locking that
467@c makes it unsafe.
6bc31da0 468This function causes all open streams of the process to be closed and
c703cd7a 469the connections to corresponding files to be broken. All buffered data
f2ea0f5b 470is written and any buffered input is discarded. The @code{fcloseall}
6bc31da0
UD
471function returns a value of @code{0} if all the files were closed
472successfully, and @code{EOF} if an error was detected.
473
0be8752b 474This function should be used only in special situations, e.g., when an
6bc31da0 475error occurred and the program must be aborted. Normally each single
0be8752b
UD
476stream should be closed separately so that problems with individual
477streams can be identified. It is also problematic since the standard
478streams (@pxref{Standard Streams}) will also be closed.
6bc31da0
UD
479
480The function @code{fcloseall} is declared in @file{stdio.h}.
481@end deftypefun
482
28f540f4
RM
483If the @code{main} function to your program returns, or if you call the
484@code{exit} function (@pxref{Normal Termination}), all open streams are
485automatically closed properly. If your program terminates in any other
486manner, such as by calling the @code{abort} function (@pxref{Aborting a
487Program}) or from a fatal signal (@pxref{Signal Handling}), open streams
488might not be closed properly. Buffered output might not be flushed and
489files may be incomplete. For more information on buffering of streams,
490see @ref{Stream Buffering}.
491
7b4161bb
UD
492@node Streams and Threads
493@section Streams and Threads
494
495@cindex threads
496@cindex multi-threaded application
497Streams can be used in multi-threaded applications in the same way they
498are used in single-threaded applications. But the programmer must be
11bf311e 499aware of the possible complications. It is important to know about
7b4161bb 500these also if the program one writes never use threads since the design
c703cd7a 501and implementation of many stream functions are heavily influenced by the
f6af7428 502requirements added by multi-threaded programming.
7b4161bb
UD
503
504The POSIX standard requires that by default the stream operations are
0bc93a2f 505atomic. I.e., issuing two stream operations for the same stream in two
7b4161bb
UD
506threads at the same time will cause the operations to be executed as if
507they were issued sequentially. The buffer operations performed while
508reading or writing are protected from other uses of the same stream. To
509do this each stream has an internal lock object which has to be
510(implicitly) acquired before any work can be done.
511
512But there are situations where this is not enough and there are also
513situations where this is not wanted. The implicit locking is not enough
514if the program requires more than one stream function call to happen
515atomically. One example would be if an output line a program wants to
516generate is created by several function calls. The functions by
517themselves would ensure only atomicity of their own operation, but not
518atomicity over all the function calls. For this it is necessary to
519perform the stream locking in the application code.
520
521@comment stdio.h
522@comment POSIX
523@deftypefun void flockfile (FILE *@var{stream})
171e9210
AO
524@safety{@prelim{}@mtsafe{}@assafe{}@acunsafe{@aculock{}}}
525@c There's no way to tell whether the lock was acquired before or after
526@c cancellation so as to unlock only when appropriate.
7b4161bb 527The @code{flockfile} function acquires the internal locking object
f6af7428 528associated with the stream @var{stream}. This ensures that no other
7b4161bb 529thread can explicitly through @code{flockfile}/@code{ftrylockfile} or
c703cd7a 530implicitly through the call of a stream function lock the stream. The
7b4161bb
UD
531thread will block until the lock is acquired. An explicit call to
532@code{funlockfile} has to be used to release the lock.
533@end deftypefun
534
535@comment stdio.h
536@comment POSIX
537@deftypefun int ftrylockfile (FILE *@var{stream})
171e9210 538@safety{@prelim{}@mtsafe{}@assafe{}@acunsafe{@aculock{}}}
f6af7428 539The @code{ftrylockfile} function tries to acquire the internal locking
7b4161bb
UD
540object associated with the stream @var{stream} just like
541@code{flockfile}. But unlike @code{flockfile} this function does not
542block if the lock is not available. @code{ftrylockfile} returns zero if
543the lock was successfully acquired. Otherwise the stream is locked by
544another thread.
545@end deftypefun
546
547@comment stdio.h
548@comment POSIX
549@deftypefun void funlockfile (FILE *@var{stream})
171e9210 550@safety{@prelim{}@mtsafe{}@assafe{}@acunsafe{@aculock{}}}
7b4161bb 551The @code{funlockfile} function releases the internal locking object of
cf822e3c 552the stream @var{stream}. The stream must have been locked before by a
7b4161bb
UD
553call to @code{flockfile} or a successful call of @code{ftrylockfile}.
554The implicit locking performed by the stream operations do not count.
555The @code{funlockfile} function does not return an error status and the
556behavior of a call for a stream which is not locked by the current
557thread is undefined.
558@end deftypefun
559
560The following example shows how the functions above can be used to
561generate an output line atomically even in multi-threaded applications
562(yes, the same job could be done with one @code{fprintf} call but it is
563sometimes not possible):
564
565@smallexample
566FILE *fp;
567@{
95fdc6a0 568 @dots{}
7b4161bb
UD
569 flockfile (fp);
570 fputs ("This is test number ", fp);
571 fprintf (fp, "%d\n", test);
572 funlockfile (fp)
573@}
574@end smallexample
575
576Without the explicit locking it would be possible for another thread to
c703cd7a 577use the stream @var{fp} after the @code{fputs} call returns and before
7b4161bb
UD
578@code{fprintf} was called with the result that the number does not
579follow the word @samp{number}.
580
581From this description it might already be clear that the locking objects
582in streams are no simple mutexes. Since locking the same stream twice
583in the same thread is allowed the locking objects must be equivalent to
584recursive mutexes. These mutexes keep track of the owner and the number
585of times the lock is acquired. The same number of @code{funlockfile}
586calls by the same threads is necessary to unlock the stream completely.
587For instance:
588
589@smallexample
590void
591foo (FILE *fp)
592@{
593 ftrylockfile (fp);
594 fputs ("in foo\n", fp);
595 /* @r{This is very wrong!!!} */
596 funlockfile (fp);
597@}
598@end smallexample
599
600It is important here that the @code{funlockfile} function is only called
601if the @code{ftrylockfile} function succeeded in locking the stream. It
602is therefore always wrong to ignore the result of @code{ftrylockfile}.
603And it makes no sense since otherwise one would use @code{flockfile}.
604The result of code like that above is that either @code{funlockfile}
f6af7428 605tries to free a stream that hasn't been locked by the current thread or it
7b4161bb
UD
606frees the stream prematurely. The code should look like this:
607
608@smallexample
609void
610foo (FILE *fp)
611@{
612 if (ftrylockfile (fp) == 0)
613 @{
614 fputs ("in foo\n", fp);
615 funlockfile (fp);
616 @}
617@}
618@end smallexample
619
c703cd7a 620Now that we covered why it is necessary to have locking it is
f6af7428 621necessary to talk about situations when locking is unwanted and what can
7b4161bb
UD
622be done. The locking operations (explicit or implicit) don't come for
623free. Even if a lock is not taken the cost is not zero. The operations
3ca9e670 624which have to be performed require memory operations that are safe in
7b4161bb
UD
625multi-processor environments. With the many local caches involved in
626such systems this is quite costly. So it is best to avoid the locking
3ca9e670
UD
627completely if it is not needed -- because the code in question is never
628used in a context where two or more threads may use a stream at a time.
7b4161bb
UD
629This can be determined most of the time for application code; for
630library code which can be used in many contexts one should default to be
631conservative and use locking.
632
633There are two basic mechanisms to avoid locking. The first is to use
634the @code{_unlocked} variants of the stream operations. The POSIX
1f77f049 635standard defines quite a few of those and @theglibc{} adds a few
7b4161bb 636more. These variants of the functions behave just like the functions
3ca9e670 637with the name without the suffix except that they do not lock the
7b4161bb 638stream. Using these functions is very desirable since they are
f6af7428 639potentially much faster. This is not only because the locking
7b4161bb 640operation itself is avoided. More importantly, functions like
0bc93a2f 641@code{putc} and @code{getc} are very simple and traditionally (before the
7b4161bb 642introduction of threads) were implemented as macros which are very fast
3ca9e670
UD
643if the buffer is not empty. With the addition of locking requirements
644these functions are no longer implemented as macros since they would
bbf70ae9 645expand to too much code.
3ca9e670 646But these macros are still available with the same functionality under the new
7b4161bb
UD
647names @code{putc_unlocked} and @code{getc_unlocked}. This possibly huge
648difference of speed also suggests the use of the @code{_unlocked}
649functions even if locking is required. The difference is that the
650locking then has to be performed in the program:
651
652@smallexample
653void
654foo (FILE *fp, char *buf)
655@{
656 flockfile (fp);
657 while (*buf != '/')
658 putc_unlocked (*buf++, fp);
659 funlockfile (fp);
660@}
661@end smallexample
662
663If in this example the @code{putc} function would be used and the
664explicit locking would be missing the @code{putc} function would have to
665acquire the lock in every call, potentially many times depending on when
666the loop terminates. Writing it the way illustrated above allows the
667@code{putc_unlocked} macro to be used which means no locking and direct
668manipulation of the buffer of the stream.
669
670A second way to avoid locking is by using a non-standard function which
1f77f049 671was introduced in Solaris and is available in @theglibc{} as well.
7b4161bb
UD
672
673@comment stdio_ext.h
674@comment GNU
675@deftypefun int __fsetlocking (FILE *@var{stream}, int @var{type})
171e9210
AO
676@safety{@prelim{}@mtsafe{@mtsrace{:stream}}@asunsafe{@asulock{}}@acsafe{}}
677@c Changing the implicit-locking status of a stream while it's in use by
678@c another thread may cause a lock to be implicitly acquired and not
679@c released, or vice-versa. This function should probably hold the lock
680@c while changing this setting, to make sure we don't change it while
681@c there are any concurrent uses. Meanwhile, callers should acquire the
682@c lock themselves to be safe, and even concurrent uses with external
683@c locking will be fine, as long as functions that require external
684@c locking are not called without holding locks.
7b4161bb
UD
685
686The @code{__fsetlocking} function can be used to select whether the
687stream operations will implicitly acquire the locking object of the
688stream @var{stream}. By default this is done but it can be disabled and
689reinstated using this function. There are three values defined for the
690@var{type} parameter.
691
692@vtable @code
693@item FSETLOCKING_INTERNAL
694The stream @code{stream} will from now on use the default internal
695locking. Every stream operation with exception of the @code{_unlocked}
696variants will implicitly lock the stream.
697
698@item FSETLOCKING_BYCALLER
c703cd7a 699After the @code{__fsetlocking} function returns, the user is responsible
7b4161bb
UD
700for locking the stream. None of the stream operations will implicitly
701do this anymore until the state is set back to
702@code{FSETLOCKING_INTERNAL}.
703
704@item FSETLOCKING_QUERY
705@code{__fsetlocking} only queries the current locking state of the
706stream. The return value will be @code{FSETLOCKING_INTERNAL} or
707@code{FSETLOCKING_BYCALLER} depending on the state.
708@end vtable
709
710The return value of @code{__fsetlocking} is either
711@code{FSETLOCKING_INTERNAL} or @code{FSETLOCKING_BYCALLER} depending on
712the state of the stream before the call.
713
714This function and the values for the @var{type} parameter are declared
715in @file{stdio_ext.h}.
716@end deftypefun
717
718This function is especially useful when program code has to be used
719which is written without knowledge about the @code{_unlocked} functions
3ca9e670 720(or if the programmer was too lazy to use them).
7b4161bb 721
b5e73f56
UD
722@node Streams and I18N
723@section Streams in Internationalized Applications
724
725@w{ISO C90} introduced the new type @code{wchar_t} to allow handling
726larger character sets. What was missing was a possibility to output
727strings of @code{wchar_t} directly. One had to convert them into
728multibyte strings using @code{mbstowcs} (there was no @code{mbsrtowcs}
729yet) and then use the normal stream functions. While this is doable it
730is very cumbersome since performing the conversions is not trivial and
731greatly increases program complexity and size.
732
733The Unix standard early on (I think in XPG4.2) introduced two additional
734format specifiers for the @code{printf} and @code{scanf} families of
735functions. Printing and reading of single wide characters was made
736possible using the @code{%C} specifier and wide character strings can be
737handled with @code{%S}. These modifiers behave just like @code{%c} and
738@code{%s} only that they expect the corresponding argument to have the
739wide character type and that the wide character and string are
740transformed into/from multibyte strings before being used.
741
742This was a beginning but it is still not good enough. Not always is it
743desirable to use @code{printf} and @code{scanf}. The other, smaller and
744faster functions cannot handle wide characters. Second, it is not
745possible to have a format string for @code{printf} and @code{scanf}
746consisting of wide characters. The result is that format strings would
747have to be generated if they have to contain non-basic characters.
748
749@cindex C++ streams
750@cindex streams, C++
751In the @w{Amendment 1} to @w{ISO C90} a whole new set of functions was
752added to solve the problem. Most of the stream functions got a
753counterpart which take a wide character or wide character string instead
754of a character or string respectively. The new functions operate on the
755same streams (like @code{stdout}). This is different from the model of
756the C++ runtime library where separate streams for wide and normal I/O
757are used.
758
759@cindex orientation, stream
760@cindex stream orientation
761Being able to use the same stream for wide and normal operations comes
762with a restriction: a stream can be used either for wide operations or
763for normal operations. Once it is decided there is no way back. Only a
764call to @code{freopen} or @code{freopen64} can reset the
765@dfn{orientation}. The orientation can be decided in three ways:
766
767@itemize @bullet
768@item
c703cd7a 769If any of the normal character functions are used (this includes the
22298d18 770@code{fread} and @code{fwrite} functions) the stream is marked as not
b5e73f56
UD
771wide oriented.
772
773@item
c703cd7a 774If any of the wide character functions are used the stream is marked as
22298d18 775wide oriented.
b5e73f56
UD
776
777@item
778The @code{fwide} function can be used to set the orientation either way.
779@end itemize
780
781It is important to never mix the use of wide and not wide operations on
782a stream. There are no diagnostics issued. The application behavior
783will simply be strange or the application will simply crash. The
c703cd7a 784@code{fwide} function can help avoid this.
b5e73f56
UD
785
786@comment wchar.h
787@comment ISO
788@deftypefun int fwide (FILE *@var{stream}, int @var{mode})
171e9210
AO
789@safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{}}@acunsafe{@aculock{}}}
790@c Querying is always safe, but changing the stream when it's in use
791@c upthread may be problematic. Like most lock-acquiring functions,
792@c this one may leak the lock if canceled.
b5e73f56 793
22298d18 794The @code{fwide} function can be used to set and query the state of the
b5e73f56
UD
795orientation of the stream @var{stream}. If the @var{mode} parameter has
796a positive value the streams get wide oriented, for negative values
797narrow oriented. It is not possible to overwrite previous orientations
798with @code{fwide}. I.e., if the stream @var{stream} was already
799oriented before the call nothing is done.
800
801If @var{mode} is zero the current orientation state is queried and
802nothing is changed.
803
804The @code{fwide} function returns a negative value, zero, or a positive
805value if the stream is narrow, not at all, or wide oriented
806respectively.
807
808This function was introduced in @w{Amendment 1} to @w{ISO C90} and is
809declared in @file{wchar.h}.
810@end deftypefun
811
812It is generally a good idea to orient a stream as early as possible.
813This can prevent surprise especially for the standard streams
814@code{stdin}, @code{stdout}, and @code{stderr}. If some library
815function in some situations uses one of these streams and this use
816orients the stream in a different way the rest of the application
817expects it one might end up with hard to reproduce errors. Remember
818that no errors are signal if the streams are used incorrectly. Leaving
819a stream unoriented after creation is normally only necessary for
820library functions which create streams which can be used in different
821contexts.
822
823When writing code which uses streams and which can be used in different
824contexts it is important to query the orientation of the stream before
825using it (unless the rules of the library interface demand a specific
826orientation). The following little, silly function illustrates this.
827
828@smallexample
829void
830print_f (FILE *fp)
831@{
832 if (fwide (fp, 0) > 0)
833 /* @r{Positive return value means wide orientation.} */
834 fputwc (L'f', fp);
835 else
836 fputc ('f', fp);
837@}
838@end smallexample
839
840Note that in this case the function @code{print_f} decides about the
841orientation of the stream if it was unoriented before (will not happen
c703cd7a 842if the advice above is followed).
b5e73f56
UD
843
844The encoding used for the @code{wchar_t} values is unspecified and the
845user must not make any assumptions about it. For I/O of @code{wchar_t}
846values this means that it is impossible to write these values directly
847to the stream. This is not what follows from the @w{ISO C} locale model
848either. What happens instead is that the bytes read from or written to
849the underlying media are first converted into the internal encoding
850chosen by the implementation for @code{wchar_t}. The external encoding
851is determined by the @code{LC_CTYPE} category of the current locale or
852by the @samp{ccs} part of the mode specification given to @code{fopen},
853@code{fopen64}, @code{freopen}, or @code{freopen64}. How and when the
c703cd7a 854conversion happens is unspecified and it happens invisibly to the user.
b5e73f56
UD
855
856Since a stream is created in the unoriented state it has at that point
857no conversion associated with it. The conversion which will be used is
858determined by the @code{LC_CTYPE} category selected at the time the
859stream is oriented. If the locales are changed at the runtime this
860might produce surprising results unless one pays attention. This is
861just another good reason to orient the stream explicitly as soon as
862possible, perhaps with a call to @code{fwide}.
863
28f540f4
RM
864@node Simple Output
865@section Simple Output by Characters or Lines
866
867@cindex writing to a stream, by characters
868This section describes functions for performing character- and
869line-oriented output.
870
c703cd7a 871These narrow stream functions are declared in the header file
b5e73f56 872@file{stdio.h} and the wide stream functions in @file{wchar.h}.
28f540f4 873@pindex stdio.h
b5e73f56 874@pindex wchar.h
28f540f4
RM
875
876@comment stdio.h
f65fd747 877@comment ISO
28f540f4 878@deftypefun int fputc (int @var{c}, FILE *@var{stream})
171e9210
AO
879@safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{}}@acunsafe{@acucorrupt{} @aculock{}}}
880@c If the stream is in use when interrupted by a signal, the recursive
881@c lock won't help ensure the stream is consistent; indeed, if fputc
882@c gets a signal precisely before the post-incremented _IO_write_ptr
883@c value is stored, we may overwrite the interrupted write. Conversely,
884@c depending on compiler optimizations, the incremented _IO_write_ptr
885@c may be stored before the character is stored in the buffer,
886@c corrupting the stream if async cancel hits between the two stores.
887@c There may be other reasons for AS- and AC-unsafety in the overflow
888@c cases.
28f540f4 889The @code{fputc} function converts the character @var{c} to type
19c3f208 890@code{unsigned char}, and writes it to the stream @var{stream}.
28f540f4
RM
891@code{EOF} is returned if a write error occurs; otherwise the
892character @var{c} is returned.
893@end deftypefun
894
b5e73f56
UD
895@comment wchar.h
896@comment ISO
897@deftypefun wint_t fputwc (wchar_t @var{wc}, FILE *@var{stream})
171e9210 898@safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{}}@acunsafe{@acucorrupt{} @aculock{}}}
b5e73f56
UD
899The @code{fputwc} function writes the wide character @var{wc} to the
900stream @var{stream}. @code{WEOF} is returned if a write error occurs;
901otherwise the character @var{wc} is returned.
902@end deftypefun
903
7b4161bb
UD
904@comment stdio.h
905@comment POSIX
906@deftypefun int fputc_unlocked (int @var{c}, FILE *@var{stream})
171e9210
AO
907@safety{@prelim{}@mtsafe{@mtsrace{:stream}}@asunsafe{@asucorrupt{}}@acunsafe{@acucorrupt{}}}
908@c The unlocked functions can't possibly satisfy the MT-Safety
909@c requirements on their own, because they require external locking for
910@c safety.
7b4161bb 911The @code{fputc_unlocked} function is equivalent to the @code{fputc}
1dc843f7 912function except that it does not implicitly lock the stream.
7b4161bb
UD
913@end deftypefun
914
b5e73f56
UD
915@comment wchar.h
916@comment POSIX
8ded91fb 917@deftypefun wint_t fputwc_unlocked (wchar_t @var{wc}, FILE *@var{stream})
171e9210 918@safety{@prelim{}@mtsafe{@mtsrace{:stream}}@asunsafe{@asucorrupt{}}@acunsafe{@acucorrupt{}}}
b5e73f56 919The @code{fputwc_unlocked} function is equivalent to the @code{fputwc}
1dc843f7 920function except that it does not implicitly lock the stream.
b5e73f56
UD
921
922This function is a GNU extension.
923@end deftypefun
924
28f540f4 925@comment stdio.h
f65fd747 926@comment ISO
28f540f4 927@deftypefun int putc (int @var{c}, FILE *@var{stream})
171e9210 928@safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{}}@acunsafe{@acucorrupt{} @aculock{}}}
28f540f4
RM
929This is just like @code{fputc}, except that most systems implement it as
930a macro, making it faster. One consequence is that it may evaluate the
931@var{stream} argument more than once, which is an exception to the
932general rule for macros. @code{putc} is usually the best function to
933use for writing a single character.
934@end deftypefun
935
b5e73f56
UD
936@comment wchar.h
937@comment ISO
938@deftypefun wint_t putwc (wchar_t @var{wc}, FILE *@var{stream})
171e9210 939@safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{}}@acunsafe{@acucorrupt{} @aculock{}}}
b5e73f56
UD
940This is just like @code{fputwc}, except that it can be implement as
941a macro, making it faster. One consequence is that it may evaluate the
942@var{stream} argument more than once, which is an exception to the
943general rule for macros. @code{putwc} is usually the best function to
944use for writing a single wide character.
945@end deftypefun
946
7b4161bb
UD
947@comment stdio.h
948@comment POSIX
949@deftypefun int putc_unlocked (int @var{c}, FILE *@var{stream})
171e9210 950@safety{@prelim{}@mtsafe{@mtsrace{:stream}}@asunsafe{@asucorrupt{}}@acunsafe{@acucorrupt{}}}
7b4161bb 951The @code{putc_unlocked} function is equivalent to the @code{putc}
1dc843f7 952function except that it does not implicitly lock the stream.
7b4161bb
UD
953@end deftypefun
954
b5e73f56
UD
955@comment wchar.h
956@comment GNU
957@deftypefun wint_t putwc_unlocked (wchar_t @var{wc}, FILE *@var{stream})
171e9210 958@safety{@prelim{}@mtsafe{@mtsrace{:stream}}@asunsafe{@asucorrupt{}}@acunsafe{@acucorrupt{}}}
b5e73f56 959The @code{putwc_unlocked} function is equivalent to the @code{putwc}
1dc843f7 960function except that it does not implicitly lock the stream.
b5e73f56
UD
961
962This function is a GNU extension.
963@end deftypefun
964
28f540f4 965@comment stdio.h
f65fd747 966@comment ISO
28f540f4 967@deftypefun int putchar (int @var{c})
171e9210 968@safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{}}@acunsafe{@acucorrupt{} @aculock{}}}
28f540f4
RM
969The @code{putchar} function is equivalent to @code{putc} with
970@code{stdout} as the value of the @var{stream} argument.
971@end deftypefun
972
b5e73f56
UD
973@comment wchar.h
974@comment ISO
c44a663d 975@deftypefun wint_t putwchar (wchar_t @var{wc})
171e9210 976@safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{}}@acunsafe{@acucorrupt{} @aculock{}}}
b5e73f56
UD
977The @code{putwchar} function is equivalent to @code{putwc} with
978@code{stdout} as the value of the @var{stream} argument.
979@end deftypefun
980
7b4161bb
UD
981@comment stdio.h
982@comment POSIX
983@deftypefun int putchar_unlocked (int @var{c})
171e9210 984@safety{@prelim{}@mtunsafe{@mtasurace{:stdout}}@asunsafe{@asucorrupt{}}@acunsafe{@acucorrupt{}}}
7b4161bb 985The @code{putchar_unlocked} function is equivalent to the @code{putchar}
1dc843f7 986function except that it does not implicitly lock the stream.
7b4161bb
UD
987@end deftypefun
988
b5e73f56
UD
989@comment wchar.h
990@comment GNU
991@deftypefun wint_t putwchar_unlocked (wchar_t @var{wc})
171e9210 992@safety{@prelim{}@mtunsafe{@mtasurace{:stdout}}@asunsafe{@asucorrupt{}}@acunsafe{@acucorrupt{}}}
b5e73f56 993The @code{putwchar_unlocked} function is equivalent to the @code{putwchar}
1dc843f7 994function except that it does not implicitly lock the stream.
b5e73f56
UD
995
996This function is a GNU extension.
997@end deftypefun
998
28f540f4 999@comment stdio.h
f65fd747 1000@comment ISO
28f540f4 1001@deftypefun int fputs (const char *@var{s}, FILE *@var{stream})
171e9210 1002@safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{}}@acunsafe{@acucorrupt{} @aculock{}}}
28f540f4
RM
1003The function @code{fputs} writes the string @var{s} to the stream
1004@var{stream}. The terminating null character is not written.
1005This function does @emph{not} add a newline character, either.
1006It outputs only the characters in the string.
1007
1008This function returns @code{EOF} if a write error occurs, and otherwise
1009a non-negative value.
1010
1011For example:
1012
1013@smallexample
1014fputs ("Are ", stdout);
1015fputs ("you ", stdout);
1016fputs ("hungry?\n", stdout);
1017@end smallexample
1018
1019@noindent
1020outputs the text @samp{Are you hungry?} followed by a newline.
1021@end deftypefun
1022
b5e73f56
UD
1023@comment wchar.h
1024@comment ISO
1025@deftypefun int fputws (const wchar_t *@var{ws}, FILE *@var{stream})
171e9210 1026@safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{}}@acunsafe{@acucorrupt{} @aculock{}}}
b5e73f56
UD
1027The function @code{fputws} writes the wide character string @var{ws} to
1028the stream @var{stream}. The terminating null character is not written.
1029This function does @emph{not} add a newline character, either. It
1030outputs only the characters in the string.
1031
1032This function returns @code{WEOF} if a write error occurs, and otherwise
1033a non-negative value.
1034@end deftypefun
1035
7b4161bb
UD
1036@comment stdio.h
1037@comment GNU
1038@deftypefun int fputs_unlocked (const char *@var{s}, FILE *@var{stream})
171e9210 1039@safety{@prelim{}@mtsafe{@mtsrace{:stream}}@asunsafe{@asucorrupt{}}@acunsafe{@acucorrupt{}}}
7b4161bb 1040The @code{fputs_unlocked} function is equivalent to the @code{fputs}
1dc843f7 1041function except that it does not implicitly lock the stream.
7b4161bb
UD
1042
1043This function is a GNU extension.
1044@end deftypefun
1045
b5e73f56
UD
1046@comment wchar.h
1047@comment GNU
1048@deftypefun int fputws_unlocked (const wchar_t *@var{ws}, FILE *@var{stream})
171e9210 1049@safety{@prelim{}@mtsafe{@mtsrace{:stream}}@asunsafe{@asucorrupt{}}@acunsafe{@acucorrupt{}}}
b5e73f56 1050The @code{fputws_unlocked} function is equivalent to the @code{fputws}
1dc843f7 1051function except that it does not implicitly lock the stream.
b5e73f56
UD
1052
1053This function is a GNU extension.
1054@end deftypefun
1055
28f540f4 1056@comment stdio.h
f65fd747 1057@comment ISO
28f540f4 1058@deftypefun int puts (const char *@var{s})
171e9210 1059@safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{}}@acunsafe{@aculock{} @acucorrupt{}}}
28f540f4
RM
1060The @code{puts} function writes the string @var{s} to the stream
1061@code{stdout} followed by a newline. The terminating null character of
1062the string is not written. (Note that @code{fputs} does @emph{not}
1063write a newline as this function does.)
1064
1065@code{puts} is the most convenient function for printing simple
1066messages. For example:
1067
1068@smallexample
1069puts ("This is a message.");
1070@end smallexample
0be8752b
UD
1071
1072@noindent
1073outputs the text @samp{This is a message.} followed by a newline.
28f540f4
RM
1074@end deftypefun
1075
1076@comment stdio.h
1077@comment SVID
1078@deftypefun int putw (int @var{w}, FILE *@var{stream})
171e9210 1079@safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{}}@acunsafe{@aculock{} @acucorrupt{}}}
28f540f4
RM
1080This function writes the word @var{w} (that is, an @code{int}) to
1081@var{stream}. It is provided for compatibility with SVID, but we
1082recommend you use @code{fwrite} instead (@pxref{Block Input/Output}).
1083@end deftypefun
1084
1085@node Character Input
1086@section Character Input
1087
1088@cindex reading from a stream, by characters
b5e73f56 1089This section describes functions for performing character-oriented
c703cd7a 1090input. These narrow stream functions are declared in the header file
b5e73f56
UD
1091@file{stdio.h} and the wide character functions are declared in
1092@file{wchar.h}.
28f540f4 1093@pindex stdio.h
b5e73f56
UD
1094@pindex wchar.h
1095
1096These functions return an @code{int} or @code{wint_t} value (for narrow
1097and wide stream functions respectively) that is either a character of
1098input, or the special value @code{EOF}/@code{WEOF} (usually -1). For
1099the narrow stream functions it is important to store the result of these
1100functions in a variable of type @code{int} instead of @code{char}, even
1101when you plan to use it only as a character. Storing @code{EOF} in a
1102@code{char} variable truncates its value to the size of a character, so
1103that it is no longer distinguishable from the valid character
1104@samp{(char) -1}. So always use an @code{int} for the result of
1105@code{getc} and friends, and check for @code{EOF} after the call; once
1106you've verified that the result is not @code{EOF}, you can be sure that
1107it will fit in a @samp{char} variable without loss of information.
28f540f4
RM
1108
1109@comment stdio.h
f65fd747 1110@comment ISO
28f540f4 1111@deftypefun int fgetc (FILE *@var{stream})
171e9210
AO
1112@safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{}}@acunsafe{@aculock{} @acucorrupt{}}}
1113@c Same caveats as fputc, but instead of losing a write in case of async
1114@c signals, we may read the same character more than once, and the
1115@c stream may be left in odd states due to cancellation in the underflow
1116@c cases.
28f540f4
RM
1117This function reads the next character as an @code{unsigned char} from
1118the stream @var{stream} and returns its value, converted to an
1119@code{int}. If an end-of-file condition or read error occurs,
19c3f208 1120@code{EOF} is returned instead.
28f540f4
RM
1121@end deftypefun
1122
b5e73f56
UD
1123@comment wchar.h
1124@comment ISO
1125@deftypefun wint_t fgetwc (FILE *@var{stream})
171e9210 1126@safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{}}@acunsafe{@aculock{} @acucorrupt{}}}
b5e73f56
UD
1127This function reads the next wide character from the stream @var{stream}
1128and returns its value. If an end-of-file condition or read error
1129occurs, @code{WEOF} is returned instead.
1130@end deftypefun
1131
7b4161bb
UD
1132@comment stdio.h
1133@comment POSIX
1134@deftypefun int fgetc_unlocked (FILE *@var{stream})
171e9210 1135@safety{@prelim{}@mtsafe{@mtsrace{:stream}}@asunsafe{@asucorrupt{}}@acunsafe{@acucorrupt{}}}
7b4161bb 1136The @code{fgetc_unlocked} function is equivalent to the @code{fgetc}
1dc843f7 1137function except that it does not implicitly lock the stream.
7b4161bb
UD
1138@end deftypefun
1139
b5e73f56
UD
1140@comment wchar.h
1141@comment GNU
1142@deftypefun wint_t fgetwc_unlocked (FILE *@var{stream})
171e9210 1143@safety{@prelim{}@mtsafe{@mtsrace{:stream}}@asunsafe{@asucorrupt{}}@acunsafe{@acucorrupt{}}}
b5e73f56 1144The @code{fgetwc_unlocked} function is equivalent to the @code{fgetwc}
1dc843f7 1145function except that it does not implicitly lock the stream.
b5e73f56
UD
1146
1147This function is a GNU extension.
1148@end deftypefun
1149
28f540f4 1150@comment stdio.h
f65fd747 1151@comment ISO
28f540f4 1152@deftypefun int getc (FILE *@var{stream})
171e9210 1153@safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{}}@acunsafe{@aculock{} @acucorrupt{}}}
28f540f4
RM
1154This is just like @code{fgetc}, except that it is permissible (and
1155typical) for it to be implemented as a macro that evaluates the
1156@var{stream} argument more than once. @code{getc} is often highly
1157optimized, so it is usually the best function to use to read a single
1158character.
1159@end deftypefun
1160
b5e73f56
UD
1161@comment wchar.h
1162@comment ISO
1163@deftypefun wint_t getwc (FILE *@var{stream})
171e9210 1164@safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{}}@acunsafe{@aculock{} @acucorrupt{}}}
b5e73f56
UD
1165This is just like @code{fgetwc}, except that it is permissible for it to
1166be implemented as a macro that evaluates the @var{stream} argument more
1167than once. @code{getwc} can be highly optimized, so it is usually the
1168best function to use to read a single wide character.
1169@end deftypefun
1170
7b4161bb
UD
1171@comment stdio.h
1172@comment POSIX
1173@deftypefun int getc_unlocked (FILE *@var{stream})
171e9210 1174@safety{@prelim{}@mtsafe{@mtsrace{:stream}}@asunsafe{@asucorrupt{}}@acunsafe{@acucorrupt{}}}
7b4161bb 1175The @code{getc_unlocked} function is equivalent to the @code{getc}
1dc843f7 1176function except that it does not implicitly lock the stream.
7b4161bb
UD
1177@end deftypefun
1178
b5e73f56
UD
1179@comment wchar.h
1180@comment GNU
1181@deftypefun wint_t getwc_unlocked (FILE *@var{stream})
171e9210 1182@safety{@prelim{}@mtsafe{@mtsrace{:stream}}@asunsafe{@asucorrupt{}}@acunsafe{@acucorrupt{}}}
b5e73f56 1183The @code{getwc_unlocked} function is equivalent to the @code{getwc}
1dc843f7 1184function except that it does not implicitly lock the stream.
b5e73f56
UD
1185
1186This function is a GNU extension.
1187@end deftypefun
1188
28f540f4 1189@comment stdio.h
f65fd747 1190@comment ISO
28f540f4 1191@deftypefun int getchar (void)
171e9210 1192@safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{}}@acunsafe{@aculock{} @acucorrupt{}}}
28f540f4
RM
1193The @code{getchar} function is equivalent to @code{getc} with @code{stdin}
1194as the value of the @var{stream} argument.
1195@end deftypefun
1196
b5e73f56
UD
1197@comment wchar.h
1198@comment ISO
1199@deftypefun wint_t getwchar (void)
171e9210 1200@safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{}}@acunsafe{@aculock{} @acucorrupt{}}}
b5e73f56
UD
1201The @code{getwchar} function is equivalent to @code{getwc} with @code{stdin}
1202as the value of the @var{stream} argument.
1203@end deftypefun
1204
7b4161bb
UD
1205@comment stdio.h
1206@comment POSIX
1207@deftypefun int getchar_unlocked (void)
171e9210 1208@safety{@prelim{}@mtunsafe{@mtasurace{:stdin}}@asunsafe{@asucorrupt{}}@acunsafe{@acucorrupt{}}}
7b4161bb 1209The @code{getchar_unlocked} function is equivalent to the @code{getchar}
1dc843f7 1210function except that it does not implicitly lock the stream.
7b4161bb
UD
1211@end deftypefun
1212
b5e73f56
UD
1213@comment wchar.h
1214@comment GNU
1215@deftypefun wint_t getwchar_unlocked (void)
171e9210 1216@safety{@prelim{}@mtunsafe{@mtasurace{:stdin}}@asunsafe{@asucorrupt{}}@acunsafe{@acucorrupt{}}}
b5e73f56 1217The @code{getwchar_unlocked} function is equivalent to the @code{getwchar}
1dc843f7 1218function except that it does not implicitly lock the stream.
b5e73f56
UD
1219
1220This function is a GNU extension.
1221@end deftypefun
1222
28f540f4
RM
1223Here is an example of a function that does input using @code{fgetc}. It
1224would work just as well using @code{getc} instead, or using
b5e73f56
UD
1225@code{getchar ()} instead of @w{@code{fgetc (stdin)}}. The code would
1226also work the same for the wide character stream functions.
28f540f4
RM
1227
1228@smallexample
1229int
1230y_or_n_p (const char *question)
1231@{
1232 fputs (question, stdout);
1233 while (1)
1234 @{
1235 int c, answer;
1236 /* @r{Write a space to separate answer from question.} */
1237 fputc (' ', stdout);
1238 /* @r{Read the first character of the line.}
162ba701 1239 @r{This should be the answer character, but might not be.} */
28f540f4
RM
1240 c = tolower (fgetc (stdin));
1241 answer = c;
1242 /* @r{Discard rest of input line.} */
1243 while (c != '\n' && c != EOF)
162ba701 1244 c = fgetc (stdin);
28f540f4
RM
1245 /* @r{Obey the answer if it was valid.} */
1246 if (answer == 'y')
162ba701 1247 return 1;
28f540f4 1248 if (answer == 'n')
162ba701 1249 return 0;
28f540f4
RM
1250 /* @r{Answer was invalid: ask for valid answer.} */
1251 fputs ("Please answer y or n:", stdout);
1252 @}
1253@}
1254@end smallexample
1255
1256@comment stdio.h
1257@comment SVID
1258@deftypefun int getw (FILE *@var{stream})
171e9210 1259@safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{}}@acunsafe{@aculock{} @acucorrupt{}}}
28f540f4
RM
1260This function reads a word (that is, an @code{int}) from @var{stream}.
1261It's provided for compatibility with SVID. We recommend you use
1262@code{fread} instead (@pxref{Block Input/Output}). Unlike @code{getc},
1263any @code{int} value could be a valid result. @code{getw} returns
1264@code{EOF} when it encounters end-of-file or an error, but there is no
1265way to distinguish this from an input word with value -1.
1266@end deftypefun
1267
1268@node Line Input
1269@section Line-Oriented Input
1270
b5e73f56 1271Since many programs interpret input on the basis of lines, it is
28f540f4
RM
1272convenient to have functions to read a line of text from a stream.
1273
1274Standard C has functions to do this, but they aren't very safe: null
1275characters and even (for @code{gets}) long lines can confuse them. So
1f77f049 1276@theglibc{} provides the nonstandard @code{getline} function that
28f540f4
RM
1277makes it easy to read lines reliably.
1278
1279Another GNU extension, @code{getdelim}, generalizes @code{getline}. It
1280reads a delimited record, defined as everything through the next
1281occurrence of a specified delimiter character.
1282
1283All these functions are declared in @file{stdio.h}.
1284
1285@comment stdio.h
1286@comment GNU
1287@deftypefun ssize_t getline (char **@var{lineptr}, size_t *@var{n}, FILE *@var{stream})
171e9210
AO
1288@safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{} @ascuheap{}}@acunsafe{@aculock{} @acucorrupt{} @acsmem{}}}
1289@c Besides the usual possibility of getting an inconsistent stream in a
1290@c signal handler or leaving it inconsistent in case of cancellation,
1291@c the possibility of leaving a dangling pointer upon cancellation
1292@c between reallocing the buffer at *lineptr and updating the pointer
1293@c brings about another case of @acucorrupt.
28f540f4
RM
1294This function reads an entire line from @var{stream}, storing the text
1295(including the newline and a terminating null character) in a buffer
1296and storing the buffer address in @code{*@var{lineptr}}.
1297
1298Before calling @code{getline}, you should place in @code{*@var{lineptr}}
1299the address of a buffer @code{*@var{n}} bytes long, allocated with
1300@code{malloc}. If this buffer is long enough to hold the line,
1301@code{getline} stores the line in this buffer. Otherwise,
1302@code{getline} makes the buffer bigger using @code{realloc}, storing the
1303new buffer address back in @code{*@var{lineptr}} and the increased size
1304back in @code{*@var{n}}.
1305@xref{Unconstrained Allocation}.
1306
1307If you set @code{*@var{lineptr}} to a null pointer, and @code{*@var{n}}
1308to zero, before the call, then @code{getline} allocates the initial
1c21d115
DS
1309buffer for you by calling @code{malloc}. This buffer remains allocated
1310even if @code{getline} encounters errors and is unable to read any bytes.
28f540f4
RM
1311
1312In either case, when @code{getline} returns, @code{*@var{lineptr}} is
1313a @code{char *} which points to the text of the line.
1314
1315When @code{getline} is successful, it returns the number of characters
1316read (including the newline, but not including the terminating null).
1317This value enables you to distinguish null characters that are part of
1318the line from the null character inserted as a terminator.
1319
1320This function is a GNU extension, but it is the recommended way to read
1321lines from a stream. The alternative standard functions are unreliable.
1322
ec28fc7c
UD
1323If an error occurs or end of file is reached without any bytes read,
1324@code{getline} returns @code{-1}.
28f540f4
RM
1325@end deftypefun
1326
1327@comment stdio.h
1328@comment GNU
1329@deftypefun ssize_t getdelim (char **@var{lineptr}, size_t *@var{n}, int @var{delimiter}, FILE *@var{stream})
171e9210
AO
1330@safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{} @ascuheap{}}@acunsafe{@aculock{} @acucorrupt{} @acsmem{}}}
1331@c See the getline @acucorrupt note.
28f540f4
RM
1332This function is like @code{getline} except that the character which
1333tells it to stop reading is not necessarily newline. The argument
1334@var{delimiter} specifies the delimiter character; @code{getdelim} keeps
1335reading until it sees that character (or end of file).
1336
1337The text is stored in @var{lineptr}, including the delimiter character
1338and a terminating null. Like @code{getline}, @code{getdelim} makes
1339@var{lineptr} bigger if it isn't big enough.
1340
1341@code{getline} is in fact implemented in terms of @code{getdelim}, just
1342like this:
1343
1344@smallexample
1345ssize_t
1346getline (char **lineptr, size_t *n, FILE *stream)
1347@{
1348 return getdelim (lineptr, n, '\n', stream);
1349@}
1350@end smallexample
1351@end deftypefun
1352
1353@comment stdio.h
f65fd747 1354@comment ISO
28f540f4 1355@deftypefun {char *} fgets (char *@var{s}, int @var{count}, FILE *@var{stream})
171e9210 1356@safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{}}@acunsafe{@aculock{} @acucorrupt{}}}
28f540f4
RM
1357The @code{fgets} function reads characters from the stream @var{stream}
1358up to and including a newline character and stores them in the string
1359@var{s}, adding a null character to mark the end of the string. You
1360must supply @var{count} characters worth of space in @var{s}, but the
1361number of characters read is at most @var{count} @minus{} 1. The extra
1362character space is used to hold the null character at the end of the
1363string.
1364
1365If the system is already at end of file when you call @code{fgets}, then
1366the contents of the array @var{s} are unchanged and a null pointer is
1367returned. A null pointer is also returned if a read error occurs.
1368Otherwise, the return value is the pointer @var{s}.
1369
1370@strong{Warning:} If the input data has a null character, you can't tell.
1371So don't use @code{fgets} unless you know the data cannot contain a null.
1372Don't use it to read files edited by the user because, if the user inserts
1373a null character, you should either handle it properly or print a clear
1374error message. We recommend using @code{getline} instead of @code{fgets}.
1375@end deftypefun
1376
b5e73f56
UD
1377@comment wchar.h
1378@comment ISO
1379@deftypefun {wchar_t *} fgetws (wchar_t *@var{ws}, int @var{count}, FILE *@var{stream})
171e9210 1380@safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{}}@acunsafe{@aculock{} @acucorrupt{}}}
b5e73f56
UD
1381The @code{fgetws} function reads wide characters from the stream
1382@var{stream} up to and including a newline character and stores them in
1383the string @var{ws}, adding a null wide character to mark the end of the
1384string. You must supply @var{count} wide characters worth of space in
1385@var{ws}, but the number of characters read is at most @var{count}
1386@minus{} 1. The extra character space is used to hold the null wide
1387character at the end of the string.
1388
1389If the system is already at end of file when you call @code{fgetws}, then
1390the contents of the array @var{ws} are unchanged and a null pointer is
1391returned. A null pointer is also returned if a read error occurs.
1392Otherwise, the return value is the pointer @var{ws}.
1393
1394@strong{Warning:} If the input data has a null wide character (which are
1395null bytes in the input stream), you can't tell. So don't use
1396@code{fgetws} unless you know the data cannot contain a null. Don't use
1397it to read files edited by the user because, if the user inserts a null
1398character, you should either handle it properly or print a clear error
1399message.
1400@comment XXX We need getwline!!!
1401@end deftypefun
1402
7b4161bb
UD
1403@comment stdio.h
1404@comment GNU
1405@deftypefun {char *} fgets_unlocked (char *@var{s}, int @var{count}, FILE *@var{stream})
171e9210 1406@safety{@prelim{}@mtsafe{@mtsrace{:stream}}@asunsafe{@asucorrupt{}}@acunsafe{@acucorrupt{}}}
7b4161bb 1407The @code{fgets_unlocked} function is equivalent to the @code{fgets}
1dc843f7 1408function except that it does not implicitly lock the stream.
7b4161bb
UD
1409
1410This function is a GNU extension.
1411@end deftypefun
1412
b5e73f56
UD
1413@comment wchar.h
1414@comment GNU
1415@deftypefun {wchar_t *} fgetws_unlocked (wchar_t *@var{ws}, int @var{count}, FILE *@var{stream})
171e9210 1416@safety{@prelim{}@mtsafe{@mtsrace{:stream}}@asunsafe{@asucorrupt{}}@acunsafe{@acucorrupt{}}}
b5e73f56 1417The @code{fgetws_unlocked} function is equivalent to the @code{fgetws}
1dc843f7 1418function except that it does not implicitly lock the stream.
b5e73f56
UD
1419
1420This function is a GNU extension.
1421@end deftypefun
1422
28f540f4 1423@comment stdio.h
f65fd747 1424@comment ISO
28f540f4 1425@deftypefn {Deprecated function} {char *} gets (char *@var{s})
171e9210 1426@safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{}}@acunsafe{@aculock{} @acucorrupt{}}}
28f540f4
RM
1427The function @code{gets} reads characters from the stream @code{stdin}
1428up to the next newline character, and stores them in the string @var{s}.
1429The newline character is discarded (note that this differs from the
1430behavior of @code{fgets}, which copies the newline character into the
1431string). If @code{gets} encounters a read error or end-of-file, it
1432returns a null pointer; otherwise it returns @var{s}.
1433
1434@strong{Warning:} The @code{gets} function is @strong{very dangerous}
1435because it provides no protection against overflowing the string
1f77f049 1436@var{s}. @Theglibc{} includes it for compatibility only. You
28f540f4
RM
1437should @strong{always} use @code{fgets} or @code{getline} instead. To
1438remind you of this, the linker (if using GNU @code{ld}) will issue a
1439warning whenever you use @code{gets}.
1440@end deftypefn
1441
1442@node Unreading
1443@section Unreading
1444@cindex peeking at input
1445@cindex unreading characters
1446@cindex pushing input back
1447
1448In parser programs it is often useful to examine the next character in
1449the input stream without removing it from the stream. This is called
1450``peeking ahead'' at the input because your program gets a glimpse of
1451the input it will read next.
1452
1453Using stream I/O, you can peek ahead at input by first reading it and
19c3f208 1454then @dfn{unreading} it (also called @dfn{pushing it back} on the stream).
28f540f4
RM
1455Unreading a character makes it available to be input again from the stream,
1456by the next call to @code{fgetc} or other input function on that stream.
1457
1458@menu
1459* Unreading Idea:: An explanation of unreading with pictures.
1460* How Unread:: How to call @code{ungetc} to do unreading.
1461@end menu
1462
1463@node Unreading Idea
1464@subsection What Unreading Means
1465
1466Here is a pictorial explanation of unreading. Suppose you have a
1467stream reading a file that contains just six characters, the letters
1468@samp{foobar}. Suppose you have read three characters so far. The
1469situation looks like this:
1470
1471@smallexample
1472f o o b a r
162ba701 1473 ^
28f540f4
RM
1474@end smallexample
1475
1476@noindent
1477so the next input character will be @samp{b}.
1478
1479@c @group Invalid outside @example
1480If instead of reading @samp{b} you unread the letter @samp{o}, you get a
1481situation like this:
1482
1483@smallexample
1484f o o b a r
162ba701 1485 |
28f540f4
RM
1486 o--
1487 ^
1488@end smallexample
1489
1490@noindent
1491so that the next input characters will be @samp{o} and @samp{b}.
1492@c @end group
1493
1494@c @group
1495If you unread @samp{9} instead of @samp{o}, you get this situation:
1496
1497@smallexample
1498f o o b a r
162ba701 1499 |
28f540f4
RM
1500 9--
1501 ^
1502@end smallexample
1503
1504@noindent
1505so that the next input characters will be @samp{9} and @samp{b}.
1506@c @end group
1507
1508@node How Unread
1509@subsection Using @code{ungetc} To Do Unreading
19c3f208 1510
28f540f4
RM
1511The function to unread a character is called @code{ungetc}, because it
1512reverses the action of @code{getc}.
1513
1514@comment stdio.h
f65fd747 1515@comment ISO
28f540f4 1516@deftypefun int ungetc (int @var{c}, FILE *@var{stream})
171e9210 1517@safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{}}@acunsafe{@aculock{} @acucorrupt{}}}
28f540f4
RM
1518The @code{ungetc} function pushes back the character @var{c} onto the
1519input stream @var{stream}. So the next input from @var{stream} will
1520read @var{c} before anything else.
1521
1522If @var{c} is @code{EOF}, @code{ungetc} does nothing and just returns
1523@code{EOF}. This lets you call @code{ungetc} with the return value of
1524@code{getc} without needing to check for an error from @code{getc}.
1525
1526The character that you push back doesn't have to be the same as the last
1527character that was actually read from the stream. In fact, it isn't
1528necessary to actually read any characters from the stream before
f4efd068
UD
1529unreading them with @code{ungetc}! But that is a strange way to write a
1530program; usually @code{ungetc} is used only to unread a character that
1f77f049 1531was just read from the same stream. @Theglibc{} supports this
f4efd068 1532even on files opened in binary mode, but other systems might not.
28f540f4 1533
1f77f049 1534@Theglibc{} only supports one character of pushback---in other
28f540f4
RM
1535words, it does not work to call @code{ungetc} twice without doing input
1536in between. Other systems might let you push back multiple characters;
1537then reading from the stream retrieves the characters in the reverse
1538order that they were pushed.
1539
1540Pushing back characters doesn't alter the file; only the internal
1541buffering for the stream is affected. If a file positioning function
a5a0310d
UD
1542(such as @code{fseek}, @code{fseeko} or @code{rewind}; @pxref{File
1543Positioning}) is called, any pending pushed-back characters are
1544discarded.
28f540f4
RM
1545
1546Unreading a character on a stream that is at end of file clears the
1547end-of-file indicator for the stream, because it makes the character of
1548input available. After you read that character, trying to read again
1549will encounter end of file.
1550@end deftypefun
1551
b5e73f56
UD
1552@comment wchar.h
1553@comment ISO
1554@deftypefun wint_t ungetwc (wint_t @var{wc}, FILE *@var{stream})
171e9210 1555@safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{}}@acunsafe{@aculock{} @acucorrupt{}}}
b5e73f56
UD
1556The @code{ungetwc} function behaves just like @code{ungetc} just that it
1557pushes back a wide character.
1558@end deftypefun
1559
28f540f4
RM
1560Here is an example showing the use of @code{getc} and @code{ungetc} to
1561skip over whitespace characters. When this function reaches a
1562non-whitespace character, it unreads that character to be seen again on
1563the next read operation on the stream.
1564
1565@smallexample
1566#include <stdio.h>
1567#include <ctype.h>
1568
1569void
1570skip_whitespace (FILE *stream)
1571@{
1572 int c;
1573 do
1574 /* @r{No need to check for @code{EOF} because it is not}
1575 @r{@code{isspace}, and @code{ungetc} ignores @code{EOF}.} */
1576 c = getc (stream);
1577 while (isspace (c));
1578 ungetc (c, stream);
1579@}
1580@end smallexample
1581
1582@node Block Input/Output
1583@section Block Input/Output
1584
1585This section describes how to do input and output operations on blocks
1586of data. You can use these functions to read and write binary data, as
1587well as to read and write text in fixed-size blocks instead of by
1588characters or lines.
1589@cindex binary I/O to a stream
1590@cindex block I/O to a stream
1591@cindex reading from a stream, by blocks
1592@cindex writing to a stream, by blocks
1593
1594Binary files are typically used to read and write blocks of data in the
1595same format as is used to represent the data in a running program. In
1596other words, arbitrary blocks of memory---not just character or string
1597objects---can be written to a binary file, and meaningfully read in
1598again by the same program.
1599
1600Storing data in binary form is often considerably more efficient than
1601using the formatted I/O functions. Also, for floating-point numbers,
1602the binary form avoids possible loss of precision in the conversion
1603process. On the other hand, binary files can't be examined or modified
1604easily using many standard file utilities (such as text editors), and
1605are not portable between different implementations of the language, or
1606different kinds of computers.
1607
1608These functions are declared in @file{stdio.h}.
1609@pindex stdio.h
1610
1611@comment stdio.h
f65fd747 1612@comment ISO
28f540f4 1613@deftypefun size_t fread (void *@var{data}, size_t @var{size}, size_t @var{count}, FILE *@var{stream})
171e9210 1614@safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{}}@acunsafe{@aculock{} @acucorrupt{}}}
28f540f4
RM
1615This function reads up to @var{count} objects of size @var{size} into
1616the array @var{data}, from the stream @var{stream}. It returns the
1617number of objects actually read, which might be less than @var{count} if
1618a read error occurs or the end of the file is reached. This function
1619returns a value of zero (and doesn't read anything) if either @var{size}
1620or @var{count} is zero.
1621
1622If @code{fread} encounters end of file in the middle of an object, it
1623returns the number of complete objects read, and discards the partial
1624object. Therefore, the stream remains at the actual end of the file.
1625@end deftypefun
1626
7b4161bb
UD
1627@comment stdio.h
1628@comment GNU
1629@deftypefun size_t fread_unlocked (void *@var{data}, size_t @var{size}, size_t @var{count}, FILE *@var{stream})
171e9210 1630@safety{@prelim{}@mtsafe{@mtsrace{:stream}}@asunsafe{@asucorrupt{}}@acunsafe{@acucorrupt{}}}
7b4161bb 1631The @code{fread_unlocked} function is equivalent to the @code{fread}
1dc843f7 1632function except that it does not implicitly lock the stream.
7b4161bb
UD
1633
1634This function is a GNU extension.
1635@end deftypefun
1636
28f540f4 1637@comment stdio.h
f65fd747 1638@comment ISO
28f540f4 1639@deftypefun size_t fwrite (const void *@var{data}, size_t @var{size}, size_t @var{count}, FILE *@var{stream})
171e9210 1640@safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{}}@acunsafe{@aculock{} @acucorrupt{}}}
28f540f4
RM
1641This function writes up to @var{count} objects of size @var{size} from
1642the array @var{data}, to the stream @var{stream}. The return value is
1643normally @var{count}, if the call succeeds. Any other value indicates
1644some sort of error, such as running out of space.
1645@end deftypefun
1646
7b4161bb
UD
1647@comment stdio.h
1648@comment GNU
1649@deftypefun size_t fwrite_unlocked (const void *@var{data}, size_t @var{size}, size_t @var{count}, FILE *@var{stream})
171e9210 1650@safety{@prelim{}@mtsafe{@mtsrace{:stream}}@asunsafe{@asucorrupt{}}@acunsafe{@acucorrupt{}}}
7b4161bb 1651The @code{fwrite_unlocked} function is equivalent to the @code{fwrite}
1dc843f7 1652function except that it does not implicitly lock the stream.
7b4161bb
UD
1653
1654This function is a GNU extension.
1655@end deftypefun
1656
28f540f4
RM
1657@node Formatted Output
1658@section Formatted Output
1659
1660@cindex format string, for @code{printf}
1661@cindex template, for @code{printf}
1662@cindex formatted output to a stream
1663@cindex writing to a stream, formatted
1664The functions described in this section (@code{printf} and related
1665functions) provide a convenient way to perform formatted output. You
1666call @code{printf} with a @dfn{format string} or @dfn{template string}
1667that specifies how to format the values of the remaining arguments.
1668
1669Unless your program is a filter that specifically performs line- or
1670character-oriented processing, using @code{printf} or one of the other
1671related functions described in this section is usually the easiest and
1672most concise way to perform output. These functions are especially
1673useful for printing error messages, tables of data, and the like.
1674
1675@menu
1676* Formatted Output Basics:: Some examples to get you started.
1677* Output Conversion Syntax:: General syntax of conversion
162ba701 1678 specifications.
28f540f4 1679* Table of Output Conversions:: Summary of output conversions and
162ba701 1680 what they do.
28f540f4
RM
1681* Integer Conversions:: Details about formatting of integers.
1682* Floating-Point Conversions:: Details about formatting of
162ba701 1683 floating-point numbers.
28f540f4 1684* Other Output Conversions:: Details about formatting of strings,
162ba701 1685 characters, pointers, and the like.
28f540f4
RM
1686* Formatted Output Functions:: Descriptions of the actual functions.
1687* Dynamic Output:: Functions that allocate memory for the output.
1688* Variable Arguments Output:: @code{vprintf} and friends.
1689* Parsing a Template String:: What kinds of args does a given template
162ba701 1690 call for?
28f540f4
RM
1691* Example of Parsing:: Sample program using @code{parse_printf_format}.
1692@end menu
1693
1694@node Formatted Output Basics
1695@subsection Formatted Output Basics
1696
1697The @code{printf} function can be used to print any number of arguments.
1698The template string argument you supply in a call provides
1699information not only about the number of additional arguments, but also
1700about their types and what style should be used for printing them.
1701
1702Ordinary characters in the template string are simply written to the
1703output stream as-is, while @dfn{conversion specifications} introduced by
1704a @samp{%} character in the template cause subsequent arguments to be
1705formatted and written to the output stream. For example,
1706@cindex conversion specifications (@code{printf})
1707
1708@smallexample
1709int pct = 37;
1710char filename[] = "foo.txt";
1711printf ("Processing of `%s' is %d%% finished.\nPlease be patient.\n",
162ba701 1712 filename, pct);
28f540f4
RM
1713@end smallexample
1714
1715@noindent
1716produces output like
1717
1718@smallexample
1719Processing of `foo.txt' is 37% finished.
1720Please be patient.
1721@end smallexample
1722
1723This example shows the use of the @samp{%d} conversion to specify that
1724an @code{int} argument should be printed in decimal notation, the
1725@samp{%s} conversion to specify printing of a string argument, and
1726the @samp{%%} conversion to print a literal @samp{%} character.
1727
1728There are also conversions for printing an integer argument as an
1729unsigned value in octal, decimal, or hexadecimal radix (@samp{%o},
1730@samp{%u}, or @samp{%x}, respectively); or as a character value
1731(@samp{%c}).
1732
1733Floating-point numbers can be printed in normal, fixed-point notation
1734using the @samp{%f} conversion or in exponential notation using the
1735@samp{%e} conversion. The @samp{%g} conversion uses either @samp{%e}
1736or @samp{%f} format, depending on what is more appropriate for the
1737magnitude of the particular number.
1738
1739You can control formatting more precisely by writing @dfn{modifiers}
1740between the @samp{%} and the character that indicates which conversion
1741to apply. These slightly alter the ordinary behavior of the conversion.
1742For example, most conversion specifications permit you to specify a
1743minimum field width and a flag indicating whether you want the result
1744left- or right-justified within the field.
1745
1746The specific flags and modifiers that are permitted and their
1747interpretation vary depending on the particular conversion. They're all
1748described in more detail in the following sections. Don't worry if this
1749all seems excessively complicated at first; you can almost always get
1750reasonable free-format output without using any of the modifiers at all.
1751The modifiers are mostly used to make the output look ``prettier'' in
1752tables.
1753
1754@node Output Conversion Syntax
1755@subsection Output Conversion Syntax
1756
1757This section provides details about the precise syntax of conversion
1758specifications that can appear in a @code{printf} template
1759string.
1760
390955cb
UD
1761Characters in the template string that are not part of a conversion
1762specification are printed as-is to the output stream. Multibyte
1763character sequences (@pxref{Character Set Handling}) are permitted in a
1764template string.
28f540f4
RM
1765
1766The conversion specifications in a @code{printf} template string have
1767the general form:
1768
f89829da 1769@smallexample
dfd2257a 1770% @r{[} @var{param-no} @r{$]} @var{flags} @var{width} @r{[} . @var{precision} @r{]} @var{type} @var{conversion}
f89829da
UD
1771@end smallexample
1772
1773@noindent
1774or
1775
1776@smallexample
1777% @r{[} @var{param-no} @r{$]} @var{flags} @var{width} . @r{*} @r{[} @var{param-no} @r{$]} @var{type} @var{conversion}
1778@end smallexample
28f540f4
RM
1779
1780For example, in the conversion specifier @samp{%-10.8ld}, the @samp{-}
1781is a flag, @samp{10} specifies the field width, the precision is
1782@samp{8}, the letter @samp{l} is a type modifier, and @samp{d} specifies
1783the conversion style. (This particular type specifier says to
1784print a @code{long int} argument in decimal notation, with a minimum of
17858 digits left-justified in a field at least 10 characters wide.)
1786
1787In more detail, output conversion specifications consist of an
1788initial @samp{%} character followed in sequence by:
1789
1790@itemize @bullet
dfd2257a
UD
1791@item
1792An optional specification of the parameter used for this format.
e8b1163e 1793Normally the parameters to the @code{printf} function are assigned to the
f2ea0f5b 1794formats in the order of appearance in the format string. But in some
dfd2257a 1795situations (such as message translation) this is not desirable and this
e8b1163e 1796extension allows an explicit parameter to be specified.
dfd2257a 1797
f89829da 1798The @var{param-no} parts of the format must be integers in the range of
dfd2257a 17991 to the maximum number of arguments present to the function call. Some
c703cd7a 1800implementations limit this number to a certain upper bound. The exact
dfd2257a
UD
1801limit can be retrieved by the following constant.
1802
1803@defvr Macro NL_ARGMAX
f89829da 1804The value of @code{NL_ARGMAX} is the maximum value allowed for the
9dcc8f11 1805specification of a positional parameter in a @code{printf} call. The
dfd2257a
UD
1806actual value in effect at runtime can be retrieved by using
1807@code{sysconf} using the @code{_SC_NL_ARGMAX} parameter @pxref{Sysconf
1808Definition}.
1809
c703cd7a 1810Some systems have a quite low limit such as @math{9} for @w{System V}
1f77f049 1811systems. @Theglibc{} has no real limit.
dfd2257a
UD
1812@end defvr
1813
1814If any of the formats has a specification for the parameter position all
0bc93a2f 1815of them in the format string shall have one. Otherwise the behavior is
dfd2257a
UD
1816undefined.
1817
19c3f208 1818@item
28f540f4
RM
1819Zero or more @dfn{flag characters} that modify the normal behavior of
1820the conversion specification.
1821@cindex flag character (@code{printf})
1822
19c3f208 1823@item
28f540f4
RM
1824An optional decimal integer specifying the @dfn{minimum field width}.
1825If the normal conversion produces fewer characters than this, the field
1826is padded with spaces to the specified width. This is a @emph{minimum}
1827value; if the normal conversion produces more characters than this, the
1828field is @emph{not} truncated. Normally, the output is right-justified
1829within the field.
1830@cindex minimum field width (@code{printf})
1831
1832You can also specify a field width of @samp{*}. This means that the
1833next argument in the argument list (before the actual value to be
1834printed) is used as the field width. The value must be an @code{int}.
1835If the value is negative, this means to set the @samp{-} flag (see
1836below) and to use the absolute value as the field width.
1837
19c3f208 1838@item
28f540f4
RM
1839An optional @dfn{precision} to specify the number of digits to be
1840written for the numeric conversions. If the precision is specified, it
1841consists of a period (@samp{.}) followed optionally by a decimal integer
1842(which defaults to zero if omitted).
1843@cindex precision (@code{printf})
1844
1845You can also specify a precision of @samp{*}. This means that the next
1846argument in the argument list (before the actual value to be printed) is
1847used as the precision. The value must be an @code{int}, and is ignored
1848if it is negative. If you specify @samp{*} for both the field width and
1849precision, the field width argument precedes the precision argument.
1850Other C library versions may not recognize this syntax.
1851
1852@item
1853An optional @dfn{type modifier character}, which is used to specify the
1854data type of the corresponding argument if it differs from the default
1855type. (For example, the integer conversions assume a type of @code{int},
1856but you can specify @samp{h}, @samp{l}, or @samp{L} for other integer
1857types.)
1858@cindex type modifier character (@code{printf})
1859
1860@item
1861A character that specifies the conversion to be applied.
1862@end itemize
1863
19c3f208 1864The exact options that are permitted and how they are interpreted vary
28f540f4
RM
1865between the different conversion specifiers. See the descriptions of the
1866individual conversions for information about the particular options that
1867they use.
1868
1869With the @samp{-Wformat} option, the GNU C compiler checks calls to
1870@code{printf} and related functions. It examines the format string and
1871verifies that the correct number and types of arguments are supplied.
1872There is also a GNU C syntax to tell the compiler that a function you
19c3f208 1873write uses a @code{printf}-style format string.
28f540f4
RM
1874@xref{Function Attributes, , Declaring Attributes of Functions,
1875gcc.info, Using GNU CC}, for more information.
1876
1877@node Table of Output Conversions
1878@subsection Table of Output Conversions
1879@cindex output conversions, for @code{printf}
1880
1881Here is a table summarizing what all the different conversions do:
1882
1883@table @asis
1884@item @samp{%d}, @samp{%i}
1885Print an integer as a signed decimal number. @xref{Integer
1886Conversions}, for details. @samp{%d} and @samp{%i} are synonymous for
1887output, but are different when used with @code{scanf} for input
1888(@pxref{Table of Input Conversions}).
1889
1890@item @samp{%o}
1891Print an integer as an unsigned octal number. @xref{Integer
1892Conversions}, for details.
1893
1894@item @samp{%u}
1895Print an integer as an unsigned decimal number. @xref{Integer
1896Conversions}, for details.
1897
1898@item @samp{%x}, @samp{%X}
1899Print an integer as an unsigned hexadecimal number. @samp{%x} uses
1900lower-case letters and @samp{%X} uses upper-case. @xref{Integer
1901Conversions}, for details.
1902
1903@item @samp{%f}
1904Print a floating-point number in normal (fixed-point) notation.
1905@xref{Floating-Point Conversions}, for details.
1906
1907@item @samp{%e}, @samp{%E}
1908Print a floating-point number in exponential notation. @samp{%e} uses
1909lower-case letters and @samp{%E} uses upper-case. @xref{Floating-Point
1910Conversions}, for details.
1911
1912@item @samp{%g}, @samp{%G}
1913Print a floating-point number in either normal or exponential notation,
1914whichever is more appropriate for its magnitude. @samp{%g} uses
1915lower-case letters and @samp{%G} uses upper-case. @xref{Floating-Point
1916Conversions}, for details.
1917
2f6d1f1b 1918@item @samp{%a}, @samp{%A}
c703cd7a 1919Print a floating-point number in a hexadecimal fractional notation with
2f6d1f1b
UD
1920the exponent to base 2 represented in decimal digits. @samp{%a} uses
1921lower-case letters and @samp{%A} uses upper-case. @xref{Floating-Point
1922Conversions}, for details.
1923
28f540f4
RM
1924@item @samp{%c}
1925Print a single character. @xref{Other Output Conversions}.
1926
b5e73f56
UD
1927@item @samp{%C}
1928This is an alias for @samp{%lc} which is supported for compatibility
1929with the Unix standard.
1930
28f540f4
RM
1931@item @samp{%s}
1932Print a string. @xref{Other Output Conversions}.
1933
b5e73f56
UD
1934@item @samp{%S}
1935This is an alias for @samp{%ls} which is supported for compatibility
1936with the Unix standard.
1937
28f540f4
RM
1938@item @samp{%p}
1939Print the value of a pointer. @xref{Other Output Conversions}.
1940
1941@item @samp{%n}
1942Get the number of characters printed so far. @xref{Other Output Conversions}.
1943Note that this conversion specification never produces any output.
1944
1945@item @samp{%m}
1946Print the string corresponding to the value of @code{errno}.
1947(This is a GNU extension.)
1948@xref{Other Output Conversions}.
1949
1950@item @samp{%%}
1951Print a literal @samp{%} character. @xref{Other Output Conversions}.
1952@end table
1953
1954If the syntax of a conversion specification is invalid, unpredictable
1955things will happen, so don't do this. If there aren't enough function
1956arguments provided to supply values for all the conversion
1957specifications in the template string, or if the arguments are not of
1958the correct types, the results are unpredictable. If you supply more
1959arguments than conversion specifications, the extra argument values are
1960simply ignored; this is sometimes useful.
1961
1962@node Integer Conversions
1963@subsection Integer Conversions
1964
1965This section describes the options for the @samp{%d}, @samp{%i},
1966@samp{%o}, @samp{%u}, @samp{%x}, and @samp{%X} conversion
1967specifications. These conversions print integers in various formats.
1968
1969The @samp{%d} and @samp{%i} conversion specifications both print an
1970@code{int} argument as a signed decimal number; while @samp{%o},
1971@samp{%u}, and @samp{%x} print the argument as an unsigned octal,
1972decimal, or hexadecimal number (respectively). The @samp{%X} conversion
1973specification is just like @samp{%x} except that it uses the characters
1974@samp{ABCDEF} as digits instead of @samp{abcdef}.
1975
1976The following flags are meaningful:
1977
1978@table @asis
1979@item @samp{-}
1980Left-justify the result in the field (instead of the normal
1981right-justification).
1982
1983@item @samp{+}
1984For the signed @samp{%d} and @samp{%i} conversions, print a
1985plus sign if the value is positive.
1986
1987@item @samp{ }
1988For the signed @samp{%d} and @samp{%i} conversions, if the result
1989doesn't start with a plus or minus sign, prefix it with a space
1990character instead. Since the @samp{+} flag ensures that the result
1991includes a sign, this flag is ignored if you supply both of them.
1992
1993@item @samp{#}
1994For the @samp{%o} conversion, this forces the leading digit to be
1995@samp{0}, as if by increasing the precision. For @samp{%x} or
1996@samp{%X}, this prefixes a leading @samp{0x} or @samp{0X} (respectively)
1997to the result. This doesn't do anything useful for the @samp{%d},
1998@samp{%i}, or @samp{%u} conversions. Using this flag produces output
1999which can be parsed by the @code{strtoul} function (@pxref{Parsing of
2000Integers}) and @code{scanf} with the @samp{%i} conversion
2001(@pxref{Numeric Input Conversions}).
2002
2003@item @samp{'}
2004Separate the digits into groups as specified by the locale specified for
2005the @code{LC_NUMERIC} category; @pxref{General Numeric}. This flag is a
2006GNU extension.
2007
2008@item @samp{0}
2009Pad the field with zeros instead of spaces. The zeros are placed after
2010any indication of sign or base. This flag is ignored if the @samp{-}
2011flag is also specified, or if a precision is specified.
2012@end table
2013
2014If a precision is supplied, it specifies the minimum number of digits to
2015appear; leading zeros are produced if necessary. If you don't specify a
2016precision, the number is printed with as many digits as it needs. If
2017you convert a value of zero with an explicit precision of zero, then no
2018characters at all are produced.
2019
2020Without a type modifier, the corresponding argument is treated as an
2021@code{int} (for the signed conversions @samp{%i} and @samp{%d}) or
2022@code{unsigned int} (for the unsigned conversions @samp{%o}, @samp{%u},
2023@samp{%x}, and @samp{%X}). Recall that since @code{printf} and friends
2024are variadic, any @code{char} and @code{short} arguments are
2025automatically converted to @code{int} by the default argument
2026promotions. For arguments of other integer types, you can use these
2027modifiers:
2028
2029@table @samp
cc3fa755
UD
2030@item hh
2031Specifies that the argument is a @code{signed char} or @code{unsigned
2032char}, as appropriate. A @code{char} argument is converted to an
2033@code{int} or @code{unsigned int} by the default argument promotions
c703cd7a 2034anyway, but the @samp{hh} modifier says to convert it back to a
cc3fa755
UD
2035@code{char} again.
2036
ec751a23 2037This modifier was introduced in @w{ISO C99}.
e852e889 2038
28f540f4
RM
2039@item h
2040Specifies that the argument is a @code{short int} or @code{unsigned
2041short int}, as appropriate. A @code{short} argument is converted to an
2042@code{int} or @code{unsigned int} by the default argument promotions
2043anyway, but the @samp{h} modifier says to convert it back to a
2044@code{short} again.
2045
e852e889
UD
2046@item j
2047Specifies that the argument is a @code{intmax_t} or @code{uintmax_t}, as
2048appropriate.
2049
ec751a23 2050This modifier was introduced in @w{ISO C99}.
e852e889 2051
28f540f4
RM
2052@item l
2053Specifies that the argument is a @code{long int} or @code{unsigned long
c703cd7a 2054int}, as appropriate. Two @samp{l} characters are like the @samp{L}
28f540f4
RM
2055modifier, below.
2056
b5e73f56
UD
2057If used with @samp{%c} or @samp{%s} the corresponding parameter is
2058considered as a wide character or wide character string respectively.
2059This use of @samp{l} was introduced in @w{Amendment 1} to @w{ISO C90}.
2060
28f540f4
RM
2061@item L
2062@itemx ll
2063@itemx q
2064Specifies that the argument is a @code{long long int}. (This type is
2065an extension supported by the GNU C compiler. On systems that don't
2066support extra-long integers, this is the same as @code{long int}.)
2067
2068The @samp{q} modifier is another name for the same thing, which comes
2069from 4.4 BSD; a @w{@code{long long int}} is sometimes called a ``quad''
2070@code{int}.
2071
e852e889
UD
2072@item t
2073Specifies that the argument is a @code{ptrdiff_t}.
2074
ec751a23 2075This modifier was introduced in @w{ISO C99}.
e852e889
UD
2076
2077@item z
2078@itemx Z
2079Specifies that the argument is a @code{size_t}.
2080
ec751a23 2081@samp{z} was introduced in @w{ISO C99}. @samp{Z} is a GNU extension
0be8752b 2082predating this addition and should not be used in new code.
28f540f4
RM
2083@end table
2084
2085Here is an example. Using the template string:
2086
2087@smallexample
2088"|%5d|%-5d|%+5d|%+-5d|% 5d|%05d|%5.0d|%5.2d|%d|\n"
2089@end smallexample
2090
2091@noindent
2092to print numbers using the different options for the @samp{%d}
2093conversion gives results like:
2094
2095@smallexample
2096| 0|0 | +0|+0 | 0|00000| | 00|0|
2097| 1|1 | +1|+1 | 1|00001| 1| 01|1|
2098| -1|-1 | -1|-1 | -1|-0001| -1| -01|-1|
776e8492 2099|100000|100000|+100000|+100000| 100000|100000|100000|100000|100000|
28f540f4
RM
2100@end smallexample
2101
2102In particular, notice what happens in the last case where the number
2103is too large to fit in the minimum field width specified.
2104
2105Here are some more examples showing how unsigned integers print under
2106various format options, using the template string:
2107
2108@smallexample
2109"|%5u|%5o|%5x|%5X|%#5o|%#5x|%#5X|%#10.8x|\n"
2110@end smallexample
2111
2112@smallexample
776e8492 2113| 0| 0| 0| 0| 0| 0| 0| 00000000|
28f540f4
RM
2114| 1| 1| 1| 1| 01| 0x1| 0X1|0x00000001|
2115|100000|303240|186a0|186A0|0303240|0x186a0|0X186A0|0x000186a0|
2116@end smallexample
2117
2118
2119@node Floating-Point Conversions
2120@subsection Floating-Point Conversions
2121
2122This section discusses the conversion specifications for floating-point
2123numbers: the @samp{%f}, @samp{%e}, @samp{%E}, @samp{%g}, and @samp{%G}
2124conversions.
2125
2126The @samp{%f} conversion prints its argument in fixed-point notation,
2127producing output of the form
2128@w{[@code{-}]@var{ddd}@code{.}@var{ddd}},
2129where the number of digits following the decimal point is controlled
2130by the precision you specify.
2131
2132The @samp{%e} conversion prints its argument in exponential notation,
2133producing output of the form
2134@w{[@code{-}]@var{d}@code{.}@var{ddd}@code{e}[@code{+}|@code{-}]@var{dd}}.
2135Again, the number of digits following the decimal point is controlled by
2136the precision. The exponent always contains at least two digits. The
2137@samp{%E} conversion is similar but the exponent is marked with the letter
2138@samp{E} instead of @samp{e}.
2139
2140The @samp{%g} and @samp{%G} conversions print the argument in the style
2141of @samp{%e} or @samp{%E} (respectively) if the exponent would be less
776e8492 2142than -4 or greater than or equal to the precision; otherwise they use
11bf311e 2143the @samp{%f} style. A precision of @code{0}, is taken as 1.
776e8492
AJ
2144Trailing zeros are removed from the fractional portion of the result and
2145a decimal-point character appears only if it is followed by a digit.
28f540f4 2146
2f6d1f1b 2147The @samp{%a} and @samp{%A} conversions are meant for representing
0be8752b 2148floating-point numbers exactly in textual form so that they can be
2f6d1f1b 2149exchanged as texts between different programs and/or machines. The
c703cd7a 2150numbers are represented in the form
2f6d1f1b
UD
2151@w{[@code{-}]@code{0x}@var{h}@code{.}@var{hhh}@code{p}[@code{+}|@code{-}]@var{dd}}.
2152At the left of the decimal-point character exactly one digit is print.
91ea72b7 2153This character is only @code{0} if the number is denormalized.
49c091e5 2154Otherwise the value is unspecified; it is implementation dependent how many
2f6d1f1b
UD
2155bits are used. The number of hexadecimal digits on the right side of
2156the decimal-point character is equal to the precision. If the precision
2157is zero it is determined to be large enough to provide an exact
2158representation of the number (or it is large enough to distinguish two
2159adjacent values if the @code{FLT_RADIX} is not a power of 2,
91ea72b7 2160@pxref{Floating Point Parameters}). For the @samp{%a} conversion
2f6d1f1b
UD
2161lower-case characters are used to represent the hexadecimal number and
2162the prefix and exponent sign are printed as @code{0x} and @code{p}
2163respectively. Otherwise upper-case characters are used and @code{0X}
2164and @code{P} are used for the representation of prefix and exponent
2165string. The exponent to the base of two is printed as a decimal number
2166using at least one digit but at most as many digits as necessary to
2167represent the value exactly.
2168
2169If the value to be printed represents infinity or a NaN, the output is
2170@w{[@code{-}]@code{inf}} or @code{nan} respectively if the conversion
2171specifier is @samp{%a}, @samp{%e}, @samp{%f}, or @samp{%g} and it is
2172@w{[@code{-}]@code{INF}} or @code{NAN} respectively if the conversion is
2173@samp{%A}, @samp{%E}, or @samp{%G}.
2174
28f540f4
RM
2175The following flags can be used to modify the behavior:
2176
2177@comment We use @asis instead of @samp so we can have ` ' as an item.
2178@table @asis
2179@item @samp{-}
2180Left-justify the result in the field. Normally the result is
2181right-justified.
2182
2183@item @samp{+}
2184Always include a plus or minus sign in the result.
2185
2186@item @samp{ }
2187If the result doesn't start with a plus or minus sign, prefix it with a
2188space instead. Since the @samp{+} flag ensures that the result includes
2189a sign, this flag is ignored if you supply both of them.
2190
2191@item @samp{#}
2192Specifies that the result should always include a decimal point, even
2193if no digits follow it. For the @samp{%g} and @samp{%G} conversions,
2194this also forces trailing zeros after the decimal point to be left
2195in place where they would otherwise be removed.
2196
2197@item @samp{'}
2198Separate the digits of the integer part of the result into groups as
2199specified by the locale specified for the @code{LC_NUMERIC} category;
2200@pxref{General Numeric}. This flag is a GNU extension.
2201
2202@item @samp{0}
2203Pad the field with zeros instead of spaces; the zeros are placed
2204after any sign. This flag is ignored if the @samp{-} flag is also
2205specified.
2206@end table
2207
2208The precision specifies how many digits follow the decimal-point
2209character for the @samp{%f}, @samp{%e}, and @samp{%E} conversions. For
2210these conversions, the default precision is @code{6}. If the precision
2211is explicitly @code{0}, this suppresses the decimal point character
2212entirely. For the @samp{%g} and @samp{%G} conversions, the precision
2213specifies how many significant digits to print. Significant digits are
2214the first digit before the decimal point, and all the digits after it.
91ea72b7 2215If the precision is @code{0} or not specified for @samp{%g} or @samp{%G},
28f540f4
RM
2216it is treated like a value of @code{1}. If the value being printed
2217cannot be expressed accurately in the specified number of digits, the
2218value is rounded to the nearest number that fits.
2219
2220Without a type modifier, the floating-point conversions use an argument
2221of type @code{double}. (By the default argument promotions, any
2222@code{float} arguments are automatically converted to @code{double}.)
2223The following type modifier is supported:
2224
2225@table @samp
2226@item L
2227An uppercase @samp{L} specifies that the argument is a @code{long
2228double}.
2229@end table
2230
2231Here are some examples showing how numbers print using the various
2232floating-point conversions. All of the numbers were printed using
2233this template string:
2234
2235@smallexample
2f6d1f1b 2236"|%13.4a|%13.4f|%13.4e|%13.4g|\n"
28f540f4
RM
2237@end smallexample
2238
2239Here is the output:
2240
2241@smallexample
2f6d1f1b
UD
2242| 0x0.0000p+0| 0.0000| 0.0000e+00| 0|
2243| 0x1.0000p-1| 0.5000| 5.0000e-01| 0.5|
2244| 0x1.0000p+0| 1.0000| 1.0000e+00| 1|
2245| -0x1.0000p+0| -1.0000| -1.0000e+00| -1|
2246| 0x1.9000p+6| 100.0000| 1.0000e+02| 100|
2247| 0x1.f400p+9| 1000.0000| 1.0000e+03| 1000|
2248| 0x1.3880p+13| 10000.0000| 1.0000e+04| 1e+04|
2249| 0x1.81c8p+13| 12345.0000| 1.2345e+04| 1.234e+04|
2250| 0x1.86a0p+16| 100000.0000| 1.0000e+05| 1e+05|
2251| 0x1.e240p+16| 123456.0000| 1.2346e+05| 1.235e+05|
28f540f4
RM
2252@end smallexample
2253
2254Notice how the @samp{%g} conversion drops trailing zeros.
2255
2256@node Other Output Conversions
2257@subsection Other Output Conversions
2258
2259This section describes miscellaneous conversions for @code{printf}.
2260
b5e73f56
UD
2261The @samp{%c} conversion prints a single character. In case there is no
2262@samp{l} modifier the @code{int} argument is first converted to an
2263@code{unsigned char}. Then, if used in a wide stream function, the
2264character is converted into the corresponding wide character. The
2265@samp{-} flag can be used to specify left-justification in the field,
2266but no other flags are defined, and no precision or type modifier can be
2267given. For example:
28f540f4
RM
2268
2269@smallexample
2270printf ("%c%c%c%c%c", 'h', 'e', 'l', 'l', 'o');
2271@end smallexample
2272
2273@noindent
2274prints @samp{hello}.
2275
c703cd7a 2276If there is an @samp{l} modifier present the argument is expected to be
b5e73f56
UD
2277of type @code{wint_t}. If used in a multibyte function the wide
2278character is converted into a multibyte character before being added to
2279the output. In this case more than one output byte can be produced.
2280
2281The @samp{%s} conversion prints a string. If no @samp{l} modifier is
2282present the corresponding argument must be of type @code{char *} (or
2283@code{const char *}). If used in a wide stream function the string is
c703cd7a 2284first converted to a wide character string. A precision can be
b5e73f56 2285specified to indicate the maximum number of characters to write;
28f540f4
RM
2286otherwise characters in the string up to but not including the
2287terminating null character are written to the output stream. The
2288@samp{-} flag can be used to specify left-justification in the field,
2289but no other flags or type modifiers are defined for this conversion.
2290For example:
2291
2292@smallexample
2293printf ("%3s%-6s", "no", "where");
2294@end smallexample
2295
2296@noindent
2297prints @samp{ nowhere }.
2298
c703cd7a
RJ
2299If there is an @samp{l} modifier present, the argument is expected to
2300be of type @code{wchar_t} (or @code{const wchar_t *}).
b5e73f56 2301
28f540f4 2302If you accidentally pass a null pointer as the argument for a @samp{%s}
1f77f049 2303conversion, @theglibc{} prints it as @samp{(null)}. We think this
28f540f4
RM
2304is more useful than crashing. But it's not good practice to pass a null
2305argument intentionally.
2306
2307The @samp{%m} conversion prints the string corresponding to the error
2308code in @code{errno}. @xref{Error Messages}. Thus:
2309
2310@smallexample
2311fprintf (stderr, "can't open `%s': %m\n", filename);
2312@end smallexample
2313
2314@noindent
2315is equivalent to:
2316
2317@smallexample
2318fprintf (stderr, "can't open `%s': %s\n", filename, strerror (errno));
2319@end smallexample
2320
2321@noindent
1f77f049 2322The @samp{%m} conversion is a @glibcadj{} extension.
28f540f4
RM
2323
2324The @samp{%p} conversion prints a pointer value. The corresponding
2325argument must be of type @code{void *}. In practice, you can use any
2326type of pointer.
2327
a7a93d50 2328In @theglibc{}, non-null pointers are printed as unsigned integers,
28f540f4
RM
2329as if a @samp{%#x} conversion were used. Null pointers print as
2330@samp{(nil)}. (Pointers might print differently in other systems.)
2331
2332For example:
2333
2334@smallexample
2335printf ("%p", "testing");
2336@end smallexample
2337
2338@noindent
2339prints @samp{0x} followed by a hexadecimal number---the address of the
2340string constant @code{"testing"}. It does not print the word
2341@samp{testing}.
2342
2343You can supply the @samp{-} flag with the @samp{%p} conversion to
2344specify left-justification, but no other flags, precision, or type
2345modifiers are defined.
2346
2347The @samp{%n} conversion is unlike any of the other output conversions.
2348It uses an argument which must be a pointer to an @code{int}, but
2349instead of printing anything it stores the number of characters printed
2350so far by this call at that location. The @samp{h} and @samp{l} type
2351modifiers are permitted to specify that the argument is of type
2352@code{short int *} or @code{long int *} instead of @code{int *}, but no
2353flags, field width, or precision are permitted.
2354
2355For example,
2356
2357@smallexample
2358int nchar;
2359printf ("%d %s%n\n", 3, "bears", &nchar);
2360@end smallexample
2361
2362@noindent
2363prints:
2364
2365@smallexample
23663 bears
2367@end smallexample
2368
2369@noindent
19c3f208 2370and sets @code{nchar} to @code{7}, because @samp{3 bears} is seven
28f540f4
RM
2371characters.
2372
2373
2374The @samp{%%} conversion prints a literal @samp{%} character. This
2375conversion doesn't use an argument, and no flags, field width,
2376precision, or type modifiers are permitted.
2377
2378
2379@node Formatted Output Functions
2380@subsection Formatted Output Functions
2381
2382This section describes how to call @code{printf} and related functions.
2383Prototypes for these functions are in the header file @file{stdio.h}.
2384Because these functions take a variable number of arguments, you
2385@emph{must} declare prototypes for them before using them. Of course,
2386the easiest way to make sure you have all the right prototypes is to
2387just include @file{stdio.h}.
2388@pindex stdio.h
2389
2390@comment stdio.h
f65fd747 2391@comment ISO
28f540f4 2392@deftypefun int printf (const char *@var{template}, @dots{})
171e9210 2393@safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@asucorrupt{} @ascuheap{}}@acunsafe{@acsmem{} @aculock{} @acucorrupt{}}}
28f540f4
RM
2394The @code{printf} function prints the optional arguments under the
2395control of the template string @var{template} to the stream
2396@code{stdout}. It returns the number of characters printed, or a
2397negative value if there was an output error.
2398@end deftypefun
2399
b5e73f56
UD
2400@comment wchar.h
2401@comment ISO
2402@deftypefun int wprintf (const wchar_t *@var{template}, @dots{})
171e9210 2403@safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@asucorrupt{} @ascuheap{}}@acunsafe{@acsmem{} @aculock{} @acucorrupt{}}}
b5e73f56
UD
2404The @code{wprintf} function prints the optional arguments under the
2405control of the wide template string @var{template} to the stream
2406@code{stdout}. It returns the number of wide characters printed, or a
2407negative value if there was an output error.
2408@end deftypefun
2409
28f540f4 2410@comment stdio.h
f65fd747 2411@comment ISO
28f540f4 2412@deftypefun int fprintf (FILE *@var{stream}, const char *@var{template}, @dots{})
171e9210 2413@safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@asucorrupt{} @ascuheap{}}@acunsafe{@acsmem{} @aculock{} @acucorrupt{}}}
28f540f4
RM
2414This function is just like @code{printf}, except that the output is
2415written to the stream @var{stream} instead of @code{stdout}.
2416@end deftypefun
2417
b5e73f56
UD
2418@comment wchar.h
2419@comment ISO
2420@deftypefun int fwprintf (FILE *@var{stream}, const wchar_t *@var{template}, @dots{})
171e9210 2421@safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@asucorrupt{} @ascuheap{}}@acunsafe{@acsmem{} @aculock{} @acucorrupt{}}}
b5e73f56
UD
2422This function is just like @code{wprintf}, except that the output is
2423written to the stream @var{stream} instead of @code{stdout}.
2424@end deftypefun
2425
28f540f4 2426@comment stdio.h
f65fd747 2427@comment ISO
28f540f4 2428@deftypefun int sprintf (char *@var{s}, const char *@var{template}, @dots{})
171e9210 2429@safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{}}}
28f540f4
RM
2430This is like @code{printf}, except that the output is stored in the character
2431array @var{s} instead of written to a stream. A null character is written
2432to mark the end of the string.
2433
2434The @code{sprintf} function returns the number of characters stored in
2435the array @var{s}, not including the terminating null character.
2436
2437The behavior of this function is undefined if copying takes place
2438between objects that overlap---for example, if @var{s} is also given
2439as an argument to be printed under control of the @samp{%s} conversion.
0a13c9e9 2440@xref{Copying Strings and Arrays}.
28f540f4
RM
2441
2442@strong{Warning:} The @code{sprintf} function can be @strong{dangerous}
2443because it can potentially output more characters than can fit in the
2444allocation size of the string @var{s}. Remember that the field width
2445given in a conversion specification is only a @emph{minimum} value.
2446
2447To avoid this problem, you can use @code{snprintf} or @code{asprintf},
2448described below.
2449@end deftypefun
2450
b5e73f56
UD
2451@comment wchar.h
2452@comment GNU
c703cd7a 2453@deftypefun int swprintf (wchar_t *@var{ws}, size_t @var{size}, const wchar_t *@var{template}, @dots{})
171e9210 2454@safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{}}}
b5e73f56
UD
2455This is like @code{wprintf}, except that the output is stored in the
2456wide character array @var{ws} instead of written to a stream. A null
2457wide character is written to mark the end of the string. The @var{size}
2458argument specifies the maximum number of characters to produce. The
2459trailing null character is counted towards this limit, so you should
2460allocate at least @var{size} wide characters for the string @var{ws}.
2461
2f278c94
UD
2462The return value is the number of characters generated for the given
2463input, excluding the trailing null. If not all output fits into the
2464provided buffer a negative value is returned. You should try again with
2465a bigger output string. @emph{Note:} this is different from how
2466@code{snprintf} handles this situation.
b5e73f56
UD
2467
2468Note that the corresponding narrow stream function takes fewer
2469parameters. @code{swprintf} in fact corresponds to the @code{snprintf}
2470function. Since the @code{sprintf} function can be dangerous and should
2471be avoided the @w{ISO C} committee refused to make the same mistake
9dcc8f11 2472again and decided to not define a function exactly corresponding to
b5e73f56
UD
2473@code{sprintf}.
2474@end deftypefun
2475
28f540f4
RM
2476@comment stdio.h
2477@comment GNU
2478@deftypefun int snprintf (char *@var{s}, size_t @var{size}, const char *@var{template}, @dots{})
171e9210 2479@safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{}}}
28f540f4
RM
2480The @code{snprintf} function is similar to @code{sprintf}, except that
2481the @var{size} argument specifies the maximum number of characters to
2482produce. The trailing null character is counted towards this limit, so
2483you should allocate at least @var{size} characters for the string @var{s}.
42443a47
JL
2484If @var{size} is zero, nothing, not even the null byte, shall be written and
2485@var{s} may be a null pointer.
28f540f4 2486
fe7bdd63 2487The return value is the number of characters which would be generated
da2d1bc5 2488for the given input, excluding the trailing null. If this value is
c703cd7a 2489greater than or equal to @var{size}, not all characters from the result have
da2d1bc5
UD
2490been stored in @var{s}. You should try again with a bigger output
2491string. Here is an example of doing this:
28f540f4
RM
2492
2493@smallexample
2494@group
2495/* @r{Construct a message describing the value of a variable}
2496 @r{whose name is @var{name} and whose value is @var{value}.} */
2497char *
2498make_message (char *name, char *value)
2499@{
2500 /* @r{Guess we need no more than 100 chars of space.} */
2501 int size = 100;
2502 char *buffer = (char *) xmalloc (size);
4cca6b86 2503 int nchars;
28f540f4
RM
2504@end group
2505@group
7ba73c63
UD
2506 if (buffer == NULL)
2507 return NULL;
2508
4cca6b86
UD
2509 /* @r{Try to print in the allocated space.} */
2510 nchars = snprintf (buffer, size, "value of %s is %s",
162ba701 2511 name, value);
4cca6b86
UD
2512@end group
2513@group
fe7bdd63 2514 if (nchars >= size)
28f540f4 2515 @{
4b8f94d3 2516 /* @r{Reallocate buffer now that we know
162ba701 2517 how much space is needed.} */
53631fa8
UD
2518 size = nchars + 1;
2519 buffer = (char *) xrealloc (buffer, size);
4cca6b86 2520
7ba73c63 2521 if (buffer != NULL)
162ba701
PIM
2522 /* @r{Try again.} */
2523 snprintf (buffer, size, "value of %s is %s",
2524 name, value);
28f540f4 2525 @}
4cca6b86
UD
2526 /* @r{The last call worked, return the string.} */
2527 return buffer;
28f540f4
RM
2528@}
2529@end group
2530@end smallexample
2531
2532In practice, it is often easier just to use @code{asprintf}, below.
fb971363 2533
1f77f049 2534@strong{Attention:} In versions of @theglibc{} prior to 2.1 the
5a74e68a
UD
2535return value is the number of characters stored, not including the
2536terminating null; unless there was not enough space in @var{s} to
2537store the result in which case @code{-1} is returned. This was
2538changed in order to comply with the @w{ISO C99} standard.
28f540f4
RM
2539@end deftypefun
2540
2541@node Dynamic Output
2542@subsection Dynamically Allocating Formatted Output
2543
2544The functions in this section do formatted output and place the results
2545in dynamically allocated memory.
2546
2547@comment stdio.h
2548@comment GNU
2549@deftypefun int asprintf (char **@var{ptr}, const char *@var{template}, @dots{})
171e9210 2550@safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{}}}
28f540f4
RM
2551This function is similar to @code{sprintf}, except that it dynamically
2552allocates a string (as with @code{malloc}; @pxref{Unconstrained
2553Allocation}) to hold the output, instead of putting the output in a
2554buffer you allocate in advance. The @var{ptr} argument should be the
3a4e0609
UD
2555address of a @code{char *} object, and a successful call to
2556@code{asprintf} stores a pointer to the newly allocated string at that
2557location.
28f540f4 2558
7ba73c63 2559The return value is the number of characters allocated for the buffer, or
cf822e3c 2560less than zero if an error occurred. Usually this means that the buffer
7ba73c63
UD
2561could not be allocated.
2562
28f540f4
RM
2563Here is how to use @code{asprintf} to get the same result as the
2564@code{snprintf} example, but more easily:
2565
2566@smallexample
2567/* @r{Construct a message describing the value of a variable}
2568 @r{whose name is @var{name} and whose value is @var{value}.} */
2569char *
2570make_message (char *name, char *value)
2571@{
2572 char *result;
7ba73c63
UD
2573 if (asprintf (&result, "value of %s is %s", name, value) < 0)
2574 return NULL;
28f540f4
RM
2575 return result;
2576@}
2577@end smallexample
2578@end deftypefun
2579
2580@comment stdio.h
2581@comment GNU
2582@deftypefun int obstack_printf (struct obstack *@var{obstack}, const char *@var{template}, @dots{})
171e9210 2583@safety{@prelim{}@mtsafe{@mtsrace{:obstack} @mtslocale{}}@asunsafe{@asucorrupt{} @ascuheap{}}@acunsafe{@acucorrupt{} @acsmem{}}}
28f540f4
RM
2584This function is similar to @code{asprintf}, except that it uses the
2585obstack @var{obstack} to allocate the space. @xref{Obstacks}.
2586
2587The characters are written onto the end of the current object.
2588To get at them, you must finish the object with @code{obstack_finish}
2589(@pxref{Growing Objects}).@refill
2590@end deftypefun
2591
2592@node Variable Arguments Output
2593@subsection Variable Arguments Output Functions
2594
2595The functions @code{vprintf} and friends are provided so that you can
2596define your own variadic @code{printf}-like functions that make use of
2597the same internals as the built-in formatted output functions.
2598
2599The most natural way to define such functions would be to use a language
2600construct to say, ``Call @code{printf} and pass this template plus all
2601of my arguments after the first five.'' But there is no way to do this
2602in C, and it would be hard to provide a way, since at the C language
2603level there is no way to tell how many arguments your function received.
2604
2605Since that method is impossible, we provide alternative functions, the
2606@code{vprintf} series, which lets you pass a @code{va_list} to describe
2607``all of my arguments after the first five.''
2608
19c3f208 2609When it is sufficient to define a macro rather than a real function,
28f540f4
RM
2610the GNU C compiler provides a way to do this much more easily with macros.
2611For example:
2612
2613@smallexample
838e5ffe 2614#define myprintf(a, b, c, d, e, rest...) \
162ba701 2615 printf (mytemplate , ## rest)
28f540f4
RM
2616@end smallexample
2617
2618@noindent
9229b83e
RM
2619@xref{Variadic Macros,,, cpp, The C preprocessor}, for details.
2620But this is limited to macros, and does not apply to real functions at all.
28f540f4
RM
2621
2622Before calling @code{vprintf} or the other functions listed in this
2623section, you @emph{must} call @code{va_start} (@pxref{Variadic
2624Functions}) to initialize a pointer to the variable arguments. Then you
2625can call @code{va_arg} to fetch the arguments that you want to handle
2626yourself. This advances the pointer past those arguments.
2627
2628Once your @code{va_list} pointer is pointing at the argument of your
2629choice, you are ready to call @code{vprintf}. That argument and all
2630subsequent arguments that were passed to your function are used by
2631@code{vprintf} along with the template that you specified separately.
2632
f546f87c
FW
2633@strong{Portability Note:} The value of the @code{va_list} pointer is
2634undetermined after the call to @code{vprintf}, so you must not use
2635@code{va_arg} after you call @code{vprintf}. Instead, you should call
2636@code{va_end} to retire the pointer from service. You can call
2637@code{va_start} again and begin fetching the arguments from the start of
2638the variable argument list. (Alternatively, you can use @code{va_copy}
2639to make a copy of the @code{va_list} pointer before calling
2640@code{vfprintf}.) Calling @code{vprintf} does not destroy the argument
2641list of your function, merely the particular pointer that you passed to
2642it.
28f540f4
RM
2643
2644Prototypes for these functions are declared in @file{stdio.h}.
2645@pindex stdio.h
2646
2647@comment stdio.h
f65fd747 2648@comment ISO
28f540f4 2649@deftypefun int vprintf (const char *@var{template}, va_list @var{ap})
171e9210 2650@safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@asucorrupt{} @ascuheap{}}@acunsafe{@acsmem{} @aculock{} @acucorrupt{}}}
28f540f4
RM
2651This function is similar to @code{printf} except that, instead of taking
2652a variable number of arguments directly, it takes an argument list
2653pointer @var{ap}.
2654@end deftypefun
2655
b5e73f56
UD
2656@comment wchar.h
2657@comment ISO
2658@deftypefun int vwprintf (const wchar_t *@var{template}, va_list @var{ap})
171e9210 2659@safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@asucorrupt{} @ascuheap{}}@acunsafe{@acsmem{} @aculock{} @acucorrupt{}}}
b5e73f56
UD
2660This function is similar to @code{wprintf} except that, instead of taking
2661a variable number of arguments directly, it takes an argument list
2662pointer @var{ap}.
2663@end deftypefun
2664
28f540f4 2665@comment stdio.h
f65fd747 2666@comment ISO
28f540f4 2667@deftypefun int vfprintf (FILE *@var{stream}, const char *@var{template}, va_list @var{ap})
171e9210
AO
2668@safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@asucorrupt{} @ascuheap{}}@acunsafe{@acsmem{} @aculock{} @acucorrupt{}}}
2669@c Although vfprintf sets up a cleanup region to release the lock on the
2670@c output stream, it doesn't use it to release args_value or string in
2671@c case of cancellation. This doesn't make it unsafe, but cancelling it
2672@c may leak memory. The unguarded use of __printf_function_table is
2673@c also of concern for all callers.
2674@c _itoa ok
2675@c _udiv_qrnnd_preinv ok
2676@c group_number ok
2677@c _i18n_number_rewrite
2678@c __wctrans ok
2679@c __towctrans @mtslocale
2680@c __wcrtomb ok? dup below
2681@c outdigit_value ok
2682@c outdigitwc_value ok
2683@c outchar ok
2684@c outstring ok
2685@c PAD ok
2686@c __printf_fp @mtslocale @ascuheap @acsmem
2687@c __printf_fphex @mtslocale
2688@c __readonly_area
2689@c [GNU/Linux] fopen, strtoul, free
2690@c __strerror_r ok if no translation, check otherwise
2691@c __btowc ? gconv-modules
2692@c __wcrtomb ok (not using internal state) gconv-modules
2693@c ARGCHECK
2694@c UNBUFFERED_P (tested before taking the stream lock)
2695@c buffered_vfprintf ok
2696@c __find_spec(wc|mb)
2697@c read_int
2698@c __libc_use_alloca
2699@c process_arg
2700@c process_string_arg
2701@c extend_alloca
2702@c __parse_one_spec(wc|mb)
2703@c *__printf_arginfo_table unguarded
2704@c __printf_va_arg_table-> unguarded
2705@c *__printf_function_table unguarded
2706@c done_add
2707@c printf_unknown
2708@c outchar
2709@c _itoa_word
28f540f4
RM
2710This is the equivalent of @code{fprintf} with the variable argument list
2711specified directly as for @code{vprintf}.
2712@end deftypefun
2713
b5e73f56
UD
2714@comment wchar.h
2715@comment ISO
2716@deftypefun int vfwprintf (FILE *@var{stream}, const wchar_t *@var{template}, va_list @var{ap})
171e9210 2717@safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@asucorrupt{} @ascuheap{}}@acunsafe{@acsmem{} @aculock{} @acucorrupt{}}}
b5e73f56
UD
2718This is the equivalent of @code{fwprintf} with the variable argument list
2719specified directly as for @code{vwprintf}.
2720@end deftypefun
2721
28f540f4 2722@comment stdio.h
f65fd747 2723@comment ISO
28f540f4 2724@deftypefun int vsprintf (char *@var{s}, const char *@var{template}, va_list @var{ap})
171e9210 2725@safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{}}}
28f540f4
RM
2726This is the equivalent of @code{sprintf} with the variable argument list
2727specified directly as for @code{vprintf}.
2728@end deftypefun
2729
b5e73f56
UD
2730@comment wchar.h
2731@comment GNU
c703cd7a 2732@deftypefun int vswprintf (wchar_t *@var{ws}, size_t @var{size}, const wchar_t *@var{template}, va_list @var{ap})
171e9210 2733@safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{}}}
b5e73f56
UD
2734This is the equivalent of @code{swprintf} with the variable argument list
2735specified directly as for @code{vwprintf}.
2736@end deftypefun
2737
28f540f4
RM
2738@comment stdio.h
2739@comment GNU
2740@deftypefun int vsnprintf (char *@var{s}, size_t @var{size}, const char *@var{template}, va_list @var{ap})
171e9210 2741@safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{}}}
28f540f4
RM
2742This is the equivalent of @code{snprintf} with the variable argument list
2743specified directly as for @code{vprintf}.
2744@end deftypefun
2745
2746@comment stdio.h
2747@comment GNU
2748@deftypefun int vasprintf (char **@var{ptr}, const char *@var{template}, va_list @var{ap})
171e9210 2749@safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{}}}
28f540f4
RM
2750The @code{vasprintf} function is the equivalent of @code{asprintf} with the
2751variable argument list specified directly as for @code{vprintf}.
2752@end deftypefun
2753
2754@comment stdio.h
2755@comment GNU
2756@deftypefun int obstack_vprintf (struct obstack *@var{obstack}, const char *@var{template}, va_list @var{ap})
171e9210
AO
2757@safety{@prelim{}@mtsafe{@mtsrace{:obstack} @mtslocale{}}@asunsafe{@asucorrupt{} @ascuheap{}}@acunsafe{@acucorrupt{} @acsmem{}}}
2758@c The obstack is not guarded by mutexes, it might be at an inconsistent
2759@c state within a signal handler, and it could be left at an
2760@c inconsistent state in case of cancellation.
28f540f4
RM
2761The @code{obstack_vprintf} function is the equivalent of
2762@code{obstack_printf} with the variable argument list specified directly
2763as for @code{vprintf}.@refill
2764@end deftypefun
2765
2766Here's an example showing how you might use @code{vfprintf}. This is a
2767function that prints error messages to the stream @code{stderr}, along
2768with a prefix indicating the name of the program
19c3f208 2769(@pxref{Error Messages}, for a description of
28f540f4
RM
2770@code{program_invocation_short_name}).
2771
2772@smallexample
2773@group
2774#include <stdio.h>
2775#include <stdarg.h>
2776
2777void
2778eprintf (const char *template, ...)
2779@{
2780 va_list ap;
2781 extern char *program_invocation_short_name;
2782
2783 fprintf (stderr, "%s: ", program_invocation_short_name);
4b8f94d3 2784 va_start (ap, template);
28f540f4
RM
2785 vfprintf (stderr, template, ap);
2786 va_end (ap);
2787@}
2788@end group
2789@end smallexample
2790
2791@noindent
2792You could call @code{eprintf} like this:
2793
2794@smallexample
2795eprintf ("file `%s' does not exist\n", filename);
2796@end smallexample
2797
2798In GNU C, there is a special construct you can use to let the compiler
2799know that a function uses a @code{printf}-style format string. Then it
2800can check the number and types of arguments in each call to the
2801function, and warn you when they do not match the format string.
2802For example, take this declaration of @code{eprintf}:
2803
2804@smallexample
2805void eprintf (const char *template, ...)
162ba701 2806 __attribute__ ((format (printf, 1, 2)));
28f540f4
RM
2807@end smallexample
2808
2809@noindent
2810This tells the compiler that @code{eprintf} uses a format string like
2811@code{printf} (as opposed to @code{scanf}; @pxref{Formatted Input});
2812the format string appears as the first argument;
2813and the arguments to satisfy the format begin with the second.
2814@xref{Function Attributes, , Declaring Attributes of Functions,
2815gcc.info, Using GNU CC}, for more information.
2816
2817@node Parsing a Template String
2818@subsection Parsing a Template String
2819@cindex parsing a template string
2820
2821You can use the function @code{parse_printf_format} to obtain
2822information about the number and types of arguments that are expected by
2823a given template string. This function permits interpreters that
2824provide interfaces to @code{printf} to avoid passing along invalid
2825arguments from the user's program, which could cause a crash.
2826
2827All the symbols described in this section are declared in the header
2828file @file{printf.h}.
2829
2830@comment printf.h
2831@comment GNU
2832@deftypefun size_t parse_printf_format (const char *@var{template}, size_t @var{n}, int *@var{argtypes})
171e9210 2833@safety{@prelim{}@mtsafe{@mtslocale{}}@assafe{}@acsafe{}}
28f540f4
RM
2834This function returns information about the number and types of
2835arguments expected by the @code{printf} template string @var{template}.
2836The information is stored in the array @var{argtypes}; each element of
2837this array describes one argument. This information is encoded using
2838the various @samp{PA_} macros, listed below.
2839
04b9968b
UD
2840The argument @var{n} specifies the number of elements in the array
2841@var{argtypes}. This is the maximum number of elements that
28f540f4
RM
2842@code{parse_printf_format} will try to write.
2843
2844@code{parse_printf_format} returns the total number of arguments required
2845by @var{template}. If this number is greater than @var{n}, then the
2846information returned describes only the first @var{n} arguments. If you
04b9968b 2847want information about additional arguments, allocate a bigger
28f540f4
RM
2848array and call @code{parse_printf_format} again.
2849@end deftypefun
2850
2851The argument types are encoded as a combination of a basic type and
2852modifier flag bits.
2853
2854@comment printf.h
2855@comment GNU
2856@deftypevr Macro int PA_FLAG_MASK
2857This macro is a bitmask for the type modifier flag bits. You can write
2858the expression @code{(argtypes[i] & PA_FLAG_MASK)} to extract just the
2859flag bits for an argument, or @code{(argtypes[i] & ~PA_FLAG_MASK)} to
2860extract just the basic type code.
2861@end deftypevr
2862
2863Here are symbolic constants that represent the basic types; they stand
2864for integer values.
2865
779ae82e 2866@vtable @code
28f540f4
RM
2867@comment printf.h
2868@comment GNU
2869@item PA_INT
28f540f4
RM
2870This specifies that the base type is @code{int}.
2871
2872@comment printf.h
2873@comment GNU
2874@item PA_CHAR
28f540f4
RM
2875This specifies that the base type is @code{int}, cast to @code{char}.
2876
2877@comment printf.h
2878@comment GNU
2879@item PA_STRING
28f540f4
RM
2880This specifies that the base type is @code{char *}, a null-terminated string.
2881
2882@comment printf.h
2883@comment GNU
2884@item PA_POINTER
28f540f4
RM
2885This specifies that the base type is @code{void *}, an arbitrary pointer.
2886
2887@comment printf.h
2888@comment GNU
2889@item PA_FLOAT
28f540f4
RM
2890This specifies that the base type is @code{float}.
2891
2892@comment printf.h
2893@comment GNU
2894@item PA_DOUBLE
28f540f4
RM
2895This specifies that the base type is @code{double}.
2896
2897@comment printf.h
2898@comment GNU
2899@item PA_LAST
28f540f4
RM
2900You can define additional base types for your own programs as offsets
2901from @code{PA_LAST}. For example, if you have data types @samp{foo}
2902and @samp{bar} with their own specialized @code{printf} conversions,
2903you could define encodings for these types as:
2904
2905@smallexample
2906#define PA_FOO PA_LAST
2907#define PA_BAR (PA_LAST + 1)
2908@end smallexample
779ae82e 2909@end vtable
28f540f4
RM
2910
2911Here are the flag bits that modify a basic type. They are combined with
2912the code for the basic type using inclusive-or.
2913
779ae82e 2914@vtable @code
28f540f4
RM
2915@comment printf.h
2916@comment GNU
2917@item PA_FLAG_PTR
28f540f4
RM
2918If this bit is set, it indicates that the encoded type is a pointer to
2919the base type, rather than an immediate value.
2920For example, @samp{PA_INT|PA_FLAG_PTR} represents the type @samp{int *}.
2921
2922@comment printf.h
2923@comment GNU
2924@item PA_FLAG_SHORT
28f540f4
RM
2925If this bit is set, it indicates that the base type is modified with
2926@code{short}. (This corresponds to the @samp{h} type modifier.)
2927
2928@comment printf.h
2929@comment GNU
2930@item PA_FLAG_LONG
28f540f4
RM
2931If this bit is set, it indicates that the base type is modified with
2932@code{long}. (This corresponds to the @samp{l} type modifier.)
2933
2934@comment printf.h
2935@comment GNU
2936@item PA_FLAG_LONG_LONG
28f540f4
RM
2937If this bit is set, it indicates that the base type is modified with
2938@code{long long}. (This corresponds to the @samp{L} type modifier.)
2939
2940@comment printf.h
2941@comment GNU
2942@item PA_FLAG_LONG_DOUBLE
28f540f4
RM
2943This is a synonym for @code{PA_FLAG_LONG_LONG}, used by convention with
2944a base type of @code{PA_DOUBLE} to indicate a type of @code{long double}.
779ae82e 2945@end vtable
28f540f4
RM
2946
2947@ifinfo
6d52618b 2948For an example of using these facilities, see @ref{Example of Parsing}.
28f540f4
RM
2949@end ifinfo
2950
2951@node Example of Parsing
2952@subsection Example of Parsing a Template String
2953
2954Here is an example of decoding argument types for a format string. We
2955assume this is part of an interpreter which contains arguments of type
2956@code{NUMBER}, @code{CHAR}, @code{STRING} and @code{STRUCTURE} (and
2957perhaps others which are not valid here).
2958
2959@smallexample
2960/* @r{Test whether the @var{nargs} specified objects}
2961 @r{in the vector @var{args} are valid}
2962 @r{for the format string @var{format}:}
2963 @r{if so, return 1.}
2964 @r{If not, return 0 after printing an error message.} */
2965
2966int
2967validate_args (char *format, int nargs, OBJECT *args)
2968@{
2969 int *argtypes;
2970 int nwanted;
2971
2972 /* @r{Get the information about the arguments.}
2973 @r{Each conversion specification must be at least two characters}
2974 @r{long, so there cannot be more specifications than half the}
2975 @r{length of the string.} */
2976
2977 argtypes = (int *) alloca (strlen (format) / 2 * sizeof (int));
2978 nwanted = parse_printf_format (string, nelts, argtypes);
2979
2980 /* @r{Check the number of arguments.} */
2981 if (nwanted > nargs)
2982 @{
2983 error ("too few arguments (at least %d required)", nwanted);
2984 return 0;
2985 @}
19c3f208 2986
28f540f4
RM
2987 /* @r{Check the C type wanted for each argument}
2988 @r{and see if the object given is suitable.} */
2989 for (i = 0; i < nwanted; i++)
2990 @{
2991 int wanted;
2992
2993 if (argtypes[i] & PA_FLAG_PTR)
162ba701 2994 wanted = STRUCTURE;
28f540f4 2995 else
162ba701
PIM
2996 switch (argtypes[i] & ~PA_FLAG_MASK)
2997 @{
2998 case PA_INT:
2999 case PA_FLOAT:
3000 case PA_DOUBLE:
3001 wanted = NUMBER;
3002 break;
3003 case PA_CHAR:
3004 wanted = CHAR;
3005 break;
3006 case PA_STRING:
3007 wanted = STRING;
3008 break;
3009 case PA_POINTER:
3010 wanted = STRUCTURE;
3011 break;
3012 @}
28f540f4 3013 if (TYPE (args[i]) != wanted)
162ba701
PIM
3014 @{
3015 error ("type mismatch for arg number %d", i);
3016 return 0;
3017 @}
28f540f4
RM
3018 @}
3019 return 1;
3020@}
3021@end smallexample
3022
3023@node Customizing Printf
3024@section Customizing @code{printf}
3025@cindex customizing @code{printf}
3026@cindex defining new @code{printf} conversions
3027@cindex extending @code{printf}
3028
1f77f049 3029@Theglibc{} lets you define your own custom conversion specifiers
28f540f4
RM
3030for @code{printf} template strings, to teach @code{printf} clever ways
3031to print the important data structures of your program.
3032
3033The way you do this is by registering the conversion with the function
3034@code{register_printf_function}; see @ref{Registering New Conversions}.
3035One of the arguments you pass to this function is a pointer to a handler
3036function that produces the actual output; see @ref{Defining the Output
3037Handler}, for information on how to write this function.
3038
3039You can also install a function that just returns information about the
3040number and type of arguments expected by the conversion specifier.
3041@xref{Parsing a Template String}, for information about this.
3042
3043The facilities of this section are declared in the header file
3044@file{printf.h}.
3045
3046@menu
19c3f208 3047* Registering New Conversions:: Using @code{register_printf_function}
162ba701 3048 to register a new output conversion.
28f540f4 3049* Conversion Specifier Options:: The handler must be able to get
162ba701
PIM
3050 the options specified in the
3051 template when it is called.
28f540f4 3052* Defining the Output Handler:: Defining the handler and arginfo
162ba701
PIM
3053 functions that are passed as arguments
3054 to @code{register_printf_function}.
28f540f4 3055* Printf Extension Example:: How to define a @code{printf}
162ba701 3056 handler function.
29bb8719 3057* Predefined Printf Handlers:: Predefined @code{printf} handlers.
28f540f4
RM
3058@end menu
3059
3060@strong{Portability Note:} The ability to extend the syntax of
f65fd747 3061@code{printf} template strings is a GNU extension. ISO standard C has
28f540f4
RM
3062nothing similar.
3063
3064@node Registering New Conversions
3065@subsection Registering New Conversions
3066
3067The function to register a new output conversion is
3068@code{register_printf_function}, declared in @file{printf.h}.
3069@pindex printf.h
3070
3071@comment printf.h
3072@comment GNU
3073@deftypefun int register_printf_function (int @var{spec}, printf_function @var{handler-function}, printf_arginfo_function @var{arginfo-function})
171e9210
AO
3074@safety{@prelim{}@mtunsafe{@mtasuconst{:printfext}}@asunsafe{@ascuheap{} @asulock{}}@acunsafe{@acsmem{} @aculock{}}}
3075@c This function is guarded by the global non-recursive libc lock, but
3076@c users of the variables it sets aren't, and those should be MT-Safe,
3077@c so we're ruling out the use of this extension with threads. Calling
3078@c it from a signal handler may self-deadlock, and cancellation may
3079@c leave the lock held, besides leaking allocated memory.
28f540f4 3080This function defines the conversion specifier character @var{spec}.
42be70d4 3081Thus, if @var{spec} is @code{'Y'}, it defines the conversion @samp{%Y}.
28f540f4
RM
3082You can redefine the built-in conversions like @samp{%s}, but flag
3083characters like @samp{#} and type modifiers like @samp{l} can never be
3084used as conversions; calling @code{register_printf_function} for those
42be70d4
UD
3085characters has no effect. It is advisable not to use lowercase letters,
3086since the ISO C standard warns that additional lowercase letters may be
3087standardized in future editions of the standard.
28f540f4
RM
3088
3089The @var{handler-function} is the function called by @code{printf} and
3090friends when this conversion appears in a template string.
3091@xref{Defining the Output Handler}, for information about how to define
3092a function to pass as this argument. If you specify a null pointer, any
3093existing handler function for @var{spec} is removed.
3094
3095The @var{arginfo-function} is the function called by
3096@code{parse_printf_format} when this conversion appears in a
3097template string. @xref{Parsing a Template String}, for information
3098about this.
3099
54d79e99
UD
3100@c The following is not true anymore. The `parse_printf_format' function
3101@c is now also called from `vfprintf' via `parse_one_spec'.
3102@c --drepper@gnu, 1996/11/14
3103@c
3104@c Normally, you install both functions for a conversion at the same time,
3105@c but if you are never going to call @code{parse_printf_format}, you do
3106@c not need to define an arginfo function.
3107
1f77f049 3108@strong{Attention:} In @theglibc{} versions before 2.0 the
54d79e99 3109@var{arginfo-function} function did not need to be installed unless
04b9968b 3110the user used the @code{parse_printf_format} function. This has changed.
54d79e99
UD
3111Now a call to any of the @code{printf} functions will call this
3112function when this format specifier appears in the format string.
28f540f4
RM
3113
3114The return value is @code{0} on success, and @code{-1} on failure
3115(which occurs if @var{spec} is out of range).
3116
3117You can redefine the standard output conversions, but this is probably
3118not a good idea because of the potential for confusion. Library routines
3119written by other people could break if you do this.
3120@end deftypefun
3121
3122@node Conversion Specifier Options
3123@subsection Conversion Specifier Options
3124
40deae08
RM
3125If you define a meaning for @samp{%A}, what if the template contains
3126@samp{%+23A} or @samp{%-#A}? To implement a sensible meaning for these,
28f540f4
RM
3127the handler when called needs to be able to get the options specified in
3128the template.
3129
838e5ffe
UD
3130Both the @var{handler-function} and @var{arginfo-function} accept an
3131argument that points to a @code{struct printf_info}, which contains
3132information about the options appearing in an instance of the conversion
3133specifier. This data type is declared in the header file
3134@file{printf.h}.
28f540f4
RM
3135@pindex printf.h
3136
3137@comment printf.h
3138@comment GNU
3139@deftp {Type} {struct printf_info}
3140This structure is used to pass information about the options appearing
3141in an instance of a conversion specifier in a @code{printf} template
3142string to the handler and arginfo functions for that specifier. It
3143contains the following members:
3144
3145@table @code
3146@item int prec
3147This is the precision specified. The value is @code{-1} if no precision
3148was specified. If the precision was given as @samp{*}, the
3149@code{printf_info} structure passed to the handler function contains the
3150actual value retrieved from the argument list. But the structure passed
3151to the arginfo function contains a value of @code{INT_MIN}, since the
3152actual value is not known.
3153
3154@item int width
3155This is the minimum field width specified. The value is @code{0} if no
3156width was specified. If the field width was given as @samp{*}, the
3157@code{printf_info} structure passed to the handler function contains the
3158actual value retrieved from the argument list. But the structure passed
3159to the arginfo function contains a value of @code{INT_MIN}, since the
3160actual value is not known.
3161
54d79e99 3162@item wchar_t spec
28f540f4
RM
3163This is the conversion specifier character specified. It's stored in
3164the structure so that you can register the same handler function for
3165multiple characters, but still have a way to tell them apart when the
3166handler function is called.
3167
3168@item unsigned int is_long_double
3169This is a boolean that is true if the @samp{L}, @samp{ll}, or @samp{q}
3170type modifier was specified. For integer conversions, this indicates
3171@code{long long int}, as opposed to @code{long double} for floating
3172point conversions.
3173
d64b6ad0
UD
3174@item unsigned int is_char
3175This is a boolean that is true if the @samp{hh} type modifier was specified.
3176
28f540f4
RM
3177@item unsigned int is_short
3178This is a boolean that is true if the @samp{h} type modifier was specified.
3179
3180@item unsigned int is_long
3181This is a boolean that is true if the @samp{l} type modifier was specified.
3182
3183@item unsigned int alt
3184This is a boolean that is true if the @samp{#} flag was specified.
3185
3186@item unsigned int space
3187This is a boolean that is true if the @samp{ } flag was specified.
3188
3189@item unsigned int left
3190This is a boolean that is true if the @samp{-} flag was specified.
3191
3192@item unsigned int showsign
3193This is a boolean that is true if the @samp{+} flag was specified.
3194
3195@item unsigned int group
3196This is a boolean that is true if the @samp{'} flag was specified.
3197
54d79e99
UD
3198@item unsigned int extra
3199This flag has a special meaning depending on the context. It could
3200be used freely by the user-defined handlers but when called from
3201the @code{printf} function this variable always contains the value
3202@code{0}.
3203
d64b6ad0
UD
3204@item unsigned int wide
3205This flag is set if the stream is wide oriented.
3206
54d79e99 3207@item wchar_t pad
28f540f4
RM
3208This is the character to use for padding the output to the minimum field
3209width. The value is @code{'0'} if the @samp{0} flag was specified, and
3210@code{' '} otherwise.
3211@end table
3212@end deftp
3213
3214
3215@node Defining the Output Handler
3216@subsection Defining the Output Handler
3217
3218Now let's look at how to define the handler and arginfo functions
3219which are passed as arguments to @code{register_printf_function}.
3220
1f77f049 3221@strong{Compatibility Note:} The interface changed in @theglibc{}
54d79e99
UD
3222version 2.0. Previously the third argument was of type
3223@code{va_list *}.
3224
28f540f4
RM
3225You should define your handler functions with a prototype like:
3226
3227@smallexample
3228int @var{function} (FILE *stream, const struct printf_info *info,
162ba701 3229 const void *const *args)
28f540f4
RM
3230@end smallexample
3231
54d79e99 3232The @var{stream} argument passed to the handler function is the stream to
28f540f4
RM
3233which it should write output.
3234
54d79e99 3235The @var{info} argument is a pointer to a structure that contains
28f540f4
RM
3236information about the various options that were included with the
3237conversion in the template string. You should not modify this structure
3238inside your handler function. @xref{Conversion Specifier Options}, for
3239a description of this data structure.
3240
54d79e99
UD
3241@c The following changes some time back. --drepper@gnu, 1996/11/14
3242@c
3243@c The @code{ap_pointer} argument is used to pass the tail of the variable
3244@c argument list containing the values to be printed to your handler.
3245@c Unlike most other functions that can be passed an explicit variable
3246@c argument list, this is a @emph{pointer} to a @code{va_list}, rather than
3247@c the @code{va_list} itself. Thus, you should fetch arguments by
3248@c means of @code{va_arg (*ap_pointer, @var{type})}.
3249@c
3250@c (Passing a pointer here allows the function that calls your handler
3251@c function to update its own @code{va_list} variable to account for the
3252@c arguments that your handler processes. @xref{Variadic Functions}.)
3253
3254The @var{args} is a vector of pointers to the arguments data.
04b9968b 3255The number of arguments was determined by calling the argument
54d79e99 3256information function provided by the user.
28f540f4
RM
3257
3258Your handler function should return a value just like @code{printf}
3259does: it should return the number of characters it has written, or a
3260negative value to indicate an error.
3261
3262@comment printf.h
3263@comment GNU
3264@deftp {Data Type} printf_function
3265This is the data type that a handler function should have.
3266@end deftp
3267
3268If you are going to use @w{@code{parse_printf_format}} in your
54d79e99 3269application, you must also define a function to pass as the
28f540f4 3270@var{arginfo-function} argument for each new conversion you install with
19c3f208 3271@code{register_printf_function}.
28f540f4 3272
54d79e99 3273You have to define these functions with a prototype like:
28f540f4
RM
3274
3275@smallexample
3276int @var{function} (const struct printf_info *info,
162ba701 3277 size_t n, int *argtypes)
28f540f4
RM
3278@end smallexample
3279
3280The return value from the function should be the number of arguments the
3281conversion expects. The function should also fill in no more than
3282@var{n} elements of the @var{argtypes} array with information about the
3283types of each of these arguments. This information is encoded using the
3284various @samp{PA_} macros. (You will notice that this is the same
3285calling convention @code{parse_printf_format} itself uses.)
3286
3287@comment printf.h
3288@comment GNU
3289@deftp {Data Type} printf_arginfo_function
3290This type is used to describe functions that return information about
3291the number and type of arguments used by a conversion specifier.
3292@end deftp
3293
3294@node Printf Extension Example
3295@subsection @code{printf} Extension Example
3296
3297Here is an example showing how to define a @code{printf} handler function.
19c3f208 3298This program defines a data structure called a @code{Widget} and
28f540f4
RM
3299defines the @samp{%W} conversion to print information about @w{@code{Widget *}}
3300arguments, including the pointer value and the name stored in the data
3301structure. The @samp{%W} conversion supports the minimum field width and
3302left-justification options, but ignores everything else.
3303
3304@smallexample
3305@include rprintf.c.texi
3306@end smallexample
3307
3308The output produced by this program looks like:
3309
3310@smallexample
3311|<Widget 0xffeffb7c: mywidget>|
3312| <Widget 0xffeffb7c: mywidget>|
3313|<Widget 0xffeffb7c: mywidget> |
3314@end smallexample
3315
29bb8719
UD
3316@node Predefined Printf Handlers
3317@subsection Predefined @code{printf} Handlers
3318
1f77f049 3319@Theglibc{} also contains a concrete and useful application of the
29bb8719
UD
3320@code{printf} handler extension. There are two functions available
3321which implement a special way to print floating-point numbers.
3322
3323@comment printf.h
3324@comment GNU
3325@deftypefun int printf_size (FILE *@var{fp}, const struct printf_info *@var{info}, const void *const *@var{args})
171e9210
AO
3326@safety{@prelim{}@mtsafe{@mtsrace{:fp} @mtslocale{}}@asunsafe{@asucorrupt{} @ascuheap{}}@acunsafe{@acsmem{} @acucorrupt{}}}
3327@c This is meant to be called by vfprintf, that should hold the lock on
3328@c the stream, but if this function is called directly, output will be
3329@c racy, besides the uses of the global locale object while other
3330@c threads may be changing it and the possbility of leaving the stream
3331@c object in an inconsistent state in case of cancellation.
29bb8719
UD
3332Print a given floating point number as for the format @code{%f} except
3333that there is a postfix character indicating the divisor for the
3334number to make this less than 1000. There are two possible divisors:
04b9968b 3335powers of 1024 or powers of 1000. Which one is used depends on the
29bb8719
UD
3336format character specified while registered this handler. If the
3337character is of lower case, 1024 is used. For upper case characters,
33381000 is used.
3339
3340The postfix tag corresponds to bytes, kilobytes, megabytes, gigabytes,
3341etc. The full table is:
3342
779ae82e 3343@ifinfo
029264d1 3344@multitable {' '} {2^10 (1024)} {zetta} {Upper} {10^24 (1000)}
29bb8719
UD
3345@item low @tab Multiplier @tab From @tab Upper @tab Multiplier
3346@item ' ' @tab 1 @tab @tab ' ' @tab 1
3347@item k @tab 2^10 (1024) @tab kilo @tab K @tab 10^3 (1000)
3348@item m @tab 2^20 @tab mega @tab M @tab 10^6
3349@item g @tab 2^30 @tab giga @tab G @tab 10^9
3350@item t @tab 2^40 @tab tera @tab T @tab 10^12
3351@item p @tab 2^50 @tab peta @tab P @tab 10^15
3352@item e @tab 2^60 @tab exa @tab E @tab 10^18
3353@item z @tab 2^70 @tab zetta @tab Z @tab 10^21
3354@item y @tab 2^80 @tab yotta @tab Y @tab 10^24
3355@end multitable
779ae82e
UD
3356@end ifinfo
3357@iftex
3358@tex
3359\hbox to\hsize{\hfil\vbox{\offinterlineskip
3360\hrule
3361\halign{\strut#& \vrule#\tabskip=1em plus2em& {\tt#}\hfil& \vrule#& #\hfil& \vrule#& #\hfil& \vrule#& {\tt#}\hfil& \vrule#& #\hfil& \vrule#\tabskip=0pt\cr
3362\noalign{\hrule}
3363\omit&height2pt&\omit&&\omit&&\omit&&\omit&&\omit&\cr
3364&& \omit low && Multiplier && From && \omit Upper && Multiplier &\cr
3365\omit&height2pt&\omit&&\omit&&\omit&&\omit&&\omit&\cr
3366\noalign{\hrule}
3367&& {\tt\char32} && 1 && && {\tt\char32} && 1 &\cr
3368&& k && $2^{10} = 1024$ && kilo && K && $10^3 = 1000$ &\cr
3369&& m && $2^{20}$ && mega && M && $10^6$ &\cr
3370&& g && $2^{30}$ && giga && G && $10^9$ &\cr
3371&& t && $2^{40}$ && tera && T && $10^{12}$ &\cr
3372&& p && $2^{50}$ && peta && P && $10^{15}$ &\cr
3373&& e && $2^{60}$ && exa && E && $10^{18}$ &\cr
3374&& z && $2^{70}$ && zetta && Z && $10^{21}$ &\cr
3375&& y && $2^{80}$ && yotta && Y && $10^{24}$ &\cr
3376\noalign{\hrule}}}\hfil}
3377@end tex
3378@end iftex
29bb8719
UD
3379
3380The default precision is 3, i.e., 1024 is printed with a lower-case
3381format character as if it were @code{%.3fk} and will yield @code{1.000k}.
3382@end deftypefun
3383
3384Due to the requirements of @code{register_printf_function} we must also
04b9968b 3385provide the function which returns information about the arguments.
29bb8719
UD
3386
3387@comment printf.h
3388@comment GNU
3389@deftypefun int printf_size_info (const struct printf_info *@var{info}, size_t @var{n}, int *@var{argtypes})
171e9210 3390@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
29bb8719
UD
3391This function will return in @var{argtypes} the information about the
3392used parameters in the way the @code{vfprintf} implementation expects
3393it. The format always takes one argument.
3394@end deftypefun
3395
3396To use these functions both functions must be registered with a call like
3397
3398@smallexample
3399register_printf_function ('B', printf_size, printf_size_info);
3400@end smallexample
3401
3402Here we register the functions to print numbers as powers of 1000 since
f2ea0f5b 3403the format character @code{'B'} is an upper-case character. If we
29bb8719
UD
3404would additionally use @code{'b'} in a line like
3405
3406@smallexample
3407register_printf_function ('b', printf_size, printf_size_info);
3408@end smallexample
3409
3410@noindent
04b9968b
UD
3411we could also print using a power of 1024. Please note that all that is
3412different in these two lines is the format specifier. The
3413@code{printf_size} function knows about the difference between lower and upper
29bb8719
UD
3414case format specifiers.
3415
3416The use of @code{'B'} and @code{'b'} is no coincidence. Rather it is
3417the preferred way to use this functionality since it is available on
04b9968b 3418some other systems which also use format specifiers.
29bb8719 3419
28f540f4
RM
3420@node Formatted Input
3421@section Formatted Input
3422
3423@cindex formatted input from a stream
3424@cindex reading from a stream, formatted
3425@cindex format string, for @code{scanf}
3426@cindex template, for @code{scanf}
3427The functions described in this section (@code{scanf} and related
3428functions) provide facilities for formatted input analogous to the
3429formatted output facilities. These functions provide a mechanism for
3430reading arbitrary values under the control of a @dfn{format string} or
3431@dfn{template string}.
3432
3433@menu
3434* Formatted Input Basics:: Some basics to get you started.
3435* Input Conversion Syntax:: Syntax of conversion specifications.
3436* Table of Input Conversions:: Summary of input conversions and what they do.
3437* Numeric Input Conversions:: Details of conversions for reading numbers.
3438* String Input Conversions:: Details of conversions for reading strings.
3439* Dynamic String Input:: String conversions that @code{malloc} the buffer.
3440* Other Input Conversions:: Details of miscellaneous other conversions.
3441* Formatted Input Functions:: Descriptions of the actual functions.
3442* Variable Arguments Input:: @code{vscanf} and friends.
3443@end menu
3444
3445@node Formatted Input Basics
3446@subsection Formatted Input Basics
3447
3448Calls to @code{scanf} are superficially similar to calls to
3449@code{printf} in that arbitrary arguments are read under the control of
3450a template string. While the syntax of the conversion specifications in
3451the template is very similar to that for @code{printf}, the
3452interpretation of the template is oriented more towards free-format
3453input and simple pattern matching, rather than fixed-field formatting.
3454For example, most @code{scanf} conversions skip over any amount of
3455``white space'' (including spaces, tabs, and newlines) in the input
3456file, and there is no concept of precision for the numeric input
3457conversions as there is for the corresponding output conversions.
3458Ordinarily, non-whitespace characters in the template are expected to
3459match characters in the input stream exactly, but a matching failure is
3460distinct from an input error on the stream.
3461@cindex conversion specifications (@code{scanf})
3462
3463Another area of difference between @code{scanf} and @code{printf} is
3464that you must remember to supply pointers rather than immediate values
3465as the optional arguments to @code{scanf}; the values that are read are
3466stored in the objects that the pointers point to. Even experienced
3467programmers tend to forget this occasionally, so if your program is
3468getting strange errors that seem to be related to @code{scanf}, you
3469might want to double-check this.
3470
3471When a @dfn{matching failure} occurs, @code{scanf} returns immediately,
3472leaving the first non-matching character as the next character to be
3473read from the stream. The normal return value from @code{scanf} is the
3474number of values that were assigned, so you can use this to determine if
3475a matching error happened before all the expected values were read.
3476@cindex matching failure, in @code{scanf}
3477
3478The @code{scanf} function is typically used for things like reading in
3479the contents of tables. For example, here is a function that uses
3480@code{scanf} to initialize an array of @code{double}:
3481
3482@smallexample
3483void
3484readarray (double *array, int n)
3485@{
3486 int i;
3487 for (i=0; i<n; i++)
3488 if (scanf (" %lf", &(array[i])) != 1)
3489 invalid_input_error ();
3490@}
3491@end smallexample
3492
3493The formatted input functions are not used as frequently as the
3494formatted output functions. Partly, this is because it takes some care
3495to use them properly. Another reason is that it is difficult to recover
3496from a matching error.
3497
3498If you are trying to read input that doesn't match a single, fixed
3499pattern, you may be better off using a tool such as Flex to generate a
3500lexical scanner, or Bison to generate a parser, rather than using
9f508673
UD
3501@code{scanf}. For more information about these tools, see @ref{Top, , ,
3502flex.info, Flex: The Lexical Scanner Generator}, and @ref{Top, , ,
28f540f4
RM
3503bison.info, The Bison Reference Manual}.
3504
3505@node Input Conversion Syntax
3506@subsection Input Conversion Syntax
3507
3508A @code{scanf} template string is a string that contains ordinary
3509multibyte characters interspersed with conversion specifications that
3510start with @samp{%}.
3511
3512Any whitespace character (as defined by the @code{isspace} function;
3513@pxref{Classification of Characters}) in the template causes any number
3514of whitespace characters in the input stream to be read and discarded.
3515The whitespace characters that are matched need not be exactly the same
3516whitespace characters that appear in the template string. For example,
3517write @samp{ , } in the template to recognize a comma with optional
3518whitespace before and after.
3519
3520Other characters in the template string that are not part of conversion
3521specifications must match characters in the input stream exactly; if
3522this is not the case, a matching failure occurs.
3523
3524The conversion specifications in a @code{scanf} template string
3525have the general form:
3526
3527@smallexample
3528% @var{flags} @var{width} @var{type} @var{conversion}
3529@end smallexample
3530
3531In more detail, an input conversion specification consists of an initial
3532@samp{%} character followed in sequence by:
3533
3534@itemize @bullet
3535@item
3536An optional @dfn{flag character} @samp{*}, which says to ignore the text
3537read for this specification. When @code{scanf} finds a conversion
3538specification that uses this flag, it reads input as directed by the
3539rest of the conversion specification, but it discards this input, does
3540not use a pointer argument, and does not increment the count of
3541successful assignments.
3542@cindex flag character (@code{scanf})
3543
3544@item
3545An optional flag character @samp{a} (valid with string conversions only)
3546which requests allocation of a buffer long enough to store the string in.
3547(This is a GNU extension.)
3548@xref{Dynamic String Input}.
3549
3550@item
3551An optional decimal integer that specifies the @dfn{maximum field
3552width}. Reading of characters from the input stream stops either when
3553this maximum is reached or when a non-matching character is found,
3554whichever happens first. Most conversions discard initial whitespace
3555characters (those that don't are explicitly documented), and these
3556discarded characters don't count towards the maximum field width.
3557String input conversions store a null character to mark the end of the
3558input; the maximum field width does not include this terminator.
3559@cindex maximum field width (@code{scanf})
3560
3561@item
3562An optional @dfn{type modifier character}. For example, you can
3563specify a type modifier of @samp{l} with integer conversions such as
3564@samp{%d} to specify that the argument is a pointer to a @code{long int}
3565rather than a pointer to an @code{int}.
3566@cindex type modifier character (@code{scanf})
3567
3568@item
3569A character that specifies the conversion to be applied.
3570@end itemize
3571
19c3f208 3572The exact options that are permitted and how they are interpreted vary
28f540f4
RM
3573between the different conversion specifiers. See the descriptions of the
3574individual conversions for information about the particular options that
3575they allow.
3576
3577With the @samp{-Wformat} option, the GNU C compiler checks calls to
3578@code{scanf} and related functions. It examines the format string and
3579verifies that the correct number and types of arguments are supplied.
3580There is also a GNU C syntax to tell the compiler that a function you
19c3f208 3581write uses a @code{scanf}-style format string.
28f540f4
RM
3582@xref{Function Attributes, , Declaring Attributes of Functions,
3583gcc.info, Using GNU CC}, for more information.
3584
3585@node Table of Input Conversions
3586@subsection Table of Input Conversions
3587@cindex input conversions, for @code{scanf}
3588
3589Here is a table that summarizes the various conversion specifications:
3590
3591@table @asis
3592@item @samp{%d}
3593Matches an optionally signed integer written in decimal. @xref{Numeric
3594Input Conversions}.
3595
3596@item @samp{%i}
3597Matches an optionally signed integer in any of the formats that the C
3598language defines for specifying an integer constant. @xref{Numeric
3599Input Conversions}.
3600
3601@item @samp{%o}
3602Matches an unsigned integer written in octal radix.
3603@xref{Numeric Input Conversions}.
3604
3605@item @samp{%u}
3606Matches an unsigned integer written in decimal radix.
3607@xref{Numeric Input Conversions}.
3608
3609@item @samp{%x}, @samp{%X}
3610Matches an unsigned integer written in hexadecimal radix.
3611@xref{Numeric Input Conversions}.
3612
3613@item @samp{%e}, @samp{%f}, @samp{%g}, @samp{%E}, @samp{%G}
3614Matches an optionally signed floating-point number. @xref{Numeric Input
3615Conversions}.
3616
3617@item @samp{%s}
b5e73f56 3618
28f540f4 3619Matches a string containing only non-whitespace characters.
b5e73f56
UD
3620@xref{String Input Conversions}. The presence of the @samp{l} modifier
3621determines whether the output is stored as a wide character string or a
3622multibyte string. If @samp{%s} is used in a wide character function the
3623string is converted as with multiple calls to @code{wcrtomb} into a
3624multibyte string. This means that the buffer must provide room for
3625@code{MB_CUR_MAX} bytes for each wide character read. In case
3626@samp{%ls} is used in a multibyte function the result is converted into
3627wide characters as with multiple calls of @code{mbrtowc} before being
3628stored in the user provided buffer.
3629
3630@item @samp{%S}
3631This is an alias for @samp{%ls} which is supported for compatibility
3632with the Unix standard.
28f540f4
RM
3633
3634@item @samp{%[}
3635Matches a string of characters that belong to a specified set.
b5e73f56
UD
3636@xref{String Input Conversions}. The presence of the @samp{l} modifier
3637determines whether the output is stored as a wide character string or a
3638multibyte string. If @samp{%[} is used in a wide character function the
3639string is converted as with multiple calls to @code{wcrtomb} into a
3640multibyte string. This means that the buffer must provide room for
3641@code{MB_CUR_MAX} bytes for each wide character read. In case
3642@samp{%l[} is used in a multibyte function the result is converted into
3643wide characters as with multiple calls of @code{mbrtowc} before being
3644stored in the user provided buffer.
28f540f4
RM
3645
3646@item @samp{%c}
3647Matches a string of one or more characters; the number of characters
3648read is controlled by the maximum field width given for the conversion.
3649@xref{String Input Conversions}.
3650
c703cd7a 3651If @samp{%c} is used in a wide stream function the read value is
b5e73f56
UD
3652converted from a wide character to the corresponding multibyte character
3653before storing it. Note that this conversion can produce more than one
c703cd7a 3654byte of output and therefore the provided buffer must be large enough for up
b5e73f56
UD
3655to @code{MB_CUR_MAX} bytes for each character. If @samp{%lc} is used in
3656a multibyte function the input is treated as a multibyte sequence (and
3657not bytes) and the result is converted as with calls to @code{mbrtowc}.
3658
3659@item @samp{%C}
3660This is an alias for @samp{%lc} which is supported for compatibility
3661with the Unix standard.
3662
28f540f4
RM
3663@item @samp{%p}
3664Matches a pointer value in the same implementation-defined format used
3665by the @samp{%p} output conversion for @code{printf}. @xref{Other Input
3666Conversions}.
3667
3668@item @samp{%n}
3669This conversion doesn't read any characters; it records the number of
3670characters read so far by this call. @xref{Other Input Conversions}.
3671
3672@item @samp{%%}
3673This matches a literal @samp{%} character in the input stream. No
3674corresponding argument is used. @xref{Other Input Conversions}.
3675@end table
3676
3677If the syntax of a conversion specification is invalid, the behavior is
3678undefined. If there aren't enough function arguments provided to supply
3679addresses for all the conversion specifications in the template strings
3680that perform assignments, or if the arguments are not of the correct
3681types, the behavior is also undefined. On the other hand, extra
3682arguments are simply ignored.
3683
3684@node Numeric Input Conversions
3685@subsection Numeric Input Conversions
3686
3687This section describes the @code{scanf} conversions for reading numeric
3688values.
3689
3690The @samp{%d} conversion matches an optionally signed integer in decimal
3691radix. The syntax that is recognized is the same as that for the
3692@code{strtol} function (@pxref{Parsing of Integers}) with the value
3693@code{10} for the @var{base} argument.
3694
3695The @samp{%i} conversion matches an optionally signed integer in any of
3696the formats that the C language defines for specifying an integer
3697constant. The syntax that is recognized is the same as that for the
3698@code{strtol} function (@pxref{Parsing of Integers}) with the value
3699@code{0} for the @var{base} argument. (You can print integers in this
3700syntax with @code{printf} by using the @samp{#} flag character with the
3701@samp{%x}, @samp{%o}, or @samp{%d} conversion. @xref{Integer Conversions}.)
3702
3703For example, any of the strings @samp{10}, @samp{0xa}, or @samp{012}
3704could be read in as integers under the @samp{%i} conversion. Each of
3705these specifies a number with decimal value @code{10}.
3706
3707The @samp{%o}, @samp{%u}, and @samp{%x} conversions match unsigned
3708integers in octal, decimal, and hexadecimal radices, respectively. The
3709syntax that is recognized is the same as that for the @code{strtoul}
3710function (@pxref{Parsing of Integers}) with the appropriate value
3711(@code{8}, @code{10}, or @code{16}) for the @var{base} argument.
3712
3713The @samp{%X} conversion is identical to the @samp{%x} conversion. They
3714both permit either uppercase or lowercase letters to be used as digits.
3715
3716The default type of the corresponding argument for the @code{%d} and
3717@code{%i} conversions is @code{int *}, and @code{unsigned int *} for the
3718other integer conversions. You can use the following type modifiers to
3719specify other sizes of integer:
3720
3721@table @samp
cc3fa755
UD
3722@item hh
3723Specifies that the argument is a @code{signed char *} or @code{unsigned
3724char *}.
3725
ec751a23 3726This modifier was introduced in @w{ISO C99}.
e852e889 3727
28f540f4
RM
3728@item h
3729Specifies that the argument is a @code{short int *} or @code{unsigned
3730short int *}.
3731
e852e889
UD
3732@item j
3733Specifies that the argument is a @code{intmax_t *} or @code{uintmax_t *}.
3734
ec751a23 3735This modifier was introduced in @w{ISO C99}.
e852e889 3736
28f540f4
RM
3737@item l
3738Specifies that the argument is a @code{long int *} or @code{unsigned
3739long int *}. Two @samp{l} characters is like the @samp{L} modifier, below.
3740
b5e73f56
UD
3741If used with @samp{%c} or @samp{%s} the corresponding parameter is
3742considered as a pointer to a wide character or wide character string
3743respectively. This use of @samp{l} was introduced in @w{Amendment 1} to
3744@w{ISO C90}.
3745
28f540f4
RM
3746@need 100
3747@item ll
3748@itemx L
3749@itemx q
3750Specifies 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
3751GNU C compiler. For systems that don't provide extra-long integers, this
3752is the same as @code{long int}.)
3753
3754The @samp{q} modifier is another name for the same thing, which comes
3755from 4.4 BSD; a @w{@code{long long int}} is sometimes called a ``quad''
3756@code{int}.
e852e889
UD
3757
3758@item t
3759Specifies that the argument is a @code{ptrdiff_t *}.
3760
ec751a23 3761This modifier was introduced in @w{ISO C99}.
e852e889
UD
3762
3763@item z
3764Specifies that the argument is a @code{size_t *}.
3765
ec751a23 3766This modifier was introduced in @w{ISO C99}.
28f540f4
RM
3767@end table
3768
3769All of the @samp{%e}, @samp{%f}, @samp{%g}, @samp{%E}, and @samp{%G}
3770input conversions are interchangeable. They all match an optionally
3771signed floating point number, in the same syntax as for the
3772@code{strtod} function (@pxref{Parsing of Floats}).
3773
3774For the floating-point input conversions, the default argument type is
3775@code{float *}. (This is different from the corresponding output
3776conversions, where the default type is @code{double}; remember that
3777@code{float} arguments to @code{printf} are converted to @code{double}
3778by the default argument promotions, but @code{float *} arguments are
3779not promoted to @code{double *}.) You can specify other sizes of float
3780using these type modifiers:
3781
3782@table @samp
3783@item l
3784Specifies that the argument is of type @code{double *}.
3785
3786@item L
3787Specifies that the argument is of type @code{long double *}.
3788@end table
3789
2c6fe0bd
UD
3790For all the above number parsing formats there is an additional optional
3791flag @samp{'}. When this flag is given the @code{scanf} function
3792expects the number represented in the input string to be formatted
3793according to the grouping rules of the currently selected locale
3794(@pxref{General Numeric}).
3795
3796If the @code{"C"} or @code{"POSIX"} locale is selected there is no
3797difference. But for a locale which specifies values for the appropriate
3798fields in the locale the input must have the correct form in the input.
3799Otherwise the longest prefix with a correct form is processed.
3800
28f540f4
RM
3801@node String Input Conversions
3802@subsection String Input Conversions
3803
3804This section describes the @code{scanf} input conversions for reading
b5e73f56
UD
3805string and character values: @samp{%s}, @samp{%S}, @samp{%[}, @samp{%c},
3806and @samp{%C}.
28f540f4
RM
3807
3808You have two options for how to receive the input from these
3809conversions:
3810
3811@itemize @bullet
3812@item
b5e73f56
UD
3813Provide a buffer to store it in. This is the default. You should
3814provide an argument of type @code{char *} or @code{wchar_t *} (the
c703cd7a 3815latter if the @samp{l} modifier is present).
28f540f4
RM
3816
3817@strong{Warning:} To make a robust program, you must make sure that the
3818input (plus its terminating null) cannot possibly exceed the size of the
3819buffer you provide. In general, the only way to do this is to specify a
3820maximum field width one less than the buffer size. @strong{If you
3821provide the buffer, always specify a maximum field width to prevent
3822overflow.}
3823
3824@item
3825Ask @code{scanf} to allocate a big enough buffer, by specifying the
3826@samp{a} flag character. This is a GNU extension. You should provide
3827an argument of type @code{char **} for the buffer address to be stored
3828in. @xref{Dynamic String Input}.
3829@end itemize
3830
3831The @samp{%c} conversion is the simplest: it matches a fixed number of
04b9968b 3832characters, always. The maximum field width says how many characters to
28f540f4
RM
3833read; if you don't specify the maximum, the default is 1. This
3834conversion doesn't append a null character to the end of the text it
3835reads. It also does not skip over initial whitespace characters. It
3836reads precisely the next @var{n} characters, and fails if it cannot get
3837that many. Since there is always a maximum field width with @samp{%c}
3838(whether specified, or 1 by default), you can always prevent overflow by
3839making the buffer long enough.
b5e73f56
UD
3840@comment Is character == byte here??? --drepper
3841
3842If the format is @samp{%lc} or @samp{%C} the function stores wide
3843characters which are converted using the conversion determined at the
3844time the stream was opened from the external byte stream. The number of
3845bytes read from the medium is limited by @code{MB_CUR_LEN * @var{n}} but
c703cd7a 3846at most @var{n} wide characters get stored in the output string.
28f540f4
RM
3847
3848The @samp{%s} conversion matches a string of non-whitespace characters.
3849It skips and discards initial whitespace, but stops when it encounters
3850more whitespace after having read something. It stores a null character
3851at the end of the text that it reads.
3852
3853For example, reading the input:
3854
3855@smallexample
3856 hello, world
3857@end smallexample
3858
3859@noindent
3860with the conversion @samp{%10c} produces @code{" hello, wo"}, but
3861reading the same input with the conversion @samp{%10s} produces
3862@code{"hello,"}.
3863
3864@strong{Warning:} If you do not specify a field width for @samp{%s},
3865then the number of characters read is limited only by where the next
3866whitespace character appears. This almost certainly means that invalid
3867input can make your program crash---which is a bug.
3868
b5e73f56
UD
3869The @samp{%ls} and @samp{%S} format are handled just like @samp{%s}
3870except that the external byte sequence is converted using the conversion
3871associated with the stream to wide characters with their own encoding.
3872A width or precision specified with the format do not directly determine
3873how many bytes are read from the stream since they measure wide
3874characters. But an upper limit can be computed by multiplying the value
3875of the width or precision by @code{MB_CUR_MAX}.
3876
28f540f4
RM
3877To read in characters that belong to an arbitrary set of your choice,
3878use the @samp{%[} conversion. You specify the set between the @samp{[}
3879character and a following @samp{]} character, using the same syntax used
9878ad46 3880in regular expressions for explicit sets of characters. As special cases:
28f540f4
RM
3881
3882@itemize @bullet
19c3f208 3883@item
28f540f4
RM
3884A literal @samp{]} character can be specified as the first character
3885of the set.
3886
19c3f208 3887@item
28f540f4
RM
3888An embedded @samp{-} character (that is, one that is not the first or
3889last character of the set) is used to specify a range of characters.
3890
19c3f208 3891@item
28f540f4 3892If a caret character @samp{^} immediately follows the initial @samp{[},
c703cd7a 3893then the set of allowed input characters is everything @emph{except}
28f540f4
RM
3894the characters listed.
3895@end itemize
3896
3897The @samp{%[} conversion does not skip over initial whitespace
3898characters.
3899
9878ad46
OB
3900Note that the @dfn{character class} syntax available in character sets
3901that appear inside regular expressions (such as @samp{[:alpha:]}) is
3902@emph{not} available in the @samp{%[} conversion.
3903
28f540f4
RM
3904Here are some examples of @samp{%[} conversions and what they mean:
3905
3906@table @samp
3907@item %25[1234567890]
3908Matches a string of up to 25 digits.
3909
3910@item %25[][]
3911Matches a string of up to 25 square brackets.
3912
3913@item %25[^ \f\n\r\t\v]
3914Matches a string up to 25 characters long that doesn't contain any of
3915the standard whitespace characters. This is slightly different from
3916@samp{%s}, because if the input begins with a whitespace character,
3917@samp{%[} reports a matching failure while @samp{%s} simply discards the
3918initial whitespace.
3919
19c3f208 3920@item %25[a-z]
28f540f4
RM
3921Matches up to 25 lowercase characters.
3922@end table
3923
b5e73f56
UD
3924As for @samp{%c} and @samp{%s} the @samp{%[} format is also modified to
3925produce wide characters if the @samp{l} modifier is present. All what
3926is said about @samp{%ls} above is true for @samp{%l[}.
3927
28f540f4
RM
3928One more reminder: the @samp{%s} and @samp{%[} conversions are
3929@strong{dangerous} if you don't specify a maximum width or use the
3930@samp{a} flag, because input too long would overflow whatever buffer you
3931have provided for it. No matter how long your buffer is, a user could
3932supply input that is longer. A well-written program reports invalid
3933input with a comprehensible error message, not with a crash.
3934
3935@node Dynamic String Input
3936@subsection Dynamically Allocating String Conversions
3937
3938A GNU extension to formatted input lets you safely read a string with no
3939maximum size. Using this feature, you don't supply a buffer; instead,
3940@code{scanf} allocates a buffer big enough to hold the data and gives
3941you its address. To use this feature, write @samp{a} as a flag
3942character, as in @samp{%as} or @samp{%a[0-9a-z]}.
3943
3944The pointer argument you supply for where to store the input should have
3945type @code{char **}. The @code{scanf} function allocates a buffer and
3946stores its address in the word that the argument points to. You should
3947free the buffer with @code{free} when you no longer need it.
3948
3949Here is an example of using the @samp{a} flag with the @samp{%[@dots{}]}
3950conversion specification to read a ``variable assignment'' of the form
3951@samp{@var{variable} = @var{value}}.
3952
3953@smallexample
3954@{
3955 char *variable, *value;
3956
3957 if (2 > scanf ("%a[a-zA-Z0-9] = %a[^\n]\n",
162ba701 3958 &variable, &value))
28f540f4
RM
3959 @{
3960 invalid_input_error ();
3961 return 0;
3962 @}
3963
3964 @dots{}
3965@}
3966@end smallexample
3967
3968@node Other Input Conversions
3969@subsection Other Input Conversions
3970
3971This section describes the miscellaneous input conversions.
3972
3973The @samp{%p} conversion is used to read a pointer value. It recognizes
04b9968b 3974the same syntax used by the @samp{%p} output conversion for
28f540f4
RM
3975@code{printf} (@pxref{Other Output Conversions}); that is, a hexadecimal
3976number just as the @samp{%x} conversion accepts. The corresponding
3977argument should be of type @code{void **}; that is, the address of a
3978place to store a pointer.
3979
3980The resulting pointer value is not guaranteed to be valid if it was not
3981originally written during the same program execution that reads it in.
3982
3983The @samp{%n} conversion produces the number of characters read so far
3984by this call. The corresponding argument should be of type @code{int *}.
3985This conversion works in the same way as the @samp{%n} conversion for
3986@code{printf}; see @ref{Other Output Conversions}, for an example.
3987
3988The @samp{%n} conversion is the only mechanism for determining the
3989success of literal matches or conversions with suppressed assignments.
3990If the @samp{%n} follows the locus of a matching failure, then no value
3991is stored for it since @code{scanf} returns before processing the
3992@samp{%n}. If you store @code{-1} in that argument slot before calling
3993@code{scanf}, the presence of @code{-1} after @code{scanf} indicates an
3994error occurred before the @samp{%n} was reached.
3995
3996Finally, the @samp{%%} conversion matches a literal @samp{%} character
3997in the input stream, without using an argument. This conversion does
3998not permit any flags, field width, or type modifier to be specified.
3999
4000@node Formatted Input Functions
4001@subsection Formatted Input Functions
4002
4003Here are the descriptions of the functions for performing formatted
4004input.
4005Prototypes for these functions are in the header file @file{stdio.h}.
4006@pindex stdio.h
4007
4008@comment stdio.h
f65fd747 4009@comment ISO
28f540f4 4010@deftypefun int scanf (const char *@var{template}, @dots{})
171e9210 4011@safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@asucorrupt{} @ascuheap{}}@acunsafe{@acsmem{} @aculock{} @acucorrupt{}}}
28f540f4
RM
4012The @code{scanf} function reads formatted input from the stream
4013@code{stdin} under the control of the template string @var{template}.
4014The optional arguments are pointers to the places which receive the
4015resulting values.
4016
4017The return value is normally the number of successful assignments. If
04b9968b
UD
4018an end-of-file condition is detected before any matches are performed,
4019including matches against whitespace and literal characters in the
4020template, then @code{EOF} is returned.
28f540f4
RM
4021@end deftypefun
4022
b5e73f56
UD
4023@comment wchar.h
4024@comment ISO
4025@deftypefun int wscanf (const wchar_t *@var{template}, @dots{})
171e9210 4026@safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@asucorrupt{} @ascuheap{}}@acunsafe{@acsmem{} @aculock{} @acucorrupt{}}}
b5e73f56
UD
4027The @code{wscanf} function reads formatted input from the stream
4028@code{stdin} under the control of the template string @var{template}.
4029The optional arguments are pointers to the places which receive the
4030resulting values.
4031
4032The return value is normally the number of successful assignments. If
4033an end-of-file condition is detected before any matches are performed,
4034including matches against whitespace and literal characters in the
4035template, then @code{WEOF} is returned.
4036@end deftypefun
4037
28f540f4 4038@comment stdio.h
f65fd747 4039@comment ISO
28f540f4 4040@deftypefun int fscanf (FILE *@var{stream}, const char *@var{template}, @dots{})
171e9210 4041@safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@asucorrupt{} @ascuheap{}}@acunsafe{@acsmem{} @aculock{} @acucorrupt{}}}
28f540f4
RM
4042This function is just like @code{scanf}, except that the input is read
4043from the stream @var{stream} instead of @code{stdin}.
4044@end deftypefun
4045
b5e73f56
UD
4046@comment wchar.h
4047@comment ISO
4048@deftypefun int fwscanf (FILE *@var{stream}, const wchar_t *@var{template}, @dots{})
171e9210 4049@safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@asucorrupt{} @ascuheap{}}@acunsafe{@acsmem{} @aculock{} @acucorrupt{}}}
b5e73f56
UD
4050This function is just like @code{wscanf}, except that the input is read
4051from the stream @var{stream} instead of @code{stdin}.
4052@end deftypefun
4053
28f540f4 4054@comment stdio.h
f65fd747 4055@comment ISO
28f540f4 4056@deftypefun int sscanf (const char *@var{s}, const char *@var{template}, @dots{})
171e9210 4057@safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{}}}
28f540f4
RM
4058This is like @code{scanf}, except that the characters are taken from the
4059null-terminated string @var{s} instead of from a stream. Reaching the
4060end of the string is treated as an end-of-file condition.
4061
4062The behavior of this function is undefined if copying takes place
4063between objects that overlap---for example, if @var{s} is also given
b5e73f56
UD
4064as an argument to receive a string read under control of the @samp{%s},
4065@samp{%S}, or @samp{%[} conversion.
4066@end deftypefun
4067
4068@comment wchar.h
4069@comment ISO
8ded91fb 4070@deftypefun int swscanf (const wchar_t *@var{ws}, const wchar_t *@var{template}, @dots{})
171e9210 4071@safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{}}}
b5e73f56
UD
4072This is like @code{wscanf}, except that the characters are taken from the
4073null-terminated string @var{ws} instead of from a stream. Reaching the
4074end of the string is treated as an end-of-file condition.
4075
4076The behavior of this function is undefined if copying takes place
4077between objects that overlap---for example, if @var{ws} is also given as
4078an argument to receive a string read under control of the @samp{%s},
4079@samp{%S}, or @samp{%[} conversion.
28f540f4
RM
4080@end deftypefun
4081
4082@node Variable Arguments Input
4083@subsection Variable Arguments Input Functions
4084
4085The functions @code{vscanf} and friends are provided so that you can
4086define your own variadic @code{scanf}-like functions that make use of
4087the same internals as the built-in formatted output functions.
4088These functions are analogous to the @code{vprintf} series of output
4089functions. @xref{Variable Arguments Output}, for important
4090information on how to use them.
4091
b5e73f56
UD
4092@strong{Portability Note:} The functions listed in this section were
4093introduced in @w{ISO C99} and were before available as GNU extensions.
28f540f4
RM
4094
4095@comment stdio.h
b5e73f56 4096@comment ISO
28f540f4 4097@deftypefun int vscanf (const char *@var{template}, va_list @var{ap})
171e9210 4098@safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@asucorrupt{} @ascuheap{}}@acunsafe{@acsmem{} @aculock{} @acucorrupt{}}}
04b9968b 4099This function is similar to @code{scanf}, but instead of taking
28f540f4
RM
4100a variable number of arguments directly, it takes an argument list
4101pointer @var{ap} of type @code{va_list} (@pxref{Variadic Functions}).
4102@end deftypefun
4103
b5e73f56
UD
4104@comment wchar.h
4105@comment ISO
4106@deftypefun int vwscanf (const wchar_t *@var{template}, va_list @var{ap})
171e9210 4107@safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@asucorrupt{} @ascuheap{}}@acunsafe{@acsmem{} @aculock{} @acucorrupt{}}}
b5e73f56
UD
4108This function is similar to @code{wscanf}, but instead of taking
4109a variable number of arguments directly, it takes an argument list
4110pointer @var{ap} of type @code{va_list} (@pxref{Variadic Functions}).
4111@end deftypefun
4112
28f540f4 4113@comment stdio.h
b5e73f56 4114@comment ISO
28f540f4 4115@deftypefun int vfscanf (FILE *@var{stream}, const char *@var{template}, va_list @var{ap})
171e9210 4116@safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@asucorrupt{} @ascuheap{}}@acunsafe{@acsmem{} @aculock{} @acucorrupt{}}}
28f540f4
RM
4117This is the equivalent of @code{fscanf} with the variable argument list
4118specified directly as for @code{vscanf}.
4119@end deftypefun
4120
b5e73f56
UD
4121@comment wchar.h
4122@comment ISO
4123@deftypefun int vfwscanf (FILE *@var{stream}, const wchar_t *@var{template}, va_list @var{ap})
171e9210 4124@safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@asucorrupt{} @ascuheap{}}@acunsafe{@acsmem{} @aculock{} @acucorrupt{}}}
b5e73f56
UD
4125This is the equivalent of @code{fwscanf} with the variable argument list
4126specified directly as for @code{vwscanf}.
4127@end deftypefun
4128
28f540f4 4129@comment stdio.h
b5e73f56 4130@comment ISO
28f540f4 4131@deftypefun int vsscanf (const char *@var{s}, const char *@var{template}, va_list @var{ap})
171e9210 4132@safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{}}}
28f540f4
RM
4133This is the equivalent of @code{sscanf} with the variable argument list
4134specified directly as for @code{vscanf}.
4135@end deftypefun
4136
b5e73f56
UD
4137@comment wchar.h
4138@comment ISO
4139@deftypefun int vswscanf (const wchar_t *@var{s}, const wchar_t *@var{template}, va_list @var{ap})
171e9210 4140@safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{}}}
b5e73f56
UD
4141This is the equivalent of @code{swscanf} with the variable argument list
4142specified directly as for @code{vwscanf}.
4143@end deftypefun
4144
28f540f4
RM
4145In GNU C, there is a special construct you can use to let the compiler
4146know that a function uses a @code{scanf}-style format string. Then it
4147can check the number and types of arguments in each call to the
4148function, and warn you when they do not match the format string.
88197030 4149For details, see @ref{Function Attributes, , Declaring Attributes of Functions,
04b9968b 4150gcc.info, Using GNU CC}.
28f540f4
RM
4151
4152@node EOF and Errors
4153@section End-Of-File and Errors
4154
4155@cindex end of file, on a stream
4156Many of the functions described in this chapter return the value of the
4157macro @code{EOF} to indicate unsuccessful completion of the operation.
4158Since @code{EOF} is used to report both end of file and random errors,
4159it's often better to use the @code{feof} function to check explicitly
4160for end of file and @code{ferror} to check for errors. These functions
4161check indicators that are part of the internal state of the stream
4162object, indicators set if the appropriate condition was detected by a
4163previous I/O operation on that stream.
4164
28f540f4 4165@comment stdio.h
f65fd747 4166@comment ISO
28f540f4 4167@deftypevr Macro int EOF
b5e73f56
UD
4168This macro is an integer value that is returned by a number of narrow
4169stream functions to indicate an end-of-file condition, or some other
1f77f049 4170error situation. With @theglibc{}, @code{EOF} is @code{-1}. In
b5e73f56
UD
4171other libraries, its value may be some other negative number.
4172
4173This symbol is declared in @file{stdio.h}.
4174@end deftypevr
4175
4176@comment wchar.h
4177@comment ISO
4178@deftypevr Macro int WEOF
4179This macro is an integer value that is returned by a number of wide
4180stream functions to indicate an end-of-file condition, or some other
1f77f049 4181error situation. With @theglibc{}, @code{WEOF} is @code{-1}. In
b5e73f56
UD
4182other libraries, its value may be some other negative number.
4183
4184This symbol is declared in @file{wchar.h}.
28f540f4
RM
4185@end deftypevr
4186
28f540f4 4187@comment stdio.h
f65fd747 4188@comment ISO
28f540f4 4189@deftypefun int feof (FILE *@var{stream})
171e9210 4190@safety{@prelim{}@mtsafe{}@assafe{}@acunsafe{@aculock{}}}
28f540f4
RM
4191The @code{feof} function returns nonzero if and only if the end-of-file
4192indicator for the stream @var{stream} is set.
b5e73f56
UD
4193
4194This symbol is declared in @file{stdio.h}.
28f540f4
RM
4195@end deftypefun
4196
7b4161bb
UD
4197@comment stdio.h
4198@comment GNU
4199@deftypefun int feof_unlocked (FILE *@var{stream})
171e9210
AO
4200@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
4201@c There isn't much of a thread unsafety risk in reading a flag word and
4202@c testing a bit in it.
7b4161bb 4203The @code{feof_unlocked} function is equivalent to the @code{feof}
1dc843f7 4204function except that it does not implicitly lock the stream.
7b4161bb
UD
4205
4206This function is a GNU extension.
b5e73f56
UD
4207
4208This symbol is declared in @file{stdio.h}.
7b4161bb
UD
4209@end deftypefun
4210
28f540f4 4211@comment stdio.h
f65fd747 4212@comment ISO
28f540f4 4213@deftypefun int ferror (FILE *@var{stream})
171e9210 4214@safety{@prelim{}@mtsafe{}@assafe{}@acunsafe{@aculock{}}}
28f540f4
RM
4215The @code{ferror} function returns nonzero if and only if the error
4216indicator for the stream @var{stream} is set, indicating that an error
4217has occurred on a previous operation on the stream.
b5e73f56
UD
4218
4219This symbol is declared in @file{stdio.h}.
28f540f4
RM
4220@end deftypefun
4221
7b4161bb
UD
4222@comment stdio.h
4223@comment GNU
4224@deftypefun int ferror_unlocked (FILE *@var{stream})
171e9210 4225@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
7b4161bb 4226The @code{ferror_unlocked} function is equivalent to the @code{ferror}
1dc843f7 4227function except that it does not implicitly lock the stream.
7b4161bb
UD
4228
4229This function is a GNU extension.
b5e73f56
UD
4230
4231This symbol is declared in @file{stdio.h}.
7b4161bb
UD
4232@end deftypefun
4233
28f540f4
RM
4234In addition to setting the error indicator associated with the stream,
4235the functions that operate on streams also set @code{errno} in the same
4236way as the corresponding low-level functions that operate on file
4237descriptors. For example, all of the functions that perform output to a
4238stream---such as @code{fputc}, @code{printf}, and @code{fflush}---are
4239implemented in terms of @code{write}, and all of the @code{errno} error
4240conditions defined for @code{write} are meaningful for these functions.
4241For more information about the descriptor-level I/O functions, see
4242@ref{Low-Level I/O}.
4243
460e040a
UD
4244@node Error Recovery
4245@section Recovering from errors
4246
7ba73c63 4247You may explicitly clear the error and EOF flags with the @code{clearerr}
460e040a
UD
4248function.
4249
4250@comment stdio.h
4251@comment ISO
4252@deftypefun void clearerr (FILE *@var{stream})
171e9210 4253@safety{@prelim{}@mtsafe{}@assafe{}@acunsafe{@aculock{}}}
460e040a
UD
4254This function clears the end-of-file and error indicators for the
4255stream @var{stream}.
4256
4257The file positioning functions (@pxref{File Positioning}) also clear the
4258end-of-file indicator for the stream.
4259@end deftypefun
4260
7b4161bb
UD
4261@comment stdio.h
4262@comment GNU
4263@deftypefun void clearerr_unlocked (FILE *@var{stream})
171e9210 4264@safety{@prelim{}@mtsafe{@mtsrace{:stream}}@assafe{}@acsafe{}}
7b4161bb 4265The @code{clearerr_unlocked} function is equivalent to the @code{clearerr}
1dc843f7 4266function except that it does not implicitly lock the stream.
7b4161bb
UD
4267
4268This function is a GNU extension.
4269@end deftypefun
4270
460e040a
UD
4271Note that it is @emph{not} correct to just clear the error flag and retry
4272a failed stream operation. After a failed write, any number of
4273characters since the last buffer flush may have been committed to the
4274file, while some buffered data may have been discarded. Merely retrying
4275can thus cause lost or repeated data.
4276
4277A failed read may leave the file pointer in an inappropriate position for
4278a second try. In both cases, you should seek to a known position before
4279retrying.
4280
4281Most errors that can happen are not recoverable --- a second try will
4282always fail again in the same way. So usually it is best to give up and
4283report the error to the user, rather than install complicated recovery
4284logic.
4285
7ba73c63 4286One important exception is @code{EINTR} (@pxref{Interrupted Primitives}).
460e040a
UD
4287Many stream I/O implementations will treat it as an ordinary error, which
4288can be quite inconvenient. You can avoid this hassle by installing all
4289signals with the @code{SA_RESTART} flag.
4290
4291For similar reasons, setting nonblocking I/O on a stream's file
4292descriptor is not usually advisable.
4293
28f540f4
RM
4294@node Binary Streams
4295@section Text and Binary Streams
4296
a7a93d50 4297@gnusystems{} and other POSIX-compatible operating systems organize all
28f540f4
RM
4298files as uniform sequences of characters. However, some other systems
4299make a distinction between files containing text and files containing
f65fd747 4300binary data, and the input and output facilities of @w{ISO C} provide for
28f540f4
RM
4301this distinction. This section tells you how to write programs portable
4302to such systems.
4303
4304@cindex text stream
4305@cindex binary stream
4306When you open a stream, you can specify either a @dfn{text stream} or a
4307@dfn{binary stream}. You indicate that you want a binary stream by
4308specifying the @samp{b} modifier in the @var{opentype} argument to
4309@code{fopen}; see @ref{Opening Streams}. Without this
4310option, @code{fopen} opens the file as a text stream.
4311
4312Text and binary streams differ in several ways:
4313
4314@itemize @bullet
4315@item
4316The data read from a text stream is divided into @dfn{lines} which are
4317terminated by newline (@code{'\n'}) characters, while a binary stream is
4318simply a long series of characters. A text stream might on some systems
4319fail to handle lines more than 254 characters long (including the
4320terminating newline character).
4321@cindex lines (in a text file)
4322
4323@item
4324On some systems, text files can contain only printing characters,
4325horizontal tab characters, and newlines, and so text streams may not
4326support other characters. However, binary streams can handle any
4327character value.
4328
4329@item
4330Space characters that are written immediately preceding a newline
4331character in a text stream may disappear when the file is read in again.
4332
4333@item
4334More generally, there need not be a one-to-one mapping between
4335characters that are read from or written to a text stream, and the
4336characters in the actual file.
4337@end itemize
4338
4339Since a binary stream is always more capable and more predictable than a
4340text stream, you might wonder what purpose text streams serve. Why not
4341simply always use binary streams? The answer is that on these operating
4342systems, text and binary streams use different file formats, and the
4343only way to read or write ``an ordinary file of text'' that can work
4344with other text-oriented programs is through a text stream.
4345
1f77f049 4346In @theglibc{}, and on all POSIX systems, there is no difference
28f540f4
RM
4347between text streams and binary streams. When you open a stream, you
4348get the same kind of stream regardless of whether you ask for binary.
4349This stream can handle any file content, and has none of the
4350restrictions that text streams sometimes have.
4351
4352@node File Positioning
4353@section File Positioning
4354@cindex file positioning on a stream
4355@cindex positioning a stream
4356@cindex seeking on a stream
4357
4358The @dfn{file position} of a stream describes where in the file the
4359stream is currently reading or writing. I/O on the stream advances the
a7a93d50 4360file position through the file. On @gnusystems{}, the file position is
28f540f4
RM
4361represented as an integer, which counts the number of bytes from the
4362beginning of the file. @xref{File Position}.
4363
4364During I/O to an ordinary disk file, you can change the file position
4365whenever you wish, so as to read or write any portion of the file. Some
4366other kinds of files may also permit this. Files which support changing
4367the file position are sometimes referred to as @dfn{random-access}
4368files.
4369
4370You can use the functions in this section to examine or modify the file
4371position indicator associated with a stream. The symbols listed below
4372are declared in the header file @file{stdio.h}.
4373@pindex stdio.h
4374
4375@comment stdio.h
f65fd747 4376@comment ISO
28f540f4 4377@deftypefun {long int} ftell (FILE *@var{stream})
171e9210 4378@safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{}}@acunsafe{@aculock{} @acucorrupt{}}}
28f540f4
RM
4379This function returns the current file position of the stream
4380@var{stream}.
4381
4382This function can fail if the stream doesn't support file positioning,
4383or if the file position can't be represented in a @code{long int}, and
4384possibly for other reasons as well. If a failure occurs, a value of
4385@code{-1} is returned.
4386@end deftypefun
4387
a5a0310d
UD
4388@comment stdio.h
4389@comment Unix98
4390@deftypefun off_t ftello (FILE *@var{stream})
171e9210 4391@safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{}}@acunsafe{@aculock{} @acucorrupt{}}}
04b9968b
UD
4392The @code{ftello} function is similar to @code{ftell}, except that it
4393returns a value of type @code{off_t}. Systems which support this type
4394use it to describe all file positions, unlike the POSIX specification
4395which uses a long int. The two are not necessarily the same size.
4396Therefore, using ftell can lead to problems if the implementation is
4397written on top of a POSIX compliant low-level I/O implementation, and using
4398@code{ftello} is preferable whenever it is available.
4399
4400If this function fails it returns @code{(off_t) -1}. This can happen due
a5a0310d
UD
4401to missing support for file positioning or internal errors. Otherwise
4402the return value is the current file position.
4403
4404The function is an extension defined in the Unix Single Specification
4405version 2.
a3a4a74e
UD
4406
4407When the sources are compiled with @code{_FILE_OFFSET_BITS == 64} on a
0be8752b 440832 bit system this function is in fact @code{ftello64}. I.e., the
a3a4a74e
UD
4409LFS interface transparently replaces the old interface.
4410@end deftypefun
4411
4412@comment stdio.h
4413@comment Unix98
4414@deftypefun off64_t ftello64 (FILE *@var{stream})
171e9210 4415@safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{}}@acunsafe{@aculock{} @acucorrupt{}}}
a3a4a74e
UD
4416This function is similar to @code{ftello} with the only difference that
4417the return value is of type @code{off64_t}. This also requires that the
4418stream @var{stream} was opened using either @code{fopen64},
4419@code{freopen64}, or @code{tmpfile64} since otherwise the underlying
9ceeb279 4420file operations to position the file pointer beyond the @twoexp{31}
a3a4a74e
UD
4421bytes limit might fail.
4422
4423If the sources are compiled with @code{_FILE_OFFSET_BITS == 64} on a 32
4424bits machine this function is available under the name @code{ftello}
4425and so transparently replaces the old interface.
a5a0310d
UD
4426@end deftypefun
4427
28f540f4 4428@comment stdio.h
f65fd747 4429@comment ISO
28f540f4 4430@deftypefun int fseek (FILE *@var{stream}, long int @var{offset}, int @var{whence})
171e9210 4431@safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{}}@acunsafe{@aculock{} @acucorrupt{}}}
28f540f4
RM
4432The @code{fseek} function is used to change the file position of the
4433stream @var{stream}. The value of @var{whence} must be one of the
4434constants @code{SEEK_SET}, @code{SEEK_CUR}, or @code{SEEK_END}, to
4435indicate whether the @var{offset} is relative to the beginning of the
4436file, the current file position, or the end of the file, respectively.
4437
4438This function returns a value of zero if the operation was successful,
4439and a nonzero value to indicate failure. A successful call also clears
4440the end-of-file indicator of @var{stream} and discards any characters
4441that were ``pushed back'' by the use of @code{ungetc}.
4442
4443@code{fseek} either flushes any buffered output before setting the file
4444position or else remembers it so it will be written later in its proper
4445place in the file.
4446@end deftypefun
4447
a5a0310d
UD
4448@comment stdio.h
4449@comment Unix98
4450@deftypefun int fseeko (FILE *@var{stream}, off_t @var{offset}, int @var{whence})
171e9210 4451@safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{}}@acunsafe{@aculock{} @acucorrupt{}}}
a5a0310d
UD
4452This function is similar to @code{fseek} but it corrects a problem with
4453@code{fseek} in a system with POSIX types. Using a value of type
4454@code{long int} for the offset is not compatible with POSIX.
4455@code{fseeko} uses the correct type @code{off_t} for the @var{offset}
4456parameter.
4457
f2ea0f5b 4458For this reason it is a good idea to prefer @code{ftello} whenever it is
a5a0310d
UD
4459available since its functionality is (if different at all) closer the
4460underlying definition.
4461
c703cd7a 4462The functionality and return value are the same as for @code{fseek}.
a5a0310d
UD
4463
4464The function is an extension defined in the Unix Single Specification
4465version 2.
a3a4a74e
UD
4466
4467When the sources are compiled with @code{_FILE_OFFSET_BITS == 64} on a
0be8752b 446832 bit system this function is in fact @code{fseeko64}. I.e., the
a3a4a74e
UD
4469LFS interface transparently replaces the old interface.
4470@end deftypefun
4471
4472@comment stdio.h
4473@comment Unix98
4474@deftypefun int fseeko64 (FILE *@var{stream}, off64_t @var{offset}, int @var{whence})
171e9210 4475@safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{}}@acunsafe{@aculock{} @acucorrupt{}}}
a3a4a74e
UD
4476This function is similar to @code{fseeko} with the only difference that
4477the @var{offset} parameter is of type @code{off64_t}. This also
4478requires that the stream @var{stream} was opened using either
4479@code{fopen64}, @code{freopen64}, or @code{tmpfile64} since otherwise
4480the underlying file operations to position the file pointer beyond the
9ceeb279 4481@twoexp{31} bytes limit might fail.
a3a4a74e
UD
4482
4483If the sources are compiled with @code{_FILE_OFFSET_BITS == 64} on a 32
4484bits machine this function is available under the name @code{fseeko}
4485and so transparently replaces the old interface.
a5a0310d
UD
4486@end deftypefun
4487
4488@strong{Portability Note:} In non-POSIX systems, @code{ftell},
4489@code{ftello}, @code{fseek} and @code{fseeko} might work reliably only
4490on binary streams. @xref{Binary Streams}.
28f540f4
RM
4491
4492The following symbolic constants are defined for use as the @var{whence}
4493argument to @code{fseek}. They are also used with the @code{lseek}
4494function (@pxref{I/O Primitives}) and to specify offsets for file locks
4495(@pxref{Control Operations}).
4496
4497@comment stdio.h
f65fd747 4498@comment ISO
28f540f4
RM
4499@deftypevr Macro int SEEK_SET
4500This is an integer constant which, when used as the @var{whence}
c703cd7a 4501argument to the @code{fseek} or @code{fseeko} functions, specifies that
a5a0310d 4502the offset provided is relative to the beginning of the file.
28f540f4
RM
4503@end deftypevr
4504
4505@comment stdio.h
f65fd747 4506@comment ISO
28f540f4
RM
4507@deftypevr Macro int SEEK_CUR
4508This is an integer constant which, when used as the @var{whence}
c703cd7a 4509argument to the @code{fseek} or @code{fseeko} functions, specifies that
a5a0310d 4510the offset provided is relative to the current file position.
28f540f4
RM
4511@end deftypevr
4512
4513@comment stdio.h
f65fd747 4514@comment ISO
28f540f4
RM
4515@deftypevr Macro int SEEK_END
4516This is an integer constant which, when used as the @var{whence}
c703cd7a 4517argument to the @code{fseek} or @code{fseeko} functions, specifies that
a5a0310d 4518the offset provided is relative to the end of the file.
28f540f4
RM
4519@end deftypevr
4520
4521@comment stdio.h
f65fd747 4522@comment ISO
28f540f4 4523@deftypefun void rewind (FILE *@var{stream})
171e9210 4524@safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{}}@acunsafe{@aculock{} @acucorrupt{}}}
28f540f4 4525The @code{rewind} function positions the stream @var{stream} at the
f2ea0f5b 4526beginning of the file. It is equivalent to calling @code{fseek} or
a5a0310d
UD
4527@code{fseeko} on the @var{stream} with an @var{offset} argument of
4528@code{0L} and a @var{whence} argument of @code{SEEK_SET}, except that
4529the return value is discarded and the error indicator for the stream is
4530reset.
28f540f4
RM
4531@end deftypefun
4532
4533These three aliases for the @samp{SEEK_@dots{}} constants exist for the
4534sake of compatibility with older BSD systems. They are defined in two
4535different header files: @file{fcntl.h} and @file{sys/file.h}.
4536
2fe82ca6 4537@vtable @code
28f540f4
RM
4538@comment sys/file.h
4539@comment BSD
4540@item L_SET
28f540f4
RM
4541An alias for @code{SEEK_SET}.
4542
4543@comment sys/file.h
4544@comment BSD
4545@item L_INCR
28f540f4
RM
4546An alias for @code{SEEK_CUR}.
4547
4548@comment sys/file.h
4549@comment BSD
4550@item L_XTND
28f540f4 4551An alias for @code{SEEK_END}.
2fe82ca6 4552@end vtable
28f540f4
RM
4553
4554@node Portable Positioning
4555@section Portable File-Position Functions
4556
a7a93d50 4557On @gnusystems{}, the file position is truly a character count. You
a5a0310d
UD
4558can specify any character count value as an argument to @code{fseek} or
4559@code{fseeko} and get reliable results for any random access file.
4560However, some @w{ISO C} systems do not represent file positions in this
4561way.
28f540f4
RM
4562
4563On some systems where text streams truly differ from binary streams, it
4564is impossible to represent the file position of a text stream as a count
4565of characters from the beginning of the file. For example, the file
4566position on some systems must encode both a record offset within the
4567file, and a character offset within the record.
4568
4569As a consequence, if you want your programs to be portable to these
4570systems, you must observe certain rules:
4571
4572@itemize @bullet
4573@item
4574The value returned from @code{ftell} on a text stream has no predictable
4575relationship to the number of characters you have read so far. The only
4576thing you can rely on is that you can use it subsequently as the
a5a0310d
UD
4577@var{offset} argument to @code{fseek} or @code{fseeko} to move back to
4578the same file position.
28f540f4 4579
19c3f208 4580@item
a5a0310d 4581In a call to @code{fseek} or @code{fseeko} on a text stream, either the
04b9968b 4582@var{offset} must be zero, or @var{whence} must be @code{SEEK_SET} and
ded5b9b7 4583the @var{offset} must be the result of an earlier call to @code{ftell}
04b9968b 4584on the same stream.
28f540f4
RM
4585
4586@item
4587The value of the file position indicator of a text stream is undefined
4588while there are characters that have been pushed back with @code{ungetc}
4589that haven't been read or discarded. @xref{Unreading}.
4590@end itemize
4591
4592But even if you observe these rules, you may still have trouble for long
4593files, because @code{ftell} and @code{fseek} use a @code{long int} value
4594to represent the file position. This type may not have room to encode
a5a0310d
UD
4595all the file positions in a large file. Using the @code{ftello} and
4596@code{fseeko} functions might help here since the @code{off_t} type is
4597expected to be able to hold all file position values but this still does
4598not help to handle additional information which must be associated with
4599a file position.
28f540f4
RM
4600
4601So if you do want to support systems with peculiar encodings for the
4602file positions, it is better to use the functions @code{fgetpos} and
4603@code{fsetpos} instead. These functions represent the file position
4604using the data type @code{fpos_t}, whose internal representation varies
4605from system to system.
4606
4607These symbols are declared in the header file @file{stdio.h}.
4608@pindex stdio.h
4609
4610@comment stdio.h
f65fd747 4611@comment ISO
28f540f4
RM
4612@deftp {Data Type} fpos_t
4613This is the type of an object that can encode information about the
4614file position of a stream, for use by the functions @code{fgetpos} and
4615@code{fsetpos}.
4616
a7a93d50 4617In @theglibc{}, @code{fpos_t} is an opaque data structure that
5988b69d
AJ
4618contains internal data to represent file offset and conversion state
4619information. In other systems, it might have a different internal
28f540f4 4620representation.
a3a4a74e 4621
0be8752b 4622When compiling with @code{_FILE_OFFSET_BITS == 64} on a 32 bit machine
5988b69d
AJ
4623this type is in fact equivalent to @code{fpos64_t} since the LFS
4624interface transparently replaces the old interface.
a3a4a74e
UD
4625@end deftp
4626
4627@comment stdio.h
4628@comment Unix98
4629@deftp {Data Type} fpos64_t
4630This is the type of an object that can encode information about the
4631file position of a stream, for use by the functions @code{fgetpos64} and
4632@code{fsetpos64}.
4633
a7a93d50 4634In @theglibc{}, @code{fpos64_t} is an opaque data structure that
5988b69d
AJ
4635contains internal data to represent file offset and conversion state
4636information. In other systems, it might have a different internal
a3a4a74e 4637representation.
28f540f4
RM
4638@end deftp
4639
4640@comment stdio.h
f65fd747 4641@comment ISO
28f540f4 4642@deftypefun int fgetpos (FILE *@var{stream}, fpos_t *@var{position})
171e9210 4643@safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{}}@acunsafe{@aculock{} @acucorrupt{}}}
28f540f4
RM
4644This function stores the value of the file position indicator for the
4645stream @var{stream} in the @code{fpos_t} object pointed to by
4646@var{position}. If successful, @code{fgetpos} returns zero; otherwise
4647it returns a nonzero value and stores an implementation-defined positive
4648value in @code{errno}.
a3a4a74e
UD
4649
4650When the sources are compiled with @code{_FILE_OFFSET_BITS == 64} on a
0be8752b 465132 bit system the function is in fact @code{fgetpos64}. I.e., the LFS
5988b69d 4652interface transparently replaces the old interface.
a3a4a74e
UD
4653@end deftypefun
4654
4655@comment stdio.h
4656@comment Unix98
4657@deftypefun int fgetpos64 (FILE *@var{stream}, fpos64_t *@var{position})
171e9210 4658@safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{}}@acunsafe{@aculock{} @acucorrupt{}}}
a3a4a74e
UD
4659This function is similar to @code{fgetpos} but the file position is
4660returned in a variable of type @code{fpos64_t} to which @var{position}
4661points.
4662
4663If the sources are compiled with @code{_FILE_OFFSET_BITS == 64} on a 32
4664bits machine this function is available under the name @code{fgetpos}
4665and so transparently replaces the old interface.
28f540f4
RM
4666@end deftypefun
4667
4668@comment stdio.h
f65fd747 4669@comment ISO
a3a4a74e 4670@deftypefun int fsetpos (FILE *@var{stream}, const fpos_t *@var{position})
171e9210 4671@safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{}}@acunsafe{@aculock{} @acucorrupt{}}}
28f540f4
RM
4672This function sets the file position indicator for the stream @var{stream}
4673to the position @var{position}, which must have been set by a previous
4674call to @code{fgetpos} on the same stream. If successful, @code{fsetpos}
4675clears the end-of-file indicator on the stream, discards any characters
4676that were ``pushed back'' by the use of @code{ungetc}, and returns a value
4677of zero. Otherwise, @code{fsetpos} returns a nonzero value and stores
4678an implementation-defined positive value in @code{errno}.
a3a4a74e
UD
4679
4680When the sources are compiled with @code{_FILE_OFFSET_BITS == 64} on a
0be8752b 468132 bit system the function is in fact @code{fsetpos64}. I.e., the LFS
5988b69d 4682interface transparently replaces the old interface.
a3a4a74e
UD
4683@end deftypefun
4684
4685@comment stdio.h
4686@comment Unix98
4687@deftypefun int fsetpos64 (FILE *@var{stream}, const fpos64_t *@var{position})
171e9210 4688@safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{}}@acunsafe{@aculock{} @acucorrupt{}}}
a3a4a74e
UD
4689This function is similar to @code{fsetpos} but the file position used
4690for positioning is provided in a variable of type @code{fpos64_t} to
4691which @var{position} points.
4692
4693If the sources are compiled with @code{_FILE_OFFSET_BITS == 64} on a 32
4694bits machine this function is available under the name @code{fsetpos}
4695and so transparently replaces the old interface.
28f540f4
RM
4696@end deftypefun
4697
4698@node Stream Buffering
4699@section Stream Buffering
4700
4701@cindex buffering of streams
4702Characters that are written to a stream are normally accumulated and
4703transmitted asynchronously to the file in a block, instead of appearing
4704as soon as they are output by the application program. Similarly,
4705streams often retrieve input from the host environment in blocks rather
4706than on a character-by-character basis. This is called @dfn{buffering}.
4707
4708If you are writing programs that do interactive input and output using
4709streams, you need to understand how buffering works when you design the
4710user interface to your program. Otherwise, you might find that output
4711(such as progress or prompt messages) doesn't appear when you intended
04b9968b 4712it to, or displays some other unexpected behavior.
28f540f4
RM
4713
4714This section deals only with controlling when characters are transmitted
4715between the stream and the file or device, and @emph{not} with how
4716things like echoing, flow control, and the like are handled on specific
4717classes of devices. For information on common control operations on
4718terminal devices, see @ref{Low-Level Terminal Interface}.
4719
4720You can bypass the stream buffering facilities altogether by using the
4721low-level input and output functions that operate on file descriptors
4722instead. @xref{Low-Level I/O}.
4723
4724@menu
4725* Buffering Concepts:: Terminology is defined here.
4726* Flushing Buffers:: How to ensure that output buffers are flushed.
4727* Controlling Buffering:: How to specify what kind of buffering to use.
4728@end menu
4729
4730@node Buffering Concepts
4731@subsection Buffering Concepts
4732
4733There are three different kinds of buffering strategies:
4734
4735@itemize @bullet
4736@item
4737Characters written to or read from an @dfn{unbuffered} stream are
4738transmitted individually to or from the file as soon as possible.
4739@cindex unbuffered stream
4740
4741@item
4742Characters written to a @dfn{line buffered} stream are transmitted to
4743the file in blocks when a newline character is encountered.
4744@cindex line buffered stream
4745
4746@item
4747Characters written to or read from a @dfn{fully buffered} stream are
4748transmitted to or from the file in blocks of arbitrary size.
4749@cindex fully buffered stream
4750@end itemize
4751
4752Newly opened streams are normally fully buffered, with one exception: a
4753stream connected to an interactive device such as a terminal is
4754initially line buffered. @xref{Controlling Buffering}, for information
4755on how to select a different kind of buffering. Usually the automatic
4756selection gives you the most convenient kind of buffering for the file
4757or device you open.
4758
4759The use of line buffering for interactive devices implies that output
4760messages ending in a newline will appear immediately---which is usually
4761what you want. Output that doesn't end in a newline might or might not
4762show up immediately, so if you want them to appear immediately, you
4763should flush buffered output explicitly with @code{fflush}, as described
4764in @ref{Flushing Buffers}.
4765
4766@node Flushing Buffers
4767@subsection Flushing Buffers
4768
4769@cindex flushing a stream
4770@dfn{Flushing} output on a buffered stream means transmitting all
4771accumulated characters to the file. There are many circumstances when
4772buffered output on a stream is flushed automatically:
4773
4774@itemize @bullet
4775@item
4776When you try to do output and the output buffer is full.
4777
4778@item
4779When the stream is closed. @xref{Closing Streams}.
4780
19c3f208 4781@item
28f540f4
RM
4782When the program terminates by calling @code{exit}.
4783@xref{Normal Termination}.
4784
4785@item
4786When a newline is written, if the stream is line buffered.
4787
4788@item
4789Whenever an input operation on @emph{any} stream actually reads data
4790from its file.
4791@end itemize
4792
4793If you want to flush the buffered output at another time, call
4794@code{fflush}, which is declared in the header file @file{stdio.h}.
4795@pindex stdio.h
4796
4797@comment stdio.h
f65fd747 4798@comment ISO
28f540f4 4799@deftypefun int fflush (FILE *@var{stream})
171e9210 4800@safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{}}@acunsafe{@aculock{} @acucorrupt{}}}
28f540f4
RM
4801This function causes any buffered output on @var{stream} to be delivered
4802to the file. If @var{stream} is a null pointer, then
4803@code{fflush} causes buffered output on @emph{all} open output streams
4804to be flushed.
4805
4806This function returns @code{EOF} if a write error occurs, or zero
4807otherwise.
4808@end deftypefun
4809
7b4161bb
UD
4810@comment stdio.h
4811@comment POSIX
4812@deftypefun int fflush_unlocked (FILE *@var{stream})
171e9210 4813@safety{@prelim{}@mtsafe{@mtsrace{:stream}}@asunsafe{@asucorrupt{}}@acunsafe{@acucorrupt{}}}
7b4161bb 4814The @code{fflush_unlocked} function is equivalent to the @code{fflush}
1dc843f7 4815function except that it does not implicitly lock the stream.
7b4161bb
UD
4816@end deftypefun
4817
4818The @code{fflush} function can be used to flush all streams currently
4819opened. While this is useful in some situations it does often more than
4820necessary since it might be done in situations when terminal input is
4821required and the program wants to be sure that all output is visible on
4822the terminal. But this means that only line buffered streams have to be
4823flushed. Solaris introduced a function especially for this. It was
1f77f049 4824always available in @theglibc{} in some form but never officially
7b4161bb
UD
4825exported.
4826
9deee4bb 4827@comment stdio_ext.h
7b4161bb
UD
4828@comment GNU
4829@deftypefun void _flushlbf (void)
171e9210 4830@safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{}}@acunsafe{@aculock{} @acucorrupt{}}}
7b4161bb
UD
4831The @code{_flushlbf} function flushes all line buffered streams
4832currently opened.
4833
4834This function is declared in the @file{stdio_ext.h} header.
4835@end deftypefun
4836
28f540f4
RM
4837@strong{Compatibility Note:} Some brain-damaged operating systems have
4838been known to be so thoroughly fixated on line-oriented input and output
4839that flushing a line buffered stream causes a newline to be written!
4840Fortunately, this ``feature'' seems to be becoming less common. You do
a7a93d50 4841not need to worry about this with @theglibc{}.
28f540f4 4842
7b4161bb
UD
4843In some situations it might be useful to not flush the output pending
4844for a stream but instead simply forget it. If transmission is costly
4845and the output is not needed anymore this is valid reasoning. In this
4846situation a non-standard function introduced in Solaris and available in
1f77f049 4847@theglibc{} can be used.
7b4161bb
UD
4848
4849@comment stdio_ext.h
4850@comment GNU
4851@deftypefun void __fpurge (FILE *@var{stream})
171e9210 4852@safety{@prelim{}@mtsafe{@mtsrace{:stream}}@asunsafe{@asucorrupt{}}@acunsafe{@acucorrupt{}}}
7b4161bb
UD
4853The @code{__fpurge} function causes the buffer of the stream
4854@var{stream} to be emptied. If the stream is currently in read mode all
4855input in the buffer is lost. If the stream is in output mode the
4856buffered output is not written to the device (or whatever other
c703cd7a 4857underlying storage) and the buffer is cleared.
7b4161bb
UD
4858
4859This function is declared in @file{stdio_ext.h}.
4860@end deftypefun
28f540f4
RM
4861
4862@node Controlling Buffering
4863@subsection Controlling Which Kind of Buffering
4864
4865After opening a stream (but before any other operations have been
4866performed on it), you can explicitly specify what kind of buffering you
4867want it to have using the @code{setvbuf} function.
4868@cindex buffering, controlling
4869
4870The facilities listed in this section are declared in the header
4871file @file{stdio.h}.
4872@pindex stdio.h
4873
4874@comment stdio.h
f65fd747 4875@comment ISO
28f540f4 4876@deftypefun int setvbuf (FILE *@var{stream}, char *@var{buf}, int @var{mode}, size_t @var{size})
171e9210 4877@safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{}}@acunsafe{@aculock{} @acucorrupt{}}}
28f540f4
RM
4878This function is used to specify that the stream @var{stream} should
4879have the buffering mode @var{mode}, which can be either @code{_IOFBF}
4880(for full buffering), @code{_IOLBF} (for line buffering), or
4881@code{_IONBF} (for unbuffered input/output).
4882
4883If you specify a null pointer as the @var{buf} argument, then @code{setvbuf}
4884allocates a buffer itself using @code{malloc}. This buffer will be freed
4885when you close the stream.
4886
4887Otherwise, @var{buf} should be a character array that can hold at least
4888@var{size} characters. You should not free the space for this array as
4889long as the stream remains open and this array remains its buffer. You
4890should usually either allocate it statically, or @code{malloc}
4891(@pxref{Unconstrained Allocation}) the buffer. Using an automatic array
4892is not a good idea unless you close the file before exiting the block
4893that declares the array.
4894
4895While the array remains a stream buffer, the stream I/O functions will
4896use the buffer for their internal purposes. You shouldn't try to access
4897the values in the array directly while the stream is using it for
4898buffering.
4899
4900The @code{setvbuf} function returns zero on success, or a nonzero value
4901if the value of @var{mode} is not valid or if the request could not
4902be honored.
4903@end deftypefun
4904
4905@comment stdio.h
f65fd747 4906@comment ISO
28f540f4
RM
4907@deftypevr Macro int _IOFBF
4908The value of this macro is an integer constant expression that can be
4909used as the @var{mode} argument to the @code{setvbuf} function to
4910specify that the stream should be fully buffered.
4911@end deftypevr
4912
4913@comment stdio.h
f65fd747 4914@comment ISO
28f540f4
RM
4915@deftypevr Macro int _IOLBF
4916The value of this macro is an integer constant expression that can be
4917used as the @var{mode} argument to the @code{setvbuf} function to
4918specify that the stream should be line buffered.
4919@end deftypevr
4920
4921@comment stdio.h
f65fd747 4922@comment ISO
28f540f4
RM
4923@deftypevr Macro int _IONBF
4924The value of this macro is an integer constant expression that can be
4925used as the @var{mode} argument to the @code{setvbuf} function to
4926specify that the stream should be unbuffered.
4927@end deftypevr
4928
4929@comment stdio.h
f65fd747 4930@comment ISO
28f540f4
RM
4931@deftypevr Macro int BUFSIZ
4932The value of this macro is an integer constant expression that is good
4933to use for the @var{size} argument to @code{setvbuf}. This value is
4934guaranteed to be at least @code{256}.
4935
4936The value of @code{BUFSIZ} is chosen on each system so as to make stream
19c3f208 4937I/O efficient. So it is a good idea to use @code{BUFSIZ} as the size
28f540f4
RM
4938for the buffer when you call @code{setvbuf}.
4939
4940Actually, you can get an even better value to use for the buffer size
4941by means of the @code{fstat} system call: it is found in the
4942@code{st_blksize} field of the file attributes. @xref{Attribute Meanings}.
4943
4944Sometimes people also use @code{BUFSIZ} as the allocation size of
4945buffers used for related purposes, such as strings used to receive a
4946line of input with @code{fgets} (@pxref{Character Input}). There is no
4947particular reason to use @code{BUFSIZ} for this instead of any other
4948integer, except that it might lead to doing I/O in chunks of an
4949efficient size.
4950@end deftypevr
4951
4952@comment stdio.h
f65fd747 4953@comment ISO
28f540f4 4954@deftypefun void setbuf (FILE *@var{stream}, char *@var{buf})
171e9210 4955@safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{}}@acunsafe{@aculock{} @acucorrupt{}}}
28f540f4
RM
4956If @var{buf} is a null pointer, the effect of this function is
4957equivalent to calling @code{setvbuf} with a @var{mode} argument of
4958@code{_IONBF}. Otherwise, it is equivalent to calling @code{setvbuf}
4959with @var{buf}, and a @var{mode} of @code{_IOFBF} and a @var{size}
4960argument of @code{BUFSIZ}.
4961
4962The @code{setbuf} function is provided for compatibility with old code;
4963use @code{setvbuf} in all new programs.
4964@end deftypefun
4965
4966@comment stdio.h
4967@comment BSD
4968@deftypefun void setbuffer (FILE *@var{stream}, char *@var{buf}, size_t @var{size})
171e9210 4969@safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{}}@acunsafe{@aculock{} @acucorrupt{}}}
28f540f4
RM
4970If @var{buf} is a null pointer, this function makes @var{stream} unbuffered.
4971Otherwise, it makes @var{stream} fully buffered using @var{buf} as the
4972buffer. The @var{size} argument specifies the length of @var{buf}.
4973
4974This function is provided for compatibility with old BSD code. Use
4975@code{setvbuf} instead.
4976@end deftypefun
4977
4978@comment stdio.h
4979@comment BSD
4980@deftypefun void setlinebuf (FILE *@var{stream})
171e9210 4981@safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{}}@acunsafe{@aculock{} @acucorrupt{}}}
28f540f4
RM
4982This function makes @var{stream} be line buffered, and allocates the
4983buffer for you.
4984
4985This function is provided for compatibility with old BSD code. Use
4986@code{setvbuf} instead.
4987@end deftypefun
4988
7b4161bb 4989It is possible to query whether a given stream is line buffered or not
1f77f049
JM
4990using a non-standard function introduced in Solaris and available in
4991@theglibc{}.
7b4161bb
UD
4992
4993@comment stdio_ext.h
4994@comment GNU
4995@deftypefun int __flbf (FILE *@var{stream})
171e9210 4996@safety{@prelim{}@mtsafe{}@assafe{}@acsafe{}}
7b4161bb
UD
4997The @code{__flbf} function will return a nonzero value in case the
4998stream @var{stream} is line buffered. Otherwise the return value is
4999zero.
5000
5001This function is declared in the @file{stdio_ext.h} header.
5002@end deftypefun
5003
5004Two more extensions allow to determine the size of the buffer and how
5005much of it is used. These functions were also introduced in Solaris.
5006
5007@comment stdio_ext.h
5008@comment GNU
5009@deftypefun size_t __fbufsize (FILE *@var{stream})
171e9210 5010@safety{@prelim{}@mtsafe{@mtsrace{:stream}}@asunsafe{@asucorrupt{}}@acsafe{}}
7b4161bb
UD
5011The @code{__fbufsize} function return the size of the buffer in the
5012stream @var{stream}. This value can be used to optimize the use of the
5013stream.
5014
5015This function is declared in the @file{stdio_ext.h} header.
5016@end deftypefun
5017
5018@comment stdio_ext.h
5019@comment GNU
cc6e48bc 5020@deftypefun size_t __fpending (FILE *@var{stream})
171e9210 5021@safety{@prelim{}@mtsafe{@mtsrace{:stream}}@asunsafe{@asucorrupt{}}@acsafe{}}
cc6e48bc 5022The @code{__fpending}
f126ef67 5023function returns the number of bytes currently in the output buffer.
c703cd7a 5024For wide-oriented streams the measuring unit is wide characters. This
f126ef67 5025function should not be used on buffers in read mode or opened read-only.
7b4161bb
UD
5026
5027This function is declared in the @file{stdio_ext.h} header.
5028@end deftypefun
5029
28f540f4
RM
5030@node Other Kinds of Streams
5031@section Other Kinds of Streams
5032
1f77f049 5033@Theglibc{} provides ways for you to define additional kinds of
28f540f4
RM
5034streams that do not necessarily correspond to an open file.
5035
5036One such type of stream takes input from or writes output to a string.
5037These kinds of streams are used internally to implement the
5038@code{sprintf} and @code{sscanf} functions. You can also create such a
5039stream explicitly, using the functions described in @ref{String Streams}.
5040
5041More generally, you can define streams that do input/output to arbitrary
5042objects using functions supplied by your program. This protocol is
5043discussed in @ref{Custom Streams}.
5044
5045@strong{Portability Note:} The facilities described in this section are
5046specific to GNU. Other systems or C implementations might or might not
5047provide equivalent functionality.
5048
5049@menu
19c3f208 5050* String Streams:: Streams that get data from or put data in
162ba701 5051 a string or memory buffer.
28f540f4 5052* Custom Streams:: Defining your own streams with an arbitrary
162ba701 5053 input data source and/or output data sink.
28f540f4
RM
5054@end menu
5055
5056@node String Streams
5057@subsection String Streams
5058
5059@cindex stream, for I/O to a string
5060@cindex string stream
5061The @code{fmemopen} and @code{open_memstream} functions allow you to do
5062I/O to a string or memory buffer. These facilities are declared in
5063@file{stdio.h}.
5064@pindex stdio.h
5065
5066@comment stdio.h
5067@comment GNU
5068@deftypefun {FILE *} fmemopen (void *@var{buf}, size_t @var{size}, const char *@var{opentype})
171e9210
AO
5069@safety{@prelim{}@mtsafe{}@asunsafe{@ascuheap{} @asulock{}}@acunsafe{@acsmem{} @aculock{}}}
5070@c Unlike open_memstream, fmemopen does (indirectly) call _IO_link_in,
5071@c bringing with it additional potential for async trouble with
5072@c list_all_lock.
28f540f4
RM
5073This function opens a stream that allows the access specified by the
5074@var{opentype} argument, that reads from or writes to the buffer specified
5075by the argument @var{buf}. This array must be at least @var{size} bytes long.
5076
5077If you specify a null pointer as the @var{buf} argument, @code{fmemopen}
04b9968b
UD
5078dynamically allocates an array @var{size} bytes long (as with @code{malloc};
5079@pxref{Unconstrained Allocation}). This is really only useful
28f540f4
RM
5080if you are going to write things to the buffer and then read them back
5081in again, because you have no way of actually getting a pointer to the
5082buffer (for this, try @code{open_memstream}, below). The buffer is
417d6b34 5083freed when the stream is closed.
28f540f4
RM
5084
5085The argument @var{opentype} is the same as in @code{fopen}
8b7fb588 5086(@pxref{Opening Streams}). If the @var{opentype} specifies
28f540f4
RM
5087append mode, then the initial file position is set to the first null
5088character in the buffer. Otherwise the initial file position is at the
5089beginning of the buffer.
5090
5091When a stream open for writing is flushed or closed, a null character
5092(zero byte) is written at the end of the buffer if it fits. You
5093should add an extra byte to the @var{size} argument to account for this.
5094Attempts to write more than @var{size} bytes to the buffer result
5095in an error.
5096
5097For a stream open for reading, null characters (zero bytes) in the
5098buffer do not count as ``end of file''. Read operations indicate end of
5099file only when the file position advances past @var{size} bytes. So, if
5100you want to read characters from a null-terminated string, you should
5101supply the length of the string as the @var{size} argument.
5102@end deftypefun
5103
5104Here is an example of using @code{fmemopen} to create a stream for
5105reading from a string:
5106
5107@smallexample
5108@include memopen.c.texi
5109@end smallexample
5110
5111This program produces the following output:
5112
5113@smallexample
5114Got f
5115Got o
5116Got o
5117Got b
5118Got a
5119Got r
5120@end smallexample
5121
5122@comment stdio.h
5123@comment GNU
5124@deftypefun {FILE *} open_memstream (char **@var{ptr}, size_t *@var{sizeloc})
171e9210 5125@safety{@prelim{}@mtsafe{}@asunsafe{@ascuheap{}}@acunsafe{@acsmem{}}}
28f540f4 5126This function opens a stream for writing to a buffer. The buffer is
29402b12
RM
5127allocated dynamically and grown as necessary, using @code{malloc}.
5128After you've closed the stream, this buffer is your responsibility to
5129clean up using @code{free} or @code{realloc}. @xref{Unconstrained Allocation}.
28f540f4
RM
5130
5131When the stream is closed with @code{fclose} or flushed with
5132@code{fflush}, the locations @var{ptr} and @var{sizeloc} are updated to
5133contain the pointer to the buffer and its size. The values thus stored
5134remain valid only as long as no further output on the stream takes
5135place. If you do more output, you must flush the stream again to store
5136new values before you use them again.
5137
5138A null character is written at the end of the buffer. This null character
5139is @emph{not} included in the size value stored at @var{sizeloc}.
5140
a5a0310d
UD
5141You can move the stream's file position with @code{fseek} or
5142@code{fseeko} (@pxref{File Positioning}). Moving the file position past
5143the end of the data already written fills the intervening space with
5144zeroes.
28f540f4
RM
5145@end deftypefun
5146
5147Here is an example of using @code{open_memstream}:
5148
5149@smallexample
5150@include memstrm.c.texi
5151@end smallexample
5152
5153This program produces the following output:
5154
5155@smallexample
5156buf = `hello', size = 5
5157buf = `hello, world', size = 12
5158@end smallexample
5159
28f540f4
RM
5160@node Custom Streams
5161@subsection Programming Your Own Custom Streams
5162@cindex custom streams
5163@cindex programming your own streams
5164
5165This section describes how you can make a stream that gets input from an
5166arbitrary data source or writes output to an arbitrary data sink
52cea457
UD
5167programmed by you. We call these @dfn{custom streams}. The functions
5168and types described here are all GNU extensions.
28f540f4
RM
5169
5170@c !!! this does not talk at all about the higher-level hooks
5171
5172@menu
5173* Streams and Cookies:: The @dfn{cookie} records where to fetch or
162ba701 5174 store data that is read or written.
28f540f4 5175* Hook Functions:: How you should define the four @dfn{hook
162ba701 5176 functions} that a custom stream needs.
28f540f4
RM
5177@end menu
5178
5179@node Streams and Cookies
5180@subsubsection Custom Streams and Cookies
5181@cindex cookie, for custom stream
5182
5183Inside every custom stream is a special object called the @dfn{cookie}.
5184This is an object supplied by you which records where to fetch or store
5185the data read or written. It is up to you to define a data type to use
5186for the cookie. The stream functions in the library never refer
5187directly to its contents, and they don't even know what the type is;
5188they record its address with type @code{void *}.
5189
5190To implement a custom stream, you must specify @emph{how} to fetch or
5191store the data in the specified place. You do this by defining
5192@dfn{hook functions} to read, write, change ``file position'', and close
5193the stream. All four of these functions will be passed the stream's
5194cookie so they can tell where to fetch or store the data. The library
5195functions don't know what's inside the cookie, but your functions will
5196know.
5197
5198When you create a custom stream, you must specify the cookie pointer,
19c3f208 5199and also the four hook functions stored in a structure of type
28f540f4
RM
5200@code{cookie_io_functions_t}.
5201
5202These facilities are declared in @file{stdio.h}.
5203@pindex stdio.h
5204
5205@comment stdio.h
5206@comment GNU
5207@deftp {Data Type} {cookie_io_functions_t}
19c3f208 5208This is a structure type that holds the functions that define the
28f540f4
RM
5209communications protocol between the stream and its cookie. It has
5210the following members:
5211
5212@table @code
5213@item cookie_read_function_t *read
5214This is the function that reads data from the cookie. If the value is a
f2ea0f5b 5215null pointer instead of a function, then read operations on this stream
28f540f4
RM
5216always return @code{EOF}.
5217
5218@item cookie_write_function_t *write
5219This is the function that writes data to the cookie. If the value is a
5220null pointer instead of a function, then data written to the stream is
5221discarded.
5222
5223@item cookie_seek_function_t *seek
5224This is the function that performs the equivalent of file positioning on
5225the cookie. If the value is a null pointer instead of a function, calls
a5a0310d
UD
5226to @code{fseek} or @code{fseeko} on this stream can only seek to
5227locations within the buffer; any attempt to seek outside the buffer will
5228return an @code{ESPIPE} error.
28f540f4
RM
5229
5230@item cookie_close_function_t *close
5231This function performs any appropriate cleanup on the cookie when
5232closing the stream. If the value is a null pointer instead of a
5233function, nothing special is done to close the cookie when the stream is
5234closed.
5235@end table
5236@end deftp
5237
5238@comment stdio.h
5239@comment GNU
5240@deftypefun {FILE *} fopencookie (void *@var{cookie}, const char *@var{opentype}, cookie_io_functions_t @var{io-functions})
171e9210 5241@safety{@prelim{}@mtsafe{}@asunsafe{@ascuheap{} @asulock{}}@acunsafe{@acsmem{} @aculock{}}}
28f540f4
RM
5242This function actually creates the stream for communicating with the
5243@var{cookie} using the functions in the @var{io-functions} argument.
5244The @var{opentype} argument is interpreted as for @code{fopen};
5245see @ref{Opening Streams}. (But note that the ``truncate on
5246open'' option is ignored.) The new stream is fully buffered.
5247
5248The @code{fopencookie} function returns the newly created stream, or a null
5249pointer in case of an error.
5250@end deftypefun
5251
5252@node Hook Functions
5253@subsubsection Custom Stream Hook Functions
5254@cindex hook functions (of custom streams)
5255
5256Here are more details on how you should define the four hook functions
5257that a custom stream needs.
5258
5259You should define the function to read data from the cookie as:
5260
5261@smallexample
62ecda3d 5262ssize_t @var{reader} (void *@var{cookie}, char *@var{buffer}, size_t @var{size})
28f540f4
RM
5263@end smallexample
5264
5265This is very similar to the @code{read} function; see @ref{I/O
5266Primitives}. Your function should transfer up to @var{size} bytes into
5267the @var{buffer}, and return the number of bytes read, or zero to
5268indicate end-of-file. You can return a value of @code{-1} to indicate
5269an error.
5270
5271You should define the function to write data to the cookie as:
5272
5273@smallexample
62ecda3d 5274ssize_t @var{writer} (void *@var{cookie}, const char *@var{buffer}, size_t @var{size})
28f540f4
RM
5275@end smallexample
5276
5277This is very similar to the @code{write} function; see @ref{I/O
5278Primitives}. Your function should transfer up to @var{size} bytes from
5279the buffer, and return the number of bytes written. You can return a
247c3ede
PB
5280value of @code{0} to indicate an error. You must not return any
5281negative value.
28f540f4
RM
5282
5283You should define the function to perform seek operations on the cookie
5284as:
5285
5286@smallexample
c1e36819 5287int @var{seeker} (void *@var{cookie}, off64_t *@var{position}, int @var{whence})
28f540f4
RM
5288@end smallexample
5289
5290For this function, the @var{position} and @var{whence} arguments are
c1e36819 5291interpreted as for @code{fgetpos}; see @ref{Portable Positioning}.
28f540f4 5292
19c3f208 5293After doing the seek operation, your function should store the resulting
28f540f4
RM
5294file position relative to the beginning of the file in @var{position}.
5295Your function should return a value of @code{0} on success and @code{-1}
5296to indicate an error.
5297
5298You should define the function to do cleanup operations on the cookie
5299appropriate for closing the stream as:
5300
5301@smallexample
5302int @var{cleaner} (void *@var{cookie})
5303@end smallexample
5304
5305Your function should return @code{-1} to indicate an error, and @code{0}
5306otherwise.
5307
5308@comment stdio.h
5309@comment GNU
fff763a5 5310@deftp {Data Type} cookie_read_function_t
28f540f4
RM
5311This is the data type that the read function for a custom stream should have.
5312If you declare the function as shown above, this is the type it will have.
5313@end deftp
5314
5315@comment stdio.h
5316@comment GNU
fff763a5 5317@deftp {Data Type} cookie_write_function_t
28f540f4
RM
5318The data type of the write function for a custom stream.
5319@end deftp
5320
5321@comment stdio.h
5322@comment GNU
fff763a5 5323@deftp {Data Type} cookie_seek_function_t
28f540f4
RM
5324The data type of the seek function for a custom stream.
5325@end deftp
5326
5327@comment stdio.h
5328@comment GNU
fff763a5 5329@deftp {Data Type} cookie_close_function_t
28f540f4
RM
5330The data type of the close function for a custom stream.
5331@end deftp
5332
5333@ignore
5334Roland says:
5335
5336@quotation
5337There is another set of functions one can give a stream, the
5338input-room and output-room functions. These functions must
5339understand stdio internals. To describe how to use these
5340functions, you also need to document lots of how stdio works
5341internally (which isn't relevant for other uses of stdio).
5342Perhaps I can write an interface spec from which you can write
5343good documentation. But it's pretty complex and deals with lots
5344of nitty-gritty details. I think it might be better to let this
5345wait until the rest of the manual is more done and polished.
5346@end quotation
5347@end ignore
5348
5349@c ??? This section could use an example.
0501d603
UD
5350
5351
5352@node Formatted Messages
5353@section Formatted Messages
5354@cindex formatted messages
5355
5356On systems which are based on System V messages of programs (especially
5357the system tools) are printed in a strict form using the @code{fmtmsg}
5358function. The uniformity sometimes helps the user to interpret messages
cf29ffbe 5359and the strictness tests of the @code{fmtmsg} function ensure that the
0501d603
UD
5360programmer follows some minimal requirements.
5361
5362@menu
5363* Printing Formatted Messages:: The @code{fmtmsg} function.
5364* Adding Severity Classes:: Add more severity classes.
5365* Example:: How to use @code{fmtmsg} and @code{addseverity}.
5366@end menu
5367
5368
5369@node Printing Formatted Messages
5370@subsection Printing Formatted Messages
5371
5372Messages can be printed to standard error and/or to the console. To
cf29ffbe 5373select the destination the programmer can use the following two values,
0501d603
UD
5374bitwise OR combined if wanted, for the @var{classification} parameter of
5375@code{fmtmsg}:
5376
5377@vtable @code
5378@item MM_PRINT
5379Display the message in standard error.
5380@item MM_CONSOLE
5381Display the message on the system console.
5382@end vtable
5383
f2ea0f5b 5384The erroneous piece of the system can be signalled by exactly one of the
0501d603
UD
5385following values which also is bitwise ORed with the
5386@var{classification} parameter to @code{fmtmsg}:
5387
5388@vtable @code
5389@item MM_HARD
5390The source of the condition is some hardware.
5391@item MM_SOFT
5392The source of the condition is some software.
5393@item MM_FIRM
5394The source of the condition is some firmware.
5395@end vtable
5396
5397A third component of the @var{classification} parameter to @code{fmtmsg}
5398can describe the part of the system which detects the problem. This is
5399done by using exactly one of the following values:
5400
5401@vtable @code
5402@item MM_APPL
f2ea0f5b 5403The erroneous condition is detected by the application.
0501d603 5404@item MM_UTIL
f2ea0f5b 5405The erroneous condition is detected by a utility.
0501d603 5406@item MM_OPSYS
f2ea0f5b 5407The erroneous condition is detected by the operating system.
0501d603
UD
5408@end vtable
5409
5410A last component of @var{classification} can signal the results of this
5411message. Exactly one of the following values can be used:
5412
5413@vtable @code
5414@item MM_RECOVER
5415It is a recoverable error.
5416@item MM_NRECOV
5417It is a non-recoverable error.
5418@end vtable
5419
5420@comment fmtmsg.h
5421@comment XPG
5422@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})
171e9210 5423@safety{@prelim{}@mtsafe{}@asunsafe{@asulock{}}@acsafe{}}
0501d603
UD
5424Display a message described by its parameters on the device(s) specified
5425in the @var{classification} parameter. The @var{label} parameter
5426identifies the source of the message. The string should consist of two
5427colon separated parts where the first part has not more than 10 and the
04b9968b 5428second part not more than 14 characters. The @var{text} parameter
f2ea0f5b 5429describes the condition of the error, the @var{action} parameter possible
0501d603
UD
5430steps to recover from the error and the @var{tag} parameter is a
5431reference to the online documentation where more information can be
5432found. It should contain the @var{label} value and a unique
5433identification number.
5434
cf29ffbe 5435Each of the parameters can be a special value which means this value
0501d603
UD
5436is to be omitted. The symbolic names for these values are:
5437
5438@vtable @code
5439@item MM_NULLLBL
5440Ignore @var{label} parameter.
5441@item MM_NULLSEV
5442Ignore @var{severity} parameter.
5443@item MM_NULLMC
5444Ignore @var{classification} parameter. This implies that nothing is
5445actually printed.
5446@item MM_NULLTXT
5447Ignore @var{text} parameter.
5448@item MM_NULLACT
5449Ignore @var{action} parameter.
5450@item MM_NULLTAG
5451Ignore @var{tag} parameter.
5452@end vtable
5453
cf29ffbe 5454There is another way certain fields can be omitted from the output to
0501d603 5455standard error. This is described below in the description of
0bc93a2f 5456environment variables influencing the behavior.
0501d603
UD
5457
5458The @var{severity} parameter can have one of the values in the following
5459table:
5460@cindex severity class
5461
5462@vtable @code
5463@item MM_NOSEV
5464Nothing is printed, this value is the same as @code{MM_NULLSEV}.
5465@item MM_HALT
5466This value is printed as @code{HALT}.
5467@item MM_ERROR
5468This value is printed as @code{ERROR}.
5469@item MM_WARNING
5470This value is printed as @code{WARNING}.
5471@item MM_INFO
5472This value is printed as @code{INFO}.
5473@end vtable
5474
5475The numeric value of these five macros are between @code{0} and
5476@code{4}. Using the environment variable @code{SEV_LEVEL} or using the
5477@code{addseverity} function one can add more severity levels with their
5478corresponding string to print. This is described below
5479(@pxref{Adding Severity Classes}).
5480
5481@noindent
5482If no parameter is ignored the output looks like this:
5483
5484@smallexample
5485@var{label}: @var{severity-string}: @var{text}
5486TO FIX: @var{action} @var{tag}
5487@end smallexample
5488
5489The colons, new line characters and the @code{TO FIX} string are
5490inserted if necessary, i.e., if the corresponding parameter is not
5491ignored.
5492
5493This function is specified in the X/Open Portability Guide. It is also
04b9968b 5494available on all systems derived from System V.
0501d603 5495
cf29ffbe 5496The function returns the value @code{MM_OK} if no error occurred. If
0501d603
UD
5497only the printing to standard error failed, it returns @code{MM_NOMSG}.
5498If printing to the console fails, it returns @code{MM_NOCON}. If
cf29ffbe 5499nothing is printed @code{MM_NOTOK} is returned. Among situations where
0501d603
UD
5500all outputs fail this last value is also returned if a parameter value
5501is incorrect.
5502@end deftypefun
5503
0bc93a2f 5504There are two environment variables which influence the behavior of
0501d603
UD
5505@code{fmtmsg}. The first is @code{MSGVERB}. It is used to control the
5506output actually happening on standard error (@emph{not} the console
04b9968b 5507output). Each of the five fields can explicitly be enabled. To do
0501d603 5508this the user has to put the @code{MSGVERB} variable with a format like
cf29ffbe 5509the following in the environment before calling the @code{fmtmsg} function
0501d603
UD
5510the first time:
5511
5512@smallexample
95fdc6a0 5513MSGVERB=@var{keyword}[:@var{keyword}[:@dots{}]]
0501d603
UD
5514@end smallexample
5515
5516Valid @var{keyword}s are @code{label}, @code{severity}, @code{text},
5517@code{action}, and @code{tag}. If the environment variable is not given
5518or is the empty string, a not supported keyword is given or the value is
5519somehow else invalid, no part of the message is masked out.
5520
0bc93a2f 5521The second environment variable which influences the behavior of
0501d603 5522@code{fmtmsg} is @code{SEV_LEVEL}. This variable and the change in the
0bc93a2f 5523behavior of @code{fmtmsg} is not specified in the X/Open Portability
0501d603 5524Guide. It is available in System V systems, though. It can be used to
cf29ffbe 5525introduce new severity levels. By default, only the five severity levels
0501d603
UD
5526described above are available. Any other numeric value would make
5527@code{fmtmsg} print nothing.
5528
5529If the user puts @code{SEV_LEVEL} with a format like
5530
5531@smallexample
95fdc6a0 5532SEV_LEVEL=[@var{description}[:@var{description}[:@dots{}]]]
0501d603
UD
5533@end smallexample
5534
5535@noindent
5536in the environment of the process before the first call to
5537@code{fmtmsg}, where @var{description} has a value of the form
5538
5539@smallexample
5540@var{severity-keyword},@var{level},@var{printstring}
5541@end smallexample
5542
5543The @var{severity-keyword} part is not used by @code{fmtmsg} but it has
5544to be present. The @var{level} part is a string representation of a
5545number. The numeric value must be a number greater than 4. This value
5546must be used in the @var{severity} parameter of @code{fmtmsg} to select
5547this class. It is not possible to overwrite any of the predefined
5548classes. The @var{printstring} is the string printed when a message of
5549this class is processed by @code{fmtmsg} (see above, @code{fmtsmg} does
5550not print the numeric value but instead the string representation).
5551
5552
5553@node Adding Severity Classes
5554@subsection Adding Severity Classes
5555@cindex severity class
5556
04b9968b 5557There is another possibility to introduce severity classes besides using
0501d603
UD
5558the environment variable @code{SEV_LEVEL}. This simplifies the task of
5559introducing new classes in a running program. One could use the
5560@code{setenv} or @code{putenv} function to set the environment variable,
cf29ffbe 5561but this is toilsome.
0501d603
UD
5562
5563@deftypefun int addseverity (int @var{severity}, const char *@var{string})
171e9210 5564@safety{@prelim{}@mtsafe{}@asunsafe{@ascuheap{} @asulock{}}@acunsafe{@aculock{} @acsmem{}}}
04b9968b 5565This function allows the introduction of new severity classes which can be
0501d603
UD
5566addressed by the @var{severity} parameter of the @code{fmtmsg} function.
5567The @var{severity} parameter of @code{addseverity} must match the value
04b9968b 5568for the parameter with the same name of @code{fmtmsg}, and @var{string}
0501d603
UD
5569is the string printed in the actual messages instead of the numeric
5570value.
5571
5572If @var{string} is @code{NULL} the severity class with the numeric value
5573according to @var{severity} is removed.
5574
cf29ffbe
UD
5575It is not possible to overwrite or remove one of the default severity
5576classes. All calls to @code{addseverity} with @var{severity} set to one
5577of the values for the default classes will fail.
5578
0501d603
UD
5579The return value is @code{MM_OK} if the task was successfully performed.
5580If the return value is @code{MM_NOTOK} something went wrong. This could
5581mean that no more memory is available or a class is not available when
5582it has to be removed.
5583
5584This function is not specified in the X/Open Portability Guide although
cf29ffbe 5585the @code{fmtsmg} function is. It is available on System V systems.
0501d603
UD
5586@end deftypefun
5587
5588
5589@node Example
5590@subsection How to use @code{fmtmsg} and @code{addseverity}
5591
c703cd7a 5592Here is a simple example program to illustrate the use of both
0501d603
UD
5593functions described in this section.
5594
5595@smallexample
5596@include fmtmsgexpl.c.texi
5597@end smallexample
5598
04b9968b
UD
5599The second call to @code{fmtmsg} illustrates a use of this function as
5600it usually occurs on System V systems, which heavily use this function.
5601It seems worthwhile to give a short explanation here of how this system
5602works on System V. The value of the
0bc93a2f 5603@var{label} field (@code{UX:cat}) says that the error occurred in the
0501d603
UD
5604Unix program @code{cat}. The explanation of the error follows and the
5605value for the @var{action} parameter is @code{"refer to manual"}. One
04b9968b 5606could be more specific here, if necessary. The @var{tag} field contains,
0501d603
UD
5607as proposed above, the value of the string given for the @var{label}
5608parameter, and additionally a unique ID (@code{001} in this case). For
5609a GNU environment this string could contain a reference to the
5610corresponding node in the Info page for the program.
5611
5612@noindent
5613Running this program without specifying the @code{MSGVERB} and
5614@code{SEV_LEVEL} function produces the following output:
5615
5616@smallexample
5617UX:cat: NOTE2: invalid syntax
5618TO FIX: refer to manual UX:cat:001
5619@end smallexample
5620
5621We see the different fields of the message and how the extra glue (the
c703cd7a 5622colons and the @code{TO FIX} string) is printed. But only one of the
0501d603
UD
5623three calls to @code{fmtmsg} produced output. The first call does not
5624print anything because the @var{label} parameter is not in the correct
8b7fb588
UD
5625form. The string must contain two fields, separated by a colon
5626(@pxref{Printing Formatted Messages}). The third @code{fmtmsg} call
0501d603
UD
5627produced no output since the class with the numeric value @code{6} is
5628not defined. Although a class with numeric value @code{5} is also not
04b9968b 5629defined by default, the call to @code{addseverity} introduces it and
0ea554bf 5630the second call to @code{fmtmsg} produces the above output.
0501d603
UD
5631
5632When we change the environment of the program to contain
5633@code{SEV_LEVEL=XXX,6,NOTE} when running it we get a different result:
5634
5635@smallexample
5636UX:cat: NOTE2: invalid syntax
5637TO FIX: refer to manual UX:cat:001
5638label:foo: NOTE: text
5639TO FIX: action tag
5640@end smallexample
5641
04b9968b 5642Now the third call to @code{fmtmsg} produced some output and we see how
0501d603
UD
5643the string @code{NOTE} from the environment variable appears in the
5644message.
5645
04b9968b 5646Now we can reduce the output by specifying which fields we are
0501d603
UD
5647interested in. If we additionally set the environment variable
5648@code{MSGVERB} to the value @code{severity:label:action} we get the
5649following output:
5650
5651@smallexample
5652UX:cat: NOTE2
5653TO FIX: refer to manual
5654label:foo: NOTE
5655TO FIX: action
5656@end smallexample
5657
5658@noindent
5659I.e., the output produced by the @var{text} and the @var{tag} parameters
cf29ffbe 5660to @code{fmtmsg} vanished. Please also note that now there is no colon
0501d603 5661after the @code{NOTE} and @code{NOTE2} strings in the output. This is
04b9968b 5662not necessary since there is no more output on this line because the text
0501d603 5663is missing.