1 /* strerror_r.c --- POSIX compatible system error routine
3 Copyright (C) 2010-2011 Free Software Foundation, Inc.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18 /* Written by Bruno Haible <bruno@clisp.org>, 2010. */
22 /* Enable declaration of sys_nerr and sys_errlist in <errno.h> on NetBSD. */
23 #define _NETBSD_SOURCE 1
30 # if GNULIB_defined_ESOCK /* native Windows platforms */
32 # include <winsock2.h>
37 #if (__GLIBC__ >= 2 || defined __UCLIBC__ || defined __CYGWIN__) && HAVE___XPG_STRERROR_R /* glibc >= 2.3.4, cygwin >= 1.7.9 */
39 # define USE_XPG_STRERROR_R 1
41 #elif HAVE_DECL_STRERROR_R && !(__GLIBC__ >= 2 || defined __UCLIBC__ || defined __CYGWIN__)
43 /* The system's strerror_r function is OK, except that its third argument
44 is 'int', not 'size_t', or its return type is wrong. */
48 # define USE_SYSTEM_STRERROR_R 1
50 #else /* (__GLIBC__ >= 2 || defined __UCLIBC__ || defined __CYGWIN__ ? !HAVE___XPG_STRERROR_R : !HAVE_DECL_STRERROR_R) */
52 /* Use the system's strerror(). Exclude glibc and cygwin because the
53 system strerror_r has the wrong return type, and cygwin 1.7.9
54 strerror_r clobbers strerror. */
57 # define USE_SYSTEM_STRERROR 1
59 # if defined __NetBSD__ || defined __hpux || ((defined _WIN32 || defined __WIN32__) && !defined __CYGWIN__) || defined __sgi || (defined __sun && !defined _LP64) || defined __CYGWIN__
61 /* No locking needed. */
63 /* Get catgets internationalization functions. */
65 # include <nl_types.h>
68 /* Get sys_nerr, sys_errlist on HP-UX (otherwise only declared in C++ mode).
69 Get sys_nerr, sys_errlist on IRIX (otherwise only declared with _SGIAPI). */
70 # if defined __hpux || defined __sgi
72 extern char *sys_errlist[];
75 /* Get sys_nerr on Solaris. */
76 # if defined __sun && !defined _LP64
80 /* Get sys_nerr, sys_errlist on native Windows. */
85 # include "glthread/lock.h"
87 /* This lock protects the buffer returned by strerror(). We assume that
88 no other uses of strerror() exist in the program. */
89 gl_lock_define_initialized(static, strerror_lock)
95 /* Copy as much of MSG into BUF as possible, without corrupting errno.
96 Return 0 if MSG fit in BUFLEN, otherwise return ERANGE. */
98 safe_copy (char *buf, size_t buflen, const char *msg)
100 size_t len = strlen (msg);
105 /* Although POSIX allows memcpy() to corrupt errno, we don't
106 know of any implementation where this is a real problem. */
107 memcpy (buf, msg, len + 1);
112 memcpy (buf, msg, buflen - 1);
113 buf[buflen - 1] = '\0';
121 strerror_r (int errnum, char *buf, size_t buflen)
124 /* Filter this out now, so that rest of this replacement knows that
125 there is room for a non-empty message and trailing NUL. */
134 #if GNULIB_defined_ETXTBSY \
135 || GNULIB_defined_ESOCK \
136 || GNULIB_defined_ENOMSG \
137 || GNULIB_defined_EIDRM \
138 || GNULIB_defined_ENOLINK \
139 || GNULIB_defined_EPROTO \
140 || GNULIB_defined_EMULTIHOP \
141 || GNULIB_defined_EBADMSG \
142 || GNULIB_defined_EOVERFLOW \
143 || GNULIB_defined_ENOTSUP \
144 || GNULIB_defined_ESTALE \
145 || GNULIB_defined_EDQUOT \
146 || GNULIB_defined_ECANCELED
148 char const *msg = NULL;
149 /* These error messages are taken from glibc/sysdeps/gnu/errlist.c. */
152 # if GNULIB_defined_ETXTBSY
154 msg = "Text file busy";
158 # if GNULIB_defined_ESOCK /* native Windows platforms */
159 /* EWOULDBLOCK is the same as EAGAIN. */
161 msg = "Operation now in progress";
164 msg = "Operation already in progress";
167 msg = "Socket operation on non-socket";
170 msg = "Destination address required";
173 msg = "Message too long";
176 msg = "Protocol wrong type for socket";
179 msg = "Protocol not available";
181 case EPROTONOSUPPORT:
182 msg = "Protocol not supported";
184 case ESOCKTNOSUPPORT:
185 msg = "Socket type not supported";
188 msg = "Operation not supported";
191 msg = "Protocol family not supported";
194 msg = "Address family not supported by protocol";
197 msg = "Address already in use";
200 msg = "Cannot assign requested address";
203 msg = "Network is down";
206 msg = "Network is unreachable";
209 msg = "Network dropped connection on reset";
212 msg = "Software caused connection abort";
215 msg = "Connection reset by peer";
218 msg = "No buffer space available";
221 msg = "Transport endpoint is already connected";
224 msg = "Transport endpoint is not connected";
227 msg = "Cannot send after transport endpoint shutdown";
230 msg = "Too many references: cannot splice";
233 msg = "Connection timed out";
236 msg = "Connection refused";
239 msg = "Too many levels of symbolic links";
242 msg = "Host is down";
245 msg = "No route to host";
248 msg = "Too many processes";
251 msg = "Too many users";
254 msg = "Disk quota exceeded";
257 msg = "Stale NFS file handle";
260 msg = "Object is remote";
263 /* WSA_INVALID_HANDLE maps to EBADF */
264 /* WSA_NOT_ENOUGH_MEMORY maps to ENOMEM */
265 /* WSA_INVALID_PARAMETER maps to EINVAL */
266 case WSA_OPERATION_ABORTED:
267 msg = "Overlapped operation aborted";
269 case WSA_IO_INCOMPLETE:
270 msg = "Overlapped I/O event object not in signaled state";
273 msg = "Overlapped operations will complete later";
275 /* WSAEINTR maps to EINTR */
276 /* WSAEBADF maps to EBADF */
277 /* WSAEACCES maps to EACCES */
278 /* WSAEFAULT maps to EFAULT */
279 /* WSAEINVAL maps to EINVAL */
280 /* WSAEMFILE maps to EMFILE */
281 /* WSAEWOULDBLOCK maps to EWOULDBLOCK */
282 /* WSAEINPROGRESS is EINPROGRESS */
283 /* WSAEALREADY is EALREADY */
284 /* WSAENOTSOCK is ENOTSOCK */
285 /* WSAEDESTADDRREQ is EDESTADDRREQ */
286 /* WSAEMSGSIZE is EMSGSIZE */
287 /* WSAEPROTOTYPE is EPROTOTYPE */
288 /* WSAENOPROTOOPT is ENOPROTOOPT */
289 /* WSAEPROTONOSUPPORT is EPROTONOSUPPORT */
290 /* WSAESOCKTNOSUPPORT is ESOCKTNOSUPPORT */
291 /* WSAEOPNOTSUPP is EOPNOTSUPP */
292 /* WSAEPFNOSUPPORT is EPFNOSUPPORT */
293 /* WSAEAFNOSUPPORT is EAFNOSUPPORT */
294 /* WSAEADDRINUSE is EADDRINUSE */
295 /* WSAEADDRNOTAVAIL is EADDRNOTAVAIL */
296 /* WSAENETDOWN is ENETDOWN */
297 /* WSAENETUNREACH is ENETUNREACH */
298 /* WSAENETRESET is ENETRESET */
299 /* WSAECONNABORTED is ECONNABORTED */
300 /* WSAECONNRESET is ECONNRESET */
301 /* WSAENOBUFS is ENOBUFS */
302 /* WSAEISCONN is EISCONN */
303 /* WSAENOTCONN is ENOTCONN */
304 /* WSAESHUTDOWN is ESHUTDOWN */
305 /* WSAETOOMANYREFS is ETOOMANYREFS */
306 /* WSAETIMEDOUT is ETIMEDOUT */
307 /* WSAECONNREFUSED is ECONNREFUSED */
308 /* WSAELOOP is ELOOP */
309 /* WSAENAMETOOLONG maps to ENAMETOOLONG */
310 /* WSAEHOSTDOWN is EHOSTDOWN */
311 /* WSAEHOSTUNREACH is EHOSTUNREACH */
312 /* WSAENOTEMPTY maps to ENOTEMPTY */
313 /* WSAEPROCLIM is EPROCLIM */
314 /* WSAEUSERS is EUSERS */
315 /* WSAEDQUOT is EDQUOT */
316 /* WSAESTALE is ESTALE */
317 /* WSAEREMOTE is EREMOTE */
319 msg = "Network subsystem is unavailable";
321 case WSAVERNOTSUPPORTED:
322 msg = "Winsock.dll version out of range";
324 case WSANOTINITIALISED:
325 msg = "Successful WSAStartup not yet performed";
328 msg = "Graceful shutdown in progress";
330 case WSAENOMORE: case WSA_E_NO_MORE:
331 msg = "No more results";
333 case WSAECANCELLED: case WSA_E_CANCELLED:
334 msg = "Call was canceled";
336 case WSAEINVALIDPROCTABLE:
337 msg = "Procedure call table is invalid";
339 case WSAEINVALIDPROVIDER:
340 msg = "Service provider is invalid";
342 case WSAEPROVIDERFAILEDINIT:
343 msg = "Service provider failed to initialize";
345 case WSASYSCALLFAILURE:
346 msg = "System call failure";
348 case WSASERVICE_NOT_FOUND:
349 msg = "Service not found";
351 case WSATYPE_NOT_FOUND:
352 msg = "Class type not found";
355 msg = "Database query was refused";
357 case WSAHOST_NOT_FOUND:
358 msg = "Host not found";
361 msg = "Nonauthoritative host not found";
364 msg = "Nonrecoverable error";
367 msg = "Valid name, no data record of requested type";
369 /* WSA_QOS_* omitted */
373 # if GNULIB_defined_ENOMSG
375 msg = "No message of desired type";
379 # if GNULIB_defined_EIDRM
381 msg = "Identifier removed";
385 # if GNULIB_defined_ENOLINK
387 msg = "Link has been severed";
391 # if GNULIB_defined_EPROTO
393 msg = "Protocol error";
397 # if GNULIB_defined_EMULTIHOP
399 msg = "Multihop attempted";
403 # if GNULIB_defined_EBADMSG
409 # if GNULIB_defined_EOVERFLOW
411 msg = "Value too large for defined data type";
415 # if GNULIB_defined_ENOTSUP
417 msg = "Not supported";
421 # if GNULIB_defined_ESTALE
423 msg = "Stale NFS file handle";
427 # if GNULIB_defined_EDQUOT
429 msg = "Disk quota exceeded";
433 # if GNULIB_defined_ECANCELED
435 msg = "Operation canceled";
441 return safe_copy (buf, buflen, msg);
447 int saved_errno = errno;
449 #if USE_XPG_STRERROR_R
452 extern int __xpg_strerror_r (int errnum, char *buf, size_t buflen);
454 ret = __xpg_strerror_r (errnum, buf, buflen);
459 /* glibc 2.13 would not touch buf on err, so we have to fall
460 back to GNU strerror_r which always returns a thread-safe
461 untruncated string to (partially) copy into our buf. */
462 safe_copy (buf, buflen, strerror_r (errnum, buf, buflen));
466 #elif USE_SYSTEM_STRERROR_R
468 if (buflen > INT_MAX)
472 /* On HP-UX 11.31, strerror_r always fails when buflen < 80. */
476 if (buflen < sizeof stackbuf)
478 ret = strerror_r (errnum, stackbuf, sizeof stackbuf);
480 ret = safe_copy (buf, buflen, stackbuf);
483 ret = strerror_r (errnum, buf, buflen);
486 ret = strerror_r (errnum, buf, buflen);
489 /* Some old implementations may return (-1, EINVAL) instead of EINVAL. */
493 /* FreeBSD rejects 0; see http://austingroupbugs.net/view.php?id=382. */
494 if (errnum == 0 && ret == EINVAL)
495 ret = safe_copy (buf, buflen, "Success");
497 #else /* USE_SYSTEM_STRERROR */
499 /* Try to do what strerror (errnum) does, but without clobbering the
500 buffer used by strerror(). */
502 # if defined __NetBSD__ || defined __hpux || ((defined _WIN32 || defined __WIN32__) && !defined __CYGWIN__) || defined __CYGWIN__ /* NetBSD, HP-UX, native Win32, Cygwin */
504 /* NetBSD: sys_nerr, sys_errlist are declared through _NETBSD_SOURCE
506 HP-UX: sys_nerr, sys_errlist are declared explicitly above.
507 native Win32: sys_nerr, sys_errlist are declared in <stdlib.h>.
508 Cygwin: sys_nerr, sys_errlist are declared in <errno.h>. */
509 if (errnum >= 0 && errnum < sys_nerr)
511 # if HAVE_CATGETS && (defined __NetBSD__ || defined __hpux)
512 # if defined __NetBSD__
513 nl_catd catd = catopen ("libc", NL_CAT_LOCALE);
516 ? catgets (catd, 1, errnum, sys_errlist[errnum])
517 : sys_errlist[errnum]);
520 nl_catd catd = catopen ("perror", NL_CAT_LOCALE);
523 ? catgets (catd, 1, 1 + errnum, sys_errlist[errnum])
524 : sys_errlist[errnum]);
527 const char *errmsg = sys_errlist[errnum];
529 if (errmsg == NULL || *errmsg == '\0')
532 ret = safe_copy (buf, buflen, errmsg);
533 # if HAVE_CATGETS && (defined __NetBSD__ || defined __hpux)
534 if (catd != (nl_catd)-1)
541 # elif defined __sgi || (defined __sun && !defined _LP64) /* IRIX, Solaris <= 9 32-bit */
543 /* For a valid error number, the system's strerror() function returns
544 a pointer to a not copied string, not to a buffer. */
545 if (errnum >= 0 && errnum < sys_nerr)
547 char *errmsg = strerror (errnum);
549 if (errmsg == NULL || *errmsg == '\0')
552 ret = safe_copy (buf, buflen, errmsg);
559 gl_lock_lock (strerror_lock);
562 char *errmsg = strerror (errnum);
564 /* For invalid error numbers, strerror() on
565 - IRIX 6.5 returns NULL,
566 - HP-UX 11 returns an empty string. */
567 if (errmsg == NULL || *errmsg == '\0')
570 ret = safe_copy (buf, buflen, errmsg);
573 gl_lock_unlock (strerror_lock);
579 if (ret == EINVAL && !*buf)
580 snprintf (buf, buflen, "Unknown error %d", errnum);