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