summaryrefslogtreecommitdiffstats
path: root/gl/strerror.c
diff options
context:
space:
mode:
Diffstat (limited to 'gl/strerror.c')
-rw-r--r--gl/strerror.c354
1 files changed, 37 insertions, 317 deletions
diff --git a/gl/strerror.c b/gl/strerror.c
index 46153ab..80a2f2e 100644
--- a/gl/strerror.c
+++ b/gl/strerror.c
@@ -1,6 +1,6 @@
1/* strerror.c --- POSIX compatible system error routine 1/* strerror.c --- POSIX compatible system error routine
2 2
3 Copyright (C) 2007-2010 Free Software Foundation, Inc. 3 Copyright (C) 2007-2013 Free Software Foundation, Inc.
4 4
5 This program is free software: you can redistribute it and/or modify 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 6 it under the terms of the GNU General Public License as published by
@@ -17,334 +17,54 @@
17 17
18#include <config.h> 18#include <config.h>
19 19
20/* Specification. */
20#include <string.h> 21#include <string.h>
21 22
22#if REPLACE_STRERROR 23#include <errno.h>
23 24#include <stdio.h>
24# include <errno.h> 25#include <stdlib.h>
25# include <stdio.h> 26#include <string.h>
26
27# if GNULIB_defined_ESOCK /* native Windows platforms */
28# if HAVE_WINSOCK2_H
29# include <winsock2.h>
30# endif
31# endif
32 27
33# include "intprops.h" 28#include "intprops.h"
29#include "strerror-override.h"
30#include "verify.h"
34 31
35/* Use the system functions, not the gnulib overrides in this file. */ 32/* Use the system functions, not the gnulib overrides in this file. */
36# undef sprintf 33#undef sprintf
37
38# undef strerror
39# if ! HAVE_DECL_STRERROR
40# define strerror(n) NULL
41# endif
42 34
43char * 35char *
44rpl_strerror (int n) 36strerror (int n)
37#undef strerror
45{ 38{
46 char const *msg = NULL; 39 static char buf[STACKBUF_LEN];
47 /* These error messages are taken from glibc/sysdeps/gnu/errlist.c. */ 40 size_t len;
48 switch (n)
49 {
50# if GNULIB_defined_ETXTBSY
51 case ETXTBSY:
52 msg = "Text file busy";
53 break;
54# endif
55
56# if GNULIB_defined_ESOCK /* native Windows platforms */
57 /* EWOULDBLOCK is the same as EAGAIN. */
58 case EINPROGRESS:
59 msg = "Operation now in progress";
60 break;
61 case EALREADY:
62 msg = "Operation already in progress";
63 break;
64 case ENOTSOCK:
65 msg = "Socket operation on non-socket";
66 break;
67 case EDESTADDRREQ:
68 msg = "Destination address required";
69 break;
70 case EMSGSIZE:
71 msg = "Message too long";
72 break;
73 case EPROTOTYPE:
74 msg = "Protocol wrong type for socket";
75 break;
76 case ENOPROTOOPT:
77 msg = "Protocol not available";
78 break;
79 case EPROTONOSUPPORT:
80 msg = "Protocol not supported";
81 break;
82 case ESOCKTNOSUPPORT:
83 msg = "Socket type not supported";
84 break;
85 case EOPNOTSUPP:
86 msg = "Operation not supported";
87 break;
88 case EPFNOSUPPORT:
89 msg = "Protocol family not supported";
90 break;
91 case EAFNOSUPPORT:
92 msg = "Address family not supported by protocol";
93 break;
94 case EADDRINUSE:
95 msg = "Address already in use";
96 break;
97 case EADDRNOTAVAIL:
98 msg = "Cannot assign requested address";
99 break;
100 case ENETDOWN:
101 msg = "Network is down";
102 break;
103 case ENETUNREACH:
104 msg = "Network is unreachable";
105 break;
106 case ENETRESET:
107 msg = "Network dropped connection on reset";
108 break;
109 case ECONNABORTED:
110 msg = "Software caused connection abort";
111 break;
112 case ECONNRESET:
113 msg = "Connection reset by peer";
114 break;
115 case ENOBUFS:
116 msg = "No buffer space available";
117 break;
118 case EISCONN:
119 msg = "Transport endpoint is already connected";
120 break;
121 case ENOTCONN:
122 msg = "Transport endpoint is not connected";
123 break;
124 case ESHUTDOWN:
125 msg = "Cannot send after transport endpoint shutdown";
126 break;
127 case ETOOMANYREFS:
128 msg = "Too many references: cannot splice";
129 break;
130 case ETIMEDOUT:
131 msg = "Connection timed out";
132 break;
133 case ECONNREFUSED:
134 msg = "Connection refused";
135 break;
136 case ELOOP:
137 msg = "Too many levels of symbolic links";
138 break;
139 case EHOSTDOWN:
140 msg = "Host is down";
141 break;
142 case EHOSTUNREACH:
143 msg = "No route to host";
144 break;
145 case EPROCLIM:
146 msg = "Too many processes";
147 break;
148 case EUSERS:
149 msg = "Too many users";
150 break;
151 case EDQUOT:
152 msg = "Disk quota exceeded";
153 break;
154 case ESTALE:
155 msg = "Stale NFS file handle";
156 break;
157 case EREMOTE:
158 msg = "Object is remote";
159 break;
160# if HAVE_WINSOCK2_H
161 /* WSA_INVALID_HANDLE maps to EBADF */
162 /* WSA_NOT_ENOUGH_MEMORY maps to ENOMEM */
163 /* WSA_INVALID_PARAMETER maps to EINVAL */
164 case WSA_OPERATION_ABORTED:
165 msg = "Overlapped operation aborted";
166 break;
167 case WSA_IO_INCOMPLETE:
168 msg = "Overlapped I/O event object not in signaled state";
169 break;
170 case WSA_IO_PENDING:
171 msg = "Overlapped operations will complete later";
172 break;
173 /* WSAEINTR maps to EINTR */
174 /* WSAEBADF maps to EBADF */
175 /* WSAEACCES maps to EACCES */
176 /* WSAEFAULT maps to EFAULT */
177 /* WSAEINVAL maps to EINVAL */
178 /* WSAEMFILE maps to EMFILE */
179 /* WSAEWOULDBLOCK maps to EWOULDBLOCK */
180 /* WSAEINPROGRESS is EINPROGRESS */
181 /* WSAEALREADY is EALREADY */
182 /* WSAENOTSOCK is ENOTSOCK */
183 /* WSAEDESTADDRREQ is EDESTADDRREQ */
184 /* WSAEMSGSIZE is EMSGSIZE */
185 /* WSAEPROTOTYPE is EPROTOTYPE */
186 /* WSAENOPROTOOPT is ENOPROTOOPT */
187 /* WSAEPROTONOSUPPORT is EPROTONOSUPPORT */
188 /* WSAESOCKTNOSUPPORT is ESOCKTNOSUPPORT */
189 /* WSAEOPNOTSUPP is EOPNOTSUPP */
190 /* WSAEPFNOSUPPORT is EPFNOSUPPORT */
191 /* WSAEAFNOSUPPORT is EAFNOSUPPORT */
192 /* WSAEADDRINUSE is EADDRINUSE */
193 /* WSAEADDRNOTAVAIL is EADDRNOTAVAIL */
194 /* WSAENETDOWN is ENETDOWN */
195 /* WSAENETUNREACH is ENETUNREACH */
196 /* WSAENETRESET is ENETRESET */
197 /* WSAECONNABORTED is ECONNABORTED */
198 /* WSAECONNRESET is ECONNRESET */
199 /* WSAENOBUFS is ENOBUFS */
200 /* WSAEISCONN is EISCONN */
201 /* WSAENOTCONN is ENOTCONN */
202 /* WSAESHUTDOWN is ESHUTDOWN */
203 /* WSAETOOMANYREFS is ETOOMANYREFS */
204 /* WSAETIMEDOUT is ETIMEDOUT */
205 /* WSAECONNREFUSED is ECONNREFUSED */
206 /* WSAELOOP is ELOOP */
207 /* WSAENAMETOOLONG maps to ENAMETOOLONG */
208 /* WSAEHOSTDOWN is EHOSTDOWN */
209 /* WSAEHOSTUNREACH is EHOSTUNREACH */
210 /* WSAENOTEMPTY maps to ENOTEMPTY */
211 /* WSAEPROCLIM is EPROCLIM */
212 /* WSAEUSERS is EUSERS */
213 /* WSAEDQUOT is EDQUOT */
214 /* WSAESTALE is ESTALE */
215 /* WSAEREMOTE is EREMOTE */
216 case WSASYSNOTREADY:
217 msg = "Network subsystem is unavailable";
218 break;
219 case WSAVERNOTSUPPORTED:
220 msg = "Winsock.dll version out of range";
221 break;
222 case WSANOTINITIALISED:
223 msg = "Successful WSAStartup not yet performed";
224 break;
225 case WSAEDISCON:
226 msg = "Graceful shutdown in progress";
227 break;
228 case WSAENOMORE: case WSA_E_NO_MORE:
229 msg = "No more results";
230 break;
231 case WSAECANCELLED: case WSA_E_CANCELLED:
232 msg = "Call was canceled";
233 break;
234 case WSAEINVALIDPROCTABLE:
235 msg = "Procedure call table is invalid";
236 break;
237 case WSAEINVALIDPROVIDER:
238 msg = "Service provider is invalid";
239 break;
240 case WSAEPROVIDERFAILEDINIT:
241 msg = "Service provider failed to initialize";
242 break;
243 case WSASYSCALLFAILURE:
244 msg = "System call failure";
245 break;
246 case WSASERVICE_NOT_FOUND:
247 msg = "Service not found";
248 break;
249 case WSATYPE_NOT_FOUND:
250 msg = "Class type not found";
251 break;
252 case WSAEREFUSED:
253 msg = "Database query was refused";
254 break;
255 case WSAHOST_NOT_FOUND:
256 msg = "Host not found";
257 break;
258 case WSATRY_AGAIN:
259 msg = "Nonauthoritative host not found";
260 break;
261 case WSANO_RECOVERY:
262 msg = "Nonrecoverable error";
263 break;
264 case WSANO_DATA:
265 msg = "Valid name, no data record of requested type";
266 break;
267 /* WSA_QOS_* omitted */
268# endif
269# endif
270
271# if GNULIB_defined_ENOMSG
272 case ENOMSG:
273 msg = "No message of desired type";
274 break;
275# endif
276
277# if GNULIB_defined_EIDRM
278 case EIDRM:
279 msg = "Identifier removed";
280 break;
281# endif
282
283# if GNULIB_defined_ENOLINK
284 case ENOLINK:
285 msg = "Link has been severed";
286 break;
287# endif
288
289# if GNULIB_defined_EPROTO
290 case EPROTO:
291 msg = "Protocol error";
292 break;
293# endif
294
295# if GNULIB_defined_EMULTIHOP
296 case EMULTIHOP:
297 msg = "Multihop attempted";
298 break;
299# endif
300
301# if GNULIB_defined_EBADMSG
302 case EBADMSG:
303 msg = "Bad message";
304 break;
305# endif
306
307# if GNULIB_defined_EOVERFLOW
308 case EOVERFLOW:
309 msg = "Value too large for defined data type";
310 break;
311# endif
312
313# if GNULIB_defined_ENOTSUP
314 case ENOTSUP:
315 msg = "Not supported";
316 break;
317# endif
318
319# if GNULIB_defined_ESTALE
320 case ESTALE:
321 msg = "Stale NFS file handle";
322 break;
323# endif
324
325# if GNULIB_defined_ECANCELED
326 case ECANCELED:
327 msg = "Operation canceled";
328 break;
329# endif
330 }
331 41
42 /* Cast away const, due to the historical signature of strerror;
43 callers should not be modifying the string. */
44 const char *msg = strerror_override (n);
332 if (msg) 45 if (msg)
333 return (char *) msg; 46 return (char *) msg;
334 47
335 { 48 msg = strerror (n);
336 char *result = strerror (n);
337 49
338 if (result == NULL || result[0] == '\0') 50 /* Our strerror_r implementation might use the system's strerror
339 { 51 buffer, so all other clients of strerror have to see the error
340 static char const fmt[] = "Unknown error (%d)"; 52 copied into a buffer that we manage. This is not thread-safe,
341 static char msg_buf[sizeof fmt + INT_STRLEN_BOUND (n)]; 53 even if the system strerror is, but portable programs shouldn't
342 sprintf (msg_buf, fmt, n); 54 be using strerror if they care about thread-safety. */
343 return msg_buf; 55 if (!msg || !*msg)
344 } 56 {
57 static char const fmt[] = "Unknown error %d";
58 verify (sizeof buf >= sizeof (fmt) + INT_STRLEN_BOUND (n));
59 sprintf (buf, fmt, n);
60 errno = EINVAL;
61 return buf;
62 }
345 63
346 return result; 64 /* Fix STACKBUF_LEN if this ever aborts. */
347 } 65 len = strlen (msg);
348} 66 if (sizeof buf <= len)
67 abort ();
349 68
350#endif 69 return memcpy (buf, msg, len + 1);
70}