Add a testase for BZ #14602
[glibc.git] / manual / errno.texi
blobfa88b1e8a1c455b08ecfcfc188ea687d07bb2a62
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 @comment errno.h
40 @comment ISO
41 @deftypevr {Variable} {volatile int} errno
42 The variable @code{errno} contains the system error number.  You can
43 change the value of @code{errno}.
45 Since @code{errno} is declared @code{volatile}, it might be changed
46 asynchronously by a signal handler; see @ref{Defining Handlers}.
47 However, a properly written signal handler saves and restores the value
48 of @code{errno}, so you generally do not need to worry about this
49 possibility except when writing signal handlers.
51 The initial value of @code{errno} at program startup is zero.  Many
52 library functions are guaranteed to set it to certain nonzero values
53 when they encounter certain kinds of errors.  These error conditions are
54 listed for each function.  These functions do not change @code{errno}
55 when they succeed; thus, the value of @code{errno} after a successful
56 call is not necessarily zero, and you should not use @code{errno} to
57 determine @emph{whether} a call failed.  The proper way to do that is
58 documented for each function.  @emph{If} the call failed, you can
59 examine @code{errno}.
61 Many library functions can set @code{errno} to a nonzero value as a
62 result of calling other library functions which might fail.  You should
63 assume that any library function might alter @code{errno} when the
64 function returns an error.
66 @strong{Portability Note:} @w{ISO C} specifies @code{errno} as a
67 ``modifiable lvalue'' rather than as a variable, permitting it to be
68 implemented as a macro.  For example, its expansion might involve a
69 function call, like @w{@code{*__errno_location ()}}.  In fact, that is
70 what it is
71 on @gnulinuxhurdsystems{}.  @Theglibc{}, on each system, does
72 whatever is right for the particular system.
74 There are a few library functions, like @code{sqrt} and @code{atan},
75 that return a perfectly legitimate value in case of an error, but also
76 set @code{errno}.  For these functions, if you want to check to see
77 whether an error occurred, the recommended method is to set @code{errno}
78 to zero before calling the function, and then check its value afterward.
79 @end deftypevr
81 @pindex errno.h
82 All the error codes have symbolic names; they are macros defined in
83 @file{errno.h}.  The names start with @samp{E} and an upper-case
84 letter or digit; you should consider names of this form to be
85 reserved names.  @xref{Reserved Names}.
87 The error code values are all positive integers and are all distinct,
88 with one exception: @code{EWOULDBLOCK} and @code{EAGAIN} are the same.
89 Since the values are distinct, you can use them as labels in a
90 @code{switch} statement; just don't use both @code{EWOULDBLOCK} and
91 @code{EAGAIN}.  Your program should not make any other assumptions about
92 the specific values of these symbolic constants.
94 The value of @code{errno} doesn't necessarily have to correspond to any
95 of these macros, since some library functions might return other error
96 codes of their own for other situations.  The only values that are
97 guaranteed to be meaningful for a particular library function are the
98 ones that this manual lists for that function.
100 Except on @gnuhurdsystems{}, almost any system call can return @code{EFAULT} if
101 it is given an invalid pointer as an argument.  Since this could only
102 happen as a result of a bug in your program, and since it will not
103 happen on @gnuhurdsystems{}, we have saved space by not mentioning
104 @code{EFAULT} in the descriptions of individual functions.
106 In some Unix systems, many system calls can also return @code{EFAULT} if
107 given as an argument a pointer into the stack, and the kernel for some
108 obscure reason fails in its attempt to extend the stack.  If this ever
109 happens, you should probably try using statically or dynamically
110 allocated memory instead of stack memory on that system.
112 @node Error Codes, Error Messages, Checking for Errors, Error Reporting
113 @section Error Codes
115 @pindex errno.h
116 The error code macros are defined in the header file @file{errno.h}.
117 All of them expand into integer constant values.  Some of these error
118 codes can't occur on @gnusystems{}, but they can occur using @theglibc{}
119 on other systems.
121 @comment errno.h
122 @comment POSIX.1: Operation not permitted
123 @deftypevr Macro int EPERM
124 @comment errno 1 @c DO NOT REMOVE
125 Operation not permitted; only the owner of the file (or other resource)
126 or processes with special privileges can perform the operation.
127 @end deftypevr
129 @comment errno.h
130 @comment POSIX.1: No such file or directory
131 @deftypevr Macro int ENOENT
132 @comment errno 2 @c DO NOT REMOVE
133 No such file or directory.  This is a ``file doesn't exist'' error
134 for ordinary files that are referenced in contexts where they are
135 expected to already exist.
136 @end deftypevr
138 @comment errno.h
139 @comment POSIX.1: No such process
140 @deftypevr Macro int ESRCH
141 @comment errno 3 @c DO NOT REMOVE
142 No process matches the specified process ID.
143 @end deftypevr
145 @comment errno.h
146 @comment POSIX.1: Interrupted system call
147 @deftypevr Macro int EINTR
148 @comment errno 4 @c DO NOT REMOVE
149 Interrupted function call; an asynchronous signal occurred and prevented
150 completion of the call.  When this happens, you should try the call
151 again.
153 You can choose to have functions resume after a signal that is handled,
154 rather than failing with @code{EINTR}; see @ref{Interrupted
155 Primitives}.
156 @end deftypevr
158 @comment errno.h
159 @comment POSIX.1: Input/output error
160 @deftypevr Macro int EIO
161 @comment errno 5 @c DO NOT REMOVE
162 Input/output error; usually used for physical read or write errors.
163 @end deftypevr
165 @comment errno.h
166 @comment POSIX.1: No such device or address
167 @deftypevr Macro int ENXIO
168 @comment errno 6 @c DO NOT REMOVE
169 No such device or address.  The system tried to use the device
170 represented by a file you specified, and it couldn't find the device.
171 This can mean that the device file was installed incorrectly, or that
172 the physical device is missing or not correctly attached to the
173 computer.
174 @end deftypevr
176 @comment errno.h
177 @comment POSIX.1: Argument list too long
178 @deftypevr Macro int E2BIG
179 @comment errno 7 @c DO NOT REMOVE
180 Argument list too long; used when the arguments passed to a new program
181 being executed with one of the @code{exec} functions (@pxref{Executing a
182 File}) occupy too much memory space.  This condition never arises on
183 @gnuhurdsystems{}.
184 @end deftypevr
186 @comment errno.h
187 @comment POSIX.1: Exec format error
188 @deftypevr Macro int ENOEXEC
189 @comment errno 8 @c DO NOT REMOVE
190 Invalid executable file format.  This condition is detected by the
191 @code{exec} functions; see @ref{Executing a File}.
192 @end deftypevr
194 @comment errno.h
195 @comment POSIX.1: Bad file descriptor
196 @deftypevr Macro int EBADF
197 @comment errno 9 @c DO NOT REMOVE
198 Bad file descriptor; for example, I/O on a descriptor that has been
199 closed or reading from a descriptor open only for writing (or vice
200 versa).
201 @end deftypevr
203 @comment errno.h
204 @comment POSIX.1: No child processes
205 @deftypevr Macro int ECHILD
206 @comment errno 10 @c DO NOT REMOVE
207 There are no child processes.  This error happens on operations that are
208 supposed to manipulate child processes, when there aren't any processes
209 to manipulate.
210 @end deftypevr
212 @comment errno.h
213 @comment POSIX.1: Resource deadlock avoided
214 @deftypevr Macro int EDEADLK
215 @comment errno 11 @c DO NOT REMOVE
216 Deadlock avoided; allocating a system resource would have resulted in a
217 deadlock situation.  The system does not guarantee that it will notice
218 all such situations.  This error means you got lucky and the system
219 noticed; it might just hang.  @xref{File Locks}, for an example.
220 @end deftypevr
222 @comment errno.h
223 @comment POSIX.1: Cannot allocate memory
224 @deftypevr Macro int ENOMEM
225 @comment errno 12 @c DO NOT REMOVE
226 No memory available.  The system cannot allocate more virtual memory
227 because its capacity is full.
228 @end deftypevr
230 @comment errno.h
231 @comment POSIX.1: Permission denied
232 @deftypevr Macro int EACCES
233 @comment errno 13 @c DO NOT REMOVE
234 Permission denied; the file permissions do not allow the attempted operation.
235 @end deftypevr
237 @comment errno.h
238 @comment POSIX.1: Bad address
239 @deftypevr Macro int EFAULT
240 @comment errno 14 @c DO NOT REMOVE
241 Bad address; an invalid pointer was detected.
242 On @gnuhurdsystems{}, this error never happens; you get a signal instead.
243 @end deftypevr
245 @comment errno.h
246 @comment BSD: Block device required
247 @deftypevr Macro int ENOTBLK
248 @comment errno 15 @c DO NOT REMOVE
249 A file that isn't a block special file was given in a situation that
250 requires one.  For example, trying to mount an ordinary file as a file
251 system in Unix gives this error.
252 @end deftypevr
254 @comment errno.h
255 @comment POSIX.1: Device or resource busy
256 @deftypevr Macro int EBUSY
257 @comment errno 16 @c DO NOT REMOVE
258 Resource busy; a system resource that can't be shared is already in use.
259 For example, if you try to delete a file that is the root of a currently
260 mounted filesystem, you get this error.
261 @end deftypevr
263 @comment errno.h
264 @comment POSIX.1: File exists
265 @deftypevr Macro int EEXIST
266 @comment errno 17 @c DO NOT REMOVE
267 File exists; an existing file was specified in a context where it only
268 makes sense to specify a new file.
269 @end deftypevr
271 @comment errno.h
272 @comment POSIX.1: Invalid cross-device link
273 @deftypevr Macro int EXDEV
274 @comment errno 18 @c DO NOT REMOVE
275 An attempt to make an improper link across file systems was detected.
276 This happens not only when you use @code{link} (@pxref{Hard Links}) but
277 also when you rename a file with @code{rename} (@pxref{Renaming Files}).
278 @end deftypevr
280 @comment errno.h
281 @comment POSIX.1: No such device
282 @deftypevr Macro int ENODEV
283 @comment errno 19 @c DO NOT REMOVE
284 The wrong type of device was given to a function that expects a
285 particular sort of device.
286 @end deftypevr
288 @comment errno.h
289 @comment POSIX.1: Not a directory
290 @deftypevr Macro int ENOTDIR
291 @comment errno 20 @c DO NOT REMOVE
292 A file that isn't a directory was specified when a directory is required.
293 @end deftypevr
295 @comment errno.h
296 @comment POSIX.1: Is a directory
297 @deftypevr Macro int EISDIR
298 @comment errno 21 @c DO NOT REMOVE
299 File is a directory; you cannot open a directory for writing,
300 or create or remove hard links to it.
301 @end deftypevr
303 @comment errno.h
304 @comment POSIX.1: Invalid argument
305 @deftypevr Macro int EINVAL
306 @comment errno 22 @c DO NOT REMOVE
307 Invalid argument.  This is used to indicate various kinds of problems
308 with passing the wrong argument to a library function.
309 @end deftypevr
311 @comment errno.h
312 @comment POSIX.1: Too many open files
313 @deftypevr Macro int EMFILE
314 @comment errno 24 @c DO NOT REMOVE
315 The current process has too many files open and can't open any more.
316 Duplicate descriptors do count toward this limit.
318 In BSD and GNU, the number of open files is controlled by a resource
319 limit that can usually be increased.  If you get this error, you might
320 want to increase the @code{RLIMIT_NOFILE} limit or make it unlimited;
321 @pxref{Limits on Resources}.
322 @end deftypevr
324 @comment errno.h
325 @comment POSIX.1: Too many open files in system
326 @deftypevr Macro int ENFILE
327 @comment errno 23 @c DO NOT REMOVE
328 There are too many distinct file openings in the entire system.  Note
329 that any number of linked channels count as just one file opening; see
330 @ref{Linked Channels}.  This error never occurs on @gnuhurdsystems{}.
331 @end deftypevr
333 @comment errno.h
334 @comment POSIX.1: Inappropriate ioctl for device
335 @deftypevr Macro int ENOTTY
336 @comment errno 25 @c DO NOT REMOVE
337 Inappropriate I/O control operation, such as trying to set terminal
338 modes on an ordinary file.
339 @end deftypevr
341 @comment errno.h
342 @comment BSD: Text file busy
343 @deftypevr Macro int ETXTBSY
344 @comment errno 26 @c DO NOT REMOVE
345 An attempt to execute a file that is currently open for writing, or
346 write to a file that is currently being executed.  Often using a
347 debugger to run a program is considered having it open for writing and
348 will cause this error.  (The name stands for ``text file busy''.)  This
349 is not an error on @gnuhurdsystems{}; the text is copied as necessary.
350 @end deftypevr
352 @comment errno.h
353 @comment POSIX.1: File too large
354 @deftypevr Macro int EFBIG
355 @comment errno 27 @c DO NOT REMOVE
356 File too big; the size of a file would be larger than allowed by the system.
357 @end deftypevr
359 @comment errno.h
360 @comment POSIX.1: No space left on device
361 @deftypevr Macro int ENOSPC
362 @comment errno 28 @c DO NOT REMOVE
363 No space left on device; write operation on a file failed because the
364 disk is full.
365 @end deftypevr
367 @comment errno.h
368 @comment POSIX.1: Illegal seek
369 @deftypevr Macro int ESPIPE
370 @comment errno 29 @c DO NOT REMOVE
371 Invalid seek operation (such as on a pipe).
372 @end deftypevr
374 @comment errno.h
375 @comment POSIX.1: Read-only file system
376 @deftypevr Macro int EROFS
377 @comment errno 30 @c DO NOT REMOVE
378 An attempt was made to modify something on a read-only file system.
379 @end deftypevr
381 @comment errno.h
382 @comment POSIX.1: Too many links
383 @deftypevr Macro int EMLINK
384 @comment errno 31 @c DO NOT REMOVE
385 Too many links; the link count of a single file would become too large.
386 @code{rename} can cause this error if the file being renamed already has
387 as many links as it can take (@pxref{Renaming Files}).
388 @end deftypevr
390 @comment errno.h
391 @comment POSIX.1: Broken pipe
392 @deftypevr Macro int EPIPE
393 @comment errno 32 @c DO NOT REMOVE
394 Broken pipe; there is no process reading from the other end of a pipe.
395 Every library function that returns this error code also generates a
396 @code{SIGPIPE} signal; this signal terminates the program if not handled
397 or blocked.  Thus, your program will never actually see @code{EPIPE}
398 unless it has handled or blocked @code{SIGPIPE}.
399 @end deftypevr
401 @comment errno.h
402 @comment ISO: Numerical argument out of domain
403 @deftypevr Macro int EDOM
404 @comment errno 33 @c DO NOT REMOVE
405 Domain error; used by mathematical functions when an argument value does
406 not fall into the domain over which the function is defined.
407 @end deftypevr
409 @comment errno.h
410 @comment ISO: Numerical result out of range
411 @deftypevr Macro int ERANGE
412 @comment errno 34 @c DO NOT REMOVE
413 Range error; used by mathematical functions when the result value is
414 not representable because of overflow or underflow.
415 @end deftypevr
417 @comment errno.h
418 @comment POSIX.1: Resource temporarily unavailable
419 @deftypevr Macro int EAGAIN
420 @comment errno 35 @c DO NOT REMOVE
421 Resource temporarily unavailable; the call might work if you try again
422 later.  The macro @code{EWOULDBLOCK} is another name for @code{EAGAIN};
423 they are always the same in @theglibc{}.
425 This error can happen in a few different situations:
427 @itemize @bullet
428 @item
429 An operation that would block was attempted on an object that has
430 non-blocking mode selected.  Trying the same operation again will block
431 until some external condition makes it possible to read, write, or
432 connect (whatever the operation).  You can use @code{select} to find out
433 when the operation will be possible; @pxref{Waiting for I/O}.
435 @strong{Portability Note:} In many older Unix systems, this condition
436 was indicated by @code{EWOULDBLOCK}, which was a distinct error code
437 different from @code{EAGAIN}.  To make your program portable, you should
438 check for both codes and treat them the same.
440 @item
441 A temporary resource shortage made an operation impossible.  @code{fork}
442 can return this error.  It indicates that the shortage is expected to
443 pass, so your program can try the call again later and it may succeed.
444 It is probably a good idea to delay for a few seconds before trying it
445 again, to allow time for other processes to release scarce resources.
446 Such shortages are usually fairly serious and affect the whole system,
447 so usually an interactive program should report the error to the user
448 and return to its command loop.
449 @end itemize
450 @end deftypevr
452 @comment errno.h
453 @comment BSD: Operation would block
454 @deftypevr Macro int EWOULDBLOCK
455 @comment errno EAGAIN @c DO NOT REMOVE
456 In @theglibc{}, this is another name for @code{EAGAIN} (above).
457 The values are always the same, on every operating system.
459 C libraries in many older Unix systems have @code{EWOULDBLOCK} as a
460 separate error code.
461 @end deftypevr
463 @comment errno.h
464 @comment BSD: Operation now in progress
465 @deftypevr Macro int EINPROGRESS
466 @comment errno 36 @c DO NOT REMOVE
467 An operation that cannot complete immediately was initiated on an object
468 that has non-blocking mode selected.  Some functions that must always
469 block (such as @code{connect}; @pxref{Connecting}) never return
470 @code{EAGAIN}.  Instead, they return @code{EINPROGRESS} to indicate that
471 the operation has begun and will take some time.  Attempts to manipulate
472 the object before the call completes return @code{EALREADY}.  You can
473 use the @code{select} function to find out when the pending operation
474 has completed; @pxref{Waiting for I/O}.
475 @end deftypevr
477 @comment errno.h
478 @comment BSD: Operation already in progress
479 @deftypevr Macro int EALREADY
480 @comment errno 37 @c DO NOT REMOVE
481 An operation is already in progress on an object that has non-blocking
482 mode selected.
483 @end deftypevr
485 @comment errno.h
486 @comment BSD: Socket operation on non-socket
487 @deftypevr Macro int ENOTSOCK
488 @comment errno 38 @c DO NOT REMOVE
489 A file that isn't a socket was specified when a socket is required.
490 @end deftypevr
492 @comment errno.h
493 @comment BSD: Message too long
494 @deftypevr Macro int EMSGSIZE
495 @comment errno 40 @c DO NOT REMOVE
496 The size of a message sent on a socket was larger than the supported
497 maximum size.
498 @end deftypevr
500 @comment errno.h
501 @comment BSD: Protocol wrong type for socket
502 @deftypevr Macro int EPROTOTYPE
503 @comment errno 41 @c DO NOT REMOVE
504 The socket type does not support the requested communications protocol.
505 @end deftypevr
507 @comment errno.h
508 @comment BSD: Protocol not available
509 @deftypevr Macro int ENOPROTOOPT
510 @comment errno 42 @c DO NOT REMOVE
511 You specified a socket option that doesn't make sense for the
512 particular protocol being used by the socket.  @xref{Socket Options}.
513 @end deftypevr
515 @comment errno.h
516 @comment BSD: Protocol not supported
517 @deftypevr Macro int EPROTONOSUPPORT
518 @comment errno 43 @c DO NOT REMOVE
519 The socket domain does not support the requested communications protocol
520 (perhaps because the requested protocol is completely invalid).
521 @xref{Creating a Socket}.
522 @end deftypevr
524 @comment errno.h
525 @comment BSD: Socket type not supported
526 @deftypevr Macro int ESOCKTNOSUPPORT
527 @comment errno 44 @c DO NOT REMOVE
528 The socket type is not supported.
529 @end deftypevr
531 @comment errno.h
532 @comment BSD: Operation not supported
533 @deftypevr Macro int EOPNOTSUPP
534 @comment errno 45 @c DO NOT REMOVE
535 The operation you requested is not supported.  Some socket functions
536 don't make sense for all types of sockets, and others may not be
537 implemented for all communications protocols.  On @gnuhurdsystems{}, this
538 error can happen for many calls when the object does not support the
539 particular operation; it is a generic indication that the server knows
540 nothing to do for that call.
541 @end deftypevr
543 @comment errno.h
544 @comment BSD: Protocol family not supported
545 @deftypevr Macro int EPFNOSUPPORT
546 @comment errno 46 @c DO NOT REMOVE
547 The socket communications protocol family you requested is not supported.
548 @end deftypevr
550 @comment errno.h
551 @comment BSD: Address family not supported by protocol
552 @deftypevr Macro int EAFNOSUPPORT
553 @comment errno 47 @c DO NOT REMOVE
554 The address family specified for a socket is not supported; it is
555 inconsistent with the protocol being used on the socket.  @xref{Sockets}.
556 @end deftypevr
558 @comment errno.h
559 @comment BSD: Address already in use
560 @deftypevr Macro int EADDRINUSE
561 @comment errno 48 @c DO NOT REMOVE
562 The requested socket address is already in use.  @xref{Socket Addresses}.
563 @end deftypevr
565 @comment errno.h
566 @comment BSD: Cannot assign requested address
567 @deftypevr Macro int EADDRNOTAVAIL
568 @comment errno 49 @c DO NOT REMOVE
569 The requested socket address is not available; for example, you tried
570 to give a socket a name that doesn't match the local host name.
571 @xref{Socket Addresses}.
572 @end deftypevr
574 @comment errno.h
575 @comment BSD: Network is down
576 @deftypevr Macro int ENETDOWN
577 @comment errno 50 @c DO NOT REMOVE
578 A socket operation failed because the network was down.
579 @end deftypevr
581 @comment errno.h
582 @comment BSD: Network is unreachable
583 @deftypevr Macro int ENETUNREACH
584 @comment errno 51 @c DO NOT REMOVE
585 A socket operation failed because the subnet containing the remote host
586 was unreachable.
587 @end deftypevr
589 @comment errno.h
590 @comment BSD: Network dropped connection on reset
591 @deftypevr Macro int ENETRESET
592 @comment errno 52 @c DO NOT REMOVE
593 A network connection was reset because the remote host crashed.
594 @end deftypevr
596 @comment errno.h
597 @comment BSD: Software caused connection abort
598 @deftypevr Macro int ECONNABORTED
599 @comment errno 53 @c DO NOT REMOVE
600 A network connection was aborted locally.
601 @end deftypevr
603 @comment errno.h
604 @comment BSD: Connection reset by peer
605 @deftypevr Macro int ECONNRESET
606 @comment errno 54 @c DO NOT REMOVE
607 A network connection was closed for reasons outside the control of the
608 local host, such as by the remote machine rebooting or an unrecoverable
609 protocol violation.
610 @end deftypevr
612 @comment errno.h
613 @comment BSD: No buffer space available
614 @deftypevr Macro int ENOBUFS
615 @comment errno 55 @c DO NOT REMOVE
616 The kernel's buffers for I/O operations are all in use.  In GNU, this
617 error is always synonymous with @code{ENOMEM}; you may get one or the
618 other from network operations.
619 @end deftypevr
621 @comment errno.h
622 @comment BSD: Transport endpoint is already connected
623 @deftypevr Macro int EISCONN
624 @comment errno 56 @c DO NOT REMOVE
625 You tried to connect a socket that is already connected.
626 @xref{Connecting}.
627 @end deftypevr
629 @comment errno.h
630 @comment BSD: Transport endpoint is not connected
631 @deftypevr Macro int ENOTCONN
632 @comment errno 57 @c DO NOT REMOVE
633 The socket is not connected to anything.  You get this error when you
634 try to transmit data over a socket, without first specifying a
635 destination for the data.  For a connectionless socket (for datagram
636 protocols, such as UDP), you get @code{EDESTADDRREQ} instead.
637 @end deftypevr
639 @comment errno.h
640 @comment BSD: Destination address required
641 @deftypevr Macro int EDESTADDRREQ
642 @comment errno 39 @c DO NOT REMOVE
643 No default destination address was set for the socket.  You get this
644 error when you try to transmit data over a connectionless socket,
645 without first specifying a destination for the data with @code{connect}.
646 @end deftypevr
648 @comment errno.h
649 @comment BSD: Cannot send after transport endpoint shutdown
650 @deftypevr Macro int ESHUTDOWN
651 @comment errno 58 @c DO NOT REMOVE
652 The socket has already been shut down.
653 @end deftypevr
655 @comment errno.h
656 @comment BSD: Too many references: cannot splice
657 @deftypevr Macro int ETOOMANYREFS
658 @comment errno 59 @c DO NOT REMOVE
660 @end deftypevr
662 @comment errno.h
663 @comment BSD: Connection timed out
664 @deftypevr Macro int ETIMEDOUT
665 @comment errno 60 @c DO NOT REMOVE
666 A socket operation with a specified timeout received no response during
667 the timeout period.
668 @end deftypevr
670 @comment errno.h
671 @comment BSD: Connection refused
672 @deftypevr Macro int ECONNREFUSED
673 @comment errno 61 @c DO NOT REMOVE
674 A remote host refused to allow the network connection (typically because
675 it is not running the requested service).
676 @end deftypevr
678 @comment errno.h
679 @comment BSD: Too many levels of symbolic links
680 @deftypevr Macro int ELOOP
681 @comment errno 62 @c DO NOT REMOVE
682 Too many levels of symbolic links were encountered in looking up a file name.
683 This often indicates a cycle of symbolic links.
684 @end deftypevr
686 @comment errno.h
687 @comment POSIX.1: File name too long
688 @deftypevr Macro int ENAMETOOLONG
689 @comment errno 63 @c DO NOT REMOVE
690 Filename too long (longer than @code{PATH_MAX}; @pxref{Limits for
691 Files}) or host name too long (in @code{gethostname} or
692 @code{sethostname}; @pxref{Host Identification}).
693 @end deftypevr
695 @comment errno.h
696 @comment BSD: Host is down
697 @deftypevr Macro int EHOSTDOWN
698 @comment errno 64 @c DO NOT REMOVE
699 The remote host for a requested network connection is down.
700 @end deftypevr
702 @comment errno.h
703 @comment BSD: No route to host
704 @deftypevr Macro int EHOSTUNREACH
705 @comment errno 65 @c DO NOT REMOVE
706 The remote host for a requested network connection is not reachable.
707 @end deftypevr
709 @comment errno.h
710 @comment POSIX.1: Directory not empty
711 @deftypevr Macro int ENOTEMPTY
712 @comment errno 66 @c DO NOT REMOVE
713 Directory not empty, where an empty directory was expected.  Typically,
714 this error occurs when you are trying to delete a directory.
715 @end deftypevr
717 @comment errno.h
718 @comment BSD: Too many processes
719 @deftypevr Macro int EPROCLIM
720 @comment errno 67 @c DO NOT REMOVE
721 This means that the per-user limit on new process would be exceeded by
722 an attempted @code{fork}.  @xref{Limits on Resources}, for details on
723 the @code{RLIMIT_NPROC} limit.
724 @end deftypevr
726 @comment errno.h
727 @comment BSD: Too many users
728 @deftypevr Macro int EUSERS
729 @comment errno 68 @c DO NOT REMOVE
730 The file quota system is confused because there are too many users.
731 @c This can probably happen in a GNU system when using NFS.
732 @end deftypevr
734 @comment errno.h
735 @comment BSD: Disk quota exceeded
736 @deftypevr Macro int EDQUOT
737 @comment errno 69 @c DO NOT REMOVE
738 The user's disk quota was exceeded.
739 @end deftypevr
741 @comment errno.h
742 @comment BSD: Stale NFS file handle
743 @deftypevr Macro int ESTALE
744 @comment errno 70 @c DO NOT REMOVE
745 Stale NFS file handle.  This indicates an internal confusion in the NFS
746 system which is due to file system rearrangements on the server host.
747 Repairing this condition usually requires unmounting and remounting
748 the NFS file system on the local host.
749 @end deftypevr
751 @comment errno.h
752 @comment BSD: Object is remote
753 @deftypevr Macro int EREMOTE
754 @comment errno 71 @c DO NOT REMOVE
755 An attempt was made to NFS-mount a remote file system with a file name that
756 already specifies an NFS-mounted file.
757 (This is an error on some operating systems, but we expect it to work
758 properly on @gnuhurdsystems{}, making this error code impossible.)
759 @end deftypevr
761 @comment errno.h
762 @comment BSD: RPC struct is bad
763 @deftypevr Macro int EBADRPC
764 @comment errno 72 @c DO NOT REMOVE
766 @end deftypevr
768 @comment errno.h
769 @comment BSD: RPC version wrong
770 @deftypevr Macro int ERPCMISMATCH
771 @comment errno 73 @c DO NOT REMOVE
773 @end deftypevr
775 @comment errno.h
776 @comment BSD: RPC program not available
777 @deftypevr Macro int EPROGUNAVAIL
778 @comment errno 74 @c DO NOT REMOVE
780 @end deftypevr
782 @comment errno.h
783 @comment BSD: RPC program version wrong
784 @deftypevr Macro int EPROGMISMATCH
785 @comment errno 75 @c DO NOT REMOVE
787 @end deftypevr
789 @comment errno.h
790 @comment BSD: RPC bad procedure for program
791 @deftypevr Macro int EPROCUNAVAIL
792 @comment errno 76 @c DO NOT REMOVE
794 @end deftypevr
796 @comment errno.h
797 @comment POSIX.1: No locks available
798 @deftypevr Macro int ENOLCK
799 @comment errno 77 @c DO NOT REMOVE
800 No locks available.  This is used by the file locking facilities; see
801 @ref{File Locks}.  This error is never generated by @gnuhurdsystems{}, but
802 it can result from an operation to an NFS server running another
803 operating system.
804 @end deftypevr
806 @comment errno.h
807 @comment BSD: Inappropriate file type or format
808 @deftypevr Macro int EFTYPE
809 @comment errno 79 @c DO NOT REMOVE
810 Inappropriate file type or format.  The file was the wrong type for the
811 operation, or a data file had the wrong format.
813 On some systems @code{chmod} returns this error if you try to set the
814 sticky bit on a non-directory file; @pxref{Setting Permissions}.
815 @end deftypevr
817 @comment errno.h
818 @comment BSD: Authentication error
819 @deftypevr Macro int EAUTH
820 @comment errno 80 @c DO NOT REMOVE
822 @end deftypevr
824 @comment errno.h
825 @comment BSD: Need authenticator
826 @deftypevr Macro int ENEEDAUTH
827 @comment errno 81 @c DO NOT REMOVE
829 @end deftypevr
831 @comment errno.h
832 @comment POSIX.1: Function not implemented
833 @deftypevr Macro int ENOSYS
834 @comment errno 78 @c DO NOT REMOVE
835 Function not implemented.  This indicates that the function called is
836 not implemented at all, either in the C library itself or in the
837 operating system.  When you get this error, you can be sure that this
838 particular function will always fail with @code{ENOSYS} unless you
839 install a new version of the C library or the operating system.
840 @end deftypevr
842 @comment errno.h
843 @comment POSIX.1: Not supported
844 @deftypevr Macro int ENOTSUP
845 @comment errno 118 @c DO NOT REMOVE
846 Not supported.  A function returns this error when certain parameter
847 values are valid, but the functionality they request is not available.
848 This can mean that the function does not implement a particular command
849 or option value or flag bit at all.  For functions that operate on some
850 object given in a parameter, such as a file descriptor or a port, it
851 might instead mean that only @emph{that specific object} (file
852 descriptor, port, etc.) is unable to support the other parameters given;
853 different file descriptors might support different ranges of parameter
854 values.
856 If the entire function is not available at all in the implementation,
857 it returns @code{ENOSYS} instead.
858 @end deftypevr
860 @comment errno.h
861 @comment ISO: Invalid or incomplete multibyte or wide character
862 @deftypevr Macro int EILSEQ
863 @comment errno 106 @c DO NOT REMOVE
864 While decoding a multibyte character the function came along an invalid
865 or an incomplete sequence of bytes or the given wide character is invalid.
866 @end deftypevr
868 @comment errno.h
869 @comment GNU: Inappropriate operation for background process
870 @deftypevr Macro int EBACKGROUND
871 @comment errno 100 @c DO NOT REMOVE
872 On @gnuhurdsystems{}, servers supporting the @code{term} protocol return
873 this error for certain operations when the caller is not in the
874 foreground process group of the terminal.  Users do not usually see this
875 error because functions such as @code{read} and @code{write} translate
876 it into a @code{SIGTTIN} or @code{SIGTTOU} signal.  @xref{Job Control},
877 for information on process groups and these signals.
878 @end deftypevr
880 @comment errno.h
881 @comment GNU: Translator died
882 @deftypevr Macro int EDIED
883 @comment errno 101 @c DO NOT REMOVE
884 On @gnuhurdsystems{}, opening a file returns this error when the file is
885 translated by a program and the translator program dies while starting
886 up, before it has connected to the file.
887 @end deftypevr
889 @comment errno.h
890 @comment GNU: ?
891 @deftypevr Macro int ED
892 @comment errno 102 @c DO NOT REMOVE
893 The experienced user will know what is wrong.
894 @c This error code is a joke.  Its perror text is part of the joke.
895 @c Don't change it.
896 @end deftypevr
898 @comment errno.h
899 @comment GNU: You really blew it this time
900 @deftypevr Macro int EGREGIOUS
901 @comment errno 103 @c DO NOT REMOVE
902 You did @strong{what}?
903 @end deftypevr
905 @comment errno.h
906 @comment GNU: Computer bought the farm
907 @deftypevr Macro int EIEIO
908 @comment errno 104 @c DO NOT REMOVE
909 Go home and have a glass of warm, dairy-fresh milk.
910 @end deftypevr
912 @comment errno.h
913 @comment GNU: Gratuitous error
914 @deftypevr Macro int EGRATUITOUS
915 @comment errno 105 @c DO NOT REMOVE
916 This error code has no purpose.
917 @end deftypevr
919 @comment errno.h
920 @comment XOPEN: Bad message
921 @deftypevr Macro int EBADMSG
922 @comment errno 107
923 @end deftypevr
925 @comment errno.h
926 @comment XOPEN: Identifier removed
927 @deftypevr Macro int EIDRM
928 @comment errno 108
929 @end deftypevr
931 @comment errno.h
932 @comment XOPEN: Multihop attempted
933 @deftypevr Macro int EMULTIHOP
934 @comment errno 109
935 @end deftypevr
937 @comment errno.h
938 @comment XOPEN: No data available
939 @deftypevr Macro int ENODATA
940 @comment errno 110
941 @end deftypevr
943 @comment errno.h
944 @comment XOPEN: Link has been severed
945 @deftypevr Macro int ENOLINK
946 @comment errno 111
947 @end deftypevr
949 @comment errno.h
950 @comment XOPEN: No message of desired type
951 @deftypevr Macro int ENOMSG
952 @comment errno 112
953 @end deftypevr
955 @comment errno.h
956 @comment XOPEN: Out of streams resources
957 @deftypevr Macro int ENOSR
958 @comment errno 113
959 @end deftypevr
961 @comment errno.h
962 @comment XOPEN: Device not a stream
963 @deftypevr Macro int ENOSTR
964 @comment errno 114
965 @end deftypevr
967 @comment errno.h
968 @comment XOPEN: Value too large for defined data type
969 @deftypevr Macro int EOVERFLOW
970 @comment errno 115
971 @end deftypevr
973 @comment errno.h
974 @comment XOPEN: Protocol error
975 @deftypevr Macro int EPROTO
976 @comment errno 116
977 @end deftypevr
979 @comment errno.h
980 @comment XOPEN: Timer expired
981 @deftypevr Macro int ETIME
982 @comment errno 117
983 @end deftypevr
985 @comment errno.h
986 @comment POSIX.1: Operation canceled
987 @deftypevr Macro int ECANCELED
988 @comment errno 119
989 Operation canceled; an asynchronous operation was canceled before it
990 completed.  @xref{Asynchronous I/O}.  When you call @code{aio_cancel},
991 the normal result is for the operations affected to complete with this
992 error; @pxref{Cancel AIO Operations}.
993 @end deftypevr
996 @emph{The following error codes are defined by the Linux/i386 kernel.
997 They are not yet documented.}
999 @comment errno.h
1000 @comment Linux???: Interrupted system call should be restarted
1001 @deftypevr Macro int ERESTART
1002 @comment errno ???/85
1003 @end deftypevr
1005 @comment errno.h
1006 @comment Linux???: Channel number out of range
1007 @deftypevr Macro int ECHRNG
1008 @comment errno ???/44
1009 @end deftypevr
1011 @comment errno.h
1012 @comment Obsolete: Level 2 not synchronized
1013 @deftypevr Macro int EL2NSYNC
1014 @comment errno ???/45
1015 @end deftypevr
1017 @comment errno.h
1018 @comment Obsolete: Level 3 halted
1019 @deftypevr Macro int EL3HLT
1020 @comment errno ???/46
1021 @end deftypevr
1023 @comment errno.h
1024 @comment Obsolete: Level 3 reset
1025 @deftypevr Macro int EL3RST
1026 @comment errno ???/47
1027 @end deftypevr
1029 @comment errno.h
1030 @comment Linux???: Link number out of range
1031 @deftypevr Macro int ELNRNG
1032 @comment errno ???/48
1033 @end deftypevr
1035 @comment errno.h
1036 @comment Linux???: Protocol driver not attached
1037 @deftypevr Macro int EUNATCH
1038 @comment errno ???/49
1039 @end deftypevr
1041 @comment errno.h
1042 @comment Linux???: No CSI structure available
1043 @deftypevr Macro int ENOCSI
1044 @comment errno ???/50
1045 @end deftypevr
1047 @comment errno.h
1048 @comment Obsolete: Level 2 halted
1049 @deftypevr Macro int EL2HLT
1050 @comment errno ???/51
1051 @end deftypevr
1053 @comment errno.h
1054 @comment Linux???: Invalid exchange
1055 @deftypevr Macro int EBADE
1056 @comment errno ???/52
1057 @end deftypevr
1059 @comment errno.h
1060 @comment Linux???: Invalid request descriptor
1061 @deftypevr Macro int EBADR
1062 @comment errno ???/53
1063 @end deftypevr
1065 @comment errno.h
1066 @comment Linux???: Exchange full
1067 @deftypevr Macro int EXFULL
1068 @comment errno ???/54
1069 @end deftypevr
1071 @comment errno.h
1072 @comment Linux???: No anode
1073 @deftypevr Macro int ENOANO
1074 @comment errno ???/55
1075 @end deftypevr
1077 @comment errno.h
1078 @comment Linux???: Invalid request code
1079 @deftypevr Macro int EBADRQC
1080 @comment errno ???/56
1081 @end deftypevr
1083 @comment errno.h
1084 @comment Linux???: Invalid slot
1085 @deftypevr Macro int EBADSLT
1086 @comment errno ???/57
1087 @end deftypevr
1089 @comment errno.h
1090 @comment Linux???: File locking deadlock error
1091 @deftypevr Macro int EDEADLOCK
1092 @comment errno ???/58
1093 @end deftypevr
1095 @comment errno.h
1096 @comment Linux???: Bad font file format
1097 @deftypevr Macro int EBFONT
1098 @comment errno ???/59
1099 @end deftypevr
1101 @comment errno.h
1102 @comment Linux???: Machine is not on the network
1103 @deftypevr Macro int ENONET
1104 @comment errno ???/64
1105 @end deftypevr
1107 @comment errno.h
1108 @comment Linux???: Package not installed
1109 @deftypevr Macro int ENOPKG
1110 @comment errno ???/65
1111 @end deftypevr
1113 @comment errno.h
1114 @comment Linux???: Advertise error
1115 @deftypevr Macro int EADV
1116 @comment errno ???/68
1117 @end deftypevr
1119 @comment errno.h
1120 @comment Linux???: Srmount error
1121 @deftypevr Macro int ESRMNT
1122 @comment errno ???/69
1123 @end deftypevr
1125 @comment errno.h
1126 @comment Linux???: Communication error on send
1127 @deftypevr Macro int ECOMM
1128 @comment errno ???/70
1129 @end deftypevr
1131 @comment errno.h
1132 @comment Linux???: RFS specific error
1133 @deftypevr Macro int EDOTDOT
1134 @comment errno ???/73
1135 @end deftypevr
1137 @comment errno.h
1138 @comment Linux???: Name not unique on network
1139 @deftypevr Macro int ENOTUNIQ
1140 @comment errno ???/76
1141 @end deftypevr
1143 @comment errno.h
1144 @comment Linux???: File descriptor in bad state
1145 @deftypevr Macro int EBADFD
1146 @comment errno ???/77
1147 @end deftypevr
1149 @comment errno.h
1150 @comment Linux???: Remote address changed
1151 @deftypevr Macro int EREMCHG
1152 @comment errno ???/78
1153 @end deftypevr
1155 @comment errno.h
1156 @comment Linux???: Can not access a needed shared library
1157 @deftypevr Macro int ELIBACC
1158 @comment errno ???/79
1159 @end deftypevr
1161 @comment errno.h
1162 @comment Linux???: Accessing a corrupted shared library
1163 @deftypevr Macro int ELIBBAD
1164 @comment errno ???/80
1165 @end deftypevr
1167 @comment errno.h
1168 @comment Linux???: .lib section in a.out corrupted
1169 @deftypevr Macro int ELIBSCN
1170 @comment errno ???/81
1171 @end deftypevr
1173 @comment errno.h
1174 @comment Linux???: Attempting to link in too many shared libraries
1175 @deftypevr Macro int ELIBMAX
1176 @comment errno ???/82
1177 @end deftypevr
1179 @comment errno.h
1180 @comment Linux???: Cannot exec a shared library directly
1181 @deftypevr Macro int ELIBEXEC
1182 @comment errno ???/83
1183 @end deftypevr
1185 @comment errno.h
1186 @comment Linux???: Streams pipe error
1187 @deftypevr Macro int ESTRPIPE
1188 @comment errno ???/86
1189 @end deftypevr
1191 @comment errno.h
1192 @comment Linux???: Structure needs cleaning
1193 @deftypevr Macro int EUCLEAN
1194 @comment errno ???/117
1195 @end deftypevr
1197 @comment errno.h
1198 @comment Linux???: Not a XENIX named type file
1199 @deftypevr Macro int ENOTNAM
1200 @comment errno ???/118
1201 @end deftypevr
1203 @comment errno.h
1204 @comment Linux???: No XENIX semaphores available
1205 @deftypevr Macro int ENAVAIL
1206 @comment errno ???/119
1207 @end deftypevr
1209 @comment errno.h
1210 @comment Linux???: Is a named type file
1211 @deftypevr Macro int EISNAM
1212 @comment errno ???/120
1213 @end deftypevr
1215 @comment errno.h
1216 @comment Linux???: Remote I/O error
1217 @deftypevr Macro int EREMOTEIO
1218 @comment errno ???/121
1219 @end deftypevr
1221 @comment errno.h
1222 @comment Linux???: No medium found
1223 @deftypevr Macro int ENOMEDIUM
1224 @comment errno ???/???
1225 @end deftypevr
1227 @comment errno.h
1228 @comment Linux???: Wrong medium type
1229 @deftypevr Macro int EMEDIUMTYPE
1230 @comment errno ???/???
1231 @end deftypevr
1233 @comment errno.h
1234 @comment Linux: Required key not available
1235 @deftypevr Macro int ENOKEY
1236 @comment errno ???/???
1237 @end deftypevr
1239 @comment errno.h
1240 @comment Linux: Key has expired
1241 @deftypevr Macro int EKEYEXPIRED
1242 @comment errno ???/???
1243 @end deftypevr
1245 @comment errno.h
1246 @comment Linux: Key has been revoked
1247 @deftypevr Macro int EKEYREVOKED
1248 @comment errno ???/???
1249 @end deftypevr
1251 @comment errno.h
1252 @comment Linux: Key was rejected by service
1253 @deftypevr Macro int EKEYREJECTED
1254 @comment errno ???/???
1255 @end deftypevr
1257 @comment errno.h
1258 @comment Linux: Owner died
1259 @deftypevr Macro int EOWNERDEAD
1260 @comment errno ???/???
1261 @end deftypevr
1263 @comment errno.h
1264 @comment Linux: State not recoverable
1265 @deftypevr Macro int ENOTRECOVERABLE
1266 @comment errno ???/???
1267 @end deftypevr
1269 @comment errno.h
1270 @comment Linux: Operation not possible due to RF-kill
1271 @deftypevr Macro int ERFKILL
1272 @comment errno ???/???
1273 @end deftypevr
1275 @comment errno.h
1276 @comment Linux: Memory page has hardware error
1277 @deftypevr Macro int EHWPOISON
1278 @comment errno ???/???
1279 @end deftypevr
1281 @node Error Messages,  , Error Codes, Error Reporting
1282 @section Error Messages
1284 The library has functions and variables designed to make it easy for
1285 your program to report informative error messages in the customary
1286 format about the failure of a library call.  The functions
1287 @code{strerror} and @code{perror} give you the standard error message
1288 for a given error code; the variable
1289 @w{@code{program_invocation_short_name}} gives you convenient access to the
1290 name of the program that encountered the error.
1292 @comment string.h
1293 @comment ISO
1294 @deftypefun {char *} strerror (int @var{errnum})
1295 The @code{strerror} function maps the error code (@pxref{Checking for
1296 Errors}) specified by the @var{errnum} argument to a descriptive error
1297 message string.  The return value is a pointer to this string.
1299 The value @var{errnum} normally comes from the variable @code{errno}.
1301 You should not modify the string returned by @code{strerror}.  Also, if
1302 you make subsequent calls to @code{strerror}, the string might be
1303 overwritten.  (But it's guaranteed that no library function ever calls
1304 @code{strerror} behind your back.)
1306 The function @code{strerror} is declared in @file{string.h}.
1307 @end deftypefun
1309 @comment string.h
1310 @comment GNU
1311 @deftypefun {char *} strerror_r (int @var{errnum}, char *@var{buf}, size_t @var{n})
1312 The @code{strerror_r} function works like @code{strerror} but instead of
1313 returning the error message in a statically allocated buffer shared by
1314 all threads in the process, it returns a private copy for the
1315 thread. This might be either some permanent global data or a message
1316 string in the user supplied buffer starting at @var{buf} with the
1317 length of @var{n} bytes.
1319 At most @var{n} characters are written (including the NUL byte) so it is
1320 up to the user to select the buffer large enough.
1322 This function should always be used in multi-threaded programs since
1323 there is no way to guarantee the string returned by @code{strerror}
1324 really belongs to the last call of the current thread.
1326 This function @code{strerror_r} is a GNU extension and it is declared in
1327 @file{string.h}.
1328 @end deftypefun
1330 @comment stdio.h
1331 @comment ISO
1332 @deftypefun void perror (const char *@var{message})
1333 This function prints an error message to the stream @code{stderr};
1334 see @ref{Standard Streams}.  The orientation of @code{stderr} is not
1335 changed.
1337 If you call @code{perror} with a @var{message} that is either a null
1338 pointer or an empty string, @code{perror} just prints the error message
1339 corresponding to @code{errno}, adding a trailing newline.
1341 If you supply a non-null @var{message} argument, then @code{perror}
1342 prefixes its output with this string.  It adds a colon and a space
1343 character to separate the @var{message} from the error string corresponding
1344 to @code{errno}.
1346 The function @code{perror} is declared in @file{stdio.h}.
1347 @end deftypefun
1349 @code{strerror} and @code{perror} produce the exact same message for any
1350 given error code; the precise text varies from system to system.  With
1351 @theglibc{}, the messages are fairly short; there are no multi-line
1352 messages or embedded newlines.  Each error message begins with a capital
1353 letter and does not include any terminating punctuation.
1355 @strong{Compatibility Note:} The @code{strerror} function was introduced
1356 in @w{ISO C89}.  Many older C systems do not support this function yet.
1358 @cindex program name
1359 @cindex name of running program
1360 Many programs that don't read input from the terminal are designed to
1361 exit if any system call fails.  By convention, the error message from
1362 such a program should start with the program's name, sans directories.
1363 You can find that name in the variable
1364 @code{program_invocation_short_name}; the full file name is stored the
1365 variable @code{program_invocation_name}.
1367 @comment errno.h
1368 @comment GNU
1369 @deftypevar {char *} program_invocation_name
1370 This variable's value is the name that was used to invoke the program
1371 running in the current process.  It is the same as @code{argv[0]}.  Note
1372 that this is not necessarily a useful file name; often it contains no
1373 directory names.  @xref{Program Arguments}.
1374 @end deftypevar
1376 @comment errno.h
1377 @comment GNU
1378 @deftypevar {char *} program_invocation_short_name
1379 This variable's value is the name that was used to invoke the program
1380 running in the current process, with directory names removed.  (That is
1381 to say, it is the same as @code{program_invocation_name} minus
1382 everything up to the last slash, if any.)
1383 @end deftypevar
1385 The library initialization code sets up both of these variables before
1386 calling @code{main}.
1388 @strong{Portability Note:} These two variables are GNU extensions.  If
1389 you want your program to work with non-GNU libraries, you must save the
1390 value of @code{argv[0]} in @code{main}, and then strip off the directory
1391 names yourself.  We added these extensions to make it possible to write
1392 self-contained error-reporting subroutines that require no explicit
1393 cooperation from @code{main}.
1395 Here is an example showing how to handle failure to open a file
1396 correctly.  The function @code{open_sesame} tries to open the named file
1397 for reading and returns a stream if successful.  The @code{fopen}
1398 library function returns a null pointer if it couldn't open the file for
1399 some reason.  In that situation, @code{open_sesame} constructs an
1400 appropriate error message using the @code{strerror} function, and
1401 terminates the program.  If we were going to make some other library
1402 calls before passing the error code to @code{strerror}, we'd have to
1403 save it in a local variable instead, because those other library
1404 functions might overwrite @code{errno} in the meantime.
1406 @smallexample
1407 #include <errno.h>
1408 #include <stdio.h>
1409 #include <stdlib.h>
1410 #include <string.h>
1412 FILE *
1413 open_sesame (char *name)
1415   FILE *stream;
1417   errno = 0;
1418   stream = fopen (name, "r");
1419   if (stream == NULL)
1420     @{
1421       fprintf (stderr, "%s: Couldn't open file %s; %s\n",
1422                program_invocation_short_name, name, strerror (errno));
1423       exit (EXIT_FAILURE);
1424     @}
1425   else
1426     return stream;
1428 @end smallexample
1430 Using @code{perror} has the advantage that the function is portable and
1431 available on all systems implementing @w{ISO C}.  But often the text
1432 @code{perror} generates is not what is wanted and there is no way to
1433 extend or change what @code{perror} does.  The GNU coding standard, for
1434 instance, requires error messages to be preceded by the program name and
1435 programs which read some input files should provide information
1436 about the input file name and the line number in case an error is
1437 encountered while reading the file.  For these occasions there are two
1438 functions available which are widely used throughout the GNU project.
1439 These functions are declared in @file{error.h}.
1441 @comment error.h
1442 @comment GNU
1443 @deftypefun void error (int @var{status}, int @var{errnum}, const char *@var{format}, @dots{})
1444 The @code{error} function can be used to report general problems during
1445 program execution.  The @var{format} argument is a format string just
1446 like those given to the @code{printf} family of functions.  The
1447 arguments required for the format can follow the @var{format} parameter.
1448 Just like @code{perror}, @code{error} also can report an error code in
1449 textual form.  But unlike @code{perror} the error value is explicitly
1450 passed to the function in the @var{errnum} parameter.  This eliminates
1451 the problem mentioned above that the error reporting function must be
1452 called immediately after the function causing the error since otherwise
1453 @code{errno} might have a different value.
1455 The @code{error} prints first the program name.  If the application
1456 defined a global variable @code{error_print_progname} and points it to a
1457 function this function will be called to print the program name.
1458 Otherwise the string from the global variable @code{program_name} is
1459 used.  The program name is followed by a colon and a space which in turn
1460 is followed by the output produced by the format string.  If the
1461 @var{errnum} parameter is non-zero the format string output is followed
1462 by a colon and a space, followed by the error message for the error code
1463 @var{errnum}.  In any case is the output terminated with a newline.
1465 The output is directed to the @code{stderr} stream.  If the
1466 @code{stderr} wasn't oriented before the call it will be narrow-oriented
1467 afterwards.
1469 The function will return unless the @var{status} parameter has a
1470 non-zero value.  In this case the function will call @code{exit} with
1471 the @var{status} value for its parameter and therefore never return.  If
1472 @code{error} returns the global variable @code{error_message_count} is
1473 incremented by one to keep track of the number of errors reported.
1474 @end deftypefun
1476 @comment error.h
1477 @comment GNU
1478 @deftypefun void error_at_line (int @var{status}, int @var{errnum}, const char *@var{fname}, unsigned int @var{lineno}, const char *@var{format}, @dots{})
1480 The @code{error_at_line} function is very similar to the @code{error}
1481 function.  The only difference are the additional parameters @var{fname}
1482 and @var{lineno}.  The handling of the other parameters is identical to
1483 that of @code{error} except that between the program name and the string
1484 generated by the format string additional text is inserted.
1486 Directly following the program name a colon, followed by the file name
1487 pointer to by @var{fname}, another colon, and a value of @var{lineno} is
1488 printed.
1490 This additional output of course is meant to be used to locate an error
1491 in an input file (like a programming language source code file etc).
1493 If the global variable @code{error_one_per_line} is set to a non-zero
1494 value @code{error_at_line} will avoid printing consecutive messages for
1495 the same file and line.  Repetition which are not directly following
1496 each other are not caught.
1498 Just like @code{error} this function only returned if @var{status} is
1499 zero.  Otherwise @code{exit} is called with the non-zero value.  If
1500 @code{error} returns the global variable @code{error_message_count} is
1501 incremented by one to keep track of the number of errors reported.
1502 @end deftypefun
1504 As mentioned above the @code{error} and @code{error_at_line} functions
1505 can be customized by defining a variable named
1506 @code{error_print_progname}.
1508 @comment error.h
1509 @comment GNU
1510 @deftypevar {void (*) error_print_progname } (void)
1511 If the @code{error_print_progname} variable is defined to a non-zero
1512 value the function pointed to is called by @code{error} or
1513 @code{error_at_line}.  It is expected to print the program name or do
1514 something similarly useful.
1516 The function is expected to be print to the @code{stderr} stream and
1517 must be able to handle whatever orientation the stream has.
1519 The variable is global and shared by all threads.
1520 @end deftypevar
1522 @comment error.h
1523 @comment GNU
1524 @deftypevar {unsigned int} error_message_count
1525 The @code{error_message_count} variable is incremented whenever one of
1526 the functions @code{error} or @code{error_at_line} returns.  The
1527 variable is global and shared by all threads.
1528 @end deftypevar
1530 @comment error.h
1531 @comment GNU
1532 @deftypevar int error_one_per_line
1533 The @code{error_one_per_line} variable influences only
1534 @code{error_at_line}.  Normally the @code{error_at_line} function
1535 creates output for every invocation.  If @code{error_one_per_line} is
1536 set to a non-zero value @code{error_at_line} keeps track of the last
1537 file name and line number for which an error was reported and avoid
1538 directly following messages for the same file and line.  This variable
1539 is global and shared by all threads.
1540 @end deftypevar
1542 @noindent
1543 A program which read some input file and reports errors in it could look
1544 like this:
1546 @smallexample
1548   char *line = NULL;
1549   size_t len = 0;
1550   unsigned int lineno = 0;
1552   error_message_count = 0;
1553   while (! feof_unlocked (fp))
1554     @{
1555       ssize_t n = getline (&line, &len, fp);
1556       if (n <= 0)
1557         /* @r{End of file or error.}  */
1558         break;
1559       ++lineno;
1561       /* @r{Process the line.}  */
1562       @dots{}
1564       if (@r{Detect error in line})
1565         error_at_line (0, errval, filename, lineno,
1566                        "some error text %s", some_variable);
1567     @}
1569   if (error_message_count != 0)
1570     error (EXIT_FAILURE, 0, "%u errors found", error_message_count);
1572 @end smallexample
1574 @code{error} and @code{error_at_line} are clearly the functions of
1575 choice and enable the programmer to write applications which follow the
1576 GNU coding standard.  @Theglibc{} additionally contains functions which
1577 are used in BSD for the same purpose.  These functions are declared in
1578 @file{err.h}.  It is generally advised to not use these functions.  They
1579 are included only for compatibility.
1581 @comment err.h
1582 @comment BSD
1583 @deftypefun void warn (const char *@var{format}, @dots{})
1584 The @code{warn} function is roughly equivalent to a call like
1585 @smallexample
1586   error (0, errno, format, @r{the parameters})
1587 @end smallexample
1588 @noindent
1589 except that the global variables @code{error} respects and modifies
1590 are not used.
1591 @end deftypefun
1593 @comment err.h
1594 @comment BSD
1595 @deftypefun void vwarn (const char *@var{format}, va_list @var{ap})
1596 The @code{vwarn} function is just like @code{warn} except that the
1597 parameters for the handling of the format string @var{format} are passed
1598 in as an value of type @code{va_list}.
1599 @end deftypefun
1601 @comment err.h
1602 @comment BSD
1603 @deftypefun void warnx (const char *@var{format}, @dots{})
1604 The @code{warnx} function is roughly equivalent to a call like
1605 @smallexample
1606   error (0, 0, format, @r{the parameters})
1607 @end smallexample
1608 @noindent
1609 except that the global variables @code{error} respects and modifies
1610 are not used.  The difference to @code{warn} is that no error number
1611 string is printed.
1612 @end deftypefun
1614 @comment err.h
1615 @comment BSD
1616 @deftypefun void vwarnx (const char *@var{format}, va_list @var{ap})
1617 The @code{vwarnx} function is just like @code{warnx} except that the
1618 parameters for the handling of the format string @var{format} are passed
1619 in as an value of type @code{va_list}.
1620 @end deftypefun
1622 @comment err.h
1623 @comment BSD
1624 @deftypefun void err (int @var{status}, const char *@var{format}, @dots{})
1625 The @code{err} function is roughly equivalent to a call like
1626 @smallexample
1627   error (status, errno, format, @r{the parameters})
1628 @end smallexample
1629 @noindent
1630 except that the global variables @code{error} respects and modifies
1631 are not used and that the program is exited even if @var{status} is zero.
1632 @end deftypefun
1634 @comment err.h
1635 @comment BSD
1636 @deftypefun void verr (int @var{status}, const char *@var{format}, va_list @var{ap})
1637 The @code{verr} function is just like @code{err} except that the
1638 parameters for the handling of the format string @var{format} are passed
1639 in as an value of type @code{va_list}.
1640 @end deftypefun
1642 @comment err.h
1643 @comment BSD
1644 @deftypefun void errx (int @var{status}, const char *@var{format}, @dots{})
1645 The @code{errx} function is roughly equivalent to a call like
1646 @smallexample
1647   error (status, 0, format, @r{the parameters})
1648 @end smallexample
1649 @noindent
1650 except that the global variables @code{error} respects and modifies
1651 are not used and that the program is exited even if @var{status}
1652 is zero.  The difference to @code{err} is that no error number
1653 string is printed.
1654 @end deftypefun
1656 @comment err.h
1657 @comment BSD
1658 @deftypefun void verrx (int @var{status}, const char *@var{format}, va_list @var{ap})
1659 The @code{verrx} function is just like @code{errx} except that the
1660 parameters for the handling of the format string @var{format} are passed
1661 in as an value of type @code{va_list}.
1662 @end deftypefun