1 /* strerror_r.c --- POSIX compatible system error routine
3 Copyright (C) 2010-2017 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
35 #include "strerror-override.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
44 int __xpg_strerror_r (int errnum
, char *buf
, size_t buflen
);
46 #elif HAVE_DECL_STRERROR_R && !(__GLIBC__ >= 2 || defined __UCLIBC__ || defined __CYGWIN__)
48 /* The system's strerror_r function is OK, except that its third argument
49 is 'int', not 'size_t', or its return type is wrong. */
53 # define USE_SYSTEM_STRERROR_R 1
55 #else /* (__GLIBC__ >= 2 || defined __UCLIBC__ || defined __CYGWIN__ ? !HAVE___XPG_STRERROR_R : !HAVE_DECL_STRERROR_R) */
57 /* Use the system's strerror(). Exclude glibc and cygwin because the
58 system strerror_r has the wrong return type, and cygwin 1.7.9
59 strerror_r clobbers strerror. */
62 # define USE_SYSTEM_STRERROR 1
64 # if defined __NetBSD__ || defined __hpux || ((defined _WIN32 || defined __WIN32__) && !defined __CYGWIN__) || defined __sgi || (defined __sun && !defined _LP64) || defined __CYGWIN__
66 /* No locking needed. */
68 /* Get catgets internationalization functions. */
70 # include <nl_types.h>
77 /* Get sys_nerr, sys_errlist on HP-UX (otherwise only declared in C++ mode).
78 Get sys_nerr, sys_errlist on IRIX (otherwise only declared with _SGIAPI). */
79 # if defined __hpux || defined __sgi
81 extern char *sys_errlist
[];
84 /* Get sys_nerr on Solaris. */
85 # if defined __sun && !defined _LP64
95 # include "glthread/lock.h"
97 /* This lock protects the buffer returned by strerror(). We assume that
98 no other uses of strerror() exist in the program. */
99 gl_lock_define_initialized(static, strerror_lock
)
105 /* On MSVC, there is no snprintf() function, just a _snprintf().
106 It is of lower quality, but sufficient for the simple use here.
107 We only have to make sure to NUL terminate the result (_snprintf
108 does not NUL terminate, like strncpy). */
111 local_snprintf (char *buf
, size_t buflen
, const char *format
, ...)
116 va_start (args
, format
);
117 result
= _vsnprintf (buf
, buflen
, format
, args
);
119 if (buflen
> 0 && (result
< 0 || result
>= buflen
))
120 buf
[buflen
- 1] = '\0';
123 # define snprintf local_snprintf
126 /* Copy as much of MSG into BUF as possible, without corrupting errno.
127 Return 0 if MSG fit in BUFLEN, otherwise return ERANGE. */
129 safe_copy (char *buf
, size_t buflen
, const char *msg
)
131 size_t len
= strlen (msg
);
136 /* Although POSIX allows memcpy() to corrupt errno, we don't
137 know of any implementation where this is a real problem. */
138 memcpy (buf
, msg
, len
+ 1);
143 memcpy (buf
, msg
, buflen
- 1);
144 buf
[buflen
- 1] = '\0';
152 strerror_r (int errnum
, char *buf
, size_t buflen
)
155 /* Filter this out now, so that rest of this replacement knows that
156 there is room for a non-empty message and trailing NUL. */
165 /* Check for gnulib overrides. */
167 char const *msg
= strerror_override (errnum
);
170 return safe_copy (buf
, buflen
, msg
);
175 int saved_errno
= errno
;
177 #if USE_XPG_STRERROR_R
180 ret
= __xpg_strerror_r (errnum
, buf
, buflen
);
185 /* glibc 2.13 would not touch buf on err, so we have to fall
186 back to GNU strerror_r which always returns a thread-safe
187 untruncated string to (partially) copy into our buf. */
188 safe_copy (buf
, buflen
, strerror_r (errnum
, buf
, buflen
));
192 #elif USE_SYSTEM_STRERROR_R
194 if (buflen
> INT_MAX
)
198 /* On HP-UX 11.31, strerror_r always fails when buflen < 80; it
199 also fails to change buf on EINVAL. */
203 if (buflen
< sizeof stackbuf
)
205 ret
= strerror_r (errnum
, stackbuf
, sizeof stackbuf
);
207 ret
= safe_copy (buf
, buflen
, stackbuf
);
210 ret
= strerror_r (errnum
, buf
, buflen
);
213 ret
= strerror_r (errnum
, buf
, buflen
);
215 /* Some old implementations may return (-1, EINVAL) instead of EINVAL. */
221 /* AIX returns 0 rather than ERANGE when truncating strings; try
222 again until we are sure we got the entire string. */
223 if (!ret
&& strlen (buf
) == buflen
- 1)
225 char stackbuf
[STACKBUF_LEN
];
227 strerror_r (errnum
, stackbuf
, sizeof stackbuf
);
228 len
= strlen (stackbuf
);
229 /* STACKBUF_LEN should have been large enough. */
230 if (len
+ 1 == sizeof stackbuf
)
236 /* Solaris 10 does not populate buf on ERANGE. OpenBSD 4.7
237 truncates early on ERANGE rather than return a partial integer.
238 We prefer the maximal string. We set buf[0] earlier, and we
239 know of no implementation that modifies buf to be an
240 unterminated string, so this strlen should be portable in
241 practice (rather than pulling in a safer strnlen). */
242 if (ret
== ERANGE
&& strlen (buf
) < buflen
- 1)
244 char stackbuf
[STACKBUF_LEN
];
246 /* STACKBUF_LEN should have been large enough. */
247 if (strerror_r (errnum
, stackbuf
, sizeof stackbuf
) == ERANGE
)
249 safe_copy (buf
, buflen
, stackbuf
);
253 #else /* USE_SYSTEM_STRERROR */
255 /* Try to do what strerror (errnum) does, but without clobbering the
256 buffer used by strerror(). */
258 # if defined __NetBSD__ || defined __hpux || ((defined _WIN32 || defined __WIN32__) && !defined __CYGWIN__) || defined __CYGWIN__ /* NetBSD, HP-UX, native Windows, Cygwin */
260 /* NetBSD: sys_nerr, sys_errlist are declared through _NETBSD_SOURCE
262 HP-UX: sys_nerr, sys_errlist are declared explicitly above.
263 native Windows: sys_nerr, sys_errlist are declared in <stdlib.h>.
264 Cygwin: sys_nerr, sys_errlist are declared in <errno.h>. */
265 if (errnum
>= 0 && errnum
< sys_nerr
)
267 # if HAVE_CATGETS && (defined __NetBSD__ || defined __hpux)
268 # if defined __NetBSD__
269 nl_catd catd
= catopen ("libc", NL_CAT_LOCALE
);
272 ? catgets (catd
, 1, errnum
, sys_errlist
[errnum
])
273 : sys_errlist
[errnum
]);
276 nl_catd catd
= catopen ("perror", NL_CAT_LOCALE
);
279 ? catgets (catd
, 1, 1 + errnum
, sys_errlist
[errnum
])
280 : sys_errlist
[errnum
]);
283 const char *errmsg
= sys_errlist
[errnum
];
285 if (errmsg
== NULL
|| *errmsg
== '\0')
288 ret
= safe_copy (buf
, buflen
, errmsg
);
289 # if HAVE_CATGETS && (defined __NetBSD__ || defined __hpux)
290 if (catd
!= (nl_catd
)-1)
297 # elif defined __sgi || (defined __sun && !defined _LP64) /* IRIX, Solaris <= 9 32-bit */
299 /* For a valid error number, the system's strerror() function returns
300 a pointer to a not copied string, not to a buffer. */
301 if (errnum
>= 0 && errnum
< sys_nerr
)
303 char *errmsg
= strerror (errnum
);
305 if (errmsg
== NULL
|| *errmsg
== '\0')
308 ret
= safe_copy (buf
, buflen
, errmsg
);
315 gl_lock_lock (strerror_lock
);
318 char *errmsg
= strerror (errnum
);
320 /* For invalid error numbers, strerror() on
321 - IRIX 6.5 returns NULL,
322 - HP-UX 11 returns an empty string. */
323 if (errmsg
== NULL
|| *errmsg
== '\0')
326 ret
= safe_copy (buf
, buflen
, errmsg
);
329 gl_lock_unlock (strerror_lock
);
335 #if (defined _WIN32 || defined __WIN32__) && !defined __CYGWIN__
336 /* MSVC 14 defines names for many error codes in the range 100..140,
337 but _sys_errlist contains strings only for the error codes
345 case 100 /* EADDRINUSE */:
346 errmsg
= "Address already in use";
348 case 101 /* EADDRNOTAVAIL */:
349 errmsg
= "Cannot assign requested address";
351 case 102 /* EAFNOSUPPORT */:
352 errmsg
= "Address family not supported by protocol";
354 case 103 /* EALREADY */:
355 errmsg
= "Operation already in progress";
357 case 105 /* ECANCELED */:
358 errmsg
= "Operation canceled";
360 case 106 /* ECONNABORTED */:
361 errmsg
= "Software caused connection abort";
363 case 107 /* ECONNREFUSED */:
364 errmsg
= "Connection refused";
366 case 108 /* ECONNRESET */:
367 errmsg
= "Connection reset by peer";
369 case 109 /* EDESTADDRREQ */:
370 errmsg
= "Destination address required";
372 case 110 /* EHOSTUNREACH */:
373 errmsg
= "No route to host";
375 case 112 /* EINPROGRESS */:
376 errmsg
= "Operation now in progress";
378 case 113 /* EISCONN */:
379 errmsg
= "Transport endpoint is already connected";
381 case 114 /* ELOOP */:
382 errmsg
= "Too many levels of symbolic links";
384 case 115 /* EMSGSIZE */:
385 errmsg
= "Message too long";
387 case 116 /* ENETDOWN */:
388 errmsg
= "Network is down";
390 case 117 /* ENETRESET */:
391 errmsg
= "Network dropped connection on reset";
393 case 118 /* ENETUNREACH */:
394 errmsg
= "Network is unreachable";
396 case 119 /* ENOBUFS */:
397 errmsg
= "No buffer space available";
399 case 123 /* ENOPROTOOPT */:
400 errmsg
= "Protocol not available";
402 case 126 /* ENOTCONN */:
403 errmsg
= "Transport endpoint is not connected";
405 case 128 /* ENOTSOCK */:
406 errmsg
= "Socket operation on non-socket";
408 case 129 /* ENOTSUP */:
409 errmsg
= "Not supported";
411 case 130 /* EOPNOTSUPP */:
412 errmsg
= "Operation not supported";
414 case 132 /* EOVERFLOW */:
415 errmsg
= "Value too large for defined data type";
417 case 133 /* EOWNERDEAD */:
418 errmsg
= "Owner died";
420 case 134 /* EPROTO */:
421 errmsg
= "Protocol error";
423 case 135 /* EPROTONOSUPPORT */:
424 errmsg
= "Protocol not supported";
426 case 136 /* EPROTOTYPE */:
427 errmsg
= "Protocol wrong type for socket";
429 case 138 /* ETIMEDOUT */:
430 errmsg
= "Connection timed out";
432 case 140 /* EWOULDBLOCK */:
433 errmsg
= "Operation would block";
440 ret
= safe_copy (buf
, buflen
, errmsg
);
444 if (ret
== EINVAL
&& !*buf
)
445 snprintf (buf
, buflen
, "Unknown error %d", errnum
);