]>
gcc.gnu.org Git - gcc.git/blob - libiberty/strerror.c
644cc75462aee8bcf7d07a6254955060ff0d839b
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 #ifdef HAVE_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 #ifdef HAVE_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 static void init_error_tables
PARAMS ((void));
43 /* Translation table for errno values. See intro(2) in most UNIX systems
44 Programmers Reference Manuals.
46 Note that this table is generally only accessed when it is used at runtime
47 to initialize errno name and message tables that are indexed by errno
50 Not all of these errnos will exist on all systems. This table is the only
51 thing that should have to be updated as new error numbers are introduced.
52 It's sort of ugly, but at least its portable. */
56 int value
; /* The numeric value from <errno.h> */
57 const char *name
; /* The equivalent symbolic value */
58 #ifndef HAVE_SYS_ERRLIST
59 const char *msg
; /* Short message about this value */
63 #ifndef HAVE_SYS_ERRLIST
64 # define ENTRY(value, name, msg) {value, name, msg}
66 # define ENTRY(value, name, msg) {value, name}
69 static const struct error_info error_table
[] =
72 ENTRY(EPERM
, "EPERM", "Not owner"),
75 ENTRY(ENOENT
, "ENOENT", "No such file or directory"),
78 ENTRY(ESRCH
, "ESRCH", "No such process"),
81 ENTRY(EINTR
, "EINTR", "Interrupted system call"),
84 ENTRY(EIO
, "EIO", "I/O error"),
87 ENTRY(ENXIO
, "ENXIO", "No such device or address"),
90 ENTRY(E2BIG
, "E2BIG", "Arg list too long"),
93 ENTRY(ENOEXEC
, "ENOEXEC", "Exec format error"),
96 ENTRY(EBADF
, "EBADF", "Bad file number"),
99 ENTRY(ECHILD
, "ECHILD", "No child processes"),
101 #if defined (EWOULDBLOCK) /* Put before EAGAIN, sometimes aliased */
102 ENTRY(EWOULDBLOCK
, "EWOULDBLOCK", "Operation would block"),
105 ENTRY(EAGAIN
, "EAGAIN", "No more processes"),
108 ENTRY(ENOMEM
, "ENOMEM", "Not enough space"),
111 ENTRY(EACCES
, "EACCES", "Permission denied"),
114 ENTRY(EFAULT
, "EFAULT", "Bad address"),
116 #if defined (ENOTBLK)
117 ENTRY(ENOTBLK
, "ENOTBLK", "Block device required"),
120 ENTRY(EBUSY
, "EBUSY", "Device busy"),
123 ENTRY(EEXIST
, "EEXIST", "File exists"),
126 ENTRY(EXDEV
, "EXDEV", "Cross-device link"),
129 ENTRY(ENODEV
, "ENODEV", "No such device"),
131 #if defined (ENOTDIR)
132 ENTRY(ENOTDIR
, "ENOTDIR", "Not a directory"),
135 ENTRY(EISDIR
, "EISDIR", "Is a directory"),
138 ENTRY(EINVAL
, "EINVAL", "Invalid argument"),
141 ENTRY(ENFILE
, "ENFILE", "File table overflow"),
144 ENTRY(EMFILE
, "EMFILE", "Too many open files"),
147 ENTRY(ENOTTY
, "ENOTTY", "Not a typewriter"),
149 #if defined (ETXTBSY)
150 ENTRY(ETXTBSY
, "ETXTBSY", "Text file busy"),
153 ENTRY(EFBIG
, "EFBIG", "File too large"),
156 ENTRY(ENOSPC
, "ENOSPC", "No space left on device"),
159 ENTRY(ESPIPE
, "ESPIPE", "Illegal seek"),
162 ENTRY(EROFS
, "EROFS", "Read-only file system"),
165 ENTRY(EMLINK
, "EMLINK", "Too many links"),
168 ENTRY(EPIPE
, "EPIPE", "Broken pipe"),
171 ENTRY(EDOM
, "EDOM", "Math argument out of domain of func"),
174 ENTRY(ERANGE
, "ERANGE", "Math result not representable"),
177 ENTRY(ENOMSG
, "ENOMSG", "No message of desired type"),
180 ENTRY(EIDRM
, "EIDRM", "Identifier removed"),
183 ENTRY(ECHRNG
, "ECHRNG", "Channel number out of range"),
185 #if defined (EL2NSYNC)
186 ENTRY(EL2NSYNC
, "EL2NSYNC", "Level 2 not synchronized"),
189 ENTRY(EL3HLT
, "EL3HLT", "Level 3 halted"),
192 ENTRY(EL3RST
, "EL3RST", "Level 3 reset"),
195 ENTRY(ELNRNG
, "ELNRNG", "Link number out of range"),
197 #if defined (EUNATCH)
198 ENTRY(EUNATCH
, "EUNATCH", "Protocol driver not attached"),
201 ENTRY(ENOCSI
, "ENOCSI", "No CSI structure available"),
204 ENTRY(EL2HLT
, "EL2HLT", "Level 2 halted"),
206 #if defined (EDEADLK)
207 ENTRY(EDEADLK
, "EDEADLK", "Deadlock condition"),
210 ENTRY(ENOLCK
, "ENOLCK", "No record locks available"),
213 ENTRY(EBADE
, "EBADE", "Invalid exchange"),
216 ENTRY(EBADR
, "EBADR", "Invalid request descriptor"),
219 ENTRY(EXFULL
, "EXFULL", "Exchange full"),
222 ENTRY(ENOANO
, "ENOANO", "No anode"),
224 #if defined (EBADRQC)
225 ENTRY(EBADRQC
, "EBADRQC", "Invalid request code"),
227 #if defined (EBADSLT)
228 ENTRY(EBADSLT
, "EBADSLT", "Invalid slot"),
230 #if defined (EDEADLOCK)
231 ENTRY(EDEADLOCK
, "EDEADLOCK", "File locking deadlock error"),
234 ENTRY(EBFONT
, "EBFONT", "Bad font file format"),
237 ENTRY(ENOSTR
, "ENOSTR", "Device not a stream"),
239 #if defined (ENODATA)
240 ENTRY(ENODATA
, "ENODATA", "No data available"),
243 ENTRY(ETIME
, "ETIME", "Timer expired"),
246 ENTRY(ENOSR
, "ENOSR", "Out of streams resources"),
249 ENTRY(ENONET
, "ENONET", "Machine is not on the network"),
252 ENTRY(ENOPKG
, "ENOPKG", "Package not installed"),
254 #if defined (EREMOTE)
255 ENTRY(EREMOTE
, "EREMOTE", "Object is remote"),
257 #if defined (ENOLINK)
258 ENTRY(ENOLINK
, "ENOLINK", "Link has been severed"),
261 ENTRY(EADV
, "EADV", "Advertise error"),
264 ENTRY(ESRMNT
, "ESRMNT", "Srmount error"),
267 ENTRY(ECOMM
, "ECOMM", "Communication error on send"),
270 ENTRY(EPROTO
, "EPROTO", "Protocol error"),
272 #if defined (EMULTIHOP)
273 ENTRY(EMULTIHOP
, "EMULTIHOP", "Multihop attempted"),
275 #if defined (EDOTDOT)
276 ENTRY(EDOTDOT
, "EDOTDOT", "RFS specific error"),
278 #if defined (EBADMSG)
279 ENTRY(EBADMSG
, "EBADMSG", "Not a data message"),
281 #if defined (ENAMETOOLONG)
282 ENTRY(ENAMETOOLONG
, "ENAMETOOLONG", "File name too long"),
284 #if defined (EOVERFLOW)
285 ENTRY(EOVERFLOW
, "EOVERFLOW", "Value too large for defined data type"),
287 #if defined (ENOTUNIQ)
288 ENTRY(ENOTUNIQ
, "ENOTUNIQ", "Name not unique on network"),
291 ENTRY(EBADFD
, "EBADFD", "File descriptor in bad state"),
293 #if defined (EREMCHG)
294 ENTRY(EREMCHG
, "EREMCHG", "Remote address changed"),
296 #if defined (ELIBACC)
297 ENTRY(ELIBACC
, "ELIBACC", "Can not access a needed shared library"),
299 #if defined (ELIBBAD)
300 ENTRY(ELIBBAD
, "ELIBBAD", "Accessing a corrupted shared library"),
302 #if defined (ELIBSCN)
303 ENTRY(ELIBSCN
, "ELIBSCN", ".lib section in a.out corrupted"),
305 #if defined (ELIBMAX)
306 ENTRY(ELIBMAX
, "ELIBMAX", "Attempting to link in too many shared libraries"),
308 #if defined (ELIBEXEC)
309 ENTRY(ELIBEXEC
, "ELIBEXEC", "Cannot exec a shared library directly"),
312 ENTRY(EILSEQ
, "EILSEQ", "Illegal byte sequence"),
315 ENTRY(ENOSYS
, "ENOSYS", "Operation not applicable"),
318 ENTRY(ELOOP
, "ELOOP", "Too many symbolic links encountered"),
320 #if defined (ERESTART)
321 ENTRY(ERESTART
, "ERESTART", "Interrupted system call should be restarted"),
323 #if defined (ESTRPIPE)
324 ENTRY(ESTRPIPE
, "ESTRPIPE", "Streams pipe error"),
326 #if defined (ENOTEMPTY)
327 ENTRY(ENOTEMPTY
, "ENOTEMPTY", "Directory not empty"),
330 ENTRY(EUSERS
, "EUSERS", "Too many users"),
332 #if defined (ENOTSOCK)
333 ENTRY(ENOTSOCK
, "ENOTSOCK", "Socket operation on non-socket"),
335 #if defined (EDESTADDRREQ)
336 ENTRY(EDESTADDRREQ
, "EDESTADDRREQ", "Destination address required"),
338 #if defined (EMSGSIZE)
339 ENTRY(EMSGSIZE
, "EMSGSIZE", "Message too long"),
341 #if defined (EPROTOTYPE)
342 ENTRY(EPROTOTYPE
, "EPROTOTYPE", "Protocol wrong type for socket"),
344 #if defined (ENOPROTOOPT)
345 ENTRY(ENOPROTOOPT
, "ENOPROTOOPT", "Protocol not available"),
347 #if defined (EPROTONOSUPPORT)
348 ENTRY(EPROTONOSUPPORT
, "EPROTONOSUPPORT", "Protocol not supported"),
350 #if defined (ESOCKTNOSUPPORT)
351 ENTRY(ESOCKTNOSUPPORT
, "ESOCKTNOSUPPORT", "Socket type not supported"),
353 #if defined (EOPNOTSUPP)
354 ENTRY(EOPNOTSUPP
, "EOPNOTSUPP", "Operation not supported on transport endpoint"),
356 #if defined (EPFNOSUPPORT)
357 ENTRY(EPFNOSUPPORT
, "EPFNOSUPPORT", "Protocol family not supported"),
359 #if defined (EAFNOSUPPORT)
360 ENTRY(EAFNOSUPPORT
, "EAFNOSUPPORT", "Address family not supported by protocol"),
362 #if defined (EADDRINUSE)
363 ENTRY(EADDRINUSE
, "EADDRINUSE", "Address already in use"),
365 #if defined (EADDRNOTAVAIL)
366 ENTRY(EADDRNOTAVAIL
, "EADDRNOTAVAIL","Cannot assign requested address"),
368 #if defined (ENETDOWN)
369 ENTRY(ENETDOWN
, "ENETDOWN", "Network is down"),
371 #if defined (ENETUNREACH)
372 ENTRY(ENETUNREACH
, "ENETUNREACH", "Network is unreachable"),
374 #if defined (ENETRESET)
375 ENTRY(ENETRESET
, "ENETRESET", "Network dropped connection because of reset"),
377 #if defined (ECONNABORTED)
378 ENTRY(ECONNABORTED
, "ECONNABORTED", "Software caused connection abort"),
380 #if defined (ECONNRESET)
381 ENTRY(ECONNRESET
, "ECONNRESET", "Connection reset by peer"),
383 #if defined (ENOBUFS)
384 ENTRY(ENOBUFS
, "ENOBUFS", "No buffer space available"),
386 #if defined (EISCONN)
387 ENTRY(EISCONN
, "EISCONN", "Transport endpoint is already connected"),
389 #if defined (ENOTCONN)
390 ENTRY(ENOTCONN
, "ENOTCONN", "Transport endpoint is not connected"),
392 #if defined (ESHUTDOWN)
393 ENTRY(ESHUTDOWN
, "ESHUTDOWN", "Cannot send after transport endpoint shutdown"),
395 #if defined (ETOOMANYREFS)
396 ENTRY(ETOOMANYREFS
, "ETOOMANYREFS", "Too many references: cannot splice"),
398 #if defined (ETIMEDOUT)
399 ENTRY(ETIMEDOUT
, "ETIMEDOUT", "Connection timed out"),
401 #if defined (ECONNREFUSED)
402 ENTRY(ECONNREFUSED
, "ECONNREFUSED", "Connection refused"),
404 #if defined (EHOSTDOWN)
405 ENTRY(EHOSTDOWN
, "EHOSTDOWN", "Host is down"),
407 #if defined (EHOSTUNREACH)
408 ENTRY(EHOSTUNREACH
, "EHOSTUNREACH", "No route to host"),
410 #if defined (EALREADY)
411 ENTRY(EALREADY
, "EALREADY", "Operation already in progress"),
413 #if defined (EINPROGRESS)
414 ENTRY(EINPROGRESS
, "EINPROGRESS", "Operation now in progress"),
417 ENTRY(ESTALE
, "ESTALE", "Stale NFS file handle"),
419 #if defined (EUCLEAN)
420 ENTRY(EUCLEAN
, "EUCLEAN", "Structure needs cleaning"),
422 #if defined (ENOTNAM)
423 ENTRY(ENOTNAM
, "ENOTNAM", "Not a XENIX named type file"),
425 #if defined (ENAVAIL)
426 ENTRY(ENAVAIL
, "ENAVAIL", "No XENIX semaphores available"),
429 ENTRY(EISNAM
, "EISNAM", "Is a named type file"),
431 #if defined (EREMOTEIO)
432 ENTRY(EREMOTEIO
, "EREMOTEIO", "Remote I/O error"),
438 /* This is not in the table, because the numeric value of EVMSERR (32767)
439 lies outside the range of sys_errlist[]. */
440 static struct { int value
; const char *name
, *msg
; }
441 evmserr
= { EVMSERR
, "EVMSERR", "VMS-specific error" };
444 /* Translation table allocated and initialized at runtime. Indexed by the
445 errno value to find the equivalent symbolic value. */
447 static const char **error_names
;
448 static int num_error_names
= 0;
450 /* Translation table allocated and initialized at runtime, if it does not
451 already exist in the host environment. Indexed by the errno value to find
452 the descriptive string.
454 We don't export it for use in other modules because even though it has the
455 same name, it differs from other implementations in that it is dynamically
456 initialized rather than statically initialized. */
458 #ifndef HAVE_SYS_ERRLIST
461 static const char **sys_errlist
;
466 extern char *sys_errlist
[];
475 init_error_tables -- initialize the name and message tables
479 static void init_error_tables ();
483 Using the error_table, which is initialized at compile time, generate
484 the error_names and the sys_errlist (if needed) tables, which are
485 indexed at runtime by a specific errno value.
489 The initialization of the tables may fail under low memory conditions,
490 in which case we don't do anything particularly useful, but we don't
491 bomb either. Who knows, it might succeed at a later point if we free
492 some memory in the meantime. In any case, the other routines know
493 how to deal with lack of a table after trying to initialize it. This
494 may or may not be considered to be a bug, that we don't specifically
495 warn about this particular failure mode.
502 const struct error_info
*eip
;
505 /* If we haven't already scanned the error_table once to find the maximum
506 errno value, then go find it now. */
508 if (num_error_names
== 0)
510 for (eip
= error_table
; eip
-> name
!= NULL
; eip
++)
512 if (eip
-> value
>= num_error_names
)
514 num_error_names
= eip
-> value
+ 1;
519 /* Now attempt to allocate the error_names table, zero it out, and then
520 initialize it from the statically initialized error_table. */
522 if (error_names
== NULL
)
524 nbytes
= num_error_names
* sizeof (char *);
525 if ((error_names
= (const char **) malloc (nbytes
)) != NULL
)
527 memset (error_names
, 0, nbytes
);
528 for (eip
= error_table
; eip
-> name
!= NULL
; eip
++)
530 error_names
[eip
-> value
] = eip
-> name
;
535 #ifndef HAVE_SYS_ERRLIST
537 /* Now attempt to allocate the sys_errlist table, zero it out, and then
538 initialize it from the statically initialized error_table. */
540 if (sys_errlist
== NULL
)
542 nbytes
= num_error_names
* sizeof (char *);
543 if ((sys_errlist
= (const char **) malloc (nbytes
)) != NULL
)
545 memset (sys_errlist
, 0, nbytes
);
546 sys_nerr
= num_error_names
;
547 for (eip
= error_table
; eip
-> name
!= NULL
; eip
++)
549 sys_errlist
[eip
-> value
] = eip
-> msg
;
562 errno_max -- return the max errno value
570 Returns the maximum errno value for which a corresponding symbolic
571 name or message is available. Note that in the case where
572 we use the sys_errlist supplied by the system, it is possible for
573 there to be more symbolic names than messages, or vice versa.
574 In fact, the manual page for perror(3C) explicitly warns that one
575 should check the size of the table (sys_nerr) before indexing it,
576 since new error codes may be added to the system before they are
577 added to the table. Thus sys_nerr might be smaller than value
578 implied by the largest errno value defined in <errno.h>.
580 We return the maximum value that can be used to obtain a meaningful
581 symbolic name or message.
590 if (error_names
== NULL
)
592 init_error_tables ();
594 maxsize
= MAX (sys_nerr
, num_error_names
);
595 return (maxsize
- 1);
598 #ifndef HAVE_STRERROR
604 strerror -- map an error number to an error message string
608 char *strerror (int errnoval)
612 Maps an errno number to an error message string, the contents of
613 which are implementation defined. On systems which have the external
614 variables sys_nerr and sys_errlist, these strings will be the same
615 as the ones used by perror().
617 If the supplied error number is within the valid range of indices
618 for the sys_errlist, but no message is available for the particular
619 error number, then returns the string "Error NUM", where NUM is the
622 If the supplied error number is not a valid index into sys_errlist,
625 The returned string is only guaranteed to be valid only until the
626 next call to strerror.
637 #ifndef HAVE_SYS_ERRLIST
639 if (error_names
== NULL
)
641 init_error_tables ();
646 if ((errnoval
< 0) || (errnoval
>= sys_nerr
))
649 if (errnoval
== evmserr
.value
)
653 /* Out of range, just return NULL */
656 else if ((sys_errlist
== NULL
) || (sys_errlist
[errnoval
] == NULL
))
658 /* In range, but no sys_errlist or no entry at this index. */
659 sprintf (buf
, "Error %d", errnoval
);
664 /* In range, and a valid message. Just return the message. */
665 msg
= (char *) sys_errlist
[errnoval
];
671 #endif /* ! HAVE_STRERROR */
678 strerrno -- map an error number to a symbolic name string
682 const char *strerrno (int errnoval)
686 Given an error number returned from a system call (typically
687 returned in errno), returns a pointer to a string containing the
688 symbolic name of that error number, as found in <errno.h>.
690 If the supplied error number is within the valid range of indices
691 for symbolic names, but no name is available for the particular
692 error number, then returns the string "Error NUM", where NUM is
695 If the supplied error number is not within the range of valid
696 indices, then returns NULL.
700 The contents of the location pointed to are only guaranteed to be
701 valid until the next call to strerrno.
712 if (error_names
== NULL
)
714 init_error_tables ();
717 if ((errnoval
< 0) || (errnoval
>= num_error_names
))
720 if (errnoval
== evmserr
.value
)
724 /* Out of range, just return NULL */
727 else if ((error_names
== NULL
) || (error_names
[errnoval
] == NULL
))
729 /* In range, but no error_names or no entry at this index. */
730 sprintf (buf
, "Error %d", errnoval
);
731 name
= (const char *) buf
;
735 /* In range, and a valid name. Just return the name. */
736 name
= error_names
[errnoval
];
746 strtoerrno -- map a symbolic errno name to a numeric value
750 int strtoerrno (char *name)
754 Given the symbolic name of a error number, map it to an errno value.
755 If no translation is found, returns 0.
767 if (error_names
== NULL
)
769 init_error_tables ();
771 for (errnoval
= 0; errnoval
< num_error_names
; errnoval
++)
773 if ((error_names
[errnoval
] != NULL
) &&
774 (strcmp (name
, error_names
[errnoval
]) == 0))
779 if (errnoval
== num_error_names
)
782 if (strcmp (name
, evmserr
.name
) == 0)
783 errnoval
= evmserr
.value
;
793 /* A simple little main that does nothing but print all the errno translations
794 if MAIN is defined and this file is compiled and linked. */
809 errnmax
= errno_max ();
810 printf ("%d entries in names table.\n", num_error_names
);
811 printf ("%d entries in messages table.\n", sys_nerr
);
812 printf ("%d is max useful index.\n", errnmax
);
814 /* Keep printing values until we get to the end of *both* tables, not
815 *either* table. Note that knowing the maximum useful index does *not*
816 relieve us of the responsibility of testing the return pointer for
819 for (errn
= 0; errn
<= errnmax
; errn
++)
821 name
= strerrno (errn
);
822 name
= (name
== NULL
) ? "<NULL>" : name
;
823 msg
= strerror (errn
);
824 msg
= (msg
== NULL
) ? "<NULL>" : msg
;
825 printf ("%-4d%-18s%s\n", errn
, name
, msg
);
This page took 0.072728 seconds and 4 git commands to generate.