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)
69 char const *msg = NULL;
70 /* These error messages are taken from glibc/sysdeps/gnu/errlist.c. */
73 # if GNULIB_defined_ETXTBSY
75 msg = "Text file busy";
79 # if GNULIB_defined_ESOCK /* native Windows platforms */
80 /* EWOULDBLOCK is the same as EAGAIN. */
82 msg = "Operation now in progress";
85 msg = "Operation already in progress";
88 msg = "Socket operation on non-socket";
91 msg = "Destination address required";
94 msg = "Message too long";
97 msg = "Protocol wrong type for socket";
100 msg = "Protocol not available";
102 case EPROTONOSUPPORT:
103 msg = "Protocol not supported";
105 case ESOCKTNOSUPPORT:
106 msg = "Socket type not supported";
109 msg = "Operation not supported";
112 msg = "Protocol family not supported";
115 msg = "Address family not supported by protocol";
118 msg = "Address already in use";
121 msg = "Cannot assign requested address";
124 msg = "Network is down";
127 msg = "Network is unreachable";
130 msg = "Network dropped connection on reset";
133 msg = "Software caused connection abort";
136 msg = "Connection reset by peer";
139 msg = "No buffer space available";
142 msg = "Transport endpoint is already connected";
145 msg = "Transport endpoint is not connected";
148 msg = "Cannot send after transport endpoint shutdown";
151 msg = "Too many references: cannot splice";
154 msg = "Connection timed out";
157 msg = "Connection refused";
160 msg = "Too many levels of symbolic links";
163 msg = "Host is down";
166 msg = "No route to host";
169 msg = "Too many processes";
172 msg = "Too many users";
175 msg = "Disk quota exceeded";
178 msg = "Stale NFS file handle";
181 msg = "Object is remote";
184 /* WSA_INVALID_HANDLE maps to EBADF */
185 /* WSA_NOT_ENOUGH_MEMORY maps to ENOMEM */
186 /* WSA_INVALID_PARAMETER maps to EINVAL */
187 case WSA_OPERATION_ABORTED:
188 msg = "Overlapped operation aborted";
190 case WSA_IO_INCOMPLETE:
191 msg = "Overlapped I/O event object not in signaled state";
194 msg = "Overlapped operations will complete later";
196 /* WSAEINTR maps to EINTR */
197 /* WSAEBADF maps to EBADF */
198 /* WSAEACCES maps to EACCES */
199 /* WSAEFAULT maps to EFAULT */
200 /* WSAEINVAL maps to EINVAL */
201 /* WSAEMFILE maps to EMFILE */
202 /* WSAEWOULDBLOCK maps to EWOULDBLOCK */
203 /* WSAEINPROGRESS is EINPROGRESS */
204 /* WSAEALREADY is EALREADY */
205 /* WSAENOTSOCK is ENOTSOCK */
206 /* WSAEDESTADDRREQ is EDESTADDRREQ */
207 /* WSAEMSGSIZE is EMSGSIZE */
208 /* WSAEPROTOTYPE is EPROTOTYPE */
209 /* WSAENOPROTOOPT is ENOPROTOOPT */
210 /* WSAEPROTONOSUPPORT is EPROTONOSUPPORT */
211 /* WSAESOCKTNOSUPPORT is ESOCKTNOSUPPORT */
212 /* WSAEOPNOTSUPP is EOPNOTSUPP */
213 /* WSAEPFNOSUPPORT is EPFNOSUPPORT */
214 /* WSAEAFNOSUPPORT is EAFNOSUPPORT */
215 /* WSAEADDRINUSE is EADDRINUSE */
216 /* WSAEADDRNOTAVAIL is EADDRNOTAVAIL */
217 /* WSAENETDOWN is ENETDOWN */
218 /* WSAENETUNREACH is ENETUNREACH */
219 /* WSAENETRESET is ENETRESET */
220 /* WSAECONNABORTED is ECONNABORTED */
221 /* WSAECONNRESET is ECONNRESET */
222 /* WSAENOBUFS is ENOBUFS */
223 /* WSAEISCONN is EISCONN */
224 /* WSAENOTCONN is ENOTCONN */
225 /* WSAESHUTDOWN is ESHUTDOWN */
226 /* WSAETOOMANYREFS is ETOOMANYREFS */
227 /* WSAETIMEDOUT is ETIMEDOUT */
228 /* WSAECONNREFUSED is ECONNREFUSED */
229 /* WSAELOOP is ELOOP */
230 /* WSAENAMETOOLONG maps to ENAMETOOLONG */
231 /* WSAEHOSTDOWN is EHOSTDOWN */
232 /* WSAEHOSTUNREACH is EHOSTUNREACH */
233 /* WSAENOTEMPTY maps to ENOTEMPTY */
234 /* WSAEPROCLIM is EPROCLIM */
235 /* WSAEUSERS is EUSERS */
236 /* WSAEDQUOT is EDQUOT */
237 /* WSAESTALE is ESTALE */
238 /* WSAEREMOTE is EREMOTE */
240 msg = "Network subsystem is unavailable";
242 case WSAVERNOTSUPPORTED:
243 msg = "Winsock.dll version out of range";
245 case WSANOTINITIALISED:
246 msg = "Successful WSAStartup not yet performed";
249 msg = "Graceful shutdown in progress";
251 case WSAENOMORE: case WSA_E_NO_MORE:
252 msg = "No more results";
254 case WSAECANCELLED: case WSA_E_CANCELLED:
255 msg = "Call was canceled";
257 case WSAEINVALIDPROCTABLE:
258 msg = "Procedure call table is invalid";
260 case WSAEINVALIDPROVIDER:
261 msg = "Service provider is invalid";
263 case WSAEPROVIDERFAILEDINIT:
264 msg = "Service provider failed to initialize";
266 case WSASYSCALLFAILURE:
267 msg = "System call failure";
269 case WSASERVICE_NOT_FOUND:
270 msg = "Service not found";
272 case WSATYPE_NOT_FOUND:
273 msg = "Class type not found";
276 msg = "Database query was refused";
278 case WSAHOST_NOT_FOUND:
279 msg = "Host not found";
282 msg = "Nonauthoritative host not found";
285 msg = "Nonrecoverable error";
288 msg = "Valid name, no data record of requested type";
290 /* WSA_QOS_* omitted */
294 # if GNULIB_defined_ENOMSG
296 msg = "No message of desired type";
300 # if GNULIB_defined_EIDRM
302 msg = "Identifier removed";
306 # if GNULIB_defined_ENOLINK
308 msg = "Link has been severed";
312 # if GNULIB_defined_EPROTO
314 msg = "Protocol error";
318 # if GNULIB_defined_EMULTIHOP
320 msg = "Multihop attempted";
324 # if GNULIB_defined_EBADMSG
330 # if GNULIB_defined_EOVERFLOW
332 msg = "Value too large for defined data type";
336 # if GNULIB_defined_ENOTSUP
338 msg = "Not supported";
342 # if GNULIB_defined_ESTALE
344 msg = "Stale NFS file handle";
348 # if GNULIB_defined_EDQUOT
350 msg = "Disk quota exceeded";
354 # if GNULIB_defined_ECANCELED
356 msg = "Operation canceled";
363 size_t len = strlen (msg);
367 memcpy (buf, msg, len + 1);
379 #if USE_SYSTEM_STRERROR_R
381 if (buflen > INT_MAX)
385 /* On HP-UX 11.31, strerror_r always fails when buflen < 80. */
389 if (buflen < sizeof (stackbuf))
391 ret = strerror_r (errnum, stackbuf, sizeof (stackbuf));
394 size_t len = strlen (stackbuf);
397 memcpy (buf, stackbuf, len + 1);
403 ret = strerror_r (errnum, buf, buflen);
405 # elif defined __CYGWIN__
406 /* Cygwin only provides the glibc interface, is thread-safe, and
407 always succeeds (although it may truncate). */
408 strerror_r (errnum, buf, buflen);
411 ret = strerror_r (errnum, buf, buflen);
415 /* On AIX 6.1, strerror_r returns -1 and sets errno to EINVAL
417 if (ret < 0 && errno == EINVAL && buflen <= 1)
419 /* Retry with a larger buffer. */
421 ret = strerror_r (errnum, largerbuf, sizeof (largerbuf));
422 if (ret < 0 && errno == EINVAL)
424 /* errnum was out of range. */
429 /* buf was too small. */
435 /* Some old implementations may return (-1, EINVAL) instead of EINVAL. */
439 /* FreeBSD rejects 0; see http://austingroupbugs.net/view.php?id=382. */
440 if (errnum == 0 && ret == EINVAL)
442 if (buflen <= strlen ("Success"))
451 strcpy (buf, "Success");
455 #elif USE_XPG_STRERROR_R
458 extern int __xpg_strerror_r (int errnum, char *buf, size_t buflen);
460 ret = __xpg_strerror_r (errnum, buf, buflen);
465 #else /* USE_SYSTEM_STRERROR */
467 gl_lock_lock (strerror_lock);
470 char *errmsg = strerror (errnum);
472 /* For invalid error numbers, strerror() on
473 - IRIX 6.5 returns NULL,
474 - HP-UX 11 returns an empty string. */
475 if (errmsg == NULL || *errmsg == '\0')
479 size_t len = strlen (errmsg);
483 memcpy (buf, errmsg, len + 1);
491 gl_lock_unlock (strerror_lock);