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. */
10 #ifndef NEED_sys_errlist
11 /* Note that errno.h (not sure what OS) or stdio.h (BSD 4.4, at least)
12 might declare sys_errlist in a way that the compiler might consider
13 incompatible with our later declaration, perhaps by using const
14 attributes. So we hide the declaration in errno.h (if any) using a
16 #define sys_errlist sys_errlist__
22 #ifndef NEED_sys_errlist
26 /* Routines imported from standard C runtime libraries. */
30 extern void *malloc (size_t size); /* 4.10.3.3 */
31 extern void *memset (void *s, int c, size_t n); /* 4.11.6.1 */
33 extern char *malloc (); /* Standard memory allocater */
34 extern char *memset ();
38 # define MAX(a,b) ((a) > (b) ? (a) : (b))
41 /* Translation table for errno values. See intro(2) in most UNIX systems
42 Programmers Reference Manuals.
44 Note that this table is generally only accessed when it is used at runtime
45 to initialize errno name and message tables that are indexed by errno
48 Not all of these errnos will exist on all systems. This table is the only
49 thing that should have to be updated as new error numbers are introduced.
50 It's sort of ugly, but at least its portable. */
54 int value; /* The numeric value from <errno.h> */
55 const char *name; /* The equivalent symbolic value */
56 #ifdef NEED_sys_errlist
57 const char *msg; /* Short message about this value */
61 #ifdef NEED_sys_errlist
62 # define ENTRY(value, name, msg) {value, name, msg}
64 # define ENTRY(value, name, msg) {value, name}
67 static const struct error_info error_table[] =
70 ENTRY(EPERM, "EPERM", "Not owner"),
73 ENTRY(ENOENT, "ENOENT", "No such file or directory"),
76 ENTRY(ESRCH, "ESRCH", "No such process"),
79 ENTRY(EINTR, "EINTR", "Interrupted system call"),
82 ENTRY(EIO, "EIO", "I/O error"),
85 ENTRY(ENXIO, "ENXIO", "No such device or address"),
88 ENTRY(E2BIG, "E2BIG", "Arg list too long"),
91 ENTRY(ENOEXEC, "ENOEXEC", "Exec format error"),
94 ENTRY(EBADF, "EBADF", "Bad file number"),
97 ENTRY(ECHILD, "ECHILD", "No child processes"),
99 #if defined (EWOULDBLOCK) /* Put before EAGAIN, sometimes aliased */
100 ENTRY(EWOULDBLOCK, "EWOULDBLOCK", "Operation would block"),
103 ENTRY(EAGAIN, "EAGAIN", "No more processes"),
106 ENTRY(ENOMEM, "ENOMEM", "Not enough space"),
109 ENTRY(EACCES, "EACCES", "Permission denied"),
112 ENTRY(EFAULT, "EFAULT", "Bad address"),
114 #if defined (ENOTBLK)
115 ENTRY(ENOTBLK, "ENOTBLK", "Block device required"),
118 ENTRY(EBUSY, "EBUSY", "Device busy"),
121 ENTRY(EEXIST, "EEXIST", "File exists"),
124 ENTRY(EXDEV, "EXDEV", "Cross-device link"),
127 ENTRY(ENODEV, "ENODEV", "No such device"),
129 #if defined (ENOTDIR)
130 ENTRY(ENOTDIR, "ENOTDIR", "Not a directory"),
133 ENTRY(EISDIR, "EISDIR", "Is a directory"),
136 ENTRY(EINVAL, "EINVAL", "Invalid argument"),
139 ENTRY(ENFILE, "ENFILE", "File table overflow"),
142 ENTRY(EMFILE, "EMFILE", "Too many open files"),
145 ENTRY(ENOTTY, "ENOTTY", "Not a typewriter"),
147 #if defined (ETXTBSY)
148 ENTRY(ETXTBSY, "ETXTBSY", "Text file busy"),
151 ENTRY(EFBIG, "EFBIG", "File too large"),
154 ENTRY(ENOSPC, "ENOSPC", "No space left on device"),
157 ENTRY(ESPIPE, "ESPIPE", "Illegal seek"),
160 ENTRY(EROFS, "EROFS", "Read-only file system"),
163 ENTRY(EMLINK, "EMLINK", "Too many links"),
166 ENTRY(EPIPE, "EPIPE", "Broken pipe"),
169 ENTRY(EDOM, "EDOM", "Math argument out of domain of func"),
172 ENTRY(ERANGE, "ERANGE", "Math result not representable"),
175 ENTRY(ENOMSG, "ENOMSG", "No message of desired type"),
178 ENTRY(EIDRM, "EIDRM", "Identifier removed"),
181 ENTRY(ECHRNG, "ECHRNG", "Channel number out of range"),
183 #if defined (EL2NSYNC)
184 ENTRY(EL2NSYNC, "EL2NSYNC", "Level 2 not synchronized"),
187 ENTRY(EL3HLT, "EL3HLT", "Level 3 halted"),
190 ENTRY(EL3RST, "EL3RST", "Level 3 reset"),
193 ENTRY(ELNRNG, "ELNRNG", "Link number out of range"),
195 #if defined (EUNATCH)
196 ENTRY(EUNATCH, "EUNATCH", "Protocol driver not attached"),
199 ENTRY(ENOCSI, "ENOCSI", "No CSI structure available"),
202 ENTRY(EL2HLT, "EL2HLT", "Level 2 halted"),
204 #if defined (EDEADLK)
205 ENTRY(EDEADLK, "EDEADLK", "Deadlock condition"),
208 ENTRY(ENOLCK, "ENOLCK", "No record locks available"),
211 ENTRY(EBADE, "EBADE", "Invalid exchange"),
214 ENTRY(EBADR, "EBADR", "Invalid request descriptor"),
217 ENTRY(EXFULL, "EXFULL", "Exchange full"),
220 ENTRY(ENOANO, "ENOANO", "No anode"),
222 #if defined (EBADRQC)
223 ENTRY(EBADRQC, "EBADRQC", "Invalid request code"),
225 #if defined (EBADSLT)
226 ENTRY(EBADSLT, "EBADSLT", "Invalid slot"),
228 #if defined (EDEADLOCK)
229 ENTRY(EDEADLOCK, "EDEADLOCK", "File locking deadlock error"),
232 ENTRY(EBFONT, "EBFONT", "Bad font file format"),
235 ENTRY(ENOSTR, "ENOSTR", "Device not a stream"),
237 #if defined (ENODATA)
238 ENTRY(ENODATA, "ENODATA", "No data available"),
241 ENTRY(ETIME, "ETIME", "Timer expired"),
244 ENTRY(ENOSR, "ENOSR", "Out of streams resources"),
247 ENTRY(ENONET, "ENONET", "Machine is not on the network"),
250 ENTRY(ENOPKG, "ENOPKG", "Package not installed"),
252 #if defined (EREMOTE)
253 ENTRY(EREMOTE, "EREMOTE", "Object is remote"),
255 #if defined (ENOLINK)
256 ENTRY(ENOLINK, "ENOLINK", "Link has been severed"),
259 ENTRY(EADV, "EADV", "Advertise error"),
262 ENTRY(ESRMNT, "ESRMNT", "Srmount error"),
265 ENTRY(ECOMM, "ECOMM", "Communication error on send"),
268 ENTRY(EPROTO, "EPROTO", "Protocol error"),
270 #if defined (EMULTIHOP)
271 ENTRY(EMULTIHOP, "EMULTIHOP", "Multihop attempted"),
273 #if defined (EDOTDOT)
274 ENTRY(EDOTDOT, "EDOTDOT", "RFS specific error"),
276 #if defined (EBADMSG)
277 ENTRY(EBADMSG, "EBADMSG", "Not a data message"),
279 #if defined (ENAMETOOLONG)
280 ENTRY(ENAMETOOLONG, "ENAMETOOLONG", "File name too long"),
282 #if defined (EOVERFLOW)
283 ENTRY(EOVERFLOW, "EOVERFLOW", "Value too large for defined data type"),
285 #if defined (ENOTUNIQ)
286 ENTRY(ENOTUNIQ, "ENOTUNIQ", "Name not unique on network"),
289 ENTRY(EBADFD, "EBADFD", "File descriptor in bad state"),
291 #if defined (EREMCHG)
292 ENTRY(EREMCHG, "EREMCHG", "Remote address changed"),
294 #if defined (ELIBACC)
295 ENTRY(ELIBACC, "ELIBACC", "Can not access a needed shared library"),
297 #if defined (ELIBBAD)
298 ENTRY(ELIBBAD, "ELIBBAD", "Accessing a corrupted shared library"),
300 #if defined (ELIBSCN)
301 ENTRY(ELIBSCN, "ELIBSCN", ".lib section in a.out corrupted"),
303 #if defined (ELIBMAX)
304 ENTRY(ELIBMAX, "ELIBMAX", "Attempting to link in too many shared libraries"),
306 #if defined (ELIBEXEC)
307 ENTRY(ELIBEXEC, "ELIBEXEC", "Cannot exec a shared library directly"),
310 ENTRY(EILSEQ, "EILSEQ", "Illegal byte sequence"),
313 ENTRY(ENOSYS, "ENOSYS", "Operation not applicable"),
316 ENTRY(ELOOP, "ELOOP", "Too many symbolic links encountered"),
318 #if defined (ERESTART)
319 ENTRY(ERESTART, "ERESTART", "Interrupted system call should be restarted"),
321 #if defined (ESTRPIPE)
322 ENTRY(ESTRPIPE, "ESTRPIPE", "Streams pipe error"),
324 #if defined (ENOTEMPTY)
325 ENTRY(ENOTEMPTY, "ENOTEMPTY", "Directory not empty"),
328 ENTRY(EUSERS, "EUSERS", "Too many users"),
330 #if defined (ENOTSOCK)
331 ENTRY(ENOTSOCK, "ENOTSOCK", "Socket operation on non-socket"),
333 #if defined (EDESTADDRREQ)
334 ENTRY(EDESTADDRREQ, "EDESTADDRREQ", "Destination address required"),
336 #if defined (EMSGSIZE)
337 ENTRY(EMSGSIZE, "EMSGSIZE", "Message too long"),
339 #if defined (EPROTOTYPE)
340 ENTRY(EPROTOTYPE, "EPROTOTYPE", "Protocol wrong type for socket"),
342 #if defined (ENOPROTOOPT)
343 ENTRY(ENOPROTOOPT, "ENOPROTOOPT", "Protocol not available"),
345 #if defined (EPROTONOSUPPORT)
346 ENTRY(EPROTONOSUPPORT, "EPROTONOSUPPORT", "Protocol not supported"),
348 #if defined (ESOCKTNOSUPPORT)
349 ENTRY(ESOCKTNOSUPPORT, "ESOCKTNOSUPPORT", "Socket type not supported"),
351 #if defined (EOPNOTSUPP)
352 ENTRY(EOPNOTSUPP, "EOPNOTSUPP", "Operation not supported on transport endpoint"),
354 #if defined (EPFNOSUPPORT)
355 ENTRY(EPFNOSUPPORT, "EPFNOSUPPORT", "Protocol family not supported"),
357 #if defined (EAFNOSUPPORT)
358 ENTRY(EAFNOSUPPORT, "EAFNOSUPPORT", "Address family not supported by protocol"),
360 #if defined (EADDRINUSE)
361 ENTRY(EADDRINUSE, "EADDRINUSE", "Address already in use"),
363 #if defined (EADDRNOTAVAIL)
364 ENTRY(EADDRNOTAVAIL, "EADDRNOTAVAIL","Cannot assign requested address"),
366 #if defined (ENETDOWN)
367 ENTRY(ENETDOWN, "ENETDOWN", "Network is down"),
369 #if defined (ENETUNREACH)
370 ENTRY(ENETUNREACH, "ENETUNREACH", "Network is unreachable"),
372 #if defined (ENETRESET)
373 ENTRY(ENETRESET, "ENETRESET", "Network dropped connection because of reset"),
375 #if defined (ECONNABORTED)
376 ENTRY(ECONNABORTED, "ECONNABORTED", "Software caused connection abort"),
378 #if defined (ECONNRESET)
379 ENTRY(ECONNRESET, "ECONNRESET", "Connection reset by peer"),
381 #if defined (ENOBUFS)
382 ENTRY(ENOBUFS, "ENOBUFS", "No buffer space available"),
384 #if defined (EISCONN)
385 ENTRY(EISCONN, "EISCONN", "Transport endpoint is already connected"),
387 #if defined (ENOTCONN)
388 ENTRY(ENOTCONN, "ENOTCONN", "Transport endpoint is not connected"),
390 #if defined (ESHUTDOWN)
391 ENTRY(ESHUTDOWN, "ESHUTDOWN", "Cannot send after transport endpoint shutdown"),
393 #if defined (ETOOMANYREFS)
394 ENTRY(ETOOMANYREFS, "ETOOMANYREFS", "Too many references: cannot splice"),
396 #if defined (ETIMEDOUT)
397 ENTRY(ETIMEDOUT, "ETIMEDOUT", "Connection timed out"),
399 #if defined (ECONNREFUSED)
400 ENTRY(ECONNREFUSED, "ECONNREFUSED", "Connection refused"),
402 #if defined (EHOSTDOWN)
403 ENTRY(EHOSTDOWN, "EHOSTDOWN", "Host is down"),
405 #if defined (EHOSTUNREACH)
406 ENTRY(EHOSTUNREACH, "EHOSTUNREACH", "No route to host"),
408 #if defined (EALREADY)
409 ENTRY(EALREADY, "EALREADY", "Operation already in progress"),
411 #if defined (EINPROGRESS)
412 ENTRY(EINPROGRESS, "EINPROGRESS", "Operation now in progress"),
415 ENTRY(ESTALE, "ESTALE", "Stale NFS file handle"),
417 #if defined (EUCLEAN)
418 ENTRY(EUCLEAN, "EUCLEAN", "Structure needs cleaning"),
420 #if defined (ENOTNAM)
421 ENTRY(ENOTNAM, "ENOTNAM", "Not a XENIX named type file"),
423 #if defined (ENAVAIL)
424 ENTRY(ENAVAIL, "ENAVAIL", "No XENIX semaphores available"),
427 ENTRY(EISNAM, "EISNAM", "Is a named type file"),
429 #if defined (EREMOTEIO)
430 ENTRY(EREMOTEIO, "EREMOTEIO", "Remote I/O error"),
436 /* This is not in the table, because the numeric value of EVMSERR (32767)
437 lies outside the range of sys_errlist[]. */
438 static struct { int value; const char *name, *msg; }
439 evmserr = { EVMSERR, "EVMSERR", "VMS-specific error" };
442 /* Translation table allocated and initialized at runtime. Indexed by the
443 errno value to find the equivalent symbolic value. */
445 static const char **error_names;
446 static int num_error_names = 0;
448 /* Translation table allocated and initialized at runtime, if it does not
449 already exist in the host environment. Indexed by the errno value to find
450 the descriptive string.
452 We don't export it for use in other modules because even though it has the
453 same name, it differs from other implementations in that it is dynamically
454 initialized rather than statically initialized. */
456 #ifdef NEED_sys_errlist
459 static const char **sys_errlist;
464 extern char *sys_errlist[];
473 init_error_tables -- initialize the name and message tables
477 static void init_error_tables ();
481 Using the error_table, which is initialized at compile time, generate
482 the error_names and the sys_errlist (if needed) tables, which are
483 indexed at runtime by a specific errno value.
487 The initialization of the tables may fail under low memory conditions,
488 in which case we don't do anything particularly useful, but we don't
489 bomb either. Who knows, it might succeed at a later point if we free
490 some memory in the meantime. In any case, the other routines know
491 how to deal with lack of a table after trying to initialize it. This
492 may or may not be considered to be a bug, that we don't specifically
493 warn about this particular failure mode.
500 const struct error_info *eip;
503 /* If we haven't already scanned the error_table once to find the maximum
504 errno value, then go find it now. */
506 if (num_error_names == 0)
508 for (eip = error_table; eip -> name != NULL; eip++)
510 if (eip -> value >= num_error_names)
512 num_error_names = eip -> value + 1;
517 /* Now attempt to allocate the error_names table, zero it out, and then
518 initialize it from the statically initialized error_table. */
520 if (error_names == NULL)
522 nbytes = num_error_names * sizeof (char *);
523 if ((error_names = (const char **) malloc (nbytes)) != NULL)
525 memset (error_names, 0, nbytes);
526 for (eip = error_table; eip -> name != NULL; eip++)
528 error_names[eip -> value] = eip -> name;
533 #ifdef NEED_sys_errlist
535 /* Now attempt to allocate the sys_errlist table, zero it out, and then
536 initialize it from the statically initialized error_table. */
538 if (sys_errlist == NULL)
540 nbytes = num_error_names * sizeof (char *);
541 if ((sys_errlist = (const char **) malloc (nbytes)) != NULL)
543 memset (sys_errlist, 0, nbytes);
544 sys_nerr = num_error_names;
545 for (eip = error_table; eip -> name != NULL; eip++)
547 sys_errlist[eip -> value] = eip -> msg;
560 errno_max -- return the max errno value
568 Returns the maximum errno value for which a corresponding symbolic
569 name or message is available. Note that in the case where
570 we use the sys_errlist supplied by the system, it is possible for
571 there to be more symbolic names than messages, or vice versa.
572 In fact, the manual page for perror(3C) explicitly warns that one
573 should check the size of the table (sys_nerr) before indexing it,
574 since new error codes may be added to the system before they are
575 added to the table. Thus sys_nerr might be smaller than value
576 implied by the largest errno value defined in <errno.h>.
578 We return the maximum value that can be used to obtain a meaningful
579 symbolic name or message.
588 if (error_names == NULL)
590 init_error_tables ();
592 maxsize = MAX (sys_nerr, num_error_names);
593 return (maxsize - 1);
602 strerror -- map an error number to an error message string
606 char *strerror (int errnoval)
610 Maps an errno number to an error message string, the contents of
611 which are implementation defined. On systems which have the external
612 variables sys_nerr and sys_errlist, these strings will be the same
613 as the ones used by perror().
615 If the supplied error number is within the valid range of indices
616 for the sys_errlist, but no message is available for the particular
617 error number, then returns the string "Error NUM", where NUM is the
620 If the supplied error number is not a valid index into sys_errlist,
623 The returned string is only guaranteed to be valid only until the
624 next call to strerror.
635 #ifdef NEED_sys_errlist
637 if (error_names == NULL)
639 init_error_tables ();
644 if ((errnoval < 0) || (errnoval >= sys_nerr))
647 if (errnoval == evmserr.value)
651 /* Out of range, just return NULL */
654 else if ((sys_errlist == NULL) || (sys_errlist[errnoval] == NULL))
656 /* In range, but no sys_errlist or no entry at this index. */
657 sprintf (buf, "Error %d", errnoval);
662 /* In range, and a valid message. Just return the message. */
663 msg = (char *) sys_errlist[errnoval];
669 #endif /* NEED_strerror */
676 strerrno -- map an error number to a symbolic name string
680 const char *strerrno (int errnoval)
684 Given an error number returned from a system call (typically
685 returned in errno), returns a pointer to a string containing the
686 symbolic name of that error number, as found in <errno.h>.
688 If the supplied error number is within the valid range of indices
689 for symbolic names, but no name is available for the particular
690 error number, then returns the string "Error NUM", where NUM is
693 If the supplied error number is not within the range of valid
694 indices, then returns NULL.
698 The contents of the location pointed to are only guaranteed to be
699 valid until the next call to strerrno.
710 if (error_names == NULL)
712 init_error_tables ();
715 if ((errnoval < 0) || (errnoval >= num_error_names))
718 if (errnoval == evmserr.value)
722 /* Out of range, just return NULL */
725 else if ((error_names == NULL) || (error_names[errnoval] == NULL))
727 /* In range, but no error_names or no entry at this index. */
728 sprintf (buf, "Error %d", errnoval);
729 name = (const char *) buf;
733 /* In range, and a valid name. Just return the name. */
734 name = error_names[errnoval];
744 strtoerrno -- map a symbolic errno name to a numeric value
748 int strtoerrno (char *name)
752 Given the symbolic name of a error number, map it to an errno value.
753 If no translation is found, returns 0.
765 if (error_names == NULL)
767 init_error_tables ();
769 for (errnoval = 0; errnoval < num_error_names; errnoval++)
771 if ((error_names[errnoval] != NULL) &&
772 (strcmp (name, error_names[errnoval]) == 0))
777 if (errnoval == num_error_names)
780 if (strcmp (name, evmserr.name) == 0)
781 errnoval = evmserr.value;
791 /* A simple little main that does nothing but print all the errno translations
792 if MAIN is defined and this file is compiled and linked. */
807 errnmax = errno_max ();
808 printf ("%d entries in names table.\n", num_error_names);
809 printf ("%d entries in messages table.\n", sys_nerr);
810 printf ("%d is max useful index.\n", errnmax);
812 /* Keep printing values until we get to the end of *both* tables, not
813 *either* table. Note that knowing the maximum useful index does *not*
814 relieve us of the responsibility of testing the return pointer for
817 for (errn = 0; errn <= errnmax; errn++)
819 name = strerrno (errn);
820 name = (name == NULL) ? "<NULL>" : name;
821 msg = strerror (errn);
822 msg = (msg == NULL) ? "<NULL>" : msg;
823 printf ("%-4d%-18s%s\n", errn, name, msg);