char *strerror(int errnum) { static char buf[_STRERROR_BUFSIZE]; __xpg_strerror_r(errnum, buf, sizeof(buf)); return buf; }
int strerror_r (int errnum, char *buf, size_t buflen) #undef strerror_r { /* Filter this out now, so that rest of this replacement knows that there is room for a non-empty message and trailing NUL. */ if (buflen <= 1) { if (buflen) *buf = '\0'; return ERANGE; } *buf = '\0'; /* Check for gnulib overrides. */ { char const *msg = strerror_override (errnum); if (msg) return safe_copy (buf, buflen, msg); } { int ret; int saved_errno = errno; #if USE_XPG_STRERROR_R { ret = __xpg_strerror_r (errnum, buf, buflen); if (ret < 0) ret = errno; if (!*buf) { /* glibc 2.13 would not touch buf on err, so we have to fall back to GNU strerror_r which always returns a thread-safe untruncated string to (partially) copy into our buf. */ safe_copy (buf, buflen, strerror_r (errnum, buf, buflen)); } } #elif USE_SYSTEM_STRERROR_R if (buflen > INT_MAX) buflen = INT_MAX; # ifdef __hpux /* On HP-UX 11.31, strerror_r always fails when buflen < 80; it also fails to change buf on EINVAL. */ { char stackbuf[80]; if (buflen < sizeof stackbuf) { ret = strerror_r (errnum, stackbuf, sizeof stackbuf); if (ret == 0) ret = safe_copy (buf, buflen, stackbuf); } else ret = strerror_r (errnum, buf, buflen); } # else ret = strerror_r (errnum, buf, buflen); /* Some old implementations may return (-1, EINVAL) instead of EINVAL. */ if (ret < 0) ret = errno; # endif # ifdef _AIX /* AIX returns 0 rather than ERANGE when truncating strings; try again until we are sure we got the entire string. */ if (!ret && strlen (buf) == buflen - 1) { char stackbuf[STACKBUF_LEN]; size_t len; strerror_r (errnum, stackbuf, sizeof stackbuf); len = strlen (stackbuf); /* STACKBUF_LEN should have been large enough. */ if (len + 1 == sizeof stackbuf) abort (); if (buflen <= len) ret = ERANGE; } # else /* Solaris 10 does not populate buf on ERANGE. OpenBSD 4.7 truncates early on ERANGE rather than return a partial integer. We prefer the maximal string. We set buf[0] earlier, and we know of no implementation that modifies buf to be an unterminated string, so this strlen should be portable in practice (rather than pulling in a safer strnlen). */ if (ret == ERANGE && strlen (buf) < buflen - 1) { char stackbuf[STACKBUF_LEN]; /* STACKBUF_LEN should have been large enough. */ if (strerror_r (errnum, stackbuf, sizeof stackbuf) == ERANGE) abort (); safe_copy (buf, buflen, stackbuf); } # endif #else /* USE_SYSTEM_STRERROR */ /* Try to do what strerror (errnum) does, but without clobbering the buffer used by strerror(). */ # if defined __NetBSD__ || defined __hpux || ((defined _WIN32 || defined __WIN32__) && !defined __CYGWIN__) || defined __CYGWIN__ /* NetBSD, HP-UX, native Windows, Cygwin */ /* NetBSD: sys_nerr, sys_errlist are declared through _NETBSD_SOURCE and <errno.h> above. HP-UX: sys_nerr, sys_errlist are declared explicitly above. native Windows: sys_nerr, sys_errlist are declared in <stdlib.h>. Cygwin: sys_nerr, sys_errlist are declared in <errno.h>. */ if (errnum >= 0 && errnum < sys_nerr) { # if HAVE_CATGETS && (defined __NetBSD__ || defined __hpux) # if defined __NetBSD__ nl_catd catd = catopen ("libc", NL_CAT_LOCALE); const char *errmsg = (catd != (nl_catd)-1 ? catgets (catd, 1, errnum, sys_errlist[errnum]) : sys_errlist[errnum]); # endif # if defined __hpux nl_catd catd = catopen ("perror", NL_CAT_LOCALE); const char *errmsg = (catd != (nl_catd)-1 ? catgets (catd, 1, 1 + errnum, sys_errlist[errnum]) : sys_errlist[errnum]); # endif # else const char *errmsg = sys_errlist[errnum]; # endif if (errmsg == NULL || *errmsg == '\0') ret = EINVAL; else ret = safe_copy (buf, buflen, errmsg); # if HAVE_CATGETS && (defined __NetBSD__ || defined __hpux) if (catd != (nl_catd)-1) catclose (catd); # endif } else ret = EINVAL; # elif defined __sgi || (defined __sun && !defined _LP64) /* IRIX, Solaris <= 9 32-bit */ /* For a valid error number, the system's strerror() function returns a pointer to a not copied string, not to a buffer. */ if (errnum >= 0 && errnum < sys_nerr) { char *errmsg = strerror (errnum); if (errmsg == NULL || *errmsg == '\0') ret = EINVAL; else ret = safe_copy (buf, buflen, errmsg); } else ret = EINVAL; # else gl_lock_lock (strerror_lock); { char *errmsg = strerror (errnum); /* For invalid error numbers, strerror() on - IRIX 6.5 returns NULL, - HP-UX 11 returns an empty string. */ if (errmsg == NULL || *errmsg == '\0') ret = EINVAL; else ret = safe_copy (buf, buflen, errmsg); } gl_lock_unlock (strerror_lock); # endif #endif if (ret == EINVAL && !*buf) snprintf (buf, buflen, "Unknown error %d", errnum); errno = saved_errno; return ret; } }
int strerror_r (int errnum, char *buf, size_t buflen) #undef strerror_r { /* Filter this out now, so that rest of this replacement knows that there is room for a non-empty message and trailing NUL. */ if (buflen <= 1) { if (buflen) *buf = '\0'; return ERANGE; } *buf = '\0'; /* Check for gnulib overrides. */ { char const *msg = strerror_override (errnum); if (msg) return safe_copy (buf, buflen, msg); } { int ret; int saved_errno = errno; #if USE_XPG_STRERROR_R { ret = __xpg_strerror_r (errnum, buf, buflen); if (ret < 0) ret = errno; if (!*buf) { /* glibc 2.13 would not touch buf on err, so we have to fall back to GNU strerror_r which always returns a thread-safe untruncated string to (partially) copy into our buf. */ safe_copy (buf, buflen, strerror_r (errnum, buf, buflen)); } } #elif USE_SYSTEM_STRERROR_R if (buflen > INT_MAX) buflen = INT_MAX; # ifdef __hpux /* On HP-UX 11.31, strerror_r always fails when buflen < 80; it also fails to change buf on EINVAL. */ { char stackbuf[80]; if (buflen < sizeof stackbuf) { ret = strerror_r (errnum, stackbuf, sizeof stackbuf); if (ret == 0) ret = safe_copy (buf, buflen, stackbuf); } else ret = strerror_r (errnum, buf, buflen); } # else ret = strerror_r (errnum, buf, buflen); /* Some old implementations may return (-1, EINVAL) instead of EINVAL. But on Haiku, valid error numbers are negative. */ # if !defined __HAIKU__ if (ret < 0) ret = errno; # endif # endif # if defined _AIX || defined __HAIKU__ /* AIX and Haiku return 0 rather than ERANGE when truncating strings; try again until we are sure we got the entire string. */ if (!ret && strlen (buf) == buflen - 1) { char stackbuf[STACKBUF_LEN]; size_t len; strerror_r (errnum, stackbuf, sizeof stackbuf); len = strlen (stackbuf); /* STACKBUF_LEN should have been large enough. */ if (len + 1 == sizeof stackbuf) abort (); if (buflen <= len) ret = ERANGE; } # else /* Solaris 10 does not populate buf on ERANGE. OpenBSD 4.7 truncates early on ERANGE rather than return a partial integer. We prefer the maximal string. We set buf[0] earlier, and we know of no implementation that modifies buf to be an unterminated string, so this strlen should be portable in practice (rather than pulling in a safer strnlen). */ if (ret == ERANGE && strlen (buf) < buflen - 1) { char stackbuf[STACKBUF_LEN]; /* STACKBUF_LEN should have been large enough. */ if (strerror_r (errnum, stackbuf, sizeof stackbuf) == ERANGE) abort (); safe_copy (buf, buflen, stackbuf); } # endif #else /* USE_SYSTEM_STRERROR */ /* Try to do what strerror (errnum) does, but without clobbering the buffer used by strerror(). */ # if defined __NetBSD__ || defined __hpux || (defined _WIN32 && !defined __CYGWIN__) || defined __CYGWIN__ /* NetBSD, HP-UX, native Windows, Cygwin */ /* NetBSD: sys_nerr, sys_errlist are declared through _NETBSD_SOURCE and <errno.h> above. HP-UX: sys_nerr, sys_errlist are declared explicitly above. native Windows: sys_nerr, sys_errlist are declared in <stdlib.h>. Cygwin: sys_nerr, sys_errlist are declared in <errno.h>. */ if (errnum >= 0 && errnum < sys_nerr) { # if HAVE_CATGETS && (defined __NetBSD__ || defined __hpux) # if defined __NetBSD__ nl_catd catd = catopen ("libc", NL_CAT_LOCALE); const char *errmsg = (catd != (nl_catd)-1 ? catgets (catd, 1, errnum, sys_errlist[errnum]) : sys_errlist[errnum]); # endif # if defined __hpux nl_catd catd = catopen ("perror", NL_CAT_LOCALE); const char *errmsg = (catd != (nl_catd)-1 ? catgets (catd, 1, 1 + errnum, sys_errlist[errnum]) : sys_errlist[errnum]); # endif # else const char *errmsg = sys_errlist[errnum]; # endif if (errmsg == NULL || *errmsg == '\0') ret = EINVAL; else ret = safe_copy (buf, buflen, errmsg); # if HAVE_CATGETS && (defined __NetBSD__ || defined __hpux) if (catd != (nl_catd)-1) catclose (catd); # endif } else ret = EINVAL; # elif defined __sgi || (defined __sun && !defined _LP64) /* IRIX, Solaris <= 9 32-bit */ /* For a valid error number, the system's strerror() function returns a pointer to a not copied string, not to a buffer. */ if (errnum >= 0 && errnum < sys_nerr) { char *errmsg = strerror (errnum); if (errmsg == NULL || *errmsg == '\0') ret = EINVAL; else ret = safe_copy (buf, buflen, errmsg); } else ret = EINVAL; # else gl_lock_lock (strerror_lock); { char *errmsg = strerror (errnum); /* For invalid error numbers, strerror() on - IRIX 6.5 returns NULL, - HP-UX 11 returns an empty string. */ if (errmsg == NULL || *errmsg == '\0') ret = EINVAL; else ret = safe_copy (buf, buflen, errmsg); } gl_lock_unlock (strerror_lock); # endif #endif #if defined _WIN32 && !defined __CYGWIN__ /* MSVC 14 defines names for many error codes in the range 100..140, but _sys_errlist contains strings only for the error codes < _sys_nerr = 43. */ if (ret == EINVAL) { const char *errmsg; switch (errnum) { case 100 /* EADDRINUSE */: errmsg = "Address already in use"; break; case 101 /* EADDRNOTAVAIL */: errmsg = "Cannot assign requested address"; break; case 102 /* EAFNOSUPPORT */: errmsg = "Address family not supported by protocol"; break; case 103 /* EALREADY */: errmsg = "Operation already in progress"; break; case 105 /* ECANCELED */: errmsg = "Operation canceled"; break; case 106 /* ECONNABORTED */: errmsg = "Software caused connection abort"; break; case 107 /* ECONNREFUSED */: errmsg = "Connection refused"; break; case 108 /* ECONNRESET */: errmsg = "Connection reset by peer"; break; case 109 /* EDESTADDRREQ */: errmsg = "Destination address required"; break; case 110 /* EHOSTUNREACH */: errmsg = "No route to host"; break; case 112 /* EINPROGRESS */: errmsg = "Operation now in progress"; break; case 113 /* EISCONN */: errmsg = "Transport endpoint is already connected"; break; case 114 /* ELOOP */: errmsg = "Too many levels of symbolic links"; break; case 115 /* EMSGSIZE */: errmsg = "Message too long"; break; case 116 /* ENETDOWN */: errmsg = "Network is down"; break; case 117 /* ENETRESET */: errmsg = "Network dropped connection on reset"; break; case 118 /* ENETUNREACH */: errmsg = "Network is unreachable"; break; case 119 /* ENOBUFS */: errmsg = "No buffer space available"; break; case 123 /* ENOPROTOOPT */: errmsg = "Protocol not available"; break; case 126 /* ENOTCONN */: errmsg = "Transport endpoint is not connected"; break; case 128 /* ENOTSOCK */: errmsg = "Socket operation on non-socket"; break; case 129 /* ENOTSUP */: errmsg = "Not supported"; break; case 130 /* EOPNOTSUPP */: errmsg = "Operation not supported"; break; case 132 /* EOVERFLOW */: errmsg = "Value too large for defined data type"; break; case 133 /* EOWNERDEAD */: errmsg = "Owner died"; break; case 134 /* EPROTO */: errmsg = "Protocol error"; break; case 135 /* EPROTONOSUPPORT */: errmsg = "Protocol not supported"; break; case 136 /* EPROTOTYPE */: errmsg = "Protocol wrong type for socket"; break; case 138 /* ETIMEDOUT */: errmsg = "Connection timed out"; break; case 140 /* EWOULDBLOCK */: errmsg = "Operation would block"; break; default: errmsg = NULL; break; } if (errmsg != NULL) ret = safe_copy (buf, buflen, errmsg); } #endif if (ret == EINVAL && !*buf) { #if defined __HAIKU__ /* For consistency with perror(). */ snprintf (buf, buflen, "Unknown Application Error (%d)", errnum); #else snprintf (buf, buflen, "Unknown error %d", errnum); #endif } errno = saved_errno; return ret; } }