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 and Cygwin. */
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)
97 strerror_r (int errnum, char *buf, size_t buflen)
100 /* Filter this out now, so that rest of this replacement knows that
101 there is room for a non-empty message and trailing NUL. */
109 #if GNULIB_defined_ETXTBSY \
110 || GNULIB_defined_ESOCK \
111 || GNULIB_defined_ENOMSG \
112 || GNULIB_defined_EIDRM \
113 || GNULIB_defined_ENOLINK \
114 || GNULIB_defined_EPROTO \
115 || GNULIB_defined_EMULTIHOP \
116 || GNULIB_defined_EBADMSG \
117 || GNULIB_defined_EOVERFLOW \
118 || GNULIB_defined_ENOTSUP \
119 || GNULIB_defined_ESTALE \
120 || GNULIB_defined_EDQUOT \
121 || GNULIB_defined_ECANCELED
123 char const *msg = NULL;
124 /* These error messages are taken from glibc/sysdeps/gnu/errlist.c. */
127 # if GNULIB_defined_ETXTBSY
129 msg = "Text file busy";
133 # if GNULIB_defined_ESOCK /* native Windows platforms */
134 /* EWOULDBLOCK is the same as EAGAIN. */
136 msg = "Operation now in progress";
139 msg = "Operation already in progress";
142 msg = "Socket operation on non-socket";
145 msg = "Destination address required";
148 msg = "Message too long";
151 msg = "Protocol wrong type for socket";
154 msg = "Protocol not available";
156 case EPROTONOSUPPORT:
157 msg = "Protocol not supported";
159 case ESOCKTNOSUPPORT:
160 msg = "Socket type not supported";
163 msg = "Operation not supported";
166 msg = "Protocol family not supported";
169 msg = "Address family not supported by protocol";
172 msg = "Address already in use";
175 msg = "Cannot assign requested address";
178 msg = "Network is down";
181 msg = "Network is unreachable";
184 msg = "Network dropped connection on reset";
187 msg = "Software caused connection abort";
190 msg = "Connection reset by peer";
193 msg = "No buffer space available";
196 msg = "Transport endpoint is already connected";
199 msg = "Transport endpoint is not connected";
202 msg = "Cannot send after transport endpoint shutdown";
205 msg = "Too many references: cannot splice";
208 msg = "Connection timed out";
211 msg = "Connection refused";
214 msg = "Too many levels of symbolic links";
217 msg = "Host is down";
220 msg = "No route to host";
223 msg = "Too many processes";
226 msg = "Too many users";
229 msg = "Disk quota exceeded";
232 msg = "Stale NFS file handle";
235 msg = "Object is remote";
238 /* WSA_INVALID_HANDLE maps to EBADF */
239 /* WSA_NOT_ENOUGH_MEMORY maps to ENOMEM */
240 /* WSA_INVALID_PARAMETER maps to EINVAL */
241 case WSA_OPERATION_ABORTED:
242 msg = "Overlapped operation aborted";
244 case WSA_IO_INCOMPLETE:
245 msg = "Overlapped I/O event object not in signaled state";
248 msg = "Overlapped operations will complete later";
250 /* WSAEINTR maps to EINTR */
251 /* WSAEBADF maps to EBADF */
252 /* WSAEACCES maps to EACCES */
253 /* WSAEFAULT maps to EFAULT */
254 /* WSAEINVAL maps to EINVAL */
255 /* WSAEMFILE maps to EMFILE */
256 /* WSAEWOULDBLOCK maps to EWOULDBLOCK */
257 /* WSAEINPROGRESS is EINPROGRESS */
258 /* WSAEALREADY is EALREADY */
259 /* WSAENOTSOCK is ENOTSOCK */
260 /* WSAEDESTADDRREQ is EDESTADDRREQ */
261 /* WSAEMSGSIZE is EMSGSIZE */
262 /* WSAEPROTOTYPE is EPROTOTYPE */
263 /* WSAENOPROTOOPT is ENOPROTOOPT */
264 /* WSAEPROTONOSUPPORT is EPROTONOSUPPORT */
265 /* WSAESOCKTNOSUPPORT is ESOCKTNOSUPPORT */
266 /* WSAEOPNOTSUPP is EOPNOTSUPP */
267 /* WSAEPFNOSUPPORT is EPFNOSUPPORT */
268 /* WSAEAFNOSUPPORT is EAFNOSUPPORT */
269 /* WSAEADDRINUSE is EADDRINUSE */
270 /* WSAEADDRNOTAVAIL is EADDRNOTAVAIL */
271 /* WSAENETDOWN is ENETDOWN */
272 /* WSAENETUNREACH is ENETUNREACH */
273 /* WSAENETRESET is ENETRESET */
274 /* WSAECONNABORTED is ECONNABORTED */
275 /* WSAECONNRESET is ECONNRESET */
276 /* WSAENOBUFS is ENOBUFS */
277 /* WSAEISCONN is EISCONN */
278 /* WSAENOTCONN is ENOTCONN */
279 /* WSAESHUTDOWN is ESHUTDOWN */
280 /* WSAETOOMANYREFS is ETOOMANYREFS */
281 /* WSAETIMEDOUT is ETIMEDOUT */
282 /* WSAECONNREFUSED is ECONNREFUSED */
283 /* WSAELOOP is ELOOP */
284 /* WSAENAMETOOLONG maps to ENAMETOOLONG */
285 /* WSAEHOSTDOWN is EHOSTDOWN */
286 /* WSAEHOSTUNREACH is EHOSTUNREACH */
287 /* WSAENOTEMPTY maps to ENOTEMPTY */
288 /* WSAEPROCLIM is EPROCLIM */
289 /* WSAEUSERS is EUSERS */
290 /* WSAEDQUOT is EDQUOT */
291 /* WSAESTALE is ESTALE */
292 /* WSAEREMOTE is EREMOTE */
294 msg = "Network subsystem is unavailable";
296 case WSAVERNOTSUPPORTED:
297 msg = "Winsock.dll version out of range";
299 case WSANOTINITIALISED:
300 msg = "Successful WSAStartup not yet performed";
303 msg = "Graceful shutdown in progress";
305 case WSAENOMORE: case WSA_E_NO_MORE:
306 msg = "No more results";
308 case WSAECANCELLED: case WSA_E_CANCELLED:
309 msg = "Call was canceled";
311 case WSAEINVALIDPROCTABLE:
312 msg = "Procedure call table is invalid";
314 case WSAEINVALIDPROVIDER:
315 msg = "Service provider is invalid";
317 case WSAEPROVIDERFAILEDINIT:
318 msg = "Service provider failed to initialize";
320 case WSASYSCALLFAILURE:
321 msg = "System call failure";
323 case WSASERVICE_NOT_FOUND:
324 msg = "Service not found";
326 case WSATYPE_NOT_FOUND:
327 msg = "Class type not found";
330 msg = "Database query was refused";
332 case WSAHOST_NOT_FOUND:
333 msg = "Host not found";
336 msg = "Nonauthoritative host not found";
339 msg = "Nonrecoverable error";
342 msg = "Valid name, no data record of requested type";
344 /* WSA_QOS_* omitted */
348 # if GNULIB_defined_ENOMSG
350 msg = "No message of desired type";
354 # if GNULIB_defined_EIDRM
356 msg = "Identifier removed";
360 # if GNULIB_defined_ENOLINK
362 msg = "Link has been severed";
366 # if GNULIB_defined_EPROTO
368 msg = "Protocol error";
372 # if GNULIB_defined_EMULTIHOP
374 msg = "Multihop attempted";
378 # if GNULIB_defined_EBADMSG
384 # if GNULIB_defined_EOVERFLOW
386 msg = "Value too large for defined data type";
390 # if GNULIB_defined_ENOTSUP
392 msg = "Not supported";
396 # if GNULIB_defined_ESTALE
398 msg = "Stale NFS file handle";
402 # if GNULIB_defined_EDQUOT
404 msg = "Disk quota exceeded";
408 # if GNULIB_defined_ECANCELED
410 msg = "Operation canceled";
417 int saved_errno = errno;
418 size_t len = strlen (msg);
423 memcpy (buf, msg, len + 1);
434 int saved_errno = errno;
436 #if USE_XPG_STRERROR_R
439 extern int __xpg_strerror_r (int errnum, char *buf, size_t buflen);
441 ret = __xpg_strerror_r (errnum, buf, buflen);
446 #elif USE_SYSTEM_STRERROR_R
448 if (buflen > INT_MAX)
452 /* On HP-UX 11.31, strerror_r always fails when buflen < 80. */
456 if (buflen < sizeof (stackbuf))
458 ret = strerror_r (errnum, stackbuf, sizeof (stackbuf));
461 size_t len = strlen (stackbuf);
464 memcpy (buf, stackbuf, len + 1);
470 ret = strerror_r (errnum, buf, buflen);
473 ret = strerror_r (errnum, buf, buflen);
476 /* Some old implementations may return (-1, EINVAL) instead of EINVAL. */
480 /* FreeBSD rejects 0; see http://austingroupbugs.net/view.php?id=382. */
481 if (errnum == 0 && ret == EINVAL)
483 if (buflen <= strlen ("Success"))
492 strcpy (buf, "Success");
496 #else /* USE_SYSTEM_STRERROR */
498 /* Try to do what strerror (errnum) does, but without clobbering the
499 buffer used by strerror(). */
501 # if defined __NetBSD__ || defined __hpux || ((defined _WIN32 || defined __WIN32__) && !defined __CYGWIN__) || defined __CYGWIN__ /* NetBSD, HP-UX, native Win32, Cygwin */
503 /* NetBSD: sys_nerr, sys_errlist are declared through _NETBSD_SOURCE
505 HP-UX: sys_nerr, sys_errlist are declared explicitly above.
506 native Win32: sys_nerr, sys_errlist are declared in <stdlib.h>.
507 Cygwin: sys_nerr, sys_errlist are declared in <stdlib.h>. */
508 if (errnum >= 0 && errnum < sys_nerr)
510 # if HAVE_CATGETS && (defined __NetBSD__ || defined __hpux)
511 # if defined __NetBSD__
512 nl_catd catd = catopen ("libc", NL_CAT_LOCALE);
515 ? catgets (catd, 1, errnum, sys_errlist[errnum])
516 : sys_errlist[errnum]);
519 nl_catd catd = catopen ("perror", NL_CAT_LOCALE);
522 ? catgets (catd, 1, 1 + errnum, sys_errlist[errnum])
523 : sys_errlist[errnum]);
526 const char *errmsg = sys_errlist[errnum];
528 if (errmsg == NULL || *errmsg == '\0')
532 size_t len = strlen (errmsg);
536 memcpy (buf, errmsg, len + 1);
542 # if HAVE_CATGETS && (defined __NetBSD__ || defined __hpux)
543 if (catd != (nl_catd)-1)
550 # elif defined __sgi || (defined __sun && !defined _LP64) /* IRIX, Solaris <= 9 32-bit */
552 /* For a valid error number, the system's strerror() function returns
553 a pointer to a not copied string, not to a buffer. */
554 if (errnum >= 0 && errnum < sys_nerr)
556 char *errmsg = strerror (errnum);
558 if (errmsg == NULL || *errmsg == '\0')
562 size_t len = strlen (errmsg);
566 memcpy (buf, errmsg, len + 1);
578 gl_lock_lock (strerror_lock);
581 char *errmsg = strerror (errnum);
583 /* For invalid error numbers, strerror() on
584 - IRIX 6.5 returns NULL,
585 - HP-UX 11 returns an empty string. */
586 if (errmsg == NULL || *errmsg == '\0')
590 size_t len = strlen (errmsg);
594 memcpy (buf, errmsg, len + 1);
602 gl_lock_unlock (strerror_lock);