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