This source file includes following definitions.
- gl_lock_define_initialized
- safe_copy
- strerror_r
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 #include <config.h>
21
22
23 #define _NETBSD_SOURCE 1
24
25
26 #include <string.h>
27
28 #include <errno.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #if !HAVE_SNPRINTF
32 # include <stdarg.h>
33 #endif
34
35 #include "strerror-override.h"
36
37 #if STRERROR_R_CHAR_P
38
39 # if HAVE___XPG_STRERROR_R
40 _GL_EXTERN_C int __xpg_strerror_r (int errnum, char *buf, size_t buflen);
41 # endif
42
43 #elif HAVE_DECL_STRERROR_R
44
45
46
47
48 # include <limits.h>
49
50 #else
51
52
53
54
55 # undef strerror
56
57 # if defined __NetBSD__ || defined __hpux || (defined _WIN32 && !defined __CYGWIN__) || defined __sgi || (defined __sun && !defined _LP64) || defined __CYGWIN__
58
59
60
61
62 # if HAVE_CATGETS
63 # include <nl_types.h>
64 # endif
65
66 #ifdef __cplusplus
67 extern "C" {
68 #endif
69
70
71
72 # if defined __hpux || defined __sgi
73 extern int sys_nerr;
74 extern char *sys_errlist[];
75 # endif
76
77
78 # if defined __sun && !defined _LP64
79 extern int sys_nerr;
80 # endif
81
82 #ifdef __cplusplus
83 }
84 #endif
85
86 # else
87
88 # include "glthread/lock.h"
89
90
91
92 gl_lock_define_initialized(static, strerror_lock)
93
94 # endif
95
96 #endif
97
98
99
100
101
102 #if !HAVE_SNPRINTF
103 static int
104 local_snprintf (char *buf, size_t buflen, const char *format, ...)
105 {
106 va_list args;
107 int result;
108
109 va_start (args, format);
110 result = _vsnprintf (buf, buflen, format, args);
111 va_end (args);
112 if (buflen > 0 && (result < 0 || result >= buflen))
113 buf[buflen - 1] = '\0';
114 return result;
115 }
116 # undef snprintf
117 # define snprintf local_snprintf
118 #endif
119
120
121
122 static int
123 safe_copy (char *buf, size_t buflen, const char *msg)
124 {
125 size_t len = strlen (msg);
126 size_t moved = len < buflen ? len : buflen - 1;
127
128
129
130 memmove (buf, msg, moved);
131 buf[moved] = '\0';
132 return len < buflen ? 0 : ERANGE;
133 }
134
135
136 int
137 strerror_r (int errnum, char *buf, size_t buflen)
138 #undef strerror_r
139 {
140
141
142 if (buflen <= 1)
143 {
144 if (buflen)
145 *buf = '\0';
146 return ERANGE;
147 }
148 *buf = '\0';
149
150
151 {
152 char const *msg = strerror_override (errnum);
153
154 if (msg)
155 return safe_copy (buf, buflen, msg);
156 }
157
158 {
159 int ret;
160 int saved_errno = errno;
161
162 #if STRERROR_R_CHAR_P
163
164 {
165 ret = 0;
166
167 # if HAVE___XPG_STRERROR_R
168 ret = __xpg_strerror_r (errnum, buf, buflen);
169 if (ret < 0)
170 ret = errno;
171 # endif
172
173 if (!*buf)
174 {
175
176
177
178 char *errstring = strerror_r (errnum, buf, buflen);
179 ret = errstring ? safe_copy (buf, buflen, errstring) : errno;
180 }
181 }
182
183 #elif HAVE_DECL_STRERROR_R
184
185 if (buflen > INT_MAX)
186 buflen = INT_MAX;
187
188 # ifdef __hpux
189
190
191 {
192 char stackbuf[80];
193
194 if (buflen < sizeof stackbuf)
195 {
196 ret = strerror_r (errnum, stackbuf, sizeof stackbuf);
197 if (ret == 0)
198 ret = safe_copy (buf, buflen, stackbuf);
199 }
200 else
201 ret = strerror_r (errnum, buf, buflen);
202 }
203 # else
204 ret = strerror_r (errnum, buf, buflen);
205
206
207
208 # if !defined __HAIKU__
209 if (ret < 0)
210 ret = errno;
211 # endif
212 # endif
213
214 # if defined _AIX || defined __HAIKU__
215
216
217 if (!ret && strlen (buf) == buflen - 1)
218 {
219 char stackbuf[STACKBUF_LEN];
220 size_t len;
221 strerror_r (errnum, stackbuf, sizeof stackbuf);
222 len = strlen (stackbuf);
223
224 if (len + 1 == sizeof stackbuf)
225 abort ();
226 if (buflen <= len)
227 ret = ERANGE;
228 }
229 # else
230
231
232
233
234
235
236 if (ret == ERANGE && strlen (buf) < buflen - 1)
237 {
238 char stackbuf[STACKBUF_LEN];
239
240
241 if (strerror_r (errnum, stackbuf, sizeof stackbuf) == ERANGE)
242 abort ();
243 safe_copy (buf, buflen, stackbuf);
244 }
245 # endif
246
247 #else
248
249
250
251
252 # if defined __NetBSD__ || defined __hpux || (defined _WIN32 && !defined __CYGWIN__) || defined __CYGWIN__
253
254
255
256
257
258
259 if (errnum >= 0 && errnum < sys_nerr)
260 {
261 # if HAVE_CATGETS && (defined __NetBSD__ || defined __hpux)
262 # if defined __NetBSD__
263 nl_catd catd = catopen ("libc", NL_CAT_LOCALE);
264 const char *errmsg =
265 (catd != (nl_catd)-1
266 ? catgets (catd, 1, errnum, sys_errlist[errnum])
267 : sys_errlist[errnum]);
268 # endif
269 # if defined __hpux
270 nl_catd catd = catopen ("perror", NL_CAT_LOCALE);
271 const char *errmsg =
272 (catd != (nl_catd)-1
273 ? catgets (catd, 1, 1 + errnum, sys_errlist[errnum])
274 : sys_errlist[errnum]);
275 # endif
276 # else
277 const char *errmsg = sys_errlist[errnum];
278 # endif
279 if (errmsg == NULL || *errmsg == '\0')
280 ret = EINVAL;
281 else
282 ret = safe_copy (buf, buflen, errmsg);
283 # if HAVE_CATGETS && (defined __NetBSD__ || defined __hpux)
284 if (catd != (nl_catd)-1)
285 catclose (catd);
286 # endif
287 }
288 else
289 ret = EINVAL;
290
291 # elif defined __sgi || (defined __sun && !defined _LP64)
292
293
294
295 if (errnum >= 0 && errnum < sys_nerr)
296 {
297 char *errmsg = strerror (errnum);
298
299 if (errmsg == NULL || *errmsg == '\0')
300 ret = EINVAL;
301 else
302 ret = safe_copy (buf, buflen, errmsg);
303 }
304 else
305 ret = EINVAL;
306
307 # else
308
309 gl_lock_lock (strerror_lock);
310
311 {
312 char *errmsg = strerror (errnum);
313
314
315
316
317 if (errmsg == NULL || *errmsg == '\0')
318 ret = EINVAL;
319 else
320 ret = safe_copy (buf, buflen, errmsg);
321 }
322
323 gl_lock_unlock (strerror_lock);
324
325 # endif
326
327 #endif
328
329 #if defined _WIN32 && !defined __CYGWIN__
330
331
332
333 if (ret == EINVAL)
334 {
335 const char *errmsg;
336
337 switch (errnum)
338 {
339 case 100 :
340 errmsg = "Address already in use";
341 break;
342 case 101 :
343 errmsg = "Cannot assign requested address";
344 break;
345 case 102 :
346 errmsg = "Address family not supported by protocol";
347 break;
348 case 103 :
349 errmsg = "Operation already in progress";
350 break;
351 case 105 :
352 errmsg = "Operation canceled";
353 break;
354 case 106 :
355 errmsg = "Software caused connection abort";
356 break;
357 case 107 :
358 errmsg = "Connection refused";
359 break;
360 case 108 :
361 errmsg = "Connection reset by peer";
362 break;
363 case 109 :
364 errmsg = "Destination address required";
365 break;
366 case 110 :
367 errmsg = "No route to host";
368 break;
369 case 112 :
370 errmsg = "Operation now in progress";
371 break;
372 case 113 :
373 errmsg = "Transport endpoint is already connected";
374 break;
375 case 114 :
376 errmsg = "Too many levels of symbolic links";
377 break;
378 case 115 :
379 errmsg = "Message too long";
380 break;
381 case 116 :
382 errmsg = "Network is down";
383 break;
384 case 117 :
385 errmsg = "Network dropped connection on reset";
386 break;
387 case 118 :
388 errmsg = "Network is unreachable";
389 break;
390 case 119 :
391 errmsg = "No buffer space available";
392 break;
393 case 123 :
394 errmsg = "Protocol not available";
395 break;
396 case 126 :
397 errmsg = "Transport endpoint is not connected";
398 break;
399 case 128 :
400 errmsg = "Socket operation on non-socket";
401 break;
402 case 129 :
403 errmsg = "Not supported";
404 break;
405 case 130 :
406 errmsg = "Operation not supported";
407 break;
408 case 132 :
409 errmsg = "Value too large for defined data type";
410 break;
411 case 133 :
412 errmsg = "Owner died";
413 break;
414 case 134 :
415 errmsg = "Protocol error";
416 break;
417 case 135 :
418 errmsg = "Protocol not supported";
419 break;
420 case 136 :
421 errmsg = "Protocol wrong type for socket";
422 break;
423 case 138 :
424 errmsg = "Connection timed out";
425 break;
426 case 140 :
427 errmsg = "Operation would block";
428 break;
429 default:
430 errmsg = NULL;
431 break;
432 }
433 if (errmsg != NULL)
434 ret = safe_copy (buf, buflen, errmsg);
435 }
436 #endif
437
438 if (ret == EINVAL && !*buf)
439 {
440 #if defined __HAIKU__
441
442 snprintf (buf, buflen, "Unknown Application Error (%d)", errnum);
443 #else
444 snprintf (buf, buflen, "Unknown error %d", errnum);
445 #endif
446 }
447
448 errno = saved_errno;
449 return ret;
450 }
451 }