Remove powerpc, sparc fdim inlines (bug 22987).
[glibc.git] / manual / errno.texi
blobb195b635f1042d284dbcec1e7e2b42d352aa5f4b
1 @node Error Reporting, Memory, Introduction, Top
2 @chapter Error Reporting
3 @c %MENU% How library functions report errors
4 @cindex error reporting
5 @cindex reporting errors
6 @cindex error codes
7 @cindex status codes
9 Many functions in @theglibc{} detect and report error conditions,
10 and sometimes your programs need to check for these error conditions.
11 For example, when you open an input file, you should verify that the
12 file was actually opened correctly, and print an error message or take
13 other appropriate action if the call to the library function failed.
15 This chapter describes how the error reporting facility works.  Your
16 program should include the header file @file{errno.h} to use this
17 facility.
18 @pindex errno.h
20 @menu
21 * Checking for Errors::         How errors are reported by library functions.
22 * Error Codes::                 Error code macros; all of these expand
23                                  into integer constant values.
24 * Error Messages::              Mapping error codes onto error messages.
25 @end menu
27 @node Checking for Errors, Error Codes,  , Error Reporting
28 @section Checking for Errors
30 Most library functions return a special value to indicate that they have
31 failed.  The special value is typically @code{-1}, a null pointer, or a
32 constant such as @code{EOF} that is defined for that purpose.  But this
33 return value tells you only that an error has occurred.  To find out
34 what kind of error it was, you need to look at the error code stored in the
35 variable @code{errno}.  This variable is declared in the header file
36 @file{errno.h}.
37 @pindex errno.h
39 @deftypevr {Variable} {volatile int} errno
40 @standards{ISO, errno.h}
41 The variable @code{errno} contains the system error number.  You can
42 change the value of @code{errno}.
44 Since @code{errno} is declared @code{volatile}, it might be changed
45 asynchronously by a signal handler; see @ref{Defining Handlers}.
46 However, a properly written signal handler saves and restores the value
47 of @code{errno}, so you generally do not need to worry about this
48 possibility except when writing signal handlers.
50 The initial value of @code{errno} at program startup is zero.  In many
51 cases, when a library function encounters an error, it will set
52 @code{errno} to a non-zero value to indicate what specific error
53 condition occurred.  The documentation for each function lists the
54 error conditions that are possible for that function.  Not all library
55 functions use this mechanism; some return an error code directly,
56 instead.
58 @strong{Warning:} Many library functions may set @code{errno} to some
59 meaningless non-zero value even if they did not encounter any errors,
60 and even if they return error codes directly.  Therefore, it is
61 usually incorrect to check @emph{whether} an error occurred by
62 inspecting the value of @code{errno}.  The proper way to check for
63 error is documented for each function.
65 @strong{Portability Note:} @w{ISO C} specifies @code{errno} as a
66 ``modifiable lvalue'' rather than as a variable, permitting it to be
67 implemented as a macro.  For example, its expansion might involve a
68 function call, like @w{@code{*__errno_location ()}}.  In fact, that is
69 what it is
70 on @gnulinuxhurdsystems{}.  @Theglibc{}, on each system, does
71 whatever is right for the particular system.
73 There are a few library functions, like @code{sqrt} and @code{atan},
74 that return a perfectly legitimate value in case of an error, but also
75 set @code{errno}.  For these functions, if you want to check to see
76 whether an error occurred, the recommended method is to set @code{errno}
77 to zero before calling the function, and then check its value afterward.
78 @end deftypevr
80 @pindex errno.h
81 All the error codes have symbolic names; they are macros defined in
82 @file{errno.h}.  The names start with @samp{E} and an upper-case
83 letter or digit; you should consider names of this form to be
84 reserved names.  @xref{Reserved Names}.
86 The error code values are all positive integers and are all distinct,
87 with one exception: @code{EWOULDBLOCK} and @code{EAGAIN} are the same.
88 Since the values are distinct, you can use them as labels in a
89 @code{switch} statement; just don't use both @code{EWOULDBLOCK} and
90 @code{EAGAIN}.  Your program should not make any other assumptions about
91 the specific values of these symbolic constants.
93 The value of @code{errno} doesn't necessarily have to correspond to any
94 of these macros, since some library functions might return other error
95 codes of their own for other situations.  The only values that are
96 guaranteed to be meaningful for a particular library function are the
97 ones that this manual lists for that function.
99 Except on @gnuhurdsystems{}, almost any system call can return @code{EFAULT} if
100 it is given an invalid pointer as an argument.  Since this could only
101 happen as a result of a bug in your program, and since it will not
102 happen on @gnuhurdsystems{}, we have saved space by not mentioning
103 @code{EFAULT} in the descriptions of individual functions.
105 In some Unix systems, many system calls can also return @code{EFAULT} if
106 given as an argument a pointer into the stack, and the kernel for some
107 obscure reason fails in its attempt to extend the stack.  If this ever
108 happens, you should probably try using statically or dynamically
109 allocated memory instead of stack memory on that system.
111 @node Error Codes, Error Messages, Checking for Errors, Error Reporting
112 @section Error Codes
114 @pindex errno.h
115 The error code macros are defined in the header file @file{errno.h}.
116 All of them expand into integer constant values.  Some of these error
117 codes can't occur on @gnusystems{}, but they can occur using @theglibc{}
118 on other systems.
120 @deftypevr Macro int EPERM
121 @standards{POSIX.1, errno.h}
122 @errno{EPERM, 1, Operation not permitted}
123 Only the owner of the file (or other resource)
124 or processes with special privileges can perform the operation.
125 @end deftypevr
127 @deftypevr Macro int ENOENT
128 @standards{POSIX.1, errno.h}
129 @errno{ENOENT, 2, No such file or directory}
130 This is a ``file doesn't exist'' error
131 for ordinary files that are referenced in contexts where they are
132 expected to already exist.
133 @end deftypevr
135 @deftypevr Macro int ESRCH
136 @standards{POSIX.1, errno.h}
137 @errno{ESRCH, 3, No such process}
138 No process matches the specified process ID.
139 @end deftypevr
141 @deftypevr Macro int EINTR
142 @standards{POSIX.1, errno.h}
143 @errno{EINTR, 4, Interrupted system call}
144 An asynchronous signal occurred and prevented
145 completion of the call.  When this happens, you should try the call
146 again.
148 You can choose to have functions resume after a signal that is handled,
149 rather than failing with @code{EINTR}; see @ref{Interrupted
150 Primitives}.
151 @end deftypevr
153 @deftypevr Macro int EIO
154 @standards{POSIX.1, errno.h}
155 @errno{EIO, 5, Input/output error}
156 Usually used for physical read or write errors.
157 @end deftypevr
159 @deftypevr Macro int ENXIO
160 @standards{POSIX.1, errno.h}
161 @errno{ENXIO, 6, No such device or address}
162 The system tried to use the device
163 represented by a file you specified, and it couldn't find the device.
164 This can mean that the device file was installed incorrectly, or that
165 the physical device is missing or not correctly attached to the
166 computer.
167 @end deftypevr
169 @deftypevr Macro int E2BIG
170 @standards{POSIX.1, errno.h}
171 @errno{E2BIG, 7, Argument list too long}
172 Used when the arguments passed to a new program
173 being executed with one of the @code{exec} functions (@pxref{Executing a
174 File}) occupy too much memory space.  This condition never arises on
175 @gnuhurdsystems{}.
176 @end deftypevr
178 @deftypevr Macro int ENOEXEC
179 @standards{POSIX.1, errno.h}
180 @errno{ENOEXEC, 8, Exec format error}
181 Invalid executable file format.  This condition is detected by the
182 @code{exec} functions; see @ref{Executing a File}.
183 @end deftypevr
185 @deftypevr Macro int EBADF
186 @standards{POSIX.1, errno.h}
187 @errno{EBADF, 9, Bad file descriptor}
188 For example, I/O on a descriptor that has been
189 closed or reading from a descriptor open only for writing (or vice
190 versa).
191 @end deftypevr
193 @deftypevr Macro int ECHILD
194 @standards{POSIX.1, errno.h}
195 @errno{ECHILD, 10, No child processes}
196 This error happens on operations that are
197 supposed to manipulate child processes, when there aren't any processes
198 to manipulate.
199 @end deftypevr
201 @deftypevr Macro int EDEADLK
202 @standards{POSIX.1, errno.h}
203 @errno{EDEADLK, 11, Resource deadlock avoided}
204 Allocating a system resource would have resulted in a
205 deadlock situation.  The system does not guarantee that it will notice
206 all such situations.  This error means you got lucky and the system
207 noticed; it might just hang.  @xref{File Locks}, for an example.
208 @end deftypevr
210 @deftypevr Macro int ENOMEM
211 @standards{POSIX.1, errno.h}
212 @errno{ENOMEM, 12, Cannot allocate memory}
213 The system cannot allocate more virtual memory
214 because its capacity is full.
215 @end deftypevr
217 @deftypevr Macro int EACCES
218 @standards{POSIX.1, errno.h}
219 @errno{EACCES, 13, Permission denied}
220 The file permissions do not allow the attempted operation.
221 @end deftypevr
223 @deftypevr Macro int EFAULT
224 @standards{POSIX.1, errno.h}
225 @errno{EFAULT, 14, Bad address}
226 An invalid pointer was detected.
227 On @gnuhurdsystems{}, this error never happens; you get a signal instead.
228 @end deftypevr
230 @deftypevr Macro int ENOTBLK
231 @standards{BSD, errno.h}
232 @errno{ENOTBLK, 15, Block device required}
233 A file that isn't a block special file was given in a situation that
234 requires one.  For example, trying to mount an ordinary file as a file
235 system in Unix gives this error.
236 @end deftypevr
238 @deftypevr Macro int EBUSY
239 @standards{POSIX.1, errno.h}
240 @errno{EBUSY, 16, Device or resource busy}
241 A system resource that can't be shared is already in use.
242 For example, if you try to delete a file that is the root of a currently
243 mounted filesystem, you get this error.
244 @end deftypevr
246 @deftypevr Macro int EEXIST
247 @standards{POSIX.1, errno.h}
248 @errno{EEXIST, 17, File exists}
249 An existing file was specified in a context where it only
250 makes sense to specify a new file.
251 @end deftypevr
253 @deftypevr Macro int EXDEV
254 @standards{POSIX.1, errno.h}
255 @errno{EXDEV, 18, Invalid cross-device link}
256 An attempt to make an improper link across file systems was detected.
257 This happens not only when you use @code{link} (@pxref{Hard Links}) but
258 also when you rename a file with @code{rename} (@pxref{Renaming Files}).
259 @end deftypevr
261 @deftypevr Macro int ENODEV
262 @standards{POSIX.1, errno.h}
263 @errno{ENODEV, 19, No such device}
264 The wrong type of device was given to a function that expects a
265 particular sort of device.
266 @end deftypevr
268 @deftypevr Macro int ENOTDIR
269 @standards{POSIX.1, errno.h}
270 @errno{ENOTDIR, 20, Not a directory}
271 A file that isn't a directory was specified when a directory is required.
272 @end deftypevr
274 @deftypevr Macro int EISDIR
275 @standards{POSIX.1, errno.h}
276 @errno{EISDIR, 21, Is a directory}
277 You cannot open a directory for writing,
278 or create or remove hard links to it.
279 @end deftypevr
281 @deftypevr Macro int EINVAL
282 @standards{POSIX.1, errno.h}
283 @errno{EINVAL, 22, Invalid argument}
284 This is used to indicate various kinds of problems
285 with passing the wrong argument to a library function.
286 @end deftypevr
288 @deftypevr Macro int EMFILE
289 @standards{POSIX.1, errno.h}
290 @errno{EMFILE, 24, Too many open files}
291 The current process has too many files open and can't open any more.
292 Duplicate descriptors do count toward this limit.
294 In BSD and GNU, the number of open files is controlled by a resource
295 limit that can usually be increased.  If you get this error, you might
296 want to increase the @code{RLIMIT_NOFILE} limit or make it unlimited;
297 @pxref{Limits on Resources}.
298 @end deftypevr
300 @deftypevr Macro int ENFILE
301 @standards{POSIX.1, errno.h}
302 @errno{ENFILE, 23, Too many open files in system}
303 There are too many distinct file openings in the entire system.  Note
304 that any number of linked channels count as just one file opening; see
305 @ref{Linked Channels}.  This error never occurs on @gnuhurdsystems{}.
306 @end deftypevr
308 @deftypevr Macro int ENOTTY
309 @standards{POSIX.1, errno.h}
310 @errno{ENOTTY, 25, Inappropriate ioctl for device}
311 Inappropriate I/O control operation, such as trying to set terminal
312 modes on an ordinary file.
313 @end deftypevr
315 @deftypevr Macro int ETXTBSY
316 @standards{BSD, errno.h}
317 @errno{ETXTBSY, 26, Text file busy}
318 An attempt to execute a file that is currently open for writing, or
319 write to a file that is currently being executed.  Often using a
320 debugger to run a program is considered having it open for writing and
321 will cause this error.  (The name stands for ``text file busy''.)  This
322 is not an error on @gnuhurdsystems{}; the text is copied as necessary.
323 @end deftypevr
325 @deftypevr Macro int EFBIG
326 @standards{POSIX.1, errno.h}
327 @errno{EFBIG, 27, File too large}
328 The size of a file would be larger than allowed by the system.
329 @end deftypevr
331 @deftypevr Macro int ENOSPC
332 @standards{POSIX.1, errno.h}
333 @errno{ENOSPC, 28, No space left on device}
334 Write operation on a file failed because the
335 disk is full.
336 @end deftypevr
338 @deftypevr Macro int ESPIPE
339 @standards{POSIX.1, errno.h}
340 @errno{ESPIPE, 29, Illegal seek}
341 Invalid seek operation (such as on a pipe).
342 @end deftypevr
344 @deftypevr Macro int EROFS
345 @standards{POSIX.1, errno.h}
346 @errno{EROFS, 30, Read-only file system}
347 An attempt was made to modify something on a read-only file system.
348 @end deftypevr
350 @deftypevr Macro int EMLINK
351 @standards{POSIX.1, errno.h}
352 @errno{EMLINK, 31, Too many links}
353 The link count of a single file would become too large.
354 @code{rename} can cause this error if the file being renamed already has
355 as many links as it can take (@pxref{Renaming Files}).
356 @end deftypevr
358 @deftypevr Macro int EPIPE
359 @standards{POSIX.1, errno.h}
360 @errno{EPIPE, 32, Broken pipe}
361 There is no process reading from the other end of a pipe.
362 Every library function that returns this error code also generates a
363 @code{SIGPIPE} signal; this signal terminates the program if not handled
364 or blocked.  Thus, your program will never actually see @code{EPIPE}
365 unless it has handled or blocked @code{SIGPIPE}.
366 @end deftypevr
368 @deftypevr Macro int EDOM
369 @standards{ISO, errno.h}
370 @errno{EDOM, 33, Numerical argument out of domain}
371 Used by mathematical functions when an argument value does
372 not fall into the domain over which the function is defined.
373 @end deftypevr
375 @deftypevr Macro int ERANGE
376 @standards{ISO, errno.h}
377 @errno{ERANGE, 34, Numerical result out of range}
378 Used by mathematical functions when the result value is
379 not representable because of overflow or underflow.
380 @end deftypevr
382 @deftypevr Macro int EAGAIN
383 @standards{POSIX.1, errno.h}
384 @errno{EAGAIN, 35, Resource temporarily unavailable}
385 The call might work if you try again
386 later.  The macro @code{EWOULDBLOCK} is another name for @code{EAGAIN};
387 they are always the same in @theglibc{}.
389 This error can happen in a few different situations:
391 @itemize @bullet
392 @item
393 An operation that would block was attempted on an object that has
394 non-blocking mode selected.  Trying the same operation again will block
395 until some external condition makes it possible to read, write, or
396 connect (whatever the operation).  You can use @code{select} to find out
397 when the operation will be possible; @pxref{Waiting for I/O}.
399 @strong{Portability Note:} In many older Unix systems, this condition
400 was indicated by @code{EWOULDBLOCK}, which was a distinct error code
401 different from @code{EAGAIN}.  To make your program portable, you should
402 check for both codes and treat them the same.
404 @item
405 A temporary resource shortage made an operation impossible.  @code{fork}
406 can return this error.  It indicates that the shortage is expected to
407 pass, so your program can try the call again later and it may succeed.
408 It is probably a good idea to delay for a few seconds before trying it
409 again, to allow time for other processes to release scarce resources.
410 Such shortages are usually fairly serious and affect the whole system,
411 so usually an interactive program should report the error to the user
412 and return to its command loop.
413 @end itemize
414 @end deftypevr
416 @deftypevr Macro int EWOULDBLOCK
417 @standards{BSD, errno.h}
418 @errno{EWOULDBLOCK, EAGAIN, Operation would block}
419 In @theglibc{}, this is another name for @code{EAGAIN} (above).
420 The values are always the same, on every operating system.
422 C libraries in many older Unix systems have @code{EWOULDBLOCK} as a
423 separate error code.
424 @end deftypevr
426 @deftypevr Macro int EINPROGRESS
427 @standards{BSD, errno.h}
428 @errno{EINPROGRESS, 36, Operation now in progress}
429 An operation that cannot complete immediately was initiated on an object
430 that has non-blocking mode selected.  Some functions that must always
431 block (such as @code{connect}; @pxref{Connecting}) never return
432 @code{EAGAIN}.  Instead, they return @code{EINPROGRESS} to indicate that
433 the operation has begun and will take some time.  Attempts to manipulate
434 the object before the call completes return @code{EALREADY}.  You can
435 use the @code{select} function to find out when the pending operation
436 has completed; @pxref{Waiting for I/O}.
437 @end deftypevr
439 @deftypevr Macro int EALREADY
440 @standards{BSD, errno.h}
441 @errno{EALREADY, 37, Operation already in progress}
442 An operation is already in progress on an object that has non-blocking
443 mode selected.
444 @end deftypevr
446 @deftypevr Macro int ENOTSOCK
447 @standards{BSD, errno.h}
448 @errno{ENOTSOCK, 38, Socket operation on non-socket}
449 A file that isn't a socket was specified when a socket is required.
450 @end deftypevr
452 @deftypevr Macro int EMSGSIZE
453 @standards{BSD, errno.h}
454 @errno{EMSGSIZE, 40, Message too long}
455 The size of a message sent on a socket was larger than the supported
456 maximum size.
457 @end deftypevr
459 @deftypevr Macro int EPROTOTYPE
460 @standards{BSD, errno.h}
461 @errno{EPROTOTYPE, 41, Protocol wrong type for socket}
462 The socket type does not support the requested communications protocol.
463 @end deftypevr
465 @deftypevr Macro int ENOPROTOOPT
466 @standards{BSD, errno.h}
467 @errno{ENOPROTOOPT, 42, Protocol not available}
468 You specified a socket option that doesn't make sense for the
469 particular protocol being used by the socket.  @xref{Socket Options}.
470 @end deftypevr
472 @deftypevr Macro int EPROTONOSUPPORT
473 @standards{BSD, errno.h}
474 @errno{EPROTONOSUPPORT, 43, Protocol not supported}
475 The socket domain does not support the requested communications protocol
476 (perhaps because the requested protocol is completely invalid).
477 @xref{Creating a Socket}.
478 @end deftypevr
480 @deftypevr Macro int ESOCKTNOSUPPORT
481 @standards{BSD, errno.h}
482 @errno{ESOCKTNOSUPPORT, 44, Socket type not supported}
483 The socket type is not supported.
484 @end deftypevr
486 @deftypevr Macro int EOPNOTSUPP
487 @standards{BSD, errno.h}
488 @errno{EOPNOTSUPP, 45, Operation not supported}
489 The operation you requested is not supported.  Some socket functions
490 don't make sense for all types of sockets, and others may not be
491 implemented for all communications protocols.  On @gnuhurdsystems{}, this
492 error can happen for many calls when the object does not support the
493 particular operation; it is a generic indication that the server knows
494 nothing to do for that call.
495 @end deftypevr
497 @deftypevr Macro int EPFNOSUPPORT
498 @standards{BSD, errno.h}
499 @errno{EPFNOSUPPORT, 46, Protocol family not supported}
500 The socket communications protocol family you requested is not supported.
501 @end deftypevr
503 @deftypevr Macro int EAFNOSUPPORT
504 @standards{BSD, errno.h}
505 @errno{EAFNOSUPPORT, 47, Address family not supported by protocol}
506 The address family specified for a socket is not supported; it is
507 inconsistent with the protocol being used on the socket.  @xref{Sockets}.
508 @end deftypevr
510 @deftypevr Macro int EADDRINUSE
511 @standards{BSD, errno.h}
512 @errno{EADDRINUSE, 48, Address already in use}
513 The requested socket address is already in use.  @xref{Socket Addresses}.
514 @end deftypevr
516 @deftypevr Macro int EADDRNOTAVAIL
517 @standards{BSD, errno.h}
518 @errno{EADDRNOTAVAIL, 49, Cannot assign requested address}
519 The requested socket address is not available; for example, you tried
520 to give a socket a name that doesn't match the local host name.
521 @xref{Socket Addresses}.
522 @end deftypevr
524 @deftypevr Macro int ENETDOWN
525 @standards{BSD, errno.h}
526 @errno{ENETDOWN, 50, Network is down}
527 A socket operation failed because the network was down.
528 @end deftypevr
530 @deftypevr Macro int ENETUNREACH
531 @standards{BSD, errno.h}
532 @errno{ENETUNREACH, 51, Network is unreachable}
533 A socket operation failed because the subnet containing the remote host
534 was unreachable.
535 @end deftypevr
537 @deftypevr Macro int ENETRESET
538 @standards{BSD, errno.h}
539 @errno{ENETRESET, 52, Network dropped connection on reset}
540 A network connection was reset because the remote host crashed.
541 @end deftypevr
543 @deftypevr Macro int ECONNABORTED
544 @standards{BSD, errno.h}
545 @errno{ECONNABORTED, 53, Software caused connection abort}
546 A network connection was aborted locally.
547 @end deftypevr
549 @deftypevr Macro int ECONNRESET
550 @standards{BSD, errno.h}
551 @errno{ECONNRESET, 54, Connection reset by peer}
552 A network connection was closed for reasons outside the control of the
553 local host, such as by the remote machine rebooting or an unrecoverable
554 protocol violation.
555 @end deftypevr
557 @deftypevr Macro int ENOBUFS
558 @standards{BSD, errno.h}
559 @errno{ENOBUFS, 55, No buffer space available}
560 The kernel's buffers for I/O operations are all in use.  In GNU, this
561 error is always synonymous with @code{ENOMEM}; you may get one or the
562 other from network operations.
563 @end deftypevr
565 @deftypevr Macro int EISCONN
566 @standards{BSD, errno.h}
567 @errno{EISCONN, 56, Transport endpoint is already connected}
568 You tried to connect a socket that is already connected.
569 @xref{Connecting}.
570 @end deftypevr
572 @deftypevr Macro int ENOTCONN
573 @standards{BSD, errno.h}
574 @errno{ENOTCONN, 57, Transport endpoint is not connected}
575 The socket is not connected to anything.  You get this error when you
576 try to transmit data over a socket, without first specifying a
577 destination for the data.  For a connectionless socket (for datagram
578 protocols, such as UDP), you get @code{EDESTADDRREQ} instead.
579 @end deftypevr
581 @deftypevr Macro int EDESTADDRREQ
582 @standards{BSD, errno.h}
583 @errno{EDESTADDRREQ, 39, Destination address required}
584 No default destination address was set for the socket.  You get this
585 error when you try to transmit data over a connectionless socket,
586 without first specifying a destination for the data with @code{connect}.
587 @end deftypevr
589 @deftypevr Macro int ESHUTDOWN
590 @standards{BSD, errno.h}
591 @errno{ESHUTDOWN, 58, Cannot send after transport endpoint shutdown}
592 The socket has already been shut down.
593 @end deftypevr
595 @deftypevr Macro int ETOOMANYREFS
596 @standards{BSD, errno.h}
597 @errno{ETOOMANYREFS, 59, Too many references: cannot splice}
598 @end deftypevr
600 @deftypevr Macro int ETIMEDOUT
601 @standards{BSD, errno.h}
602 @errno{ETIMEDOUT, 60, Connection timed out}
603 A socket operation with a specified timeout received no response during
604 the timeout period.
605 @end deftypevr
607 @deftypevr Macro int ECONNREFUSED
608 @standards{BSD, errno.h}
609 @errno{ECONNREFUSED, 61, Connection refused}
610 A remote host refused to allow the network connection (typically because
611 it is not running the requested service).
612 @end deftypevr
614 @deftypevr Macro int ELOOP
615 @standards{BSD, errno.h}
616 @errno{ELOOP, 62, Too many levels of symbolic links}
617 Too many levels of symbolic links were encountered in looking up a file name.
618 This often indicates a cycle of symbolic links.
619 @end deftypevr
621 @deftypevr Macro int ENAMETOOLONG
622 @standards{POSIX.1, errno.h}
623 @errno{ENAMETOOLONG, 63, File name too long}
624 Filename too long (longer than @code{PATH_MAX}; @pxref{Limits for
625 Files}) or host name too long (in @code{gethostname} or
626 @code{sethostname}; @pxref{Host Identification}).
627 @end deftypevr
629 @deftypevr Macro int EHOSTDOWN
630 @standards{BSD, errno.h}
631 @errno{EHOSTDOWN, 64, Host is down}
632 The remote host for a requested network connection is down.
633 @end deftypevr
635 @deftypevr Macro int EHOSTUNREACH
636 @standards{BSD, errno.h}
637 @errno{EHOSTUNREACH, 65, No route to host}
638 The remote host for a requested network connection is not reachable.
639 @end deftypevr
641 @deftypevr Macro int ENOTEMPTY
642 @standards{POSIX.1, errno.h}
643 @errno{ENOTEMPTY, 66, Directory not empty}
644 Directory not empty, where an empty directory was expected.  Typically,
645 this error occurs when you are trying to delete a directory.
646 @end deftypevr
648 @deftypevr Macro int EPROCLIM
649 @standards{BSD, errno.h}
650 @errno{EPROCLIM, 67, Too many processes}
651 This means that the per-user limit on new process would be exceeded by
652 an attempted @code{fork}.  @xref{Limits on Resources}, for details on
653 the @code{RLIMIT_NPROC} limit.
654 @end deftypevr
656 @deftypevr Macro int EUSERS
657 @standards{BSD, errno.h}
658 @errno{EUSERS, 68, Too many users}
659 The file quota system is confused because there are too many users.
660 @c This can probably happen in a GNU system when using NFS.
661 @end deftypevr
663 @deftypevr Macro int EDQUOT
664 @standards{BSD, errno.h}
665 @errno{EDQUOT, 69, Disk quota exceeded}
666 The user's disk quota was exceeded.
667 @end deftypevr
669 @deftypevr Macro int ESTALE
670 @standards{BSD, errno.h}
671 @errno{ESTALE, 70, Stale file handle}
672 This indicates an internal confusion in the
673 file system which is due to file system rearrangements on the server host
674 for NFS file systems or corruption in other file systems.
675 Repairing this condition usually requires unmounting, possibly repairing
676 and remounting the file system.
677 @end deftypevr
679 @deftypevr Macro int EREMOTE
680 @standards{BSD, errno.h}
681 @errno{EREMOTE, 71, Object is remote}
682 An attempt was made to NFS-mount a remote file system with a file name that
683 already specifies an NFS-mounted file.
684 (This is an error on some operating systems, but we expect it to work
685 properly on @gnuhurdsystems{}, making this error code impossible.)
686 @end deftypevr
688 @deftypevr Macro int EBADRPC
689 @standards{BSD, errno.h}
690 @errno{EBADRPC, 72, RPC struct is bad}
691 @end deftypevr
693 @deftypevr Macro int ERPCMISMATCH
694 @standards{BSD, errno.h}
695 @errno{ERPCMISMATCH, 73, RPC version wrong}
696 @end deftypevr
698 @deftypevr Macro int EPROGUNAVAIL
699 @standards{BSD, errno.h}
700 @errno{EPROGUNAVAIL, 74, RPC program not available}
701 @end deftypevr
703 @deftypevr Macro int EPROGMISMATCH
704 @standards{BSD, errno.h}
705 @errno{EPROGMISMATCH, 75, RPC program version wrong}
706 @end deftypevr
708 @deftypevr Macro int EPROCUNAVAIL
709 @standards{BSD, errno.h}
710 @errno{EPROCUNAVAIL, 76, RPC bad procedure for program}
711 @end deftypevr
713 @deftypevr Macro int ENOLCK
714 @standards{POSIX.1, errno.h}
715 @errno{ENOLCK, 77, No locks available}
716 This is used by the file locking facilities; see
717 @ref{File Locks}.  This error is never generated by @gnuhurdsystems{}, but
718 it can result from an operation to an NFS server running another
719 operating system.
720 @end deftypevr
722 @deftypevr Macro int EFTYPE
723 @standards{BSD, errno.h}
724 @errno{EFTYPE, 79, Inappropriate file type or format}
725 The file was the wrong type for the
726 operation, or a data file had the wrong format.
728 On some systems @code{chmod} returns this error if you try to set the
729 sticky bit on a non-directory file; @pxref{Setting Permissions}.
730 @end deftypevr
732 @deftypevr Macro int EAUTH
733 @standards{BSD, errno.h}
734 @errno{EAUTH, 80, Authentication error}
735 @end deftypevr
737 @deftypevr Macro int ENEEDAUTH
738 @standards{BSD, errno.h}
739 @errno{ENEEDAUTH, 81, Need authenticator}
740 @end deftypevr
742 @deftypevr Macro int ENOSYS
743 @standards{POSIX.1, errno.h}
744 @errno{ENOSYS, 78, Function not implemented}
745 This indicates that the function called is
746 not implemented at all, either in the C library itself or in the
747 operating system.  When you get this error, you can be sure that this
748 particular function will always fail with @code{ENOSYS} unless you
749 install a new version of the C library or the operating system.
750 @end deftypevr
752 @deftypevr Macro int ENOTSUP
753 @standards{POSIX.1, errno.h}
754 @errno{ENOTSUP, 118, Not supported}
755 A function returns this error when certain parameter
756 values are valid, but the functionality they request is not available.
757 This can mean that the function does not implement a particular command
758 or option value or flag bit at all.  For functions that operate on some
759 object given in a parameter, such as a file descriptor or a port, it
760 might instead mean that only @emph{that specific object} (file
761 descriptor, port, etc.) is unable to support the other parameters given;
762 different file descriptors might support different ranges of parameter
763 values.
765 If the entire function is not available at all in the implementation,
766 it returns @code{ENOSYS} instead.
767 @end deftypevr
769 @deftypevr Macro int EILSEQ
770 @standards{ISO, errno.h}
771 @errno{EILSEQ, 106, Invalid or incomplete multibyte or wide character}
772 While decoding a multibyte character the function came along an invalid
773 or an incomplete sequence of bytes or the given wide character is invalid.
774 @end deftypevr
776 @deftypevr Macro int EBACKGROUND
777 @standards{GNU, errno.h}
778 @errno{EBACKGROUND, 100, Inappropriate operation for background process}
779 On @gnuhurdsystems{}, servers supporting the @code{term} protocol return
780 this error for certain operations when the caller is not in the
781 foreground process group of the terminal.  Users do not usually see this
782 error because functions such as @code{read} and @code{write} translate
783 it into a @code{SIGTTIN} or @code{SIGTTOU} signal.  @xref{Job Control},
784 for information on process groups and these signals.
785 @end deftypevr
787 @deftypevr Macro int EDIED
788 @standards{GNU, errno.h}
789 @errno{EDIED, 101, Translator died}
790 On @gnuhurdsystems{}, opening a file returns this error when the file is
791 translated by a program and the translator program dies while starting
792 up, before it has connected to the file.
793 @end deftypevr
795 @deftypevr Macro int ED
796 @standards{GNU, errno.h}
797 @errno{ED, 102, ?}
798 The experienced user will know what is wrong.
799 @c This error code is a joke.  Its perror text is part of the joke.
800 @c Don't change it.
801 @end deftypevr
803 @deftypevr Macro int EGREGIOUS
804 @standards{GNU, errno.h}
805 @errno{EGREGIOUS, 103, You really blew it this time}
806 You did @strong{what}?
807 @end deftypevr
809 @deftypevr Macro int EIEIO
810 @standards{GNU, errno.h}
811 @errno{EIEIO, 104, Computer bought the farm}
812 Go home and have a glass of warm, dairy-fresh milk.
813 @end deftypevr
815 @deftypevr Macro int EGRATUITOUS
816 @standards{GNU, errno.h}
817 @errno{EGRATUITOUS, 105, Gratuitous error}
818 This error code has no purpose.
819 @end deftypevr
821 @deftypevr Macro int EBADMSG
822 @standards{XOPEN, errno.h}
823 @errno{EBADMSG, 107, Bad message}
824 @end deftypevr
826 @deftypevr Macro int EIDRM
827 @standards{XOPEN, errno.h}
828 @errno{EIDRM, 108, Identifier removed}
829 @end deftypevr
831 @deftypevr Macro int EMULTIHOP
832 @standards{XOPEN, errno.h}
833 @errno{EMULTIHOP, 109, Multihop attempted}
834 @end deftypevr
836 @deftypevr Macro int ENODATA
837 @standards{XOPEN, errno.h}
838 @errno{ENODATA, 110, No data available}
839 @end deftypevr
841 @deftypevr Macro int ENOLINK
842 @standards{XOPEN, errno.h}
843 @errno{ENOLINK, 111, Link has been severed}
844 @end deftypevr
846 @deftypevr Macro int ENOMSG
847 @standards{XOPEN, errno.h}
848 @errno{ENOMSG, 112, No message of desired type}
849 @end deftypevr
851 @deftypevr Macro int ENOSR
852 @standards{XOPEN, errno.h}
853 @errno{ENOSR, 113, Out of streams resources}
854 @end deftypevr
856 @deftypevr Macro int ENOSTR
857 @standards{XOPEN, errno.h}
858 @errno{ENOSTR, 114, Device not a stream}
859 @end deftypevr
861 @deftypevr Macro int EOVERFLOW
862 @standards{XOPEN, errno.h}
863 @errno{EOVERFLOW, 115, Value too large for defined data type}
864 @end deftypevr
866 @deftypevr Macro int EPROTO
867 @standards{XOPEN, errno.h}
868 @errno{EPROTO, 116, Protocol error}
869 @end deftypevr
871 @deftypevr Macro int ETIME
872 @standards{XOPEN, errno.h}
873 @errno{ETIME, 117, Timer expired}
874 @end deftypevr
876 @deftypevr Macro int ECANCELED
877 @standards{POSIX.1, errno.h}
878 @errno{ECANCELED, 119, Operation canceled}
879 An asynchronous operation was canceled before it
880 completed.  @xref{Asynchronous I/O}.  When you call @code{aio_cancel},
881 the normal result is for the operations affected to complete with this
882 error; @pxref{Cancel AIO Operations}.
883 @end deftypevr
885 @deftypevr Macro int EOWNERDEAD
886 @standards{GNU, errno.h}
887 @errno{EOWNERDEAD, 120, Owner died}
888 @end deftypevr
890 @deftypevr Macro int ENOTRECOVERABLE
891 @standards{GNU, errno.h}
892 @errno{ENOTRECOVERABLE, 121, State not recoverable}
893 @end deftypevr
896 @emph{The following error codes are defined by the Linux/i386 kernel.
897 They are not yet documented.}
899 @deftypevr Macro int ERESTART
900 @standards{Linux???, errno.h}
901 @errno{ERESTART, ???/85, Interrupted system call should be restarted}
902 @end deftypevr
904 @deftypevr Macro int ECHRNG
905 @standards{Linux???, errno.h}
906 @errno{ECHRNG, ???/44, Channel number out of range}
907 @end deftypevr
909 @deftypevr Macro int EL2NSYNC
910 @standards{Obsolete, errno.h}
911 @errno{EL2NSYNC, ???/45, Level 2 not synchronized}
912 @end deftypevr
914 @deftypevr Macro int EL3HLT
915 @standards{Obsolete, errno.h}
916 @errno{EL3HLT, ???/46, Level 3 halted}
917 @end deftypevr
919 @deftypevr Macro int EL3RST
920 @standards{Obsolete, errno.h}
921 @errno{EL3RST, ???/47, Level 3 reset}
922 @end deftypevr
924 @deftypevr Macro int ELNRNG
925 @standards{Linux???, errno.h}
926 @errno{ELNRNG, ???/48, Link number out of range}
927 @end deftypevr
929 @deftypevr Macro int EUNATCH
930 @standards{Linux???, errno.h}
931 @errno{EUNATCH, ???/49, Protocol driver not attached}
932 @end deftypevr
934 @deftypevr Macro int ENOCSI
935 @standards{Linux???, errno.h}
936 @errno{ENOCSI, ???/50, No CSI structure available}
937 @end deftypevr
939 @deftypevr Macro int EL2HLT
940 @standards{Obsolete, errno.h}
941 @errno{EL2HLT, ???/51, Level 2 halted}
942 @end deftypevr
944 @deftypevr Macro int EBADE
945 @standards{Linux???, errno.h}
946 @errno{EBADE, ???/52, Invalid exchange}
947 @end deftypevr
949 @deftypevr Macro int EBADR
950 @standards{Linux???, errno.h}
951 @errno{EBADR, ???/53, Invalid request descriptor}
952 @end deftypevr
954 @deftypevr Macro int EXFULL
955 @standards{Linux???, errno.h}
956 @errno{EXFULL, ???/54, Exchange full}
957 @end deftypevr
959 @deftypevr Macro int ENOANO
960 @standards{Linux???, errno.h}
961 @errno{ENOANO, ???/55, No anode}
962 @end deftypevr
964 @deftypevr Macro int EBADRQC
965 @standards{Linux???, errno.h}
966 @errno{EBADRQC, ???/56, Invalid request code}
967 @end deftypevr
969 @deftypevr Macro int EBADSLT
970 @standards{Linux???, errno.h}
971 @errno{EBADSLT, ???/57, Invalid slot}
972 @end deftypevr
974 @deftypevr Macro int EDEADLOCK
975 @standards{Linux???, errno.h}
976 @errno{EDEADLOCK, ???/58, File locking deadlock error}
977 @end deftypevr
979 @deftypevr Macro int EBFONT
980 @standards{Linux???, errno.h}
981 @errno{EBFONT, ???/59, Bad font file format}
982 @end deftypevr
984 @deftypevr Macro int ENONET
985 @standards{Linux???, errno.h}
986 @errno{ENONET, ???/64, Machine is not on the network}
987 @end deftypevr
989 @deftypevr Macro int ENOPKG
990 @standards{Linux???, errno.h}
991 @errno{ENOPKG, ???/65, Package not installed}
992 @end deftypevr
994 @deftypevr Macro int EADV
995 @standards{Linux???, errno.h}
996 @errno{EADV, ???/68, Advertise error}
997 @end deftypevr
999 @deftypevr Macro int ESRMNT
1000 @standards{Linux???, errno.h}
1001 @errno{ESRMNT, ???/69, Srmount error}
1002 @end deftypevr
1004 @deftypevr Macro int ECOMM
1005 @standards{Linux???, errno.h}
1006 @errno{ECOMM, ???/70, Communication error on send}
1007 @end deftypevr
1009 @deftypevr Macro int EDOTDOT
1010 @standards{Linux???, errno.h}
1011 @errno{EDOTDOT, ???/73, RFS specific error}
1012 @end deftypevr
1014 @deftypevr Macro int ENOTUNIQ
1015 @standards{Linux???, errno.h}
1016 @errno{ENOTUNIQ, ???/76, Name not unique on network}
1017 @end deftypevr
1019 @deftypevr Macro int EBADFD
1020 @standards{Linux???, errno.h}
1021 @errno{EBADFD, ???/77, File descriptor in bad state}
1022 @end deftypevr
1024 @deftypevr Macro int EREMCHG
1025 @standards{Linux???, errno.h}
1026 @errno{EREMCHG, ???/78, Remote address changed}
1027 @end deftypevr
1029 @deftypevr Macro int ELIBACC
1030 @standards{Linux???, errno.h}
1031 @errno{ELIBACC, ???/79, Can not access a needed shared library}
1032 @end deftypevr
1034 @deftypevr Macro int ELIBBAD
1035 @standards{Linux???, errno.h}
1036 @errno{ELIBBAD, ???/80, Accessing a corrupted shared library}
1037 @end deftypevr
1039 @deftypevr Macro int ELIBSCN
1040 @standards{Linux???, errno.h}
1041 @errno{ELIBSCN, ???/81, .lib section in a.out corrupted}
1042 @end deftypevr
1044 @deftypevr Macro int ELIBMAX
1045 @standards{Linux???, errno.h}
1046 @errno{ELIBMAX, ???/82, Attempting to link in too many shared libraries}
1047 @end deftypevr
1049 @deftypevr Macro int ELIBEXEC
1050 @standards{Linux???, errno.h}
1051 @errno{ELIBEXEC, ???/83, Cannot exec a shared library directly}
1052 @end deftypevr
1054 @deftypevr Macro int ESTRPIPE
1055 @standards{Linux???, errno.h}
1056 @errno{ESTRPIPE, ???/86, Streams pipe error}
1057 @end deftypevr
1059 @deftypevr Macro int EUCLEAN
1060 @standards{Linux???, errno.h}
1061 @errno{EUCLEAN, ???/117, Structure needs cleaning}
1062 @end deftypevr
1064 @deftypevr Macro int ENOTNAM
1065 @standards{Linux???, errno.h}
1066 @errno{ENOTNAM, ???/118, Not a XENIX named type file}
1067 @end deftypevr
1069 @deftypevr Macro int ENAVAIL
1070 @standards{Linux???, errno.h}
1071 @errno{ENAVAIL, ???/119, No XENIX semaphores available}
1072 @end deftypevr
1074 @deftypevr Macro int EISNAM
1075 @standards{Linux???, errno.h}
1076 @errno{EISNAM, ???/120, Is a named type file}
1077 @end deftypevr
1079 @deftypevr Macro int EREMOTEIO
1080 @standards{Linux???, errno.h}
1081 @errno{EREMOTEIO, ???/121, Remote I/O error}
1082 @end deftypevr
1084 @deftypevr Macro int ENOMEDIUM
1085 @standards{Linux???, errno.h}
1086 @errno{ENOMEDIUM, ???/???, No medium found}
1087 @end deftypevr
1089 @deftypevr Macro int EMEDIUMTYPE
1090 @standards{Linux???, errno.h}
1091 @errno{EMEDIUMTYPE, ???/???, Wrong medium type}
1092 @end deftypevr
1094 @deftypevr Macro int ENOKEY
1095 @standards{Linux, errno.h}
1096 @errno{ENOKEY, ???/???, Required key not available}
1097 @end deftypevr
1099 @deftypevr Macro int EKEYEXPIRED
1100 @standards{Linux, errno.h}
1101 @errno{EKEYEXPIRED, ???/???, Key has expired}
1102 @end deftypevr
1104 @deftypevr Macro int EKEYREVOKED
1105 @standards{Linux, errno.h}
1106 @errno{EKEYREVOKED, ???/???, Key has been revoked}
1107 @end deftypevr
1109 @deftypevr Macro int EKEYREJECTED
1110 @standards{Linux, errno.h}
1111 @errno{EKEYREJECTED, ???/???, Key was rejected by service}
1112 @end deftypevr
1114 @deftypevr Macro int ERFKILL
1115 @standards{Linux, errno.h}
1116 @errno{ERFKILL, ???/???, Operation not possible due to RF-kill}
1117 @end deftypevr
1119 @deftypevr Macro int EHWPOISON
1120 @standards{Linux, errno.h}
1121 @errno{EHWPOISON, ???/???, Memory page has hardware error}
1122 @end deftypevr
1124 @node Error Messages,  , Error Codes, Error Reporting
1125 @section Error Messages
1127 The library has functions and variables designed to make it easy for
1128 your program to report informative error messages in the customary
1129 format about the failure of a library call.  The functions
1130 @code{strerror} and @code{perror} give you the standard error message
1131 for a given error code; the variable
1132 @w{@code{program_invocation_short_name}} gives you convenient access to the
1133 name of the program that encountered the error.
1135 @deftypefun {char *} strerror (int @var{errnum})
1136 @standards{ISO, string.h}
1137 @safety{@prelim{}@mtunsafe{@mtasurace{:strerror}}@asunsafe{@ascuheap{} @ascuintl{}}@acunsafe{@acsmem{}}}
1138 @c Calls strerror_r with a static buffer allocated with malloc on the
1139 @c first use.
1140 The @code{strerror} function maps the error code (@pxref{Checking for
1141 Errors}) specified by the @var{errnum} argument to a descriptive error
1142 message string.  The return value is a pointer to this string.
1144 The value @var{errnum} normally comes from the variable @code{errno}.
1146 You should not modify the string returned by @code{strerror}.  Also, if
1147 you make subsequent calls to @code{strerror}, the string might be
1148 overwritten.  (But it's guaranteed that no library function ever calls
1149 @code{strerror} behind your back.)
1151 The function @code{strerror} is declared in @file{string.h}.
1152 @end deftypefun
1154 @deftypefun {char *} strerror_r (int @var{errnum}, char *@var{buf}, size_t @var{n})
1155 @standards{GNU, string.h}
1156 @safety{@prelim{}@mtsafe{}@asunsafe{@ascuintl{}}@acunsafe{}}
1157 The @code{strerror_r} function works like @code{strerror} but instead of
1158 returning the error message in a statically allocated buffer shared by
1159 all threads in the process, it returns a private copy for the
1160 thread.  This might be either some permanent global data or a message
1161 string in the user supplied buffer starting at @var{buf} with the
1162 length of @var{n} bytes.
1164 At most @var{n} characters are written (including the NUL byte) so it is
1165 up to the user to select a buffer large enough.
1167 This function should always be used in multi-threaded programs since
1168 there is no way to guarantee the string returned by @code{strerror}
1169 really belongs to the last call of the current thread.
1171 The function @code{strerror_r} is a GNU extension and it is declared in
1172 @file{string.h}.
1173 @end deftypefun
1175 @deftypefun void perror (const char *@var{message})
1176 @standards{ISO, stdio.h}
1177 @safety{@prelim{}@mtsafe{@mtasurace{:stderr}}@asunsafe{@asucorrupt{} @ascuintl{} @ascuheap{} @asulock{}}@acunsafe{@acucorrupt{} @aculock{} @acsmem{} @acsfd{}}}
1178 @c Besides strerror_r's and some of fprintf's issues, if stderr is not
1179 @c oriented yet, create a new stream with a dup of stderr's fd and write
1180 @c to that instead of stderr, to avoid orienting it.
1181 This function prints an error message to the stream @code{stderr};
1182 see @ref{Standard Streams}.  The orientation of @code{stderr} is not
1183 changed.
1185 If you call @code{perror} with a @var{message} that is either a null
1186 pointer or an empty string, @code{perror} just prints the error message
1187 corresponding to @code{errno}, adding a trailing newline.
1189 If you supply a non-null @var{message} argument, then @code{perror}
1190 prefixes its output with this string.  It adds a colon and a space
1191 character to separate the @var{message} from the error string corresponding
1192 to @code{errno}.
1194 The function @code{perror} is declared in @file{stdio.h}.
1195 @end deftypefun
1197 @code{strerror} and @code{perror} produce the exact same message for any
1198 given error code; the precise text varies from system to system.  With
1199 @theglibc{}, the messages are fairly short; there are no multi-line
1200 messages or embedded newlines.  Each error message begins with a capital
1201 letter and does not include any terminating punctuation.
1203 @cindex program name
1204 @cindex name of running program
1205 Many programs that don't read input from the terminal are designed to
1206 exit if any system call fails.  By convention, the error message from
1207 such a program should start with the program's name, sans directories.
1208 You can find that name in the variable
1209 @code{program_invocation_short_name}; the full file name is stored the
1210 variable @code{program_invocation_name}.
1212 @deftypevar {char *} program_invocation_name
1213 @standards{GNU, errno.h}
1214 This variable's value is the name that was used to invoke the program
1215 running in the current process.  It is the same as @code{argv[0]}.  Note
1216 that this is not necessarily a useful file name; often it contains no
1217 directory names.  @xref{Program Arguments}.
1219 This variable is a GNU extension and is declared in @file{errno.h}.
1220 @end deftypevar
1222 @deftypevar {char *} program_invocation_short_name
1223 @standards{GNU, errno.h}
1224 This variable's value is the name that was used to invoke the program
1225 running in the current process, with directory names removed.  (That is
1226 to say, it is the same as @code{program_invocation_name} minus
1227 everything up to the last slash, if any.)
1229 This variable is a GNU extension and is declared in @file{errno.h}.
1230 @end deftypevar
1232 The library initialization code sets up both of these variables before
1233 calling @code{main}.
1235 @strong{Portability Note:} If you want your program to work with
1236 non-GNU libraries, you must save the value of @code{argv[0]} in
1237 @code{main}, and then strip off the directory names yourself.  We
1238 added these extensions to make it possible to write self-contained
1239 error-reporting subroutines that require no explicit cooperation from
1240 @code{main}.
1242 Here is an example showing how to handle failure to open a file
1243 correctly.  The function @code{open_sesame} tries to open the named file
1244 for reading and returns a stream if successful.  The @code{fopen}
1245 library function returns a null pointer if it couldn't open the file for
1246 some reason.  In that situation, @code{open_sesame} constructs an
1247 appropriate error message using the @code{strerror} function, and
1248 terminates the program.  If we were going to make some other library
1249 calls before passing the error code to @code{strerror}, we'd have to
1250 save it in a local variable instead, because those other library
1251 functions might overwrite @code{errno} in the meantime.
1253 @smallexample
1254 #define _GNU_SOURCE
1256 #include <errno.h>
1257 #include <stdio.h>
1258 #include <stdlib.h>
1259 #include <string.h>
1261 FILE *
1262 open_sesame (char *name)
1264   FILE *stream;
1266   errno = 0;
1267   stream = fopen (name, "r");
1268   if (stream == NULL)
1269     @{
1270       fprintf (stderr, "%s: Couldn't open file %s; %s\n",
1271                program_invocation_short_name, name, strerror (errno));
1272       exit (EXIT_FAILURE);
1273     @}
1274   else
1275     return stream;
1277 @end smallexample
1279 Using @code{perror} has the advantage that the function is portable and
1280 available on all systems implementing @w{ISO C}.  But often the text
1281 @code{perror} generates is not what is wanted and there is no way to
1282 extend or change what @code{perror} does.  The GNU coding standard, for
1283 instance, requires error messages to be preceded by the program name and
1284 programs which read some input files should provide information
1285 about the input file name and the line number in case an error is
1286 encountered while reading the file.  For these occasions there are two
1287 functions available which are widely used throughout the GNU project.
1288 These functions are declared in @file{error.h}.
1290 @deftypefun void error (int @var{status}, int @var{errnum}, const char *@var{format}, @dots{})
1291 @standards{GNU, error.h}
1292 @safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@asucorrupt{} @asuheap{} @asuintl{}}@acsafe{}}
1293 @c Cancellation is disabled throughout the execution.  It flushes stdout
1294 @c and then holds a lock on stderr while printing the program name and
1295 @c then running error_tail.  The non-wide case just runs vfprintf; the
1296 @c wide case converts the message to an alloca/malloc-allocated buffer
1297 @c with mbsrtowcs, then prints it with vfwprintf.  Afterwards,
1298 @c print_errno_message calls strerror_r and fxprintf.
1299 The @code{error} function can be used to report general problems during
1300 program execution.  The @var{format} argument is a format string just
1301 like those given to the @code{printf} family of functions.  The
1302 arguments required for the format can follow the @var{format} parameter.
1303 Just like @code{perror}, @code{error} also can report an error code in
1304 textual form.  But unlike @code{perror} the error value is explicitly
1305 passed to the function in the @var{errnum} parameter.  This eliminates
1306 the problem mentioned above that the error reporting function must be
1307 called immediately after the function causing the error since otherwise
1308 @code{errno} might have a different value.
1310 @code{error} prints first the program name.  If the application
1311 defined a global variable @code{error_print_progname} and points it to a
1312 function this function will be called to print the program name.
1313 Otherwise the string from the global variable @code{program_name} is
1314 used.  The program name is followed by a colon and a space which in turn
1315 is followed by the output produced by the format string.  If the
1316 @var{errnum} parameter is non-zero the format string output is followed
1317 by a colon and a space, followed by the error message for the error code
1318 @var{errnum}.  In any case is the output terminated with a newline.
1320 The output is directed to the @code{stderr} stream.  If the
1321 @code{stderr} wasn't oriented before the call it will be narrow-oriented
1322 afterwards.
1324 The function will return unless the @var{status} parameter has a
1325 non-zero value.  In this case the function will call @code{exit} with
1326 the @var{status} value for its parameter and therefore never return.  If
1327 @code{error} returns, the global variable @code{error_message_count} is
1328 incremented by one to keep track of the number of errors reported.
1329 @end deftypefun
1331 @deftypefun void error_at_line (int @var{status}, int @var{errnum}, const char *@var{fname}, unsigned int @var{lineno}, const char *@var{format}, @dots{})
1332 @standards{GNU, error.h}
1333 @safety{@prelim{}@mtunsafe{@mtasurace{:error_at_line/error_one_per_line} @mtslocale{}}@asunsafe{@asucorrupt{} @asuheap{} @asuintl{}}@acunsafe{@acucorrupt{/error_one_per_line}}}
1334 @c The error_one_per_line variable is accessed (without any form of
1335 @c synchronization, but since it's an int used once, it should be safe
1336 @c enough) and, if this mode is enabled, static variables used to hold
1337 @c the last printed file name and line number are accessed and modified
1338 @c without synchronization; the update is not atomic and it occurs
1339 @c before disabling cancellation, so it can be interrupted after only
1340 @c one of the two variables is modified.  After that, it's very much
1341 @c like error.
1343 The @code{error_at_line} function is very similar to the @code{error}
1344 function.  The only differences are the additional parameters @var{fname}
1345 and @var{lineno}.  The handling of the other parameters is identical to
1346 that of @code{error} except that between the program name and the string
1347 generated by the format string additional text is inserted.
1349 Directly following the program name a colon, followed by the file name
1350 pointed to by @var{fname}, another colon, and the value of @var{lineno} is
1351 printed.
1353 This additional output of course is meant to be used to locate an error
1354 in an input file (like a programming language source code file etc).
1356 If the global variable @code{error_one_per_line} is set to a non-zero
1357 value @code{error_at_line} will avoid printing consecutive messages for
1358 the same file and line.  Repetition which are not directly following
1359 each other are not caught.
1361 Just like @code{error} this function only returns if @var{status} is
1362 zero.  Otherwise @code{exit} is called with the non-zero value.  If
1363 @code{error} returns, the global variable @code{error_message_count} is
1364 incremented by one to keep track of the number of errors reported.
1365 @end deftypefun
1367 As mentioned above, the @code{error} and @code{error_at_line} functions
1368 can be customized by defining a variable named
1369 @code{error_print_progname}.
1371 @deftypevar {void (*error_print_progname)} (void)
1372 @standards{GNU, error.h}
1373 If the @code{error_print_progname} variable is defined to a non-zero
1374 value the function pointed to is called by @code{error} or
1375 @code{error_at_line}.  It is expected to print the program name or do
1376 something similarly useful.
1378 The function is expected to print to the @code{stderr} stream and
1379 must be able to handle whatever orientation the stream has.
1381 The variable is global and shared by all threads.
1382 @end deftypevar
1384 @deftypevar {unsigned int} error_message_count
1385 @standards{GNU, error.h}
1386 The @code{error_message_count} variable is incremented whenever one of
1387 the functions @code{error} or @code{error_at_line} returns.  The
1388 variable is global and shared by all threads.
1389 @end deftypevar
1391 @deftypevar int error_one_per_line
1392 @standards{GNU, error.h}
1393 The @code{error_one_per_line} variable influences only
1394 @code{error_at_line}.  Normally the @code{error_at_line} function
1395 creates output for every invocation.  If @code{error_one_per_line} is
1396 set to a non-zero value @code{error_at_line} keeps track of the last
1397 file name and line number for which an error was reported and avoids
1398 directly following messages for the same file and line.  This variable
1399 is global and shared by all threads.
1400 @end deftypevar
1402 @noindent
1403 A program which read some input file and reports errors in it could look
1404 like this:
1406 @smallexample
1408   char *line = NULL;
1409   size_t len = 0;
1410   unsigned int lineno = 0;
1412   error_message_count = 0;
1413   while (! feof_unlocked (fp))
1414     @{
1415       ssize_t n = getline (&line, &len, fp);
1416       if (n <= 0)
1417         /* @r{End of file or error.}  */
1418         break;
1419       ++lineno;
1421       /* @r{Process the line.}  */
1422       @dots{}
1424       if (@r{Detect error in line})
1425         error_at_line (0, errval, filename, lineno,
1426                        "some error text %s", some_variable);
1427     @}
1429   if (error_message_count != 0)
1430     error (EXIT_FAILURE, 0, "%u errors found", error_message_count);
1432 @end smallexample
1434 @code{error} and @code{error_at_line} are clearly the functions of
1435 choice and enable the programmer to write applications which follow the
1436 GNU coding standard.  @Theglibc{} additionally contains functions which
1437 are used in BSD for the same purpose.  These functions are declared in
1438 @file{err.h}.  It is generally advised to not use these functions.  They
1439 are included only for compatibility.
1441 @deftypefun void warn (const char *@var{format}, @dots{})
1442 @standards{BSD, err.h}
1443 @safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@asucorrupt{} @ascuheap{} @ascuintl{}}@acunsafe{@acucorrupt{} @aculock{} @acsmem{}}}
1444 @c Just calls vwarn with the va_list.
1445 The @code{warn} function is roughly equivalent to a call like
1446 @smallexample
1447   error (0, errno, format, @r{the parameters})
1448 @end smallexample
1449 @noindent
1450 except that the global variables @code{error} respects and modifies
1451 are not used.
1452 @end deftypefun
1454 @deftypefun void vwarn (const char *@var{format}, va_list @var{ap})
1455 @standards{BSD, err.h}
1456 @safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@asucorrupt{} @ascuheap{} @ascuintl{}}@acunsafe{@acucorrupt{} @aculock{} @acsmem{}}}
1457 @c While holding stderr's recursive lock, it prints the programname, the
1458 @c given message, and the error string with fw?printf's %m.  When the
1459 @c stream is wide, convert_and_print converts the format string to an
1460 @c alloca/malloc-created buffer using mbsrtowcs and then calls fwprintf.
1461 The @code{vwarn} function is just like @code{warn} except that the
1462 parameters for the handling of the format string @var{format} are passed
1463 in as a value of type @code{va_list}.
1464 @end deftypefun
1466 @deftypefun void warnx (const char *@var{format}, @dots{})
1467 @standards{BSD, err.h}
1468 @safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@asucorrupt{} @ascuheap{}}@acunsafe{@acucorrupt{} @aculock{} @acsmem{}}}
1469 @c Same as warn, but without the strerror translation issues.
1470 The @code{warnx} function is roughly equivalent to a call like
1471 @smallexample
1472   error (0, 0, format, @r{the parameters})
1473 @end smallexample
1474 @noindent
1475 except that the global variables @code{error} respects and modifies
1476 are not used.  The difference to @code{warn} is that no error number
1477 string is printed.
1478 @end deftypefun
1480 @deftypefun void vwarnx (const char *@var{format}, va_list @var{ap})
1481 @standards{BSD, err.h}
1482 @safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@asucorrupt{} @ascuheap{}}@acunsafe{@acucorrupt{} @aculock{} @acsmem{}}}
1483 @c Same as vwarn, but without the strerror translation issues.
1484 The @code{vwarnx} function is just like @code{warnx} except that the
1485 parameters for the handling of the format string @var{format} are passed
1486 in as a value of type @code{va_list}.
1487 @end deftypefun
1489 @deftypefun void err (int @var{status}, const char *@var{format}, @dots{})
1490 @standards{BSD, err.h}
1491 @safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@asucorrupt{} @ascuheap{} @ascuintl{}}@acunsafe{@acucorrupt{} @aculock{} @acsmem{}}}
1492 @c Same as warn followed by exit.
1493 The @code{err} function is roughly equivalent to a call like
1494 @smallexample
1495   error (status, errno, format, @r{the parameters})
1496 @end smallexample
1497 @noindent
1498 except that the global variables @code{error} respects and modifies
1499 are not used and that the program is exited even if @var{status} is zero.
1500 @end deftypefun
1502 @deftypefun void verr (int @var{status}, const char *@var{format}, va_list @var{ap})
1503 @standards{BSD, err.h}
1504 @safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@asucorrupt{} @ascuheap{} @ascuintl{}}@acunsafe{@acucorrupt{} @aculock{} @acsmem{}}}
1505 @c Same as vwarn followed by exit.
1506 The @code{verr} function is just like @code{err} except that the
1507 parameters for the handling of the format string @var{format} are passed
1508 in as a value of type @code{va_list}.
1509 @end deftypefun
1511 @deftypefun void errx (int @var{status}, const char *@var{format}, @dots{})
1512 @standards{BSD, err.h}
1513 @safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@asucorrupt{} @ascuheap{}}@acunsafe{@acucorrupt{} @aculock{} @acsmem{}}}
1514 @c Same as warnx followed by exit.
1515 The @code{errx} function is roughly equivalent to a call like
1516 @smallexample
1517   error (status, 0, format, @r{the parameters})
1518 @end smallexample
1519 @noindent
1520 except that the global variables @code{error} respects and modifies
1521 are not used and that the program is exited even if @var{status}
1522 is zero.  The difference to @code{err} is that no error number
1523 string is printed.
1524 @end deftypefun
1526 @deftypefun void verrx (int @var{status}, const char *@var{format}, va_list @var{ap})
1527 @standards{BSD, err.h}
1528 @safety{@prelim{}@mtsafe{@mtslocale{}}@asunsafe{@asucorrupt{} @ascuheap{}}@acunsafe{@acucorrupt{} @aculock{} @acsmem{}}}
1529 @c Same as vwarnx followed by exit.
1530 The @code{verrx} function is just like @code{errx} except that the
1531 parameters for the handling of the format string @var{format} are passed
1532 in as a value of type @code{va_list}.
1533 @end deftypefun