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. */
27 # if GNULIB_defined_ESOCK /* native Windows platforms */
29 # include <winsock2.h>
34 #if HAVE_DECL_STRERROR_R && !(__GLIBC__ >= 2 || defined __UCLIBC__)
36 /* The system's strerror_r function is OK, except that its third argument
37 is 'int', not 'size_t', or its return type is wrong. */
41 # define USE_SYSTEM_STRERROR_R 1
43 #elif (__GLIBC__ >= 2 || defined __UCLIBC__) && HAVE___XPG_STRERROR_R /* glibc >= 2.3.4 */
45 # define USE_XPG_STRERROR_R 1
47 #else /* (__GLIBC__ >= 2 || defined __UCLIBC__ ? !HAVE___XPG_STRERROR_R : !HAVE_DECL_STRERROR_R) */
49 # include "glthread/lock.h"
51 /* Use strerror(), with locking. */
54 # define USE_SYSTEM_STRERROR 1
56 /* This lock protects the buffer returned by strerror(). We assume that
57 no other uses of strerror() exist in the program. */
58 gl_lock_define_initialized(static, strerror_lock)
64 strerror_r (int errnum, char *buf, size_t buflen)
67 #if GNULIB_defined_ETXTBSY \
68 || GNULIB_defined_ESOCK \
69 || GNULIB_defined_ENOMSG \
70 || GNULIB_defined_EIDRM \
71 || GNULIB_defined_ENOLINK \
72 || GNULIB_defined_EPROTO \
73 || GNULIB_defined_EMULTIHOP \
74 || GNULIB_defined_EBADMSG \
75 || GNULIB_defined_EOVERFLOW \
76 || GNULIB_defined_ENOTSUP \
77 || GNULIB_defined_ESTALE \
78 || GNULIB_defined_EDQUOT \
79 || GNULIB_defined_ECANCELED
81 char const *msg = NULL;
82 /* These error messages are taken from glibc/sysdeps/gnu/errlist.c. */
85 # if GNULIB_defined_ETXTBSY
87 msg = "Text file busy";
91 # if GNULIB_defined_ESOCK /* native Windows platforms */
92 /* EWOULDBLOCK is the same as EAGAIN. */
94 msg = "Operation now in progress";
97 msg = "Operation already in progress";
100 msg = "Socket operation on non-socket";
103 msg = "Destination address required";
106 msg = "Message too long";
109 msg = "Protocol wrong type for socket";
112 msg = "Protocol not available";
114 case EPROTONOSUPPORT:
115 msg = "Protocol not supported";
117 case ESOCKTNOSUPPORT:
118 msg = "Socket type not supported";
121 msg = "Operation not supported";
124 msg = "Protocol family not supported";
127 msg = "Address family not supported by protocol";
130 msg = "Address already in use";
133 msg = "Cannot assign requested address";
136 msg = "Network is down";
139 msg = "Network is unreachable";
142 msg = "Network dropped connection on reset";
145 msg = "Software caused connection abort";
148 msg = "Connection reset by peer";
151 msg = "No buffer space available";
154 msg = "Transport endpoint is already connected";
157 msg = "Transport endpoint is not connected";
160 msg = "Cannot send after transport endpoint shutdown";
163 msg = "Too many references: cannot splice";
166 msg = "Connection timed out";
169 msg = "Connection refused";
172 msg = "Too many levels of symbolic links";
175 msg = "Host is down";
178 msg = "No route to host";
181 msg = "Too many processes";
184 msg = "Too many users";
187 msg = "Disk quota exceeded";
190 msg = "Stale NFS file handle";
193 msg = "Object is remote";
196 /* WSA_INVALID_HANDLE maps to EBADF */
197 /* WSA_NOT_ENOUGH_MEMORY maps to ENOMEM */
198 /* WSA_INVALID_PARAMETER maps to EINVAL */
199 case WSA_OPERATION_ABORTED:
200 msg = "Overlapped operation aborted";
202 case WSA_IO_INCOMPLETE:
203 msg = "Overlapped I/O event object not in signaled state";
206 msg = "Overlapped operations will complete later";
208 /* WSAEINTR maps to EINTR */
209 /* WSAEBADF maps to EBADF */
210 /* WSAEACCES maps to EACCES */
211 /* WSAEFAULT maps to EFAULT */
212 /* WSAEINVAL maps to EINVAL */
213 /* WSAEMFILE maps to EMFILE */
214 /* WSAEWOULDBLOCK maps to EWOULDBLOCK */
215 /* WSAEINPROGRESS is EINPROGRESS */
216 /* WSAEALREADY is EALREADY */
217 /* WSAENOTSOCK is ENOTSOCK */
218 /* WSAEDESTADDRREQ is EDESTADDRREQ */
219 /* WSAEMSGSIZE is EMSGSIZE */
220 /* WSAEPROTOTYPE is EPROTOTYPE */
221 /* WSAENOPROTOOPT is ENOPROTOOPT */
222 /* WSAEPROTONOSUPPORT is EPROTONOSUPPORT */
223 /* WSAESOCKTNOSUPPORT is ESOCKTNOSUPPORT */
224 /* WSAEOPNOTSUPP is EOPNOTSUPP */
225 /* WSAEPFNOSUPPORT is EPFNOSUPPORT */
226 /* WSAEAFNOSUPPORT is EAFNOSUPPORT */
227 /* WSAEADDRINUSE is EADDRINUSE */
228 /* WSAEADDRNOTAVAIL is EADDRNOTAVAIL */
229 /* WSAENETDOWN is ENETDOWN */
230 /* WSAENETUNREACH is ENETUNREACH */
231 /* WSAENETRESET is ENETRESET */
232 /* WSAECONNABORTED is ECONNABORTED */
233 /* WSAECONNRESET is ECONNRESET */
234 /* WSAENOBUFS is ENOBUFS */
235 /* WSAEISCONN is EISCONN */
236 /* WSAENOTCONN is ENOTCONN */
237 /* WSAESHUTDOWN is ESHUTDOWN */
238 /* WSAETOOMANYREFS is ETOOMANYREFS */
239 /* WSAETIMEDOUT is ETIMEDOUT */
240 /* WSAECONNREFUSED is ECONNREFUSED */
241 /* WSAELOOP is ELOOP */
242 /* WSAENAMETOOLONG maps to ENAMETOOLONG */
243 /* WSAEHOSTDOWN is EHOSTDOWN */
244 /* WSAEHOSTUNREACH is EHOSTUNREACH */
245 /* WSAENOTEMPTY maps to ENOTEMPTY */
246 /* WSAEPROCLIM is EPROCLIM */
247 /* WSAEUSERS is EUSERS */
248 /* WSAEDQUOT is EDQUOT */
249 /* WSAESTALE is ESTALE */
250 /* WSAEREMOTE is EREMOTE */
252 msg = "Network subsystem is unavailable";
254 case WSAVERNOTSUPPORTED:
255 msg = "Winsock.dll version out of range";
257 case WSANOTINITIALISED:
258 msg = "Successful WSAStartup not yet performed";
261 msg = "Graceful shutdown in progress";
263 case WSAENOMORE: case WSA_E_NO_MORE:
264 msg = "No more results";
266 case WSAECANCELLED: case WSA_E_CANCELLED:
267 msg = "Call was canceled";
269 case WSAEINVALIDPROCTABLE:
270 msg = "Procedure call table is invalid";
272 case WSAEINVALIDPROVIDER:
273 msg = "Service provider is invalid";
275 case WSAEPROVIDERFAILEDINIT:
276 msg = "Service provider failed to initialize";
278 case WSASYSCALLFAILURE:
279 msg = "System call failure";
281 case WSASERVICE_NOT_FOUND:
282 msg = "Service not found";
284 case WSATYPE_NOT_FOUND:
285 msg = "Class type not found";
288 msg = "Database query was refused";
290 case WSAHOST_NOT_FOUND:
291 msg = "Host not found";
294 msg = "Nonauthoritative host not found";
297 msg = "Nonrecoverable error";
300 msg = "Valid name, no data record of requested type";
302 /* WSA_QOS_* omitted */
306 # if GNULIB_defined_ENOMSG
308 msg = "No message of desired type";
312 # if GNULIB_defined_EIDRM
314 msg = "Identifier removed";
318 # if GNULIB_defined_ENOLINK
320 msg = "Link has been severed";
324 # if GNULIB_defined_EPROTO
326 msg = "Protocol error";
330 # if GNULIB_defined_EMULTIHOP
332 msg = "Multihop attempted";
336 # if GNULIB_defined_EBADMSG
342 # if GNULIB_defined_EOVERFLOW
344 msg = "Value too large for defined data type";
348 # if GNULIB_defined_ENOTSUP
350 msg = "Not supported";
354 # if GNULIB_defined_ESTALE
356 msg = "Stale NFS file handle";
360 # if GNULIB_defined_EDQUOT
362 msg = "Disk quota exceeded";
366 # if GNULIB_defined_ECANCELED
368 msg = "Operation canceled";
375 size_t len = strlen (msg);
379 memcpy (buf, msg, len + 1);
391 #if USE_SYSTEM_STRERROR_R
393 if (buflen > INT_MAX)
397 /* On HP-UX 11.31, strerror_r always fails when buflen < 80. */
401 if (buflen < sizeof (stackbuf))
403 ret = strerror_r (errnum, stackbuf, sizeof (stackbuf));
406 size_t len = strlen (stackbuf);
409 memcpy (buf, stackbuf, len + 1);
415 ret = strerror_r (errnum, buf, buflen);
417 # elif defined __CYGWIN__
418 /* Cygwin only provides the glibc interface, is thread-safe, and
419 always succeeds (although it may truncate). */
420 strerror_r (errnum, buf, buflen);
423 ret = strerror_r (errnum, buf, buflen);
427 /* On AIX 6.1, strerror_r returns -1 and sets errno to EINVAL
429 if (ret < 0 && errno == EINVAL && buflen <= 1)
431 /* Retry with a larger buffer. */
433 ret = strerror_r (errnum, largerbuf, sizeof (largerbuf));
434 if (ret < 0 && errno == EINVAL)
436 /* errnum was out of range. */
441 /* buf was too small. */
447 /* Some old implementations may return (-1, EINVAL) instead of EINVAL. */
451 /* FreeBSD rejects 0; see http://austingroupbugs.net/view.php?id=382. */
452 if (errnum == 0 && ret == EINVAL)
454 if (buflen <= strlen ("Success"))
463 strcpy (buf, "Success");
467 #elif USE_XPG_STRERROR_R
470 extern int __xpg_strerror_r (int errnum, char *buf, size_t buflen);
472 ret = __xpg_strerror_r (errnum, buf, buflen);
477 #else /* USE_SYSTEM_STRERROR */
479 gl_lock_lock (strerror_lock);
482 char *errmsg = strerror (errnum);
484 /* For invalid error numbers, strerror() on
485 - IRIX 6.5 returns NULL,
486 - HP-UX 11 returns an empty string. */
487 if (errmsg == NULL || *errmsg == '\0')
491 size_t len = strlen (errmsg);
495 memcpy (buf, errmsg, len + 1);
503 gl_lock_unlock (strerror_lock);