📄 strerror.c
字号:
ECONNREFUSED, "ECONNREFUSED", "Connection refused",#endif#if defined (EHOSTDOWN) EHOSTDOWN, "EHOSTDOWN", "Host is down",#endif#if defined (EHOSTUNREACH) EHOSTUNREACH, "EHOSTUNREACH", "No route to host",#endif#if defined (EALREADY) EALREADY, "EALREADY", "Operation already in progress",#endif#if defined (EINPROGRESS) EINPROGRESS, "EINPROGRESS", "Operation now in progress",#endif#if defined (ESTALE) ESTALE, "ESTALE", "Stale NFS file handle",#endif#if defined (EUCLEAN) EUCLEAN, "EUCLEAN", "Structure needs cleaning",#endif#if defined (ENOTNAM) ENOTNAM, "ENOTNAM", "Not a XENIX named type file",#endif#if defined (ENAVAIL) ENAVAIL, "ENAVAIL", "No XENIX semaphores available",#endif#if defined (EISNAM) EISNAM, "EISNAM", "Is a named type file",#endif#if defined (EREMOTEIO) EREMOTEIO, "EREMOTEIO", "Remote I/O error",#endif 0, NULL, NULL};/* Translation table allocated and initialized at runtime. Indexed by the errno value to find the equivalent symbolic value. */static char **error_names;static int num_error_names = 0;/* Translation table allocated and initialized at runtime, if it does not already exist in the host environment. Indexed by the errno value to find the descriptive string. We don't export it for use in other modules because even though it has the same name, it differs from other implementations in that it is dynamically initialized rather than statically initialized. */#ifdef notdef#ifdef NEED_sys_errliststatic int sys_nerr;static char **sys_errlist;#elseextern int sys_nerr;extern char *sys_errlist[];#endif#endif/*NAME init_error_tables -- initialize the name and message tablesSYNOPSIS static void init_error_tables ();DESCRIPTION Using the error_table, which is initialized at compile time, generate the error_names and the sys_errlist (if needed) tables, which are indexed at runtime by a specific errno value.BUGS The initialization of the tables may fail under low memory conditions, in which case we don't do anything particularly useful, but we don't bomb either. Who knows, it might succeed at a later point if we free some memory in the meantime. In any case, the other routines know how to deal with lack of a table after trying to initialize it. This may or may not be considered to be a bug, that we don't specifically warn about this particular failure mode.*/static voidinit_error_tables (){ struct error_info *eip; int nbytes; /* If we haven't already scanned the error_table once to find the maximum errno value, then go find it now. */ if (num_error_names == 0) { for (eip = error_table; eip -> name != NULL; eip++) { if (eip -> value >= num_error_names) { num_error_names = eip -> value + 1; } } } /* Now attempt to allocate the error_names table, zero it out, and then initialize it from the statically initialized error_table. */ if (error_names == NULL) { nbytes = num_error_names * sizeof (char *); if ((error_names = (char **) malloc (nbytes)) != NULL) { memset (error_names, 0, nbytes); for (eip = error_table; eip -> name != NULL; eip++) { error_names[eip -> value] = eip -> name; } } }#ifdef NEED_sys_errlist /* Now attempt to allocate the sys_errlist table, zero it out, and then initialize it from the statically initialized error_table. */ if (sys_errlist == NULL) { nbytes = num_error_names * sizeof (char *); if ((sys_errlist = (char **) malloc (nbytes)) != NULL) { memset (sys_errlist, 0, nbytes); sys_nerr = num_error_names; for (eip = error_table; eip -> name != NULL; eip++) { sys_errlist[eip -> value] = eip -> msg; } } }#endif}/*NAME errno_max -- return the max errno valueSYNOPSIS int errno_max ();DESCRIPTION Returns the maximum errno value for which a corresponding symbolic name or message is available. Note that in the case where we use the sys_errlist supplied by the system, it is possible for there to be more symbolic names than messages, or vice versa. In fact, the manual page for perror(3C) explicitly warns that one should check the size of the table (sys_nerr) before indexing it, since new error codes may be added to the system before they are added to the table. Thus sys_nerr might be smaller than value implied by the largest errno value defined in <errno.h>. We return the maximum value that can be used to obtain a meaningful symbolic name or message.*/interrno_max (){ int maxsize; if (error_names == NULL) { init_error_tables (); } maxsize = MAX (sys_nerr, num_error_names); return (maxsize - 1);}#ifdef NEED_strerror/*NAME strerror -- map an error number to an error message stringSYNOPSIS char *strerror (int errnoval)DESCRIPTION Maps an errno number to an error message string, the contents of which are implementation defined. On systems which have the external variables sys_nerr and sys_errlist, these strings will be the same as the ones used by perror(). If the supplied error number is within the valid range of indices for the sys_errlist, but no message is available for the particular error number, then returns the string "Error NUM", where NUM is the error number. If the supplied error number is not a valid index into sys_errlist, returns NULL. The returned string is only guaranteed to be valid only until the next call to strerror.*/char *strerror (errnoval) int errnoval;{ char *msg; static char buf[32];#ifdef NEED_sys_errlist if (error_names == NULL) { init_error_tables (); }#endif if ((errnoval < 0) || (errnoval >= sys_nerr)) { /* Out of range, just return NULL */ msg = NULL; } else if ((sys_errlist == NULL) || (sys_errlist[errnoval] == NULL)) { /* In range, but no sys_errlist or no entry at this index. */ sprintf (buf, "Error %d", errnoval); msg = buf; } else { /* In range, and a valid message. Just return the message. */ msg = sys_errlist[errnoval]; } return (msg);}#endif /* NEED_strerror *//*NAME strerrno -- map an error number to a symbolic name stringSYNOPSIS char *strerrno (int errnoval)DESCRIPTION Given an error number returned from a system call (typically returned in errno), returns a pointer to a string containing the symbolic name of that error number, as found in <errno.h>. If the supplied error number is within the valid range of indices for symbolic names, but no name is available for the particular error number, then returns the string "Error NUM", where NUM is the error number. If the supplied error number is not within the range of valid indices, then returns NULL.BUGS The contents of the location pointed to are only guaranteed to be valid until the next call to strerrno.*/char *strerrno (errnoval) int errnoval;{ char *name; static char buf[32]; if (error_names == NULL) { init_error_tables (); } if ((errnoval < 0) || (errnoval >= num_error_names)) { /* Out of range, just return NULL */ name = NULL; } else if ((error_names == NULL) || (error_names[errnoval] == NULL)) { /* In range, but no error_names or no entry at this index. */ sprintf (buf, "Error %d", errnoval); name = buf; } else { /* In range, and a valid name. Just return the name. */ name = error_names[errnoval]; } return (name);}/*NAME strtoerrno -- map a symbolic errno name to a numeric valueSYNOPSIS int strtoerrno (char *name)DESCRIPTION Given the symbolic name of a error number, map it to an errno value. If no translation is found, returns 0.*/intstrtoerrno (name) char *name;{ int errnoval = 0; if (name != NULL) { if (error_names == NULL) { init_error_tables (); } for (errnoval = 0; errnoval < num_error_names; errnoval++) { if ((error_names[errnoval] != NULL) && (strcmp (name, error_names[errnoval]) == 0)) { break; } } if (errnoval == num_error_names) { errnoval = 0; } } return (errnoval);}/* A simple little main that does nothing but print all the errno translations if MAIN is defined and this file is compiled and linked. */#ifdef MAINmain (){ int errn; int errnmax; char *name; char *msg; char *strerrno (); char *strerror (); errnmax = errno_max (); printf ("%d entries in names table.\n", num_error_names); printf ("%d entries in messages table.\n", sys_nerr); printf ("%d is max useful index.\n", errnmax); /* Keep printing values until we get to the end of *both* tables, not *either* table. Note that knowing the maximum useful index does *not* relieve us of the responsibility of testing the return pointer for NULL. */ for (errn = 0; errn <= errnmax; errn++) { name = strerrno (errn); name = (name == NULL) ? "<NULL>" : name; msg = strerror (errn); msg = (msg == NULL) ? "<NULL>" : msg; printf ("%-4d%-18s%s\n", errn, name, msg); }}#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -