1 /* Extended support for using errno values.
2 Written by Fred Fish. fnf@cygnus.com
3 This file is in the public domain. --Per Bothner. */
11 #ifdef HAVE_SYS_ERRLIST
12 /* Note that errno.h (not sure what OS) or stdio.h (BSD 4.4, at least)
13 might declare sys_errlist in a way that the compiler might consider
14 incompatible with our later declaration, perhaps by using const
15 attributes. So we hide the declaration in errno.h (if any) using a
17 #define sys_nerr sys_nerr__
18 #define sys_errlist sys_errlist__
26 #ifdef HAVE_SYS_ERRLIST
32 /* Routines imported from standard C runtime libraries. */
47 # define MAX(a,b) ((a) > (b) ? (a) : (b))
50 static void init_error_tables
PARAMS ((void));
52 /* Translation table for errno values. See intro(2) in most UNIX systems
53 Programmers Reference Manuals.
55 Note that this table is generally only accessed when it is used at runtime
56 to initialize errno name and message tables that are indexed by errno
59 Not all of these errnos will exist on all systems. This table is the only
60 thing that should have to be updated as new error numbers are introduced.
61 It's sort of ugly, but at least its portable. */
65 const int value
; /* The numeric value from <errno.h> */
66 const char *const name
; /* The equivalent symbolic value */
67 #ifndef HAVE_SYS_ERRLIST
68 const char *const msg
; /* Short message about this value */
72 #ifndef HAVE_SYS_ERRLIST
73 # define ENTRY(value, name, msg) {value, name, msg}
75 # define ENTRY(value, name, msg) {value, name}
78 static const struct error_info error_table
[] =
81 ENTRY(EPERM
, "EPERM", "Not owner"),
84 ENTRY(ENOENT
, "ENOENT", "No such file or directory"),
87 ENTRY(ESRCH
, "ESRCH", "No such process"),
90 ENTRY(EINTR
, "EINTR", "Interrupted system call"),
93 ENTRY(EIO
, "EIO", "I/O error"),
96 ENTRY(ENXIO
, "ENXIO", "No such device or address"),
99 ENTRY(E2BIG
, "E2BIG", "Arg list too long"),
101 #if defined (ENOEXEC)
102 ENTRY(ENOEXEC
, "ENOEXEC", "Exec format error"),
105 ENTRY(EBADF
, "EBADF", "Bad file number"),
108 ENTRY(ECHILD
, "ECHILD", "No child processes"),
110 #if defined (EWOULDBLOCK) /* Put before EAGAIN, sometimes aliased */
111 ENTRY(EWOULDBLOCK
, "EWOULDBLOCK", "Operation would block"),
114 ENTRY(EAGAIN
, "EAGAIN", "No more processes"),
117 ENTRY(ENOMEM
, "ENOMEM", "Not enough space"),
120 ENTRY(EACCES
, "EACCES", "Permission denied"),
123 ENTRY(EFAULT
, "EFAULT", "Bad address"),
125 #if defined (ENOTBLK)
126 ENTRY(ENOTBLK
, "ENOTBLK", "Block device required"),
129 ENTRY(EBUSY
, "EBUSY", "Device busy"),
132 ENTRY(EEXIST
, "EEXIST", "File exists"),
135 ENTRY(EXDEV
, "EXDEV", "Cross-device link"),
138 ENTRY(ENODEV
, "ENODEV", "No such device"),
140 #if defined (ENOTDIR)
141 ENTRY(ENOTDIR
, "ENOTDIR", "Not a directory"),
144 ENTRY(EISDIR
, "EISDIR", "Is a directory"),
147 ENTRY(EINVAL
, "EINVAL", "Invalid argument"),
150 ENTRY(ENFILE
, "ENFILE", "File table overflow"),
153 ENTRY(EMFILE
, "EMFILE", "Too many open files"),
156 ENTRY(ENOTTY
, "ENOTTY", "Not a typewriter"),
158 #if defined (ETXTBSY)
159 ENTRY(ETXTBSY
, "ETXTBSY", "Text file busy"),
162 ENTRY(EFBIG
, "EFBIG", "File too large"),
165 ENTRY(ENOSPC
, "ENOSPC", "No space left on device"),
168 ENTRY(ESPIPE
, "ESPIPE", "Illegal seek"),
171 ENTRY(EROFS
, "EROFS", "Read-only file system"),
174 ENTRY(EMLINK
, "EMLINK", "Too many links"),
177 ENTRY(EPIPE
, "EPIPE", "Broken pipe"),
180 ENTRY(EDOM
, "EDOM", "Math argument out of domain of func"),
183 ENTRY(ERANGE
, "ERANGE", "Math result not representable"),
186 ENTRY(ENOMSG
, "ENOMSG", "No message of desired type"),
189 ENTRY(EIDRM
, "EIDRM", "Identifier removed"),
192 ENTRY(ECHRNG
, "ECHRNG", "Channel number out of range"),
194 #if defined (EL2NSYNC)
195 ENTRY(EL2NSYNC
, "EL2NSYNC", "Level 2 not synchronized"),
198 ENTRY(EL3HLT
, "EL3HLT", "Level 3 halted"),
201 ENTRY(EL3RST
, "EL3RST", "Level 3 reset"),
204 ENTRY(ELNRNG
, "ELNRNG", "Link number out of range"),
206 #if defined (EUNATCH)
207 ENTRY(EUNATCH
, "EUNATCH", "Protocol driver not attached"),
210 ENTRY(ENOCSI
, "ENOCSI", "No CSI structure available"),
213 ENTRY(EL2HLT
, "EL2HLT", "Level 2 halted"),
215 #if defined (EDEADLK)
216 ENTRY(EDEADLK
, "EDEADLK", "Deadlock condition"),
219 ENTRY(ENOLCK
, "ENOLCK", "No record locks available"),
222 ENTRY(EBADE
, "EBADE", "Invalid exchange"),
225 ENTRY(EBADR
, "EBADR", "Invalid request descriptor"),
228 ENTRY(EXFULL
, "EXFULL", "Exchange full"),
231 ENTRY(ENOANO
, "ENOANO", "No anode"),
233 #if defined (EBADRQC)
234 ENTRY(EBADRQC
, "EBADRQC", "Invalid request code"),
236 #if defined (EBADSLT)
237 ENTRY(EBADSLT
, "EBADSLT", "Invalid slot"),
239 #if defined (EDEADLOCK)
240 ENTRY(EDEADLOCK
, "EDEADLOCK", "File locking deadlock error"),
243 ENTRY(EBFONT
, "EBFONT", "Bad font file format"),
246 ENTRY(ENOSTR
, "ENOSTR", "Device not a stream"),
248 #if defined (ENODATA)
249 ENTRY(ENODATA
, "ENODATA", "No data available"),
252 ENTRY(ETIME
, "ETIME", "Timer expired"),
255 ENTRY(ENOSR
, "ENOSR", "Out of streams resources"),
258 ENTRY(ENONET
, "ENONET", "Machine is not on the network"),
261 ENTRY(ENOPKG
, "ENOPKG", "Package not installed"),
263 #if defined (EREMOTE)
264 ENTRY(EREMOTE
, "EREMOTE", "Object is remote"),
266 #if defined (ENOLINK)
267 ENTRY(ENOLINK
, "ENOLINK", "Link has been severed"),
270 ENTRY(EADV
, "EADV", "Advertise error"),
273 ENTRY(ESRMNT
, "ESRMNT", "Srmount error"),
276 ENTRY(ECOMM
, "ECOMM", "Communication error on send"),
279 ENTRY(EPROTO
, "EPROTO", "Protocol error"),
281 #if defined (EMULTIHOP)
282 ENTRY(EMULTIHOP
, "EMULTIHOP", "Multihop attempted"),
284 #if defined (EDOTDOT)
285 ENTRY(EDOTDOT
, "EDOTDOT", "RFS specific error"),
287 #if defined (EBADMSG)
288 ENTRY(EBADMSG
, "EBADMSG", "Not a data message"),
290 #if defined (ENAMETOOLONG)
291 ENTRY(ENAMETOOLONG
, "ENAMETOOLONG", "File name too long"),
293 #if defined (EOVERFLOW)
294 ENTRY(EOVERFLOW
, "EOVERFLOW", "Value too large for defined data type"),
296 #if defined (ENOTUNIQ)
297 ENTRY(ENOTUNIQ
, "ENOTUNIQ", "Name not unique on network"),
300 ENTRY(EBADFD
, "EBADFD", "File descriptor in bad state"),
302 #if defined (EREMCHG)
303 ENTRY(EREMCHG
, "EREMCHG", "Remote address changed"),
305 #if defined (ELIBACC)
306 ENTRY(ELIBACC
, "ELIBACC", "Can not access a needed shared library"),
308 #if defined (ELIBBAD)
309 ENTRY(ELIBBAD
, "ELIBBAD", "Accessing a corrupted shared library"),
311 #if defined (ELIBSCN)
312 ENTRY(ELIBSCN
, "ELIBSCN", ".lib section in a.out corrupted"),
314 #if defined (ELIBMAX)
315 ENTRY(ELIBMAX
, "ELIBMAX", "Attempting to link in too many shared libraries"),
317 #if defined (ELIBEXEC)
318 ENTRY(ELIBEXEC
, "ELIBEXEC", "Cannot exec a shared library directly"),
321 ENTRY(EILSEQ
, "EILSEQ", "Illegal byte sequence"),
324 ENTRY(ENOSYS
, "ENOSYS", "Operation not applicable"),
327 ENTRY(ELOOP
, "ELOOP", "Too many symbolic links encountered"),
329 #if defined (ERESTART)
330 ENTRY(ERESTART
, "ERESTART", "Interrupted system call should be restarted"),
332 #if defined (ESTRPIPE)
333 ENTRY(ESTRPIPE
, "ESTRPIPE", "Streams pipe error"),
335 #if defined (ENOTEMPTY)
336 ENTRY(ENOTEMPTY
, "ENOTEMPTY", "Directory not empty"),
339 ENTRY(EUSERS
, "EUSERS", "Too many users"),
341 #if defined (ENOTSOCK)
342 ENTRY(ENOTSOCK
, "ENOTSOCK", "Socket operation on non-socket"),
344 #if defined (EDESTADDRREQ)
345 ENTRY(EDESTADDRREQ
, "EDESTADDRREQ", "Destination address required"),
347 #if defined (EMSGSIZE)
348 ENTRY(EMSGSIZE
, "EMSGSIZE", "Message too long"),
350 #if defined (EPROTOTYPE)
351 ENTRY(EPROTOTYPE
, "EPROTOTYPE", "Protocol wrong type for socket"),
353 #if defined (ENOPROTOOPT)
354 ENTRY(ENOPROTOOPT
, "ENOPROTOOPT", "Protocol not available"),
356 #if defined (EPROTONOSUPPORT)
357 ENTRY(EPROTONOSUPPORT
, "EPROTONOSUPPORT", "Protocol not supported"),
359 #if defined (ESOCKTNOSUPPORT)
360 ENTRY(ESOCKTNOSUPPORT
, "ESOCKTNOSUPPORT", "Socket type not supported"),
362 #if defined (EOPNOTSUPP)
363 ENTRY(EOPNOTSUPP
, "EOPNOTSUPP", "Operation not supported on transport endpoint"),
365 #if defined (EPFNOSUPPORT)
366 ENTRY(EPFNOSUPPORT
, "EPFNOSUPPORT", "Protocol family not supported"),
368 #if defined (EAFNOSUPPORT)
369 ENTRY(EAFNOSUPPORT
, "EAFNOSUPPORT", "Address family not supported by protocol"),
371 #if defined (EADDRINUSE)
372 ENTRY(EADDRINUSE
, "EADDRINUSE", "Address already in use"),
374 #if defined (EADDRNOTAVAIL)
375 ENTRY(EADDRNOTAVAIL
, "EADDRNOTAVAIL","Cannot assign requested address"),
377 #if defined (ENETDOWN)
378 ENTRY(ENETDOWN
, "ENETDOWN", "Network is down"),
380 #if defined (ENETUNREACH)
381 ENTRY(ENETUNREACH
, "ENETUNREACH", "Network is unreachable"),
383 #if defined (ENETRESET)
384 ENTRY(ENETRESET
, "ENETRESET", "Network dropped connection because of reset"),
386 #if defined (ECONNABORTED)
387 ENTRY(ECONNABORTED
, "ECONNABORTED", "Software caused connection abort"),
389 #if defined (ECONNRESET)
390 ENTRY(ECONNRESET
, "ECONNRESET", "Connection reset by peer"),
392 #if defined (ENOBUFS)
393 ENTRY(ENOBUFS
, "ENOBUFS", "No buffer space available"),
395 #if defined (EISCONN)
396 ENTRY(EISCONN
, "EISCONN", "Transport endpoint is already connected"),
398 #if defined (ENOTCONN)
399 ENTRY(ENOTCONN
, "ENOTCONN", "Transport endpoint is not connected"),
401 #if defined (ESHUTDOWN)
402 ENTRY(ESHUTDOWN
, "ESHUTDOWN", "Cannot send after transport endpoint shutdown"),
404 #if defined (ETOOMANYREFS)
405 ENTRY(ETOOMANYREFS
, "ETOOMANYREFS", "Too many references: cannot splice"),
407 #if defined (ETIMEDOUT)
408 ENTRY(ETIMEDOUT
, "ETIMEDOUT", "Connection timed out"),
410 #if defined (ECONNREFUSED)
411 ENTRY(ECONNREFUSED
, "ECONNREFUSED", "Connection refused"),
413 #if defined (EHOSTDOWN)
414 ENTRY(EHOSTDOWN
, "EHOSTDOWN", "Host is down"),
416 #if defined (EHOSTUNREACH)
417 ENTRY(EHOSTUNREACH
, "EHOSTUNREACH", "No route to host"),
419 #if defined (EALREADY)
420 ENTRY(EALREADY
, "EALREADY", "Operation already in progress"),
422 #if defined (EINPROGRESS)
423 ENTRY(EINPROGRESS
, "EINPROGRESS", "Operation now in progress"),
426 ENTRY(ESTALE
, "ESTALE", "Stale NFS file handle"),
428 #if defined (EUCLEAN)
429 ENTRY(EUCLEAN
, "EUCLEAN", "Structure needs cleaning"),
431 #if defined (ENOTNAM)
432 ENTRY(ENOTNAM
, "ENOTNAM", "Not a XENIX named type file"),
434 #if defined (ENAVAIL)
435 ENTRY(ENAVAIL
, "ENAVAIL", "No XENIX semaphores available"),
438 ENTRY(EISNAM
, "EISNAM", "Is a named type file"),
440 #if defined (EREMOTEIO)
441 ENTRY(EREMOTEIO
, "EREMOTEIO", "Remote I/O error"),
447 /* This is not in the table, because the numeric value of EVMSERR (32767)
448 lies outside the range of sys_errlist[]. */
449 static struct { int value
; const char *name
, *msg
; }
450 evmserr
= { EVMSERR
, "EVMSERR", "VMS-specific error" };
453 /* Translation table allocated and initialized at runtime. Indexed by the
454 errno value to find the equivalent symbolic value. */
456 static const char **error_names
;
457 static int num_error_names
= 0;
459 /* Translation table allocated and initialized at runtime, if it does not
460 already exist in the host environment. Indexed by the errno value to find
461 the descriptive string.
463 We don't export it for use in other modules because even though it has the
464 same name, it differs from other implementations in that it is dynamically
465 initialized rather than statically initialized. */
467 #ifndef HAVE_STRERROR
468 #ifndef HAVE_SYS_ERRLIST
471 static const char **sys_errlist
;
476 extern char *sys_errlist
[];
485 init_error_tables -- initialize the name and message tables
489 static void init_error_tables ();
493 Using the error_table, which is initialized at compile time, generate
494 the error_names and the sys_errlist (if needed) tables, which are
495 indexed at runtime by a specific errno value.
499 The initialization of the tables may fail under low memory conditions,
500 in which case we don't do anything particularly useful, but we don't
501 bomb either. Who knows, it might succeed at a later point if we free
502 some memory in the meantime. In any case, the other routines know
503 how to deal with lack of a table after trying to initialize it. This
504 may or may not be considered to be a bug, that we don't specifically
505 warn about this particular failure mode.
512 const struct error_info
*eip
;
515 /* If we haven't already scanned the error_table once to find the maximum
516 errno value, then go find it now. */
518 if (num_error_names
== 0)
520 for (eip
= error_table
; eip
-> name
!= NULL
; eip
++)
522 if (eip
-> value
>= num_error_names
)
524 num_error_names
= eip
-> value
+ 1;
529 /* Now attempt to allocate the error_names table, zero it out, and then
530 initialize it from the statically initialized error_table. */
532 if (error_names
== NULL
)
534 nbytes
= num_error_names
* sizeof (char *);
535 if ((error_names
= (const char **) malloc (nbytes
)) != NULL
)
537 memset (error_names
, 0, nbytes
);
538 for (eip
= error_table
; eip
-> name
!= NULL
; eip
++)
540 error_names
[eip
-> value
] = eip
-> name
;
545 #ifndef HAVE_STRERROR
546 #ifndef HAVE_SYS_ERRLIST
548 /* Now attempt to allocate the sys_errlist table, zero it out, and then
549 initialize it from the statically initialized error_table. */
551 if (sys_errlist
== NULL
)
553 nbytes
= num_error_names
* sizeof (char *);
554 if ((sys_errlist
= (const char **) malloc (nbytes
)) != NULL
)
556 memset (sys_errlist
, 0, nbytes
);
557 sys_nerr
= num_error_names
;
558 for (eip
= error_table
; eip
-> name
!= NULL
; eip
++)
560 sys_errlist
[eip
-> value
] = eip
-> msg
;
573 @deftypefn Extension int errno_max (void)
575 Returns the maximum @code{errno} value for which a corresponding
576 symbolic name or message is available. Note that in the case where we
577 use the @code{sys_errlist} supplied by the system, it is possible for
578 there to be more symbolic names than messages, or vice versa. In
579 fact, the manual page for @code{perror(3C)} explicitly warns that one
580 should check the size of the table (@code{sys_nerr}) before indexing
581 it, since new error codes may be added to the system before they are
582 added to the table. Thus @code{sys_nerr} might be smaller than value
583 implied by the largest @code{errno} value defined in @code{<errno.h>}.
585 We return the maximum value that can be used to obtain a meaningful
586 symbolic name or message.
597 if (error_names
== NULL
)
599 init_error_tables ();
601 maxsize
= MAX (sys_nerr
, num_error_names
);
602 return (maxsize
- 1);
605 #ifndef HAVE_STRERROR
609 @deftypefn Supplemental char* strerror (int @var{errnoval})
611 Maps an @code{errno} number to an error message string, the contents
612 of which are implementation defined. On systems which have the
613 external variables @code{sys_nerr} and @code{sys_errlist}, these
614 strings will be the same as the ones used by @code{perror}.
616 If the supplied error number is within the valid range of indices for
617 the @code{sys_errlist}, but no message is available for the particular
618 error number, then returns the string @samp{Error @var{num}}, where
619 @var{num} is the error number.
621 If the supplied error number is not a valid index into
622 @code{sys_errlist}, returns @code{NULL}.
624 The returned string is only guaranteed to be valid only until the
625 next call to @code{strerror}.
638 #ifndef HAVE_SYS_ERRLIST
640 if (error_names
== NULL
)
642 init_error_tables ();
647 if ((errnoval
< 0) || (errnoval
>= sys_nerr
))
650 if (errnoval
== evmserr
.value
)
654 /* Out of range, just return NULL */
657 else if ((sys_errlist
== NULL
) || (sys_errlist
[errnoval
] == NULL
))
659 /* In range, but no sys_errlist or no entry at this index. */
660 sprintf (buf
, "Error %d", errnoval
);
665 /* In range, and a valid message. Just return the message. */
666 msg
= (char *) sys_errlist
[errnoval
];
672 #endif /* ! HAVE_STRERROR */
677 @deftypefn Replacement {const char*} strerrno (int @var{errnum})
679 Given an error number returned from a system call (typically returned
680 in @code{errno}), returns a pointer to a string containing the
681 symbolic name of that error number, as found in @code{<errno.h>}.
683 If the supplied error number is within the valid range of indices for
684 symbolic names, but no name is available for the particular error
685 number, then returns the string @samp{Error @var{num}}, where @var{num}
688 If the supplied error number is not within the range of valid
689 indices, then returns @code{NULL}.
691 The contents of the location pointed to are only guaranteed to be
692 valid until the next call to @code{strerrno}.
705 if (error_names
== NULL
)
707 init_error_tables ();
710 if ((errnoval
< 0) || (errnoval
>= num_error_names
))
713 if (errnoval
== evmserr
.value
)
717 /* Out of range, just return NULL */
720 else if ((error_names
== NULL
) || (error_names
[errnoval
] == NULL
))
722 /* In range, but no error_names or no entry at this index. */
723 sprintf (buf
, "Error %d", errnoval
);
724 name
= (const char *) buf
;
728 /* In range, and a valid name. Just return the name. */
729 name
= error_names
[errnoval
];
737 @deftypefn Extension int strtoerrno (const char *@var{name})
739 Given the symbolic name of a error number (e.g., @code{EACCES}), map it
740 to an errno value. If no translation is found, returns 0.
754 if (error_names
== NULL
)
756 init_error_tables ();
758 for (errnoval
= 0; errnoval
< num_error_names
; errnoval
++)
760 if ((error_names
[errnoval
] != NULL
) &&
761 (strcmp (name
, error_names
[errnoval
]) == 0))
766 if (errnoval
== num_error_names
)
769 if (strcmp (name
, evmserr
.name
) == 0)
770 errnoval
= evmserr
.value
;
780 /* A simple little main that does nothing but print all the errno translations
781 if MAIN is defined and this file is compiled and linked. */
796 errnmax
= errno_max ();
797 printf ("%d entries in names table.\n", num_error_names
);
798 printf ("%d entries in messages table.\n", sys_nerr
);
799 printf ("%d is max useful index.\n", errnmax
);
801 /* Keep printing values until we get to the end of *both* tables, not
802 *either* table. Note that knowing the maximum useful index does *not*
803 relieve us of the responsibility of testing the return pointer for
806 for (errn
= 0; errn
<= errnmax
; errn
++)
808 name
= strerrno (errn
);
809 name
= (name
== NULL
) ? "<NULL>" : name
;
810 msg
= strerror (errn
);
811 msg
= (msg
== NULL
) ? "<NULL>" : msg
;
812 printf ("%-4d%-18s%s\n", errn
, name
, msg
);