summaryrefslogtreecommitdiff
path: root/lib/strerror_r.c
blob: fe1185ba4e06b03877ca86f8a331aa496e56b93a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
/* strerror_r.c --- POSIX compatible system error routine

   Copyright (C) 2010-2011 Free Software Foundation, Inc.

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

/* Written by Bruno Haible <bruno@clisp.org>, 2010.  */

#include <config.h>

/* Enable declaration of sys_nerr and sys_errlist in <errno.h> on NetBSD.  */
#define _NETBSD_SOURCE 1

/* Specification.  */
#include <string.h>

#include <errno.h>

# if GNULIB_defined_ESOCK /* native Windows platforms */
#  if HAVE_WINSOCK2_H
#   include <winsock2.h>
#  endif
# endif


#if (__GLIBC__ >= 2 || defined __UCLIBC__ || defined __CYGWIN__) && HAVE___XPG_STRERROR_R /* glibc >= 2.3.4, cygwin >= 1.7.9 */

# define USE_XPG_STRERROR_R 1

#elif HAVE_DECL_STRERROR_R && !(__GLIBC__ >= 2 || defined __UCLIBC__)

/* The system's strerror_r function is OK, except that its third argument
   is 'int', not 'size_t', or its return type is wrong.  */

# include <limits.h>

# define USE_SYSTEM_STRERROR_R 1

#else /* (__GLIBC__ >= 2 || defined __UCLIBC__ ? !HAVE___XPG_STRERROR_R : !HAVE_DECL_STRERROR_R) */

/* Use the system's strerror().  */
# undef strerror

# define USE_SYSTEM_STRERROR 1

# if defined __NetBSD__ || defined __hpux || ((defined _WIN32 || defined __WIN32__) && !defined __CYGWIN__) || defined __sgi || (defined __sun && !defined _LP64)

/* No locking needed.  */

/* Get catgets internationalization functions.  */
#  if HAVE_CATGETS
#   include <nl_types.h>
#  endif

/* Get sys_nerr, sys_errlist on HP-UX (otherwise only declared in C++ mode).
   Get sys_nerr, sys_errlist on IRIX (otherwise only declared with _SGIAPI).  */
#  if defined __hpux || defined __sgi
extern int sys_nerr;
extern char *sys_errlist[];
#  endif

/* Get sys_nerr on Solaris.  */
#  if defined __sun && !defined _LP64
extern int sys_nerr;
#  endif

/* Get sys_nerr, sys_errlist on native Windows.  */
#  include <stdlib.h>

# else

#  include "glthread/lock.h"

/* This lock protects the buffer returned by strerror().  We assume that
   no other uses of strerror() exist in the program.  */
gl_lock_define_initialized(static, strerror_lock)

# endif

#endif


int
strerror_r (int errnum, char *buf, size_t buflen)
#undef strerror_r
{
#if GNULIB_defined_ETXTBSY \
    || GNULIB_defined_ESOCK \
    || GNULIB_defined_ENOMSG \
    || GNULIB_defined_EIDRM \
    || GNULIB_defined_ENOLINK \
    || GNULIB_defined_EPROTO \
    || GNULIB_defined_EMULTIHOP \
    || GNULIB_defined_EBADMSG \
    || GNULIB_defined_EOVERFLOW \
    || GNULIB_defined_ENOTSUP \
    || GNULIB_defined_ESTALE \
    || GNULIB_defined_EDQUOT \
    || GNULIB_defined_ECANCELED
  {
    char const *msg = NULL;
    /* These error messages are taken from glibc/sysdeps/gnu/errlist.c.  */
    switch (errnum)
      {
# if GNULIB_defined_ETXTBSY
      case ETXTBSY:
        msg = "Text file busy";
        break;
# endif

# if GNULIB_defined_ESOCK /* native Windows platforms */
      /* EWOULDBLOCK is the same as EAGAIN.  */
      case EINPROGRESS:
        msg = "Operation now in progress";
        break;
      case EALREADY:
        msg = "Operation already in progress";
        break;
      case ENOTSOCK:
        msg = "Socket operation on non-socket";
        break;
      case EDESTADDRREQ:
        msg = "Destination address required";
        break;
      case EMSGSIZE:
        msg = "Message too long";
        break;
      case EPROTOTYPE:
        msg = "Protocol wrong type for socket";
        break;
      case ENOPROTOOPT:
        msg = "Protocol not available";
        break;
      case EPROTONOSUPPORT:
        msg = "Protocol not supported";
        break;
      case ESOCKTNOSUPPORT:
        msg = "Socket type not supported";
        break;
      case EOPNOTSUPP:
        msg = "Operation not supported";
        break;
      case EPFNOSUPPORT:
        msg = "Protocol family not supported";
        break;
      case EAFNOSUPPORT:
        msg = "Address family not supported by protocol";
        break;
      case EADDRINUSE:
        msg = "Address already in use";
        break;
      case EADDRNOTAVAIL:
        msg = "Cannot assign requested address";
        break;
      case ENETDOWN:
        msg = "Network is down";
        break;
      case ENETUNREACH:
        msg = "Network is unreachable";
        break;
      case ENETRESET:
        msg = "Network dropped connection on reset";
        break;
      case ECONNABORTED:
        msg = "Software caused connection abort";
        break;
      case ECONNRESET:
        msg = "Connection reset by peer";
        break;
      case ENOBUFS:
        msg = "No buffer space available";
        break;
      case EISCONN:
        msg = "Transport endpoint is already connected";
        break;
      case ENOTCONN:
        msg = "Transport endpoint is not connected";
        break;
      case ESHUTDOWN:
        msg = "Cannot send after transport endpoint shutdown";
        break;
      case ETOOMANYREFS:
        msg = "Too many references: cannot splice";
        break;
      case ETIMEDOUT:
        msg = "Connection timed out";
        break;
      case ECONNREFUSED:
        msg = "Connection refused";
        break;
      case ELOOP:
        msg = "Too many levels of symbolic links";
        break;
      case EHOSTDOWN:
        msg = "Host is down";
        break;
      case EHOSTUNREACH:
        msg = "No route to host";
        break;
      case EPROCLIM:
        msg = "Too many processes";
        break;
      case EUSERS:
        msg = "Too many users";
        break;
      case EDQUOT:
        msg = "Disk quota exceeded";
        break;
      case ESTALE:
        msg = "Stale NFS file handle";
        break;
      case EREMOTE:
        msg = "Object is remote";
        break;
#  if HAVE_WINSOCK2_H
      /* WSA_INVALID_HANDLE maps to EBADF */
      /* WSA_NOT_ENOUGH_MEMORY maps to ENOMEM */
      /* WSA_INVALID_PARAMETER maps to EINVAL */
      case WSA_OPERATION_ABORTED:
        msg = "Overlapped operation aborted";
        break;
      case WSA_IO_INCOMPLETE:
        msg = "Overlapped I/O event object not in signaled state";
        break;
      case WSA_IO_PENDING:
        msg = "Overlapped operations will complete later";
        break;
      /* WSAEINTR maps to EINTR */
      /* WSAEBADF maps to EBADF */
      /* WSAEACCES maps to EACCES */
      /* WSAEFAULT maps to EFAULT */
      /* WSAEINVAL maps to EINVAL */
      /* WSAEMFILE maps to EMFILE */
      /* WSAEWOULDBLOCK maps to EWOULDBLOCK */
      /* WSAEINPROGRESS is EINPROGRESS */
      /* WSAEALREADY is EALREADY */
      /* WSAENOTSOCK is ENOTSOCK */
      /* WSAEDESTADDRREQ is EDESTADDRREQ */
      /* WSAEMSGSIZE is EMSGSIZE */
      /* WSAEPROTOTYPE is EPROTOTYPE */
      /* WSAENOPROTOOPT is ENOPROTOOPT */
      /* WSAEPROTONOSUPPORT is EPROTONOSUPPORT */
      /* WSAESOCKTNOSUPPORT is ESOCKTNOSUPPORT */
      /* WSAEOPNOTSUPP is EOPNOTSUPP */
      /* WSAEPFNOSUPPORT is EPFNOSUPPORT */
      /* WSAEAFNOSUPPORT is EAFNOSUPPORT */
      /* WSAEADDRINUSE is EADDRINUSE */
      /* WSAEADDRNOTAVAIL is EADDRNOTAVAIL */
      /* WSAENETDOWN is ENETDOWN */
      /* WSAENETUNREACH is ENETUNREACH */
      /* WSAENETRESET is ENETRESET */
      /* WSAECONNABORTED is ECONNABORTED */
      /* WSAECONNRESET is ECONNRESET */
      /* WSAENOBUFS is ENOBUFS */
      /* WSAEISCONN is EISCONN */
      /* WSAENOTCONN is ENOTCONN */
      /* WSAESHUTDOWN is ESHUTDOWN */
      /* WSAETOOMANYREFS is ETOOMANYREFS */
      /* WSAETIMEDOUT is ETIMEDOUT */
      /* WSAECONNREFUSED is ECONNREFUSED */
      /* WSAELOOP is ELOOP */
      /* WSAENAMETOOLONG maps to ENAMETOOLONG */
      /* WSAEHOSTDOWN is EHOSTDOWN */
      /* WSAEHOSTUNREACH is EHOSTUNREACH */
      /* WSAENOTEMPTY maps to ENOTEMPTY */
      /* WSAEPROCLIM is EPROCLIM */
      /* WSAEUSERS is EUSERS */
      /* WSAEDQUOT is EDQUOT */
      /* WSAESTALE is ESTALE */
      /* WSAEREMOTE is EREMOTE */
      case WSASYSNOTREADY:
        msg = "Network subsystem is unavailable";
        break;
      case WSAVERNOTSUPPORTED:
        msg = "Winsock.dll version out of range";
        break;
      case WSANOTINITIALISED:
        msg = "Successful WSAStartup not yet performed";
        break;
      case WSAEDISCON:
        msg = "Graceful shutdown in progress";
        break;
      case WSAENOMORE: case WSA_E_NO_MORE:
        msg = "No more results";
        break;
      case WSAECANCELLED: case WSA_E_CANCELLED:
        msg = "Call was canceled";
        break;
      case WSAEINVALIDPROCTABLE:
        msg = "Procedure call table is invalid";
        break;
      case WSAEINVALIDPROVIDER:
        msg = "Service provider is invalid";
        break;
      case WSAEPROVIDERFAILEDINIT:
        msg = "Service provider failed to initialize";
        break;
      case WSASYSCALLFAILURE:
        msg = "System call failure";
        break;
      case WSASERVICE_NOT_FOUND:
        msg = "Service not found";
        break;
      case WSATYPE_NOT_FOUND:
        msg = "Class type not found";
        break;
      case WSAEREFUSED:
        msg = "Database query was refused";
        break;
      case WSAHOST_NOT_FOUND:
        msg = "Host not found";
        break;
      case WSATRY_AGAIN:
        msg = "Nonauthoritative host not found";
        break;
      case WSANO_RECOVERY:
        msg = "Nonrecoverable error";
        break;
      case WSANO_DATA:
        msg = "Valid name, no data record of requested type";
        break;
      /* WSA_QOS_* omitted */
#  endif
# endif

# if GNULIB_defined_ENOMSG
      case ENOMSG:
        msg = "No message of desired type";
        break;
# endif

# if GNULIB_defined_EIDRM
      case EIDRM:
        msg = "Identifier removed";
        break;
# endif

# if GNULIB_defined_ENOLINK
      case ENOLINK:
        msg = "Link has been severed";
        break;
# endif

# if GNULIB_defined_EPROTO
      case EPROTO:
        msg = "Protocol error";
        break;
# endif

# if GNULIB_defined_EMULTIHOP
      case EMULTIHOP:
        msg = "Multihop attempted";
        break;
# endif

# if GNULIB_defined_EBADMSG
      case EBADMSG:
        msg = "Bad message";
        break;
# endif

# if GNULIB_defined_EOVERFLOW
      case EOVERFLOW:
        msg = "Value too large for defined data type";
        break;
# endif

# if GNULIB_defined_ENOTSUP
      case ENOTSUP:
        msg = "Not supported";
        break;
# endif

# if GNULIB_defined_ESTALE
      case ESTALE:
        msg = "Stale NFS file handle";
        break;
# endif

# if GNULIB_defined_EDQUOT
      case EDQUOT:
        msg = "Disk quota exceeded";
        break;
# endif

# if GNULIB_defined_ECANCELED
      case ECANCELED:
        msg = "Operation canceled";
        break;
# endif
      }

    if (msg)
      {
        size_t len = strlen (msg);

        if (len < buflen)
          {
            memcpy (buf, msg, len + 1);
            return 0;
          }
        else
          return ERANGE;
      }
  }
#endif

  {
    int ret;

#if USE_SYSTEM_STRERROR_R

    if (buflen > INT_MAX)
      buflen = INT_MAX;

# ifdef __hpux
    /* On HP-UX 11.31, strerror_r always fails when buflen < 80.  */
    {
      char stackbuf[80];

      if (buflen < sizeof (stackbuf))
        {
          ret = strerror_r (errnum, stackbuf, sizeof (stackbuf));
          if (ret == 0)
            {
              size_t len = strlen (stackbuf);

              if (len < buflen)
                memcpy (buf, stackbuf, len + 1);
              else
                ret = ERANGE;
            }
        }
      else
        ret = strerror_r (errnum, buf, buflen);
    }
# elif defined __CYGWIN__
    /* Cygwin 1.7.8 only provides the glibc interface, is thread-safe, and
       always succeeds (although it may truncate). */
    strerror_r (errnum, buf, buflen);
    ret = 0;
# else
    ret = strerror_r (errnum, buf, buflen);
# endif

# ifdef _AIX
    /* On AIX 6.1, strerror_r returns -1 and sets errno to EINVAL
       if buflen <= 1.  */
    if (ret < 0 && errno == EINVAL && buflen <= 1)
      {
        /* Retry with a larger buffer.  */
        char largerbuf[10];
        ret = strerror_r (errnum, largerbuf, sizeof (largerbuf));
        if (ret < 0 && errno == EINVAL)
          {
            /* errnum was out of range.  */
            ret = EINVAL;
          }
        else
          {
            /* buf was too small.  */
            ret = ERANGE;
          }
      }
# endif

    /* Some old implementations may return (-1, EINVAL) instead of EINVAL.  */
    if (ret < 0)
      ret = errno;

    /* FreeBSD rejects 0; see http://austingroupbugs.net/view.php?id=382.  */
    if (errnum == 0 && ret == EINVAL)
      {
        if (buflen <= strlen ("Success"))
          {
            ret = ERANGE;
            if (buflen)
              buf[0] = 0;
          }
        else
          {
            ret = 0;
            strcpy (buf, "Success");
          }
      }

#elif USE_XPG_STRERROR_R

    {
      extern int __xpg_strerror_r (int errnum, char *buf, size_t buflen);

      ret = __xpg_strerror_r (errnum, buf, buflen);
      if (ret < 0)
        ret = errno;
    }

#else /* USE_SYSTEM_STRERROR */

    /* Try to do what strerror (errnum) does, but without clobbering the
       buffer used by strerror().  */

# if defined __NetBSD__ || defined __hpux || ((defined _WIN32 || defined __WIN32__) && !defined __CYGWIN__) /* NetBSD, HP-UX, native Win32 */

    /* NetBSD:        sys_nerr, sys_errlist are declared through _NETBSD_SOURCE
                      and <errno.h> above.
       HP-UX:         sys_nerr, sys_errlist are declared explicitly above.
       native Win32:  sys_nerr, sys_errlist are declared in <stdlib.h>.  */
    if (errnum >= 0 && errnum < sys_nerr)
      {
#  if HAVE_CATGETS && (defined __NetBSD__ || defined __hpux)
        int saved_errno = errno;
#   if defined __NetBSD__
        nl_catd catd = catopen ("libc", NL_CAT_LOCALE);
        const char *errmsg =
          (catd != (nl_catd)-1
           ? catgets (catd, 1, errnum, sys_errlist[errnum])
           : sys_errlist[errnum]);
#   endif
#   if defined __hpux
        nl_catd catd = catopen ("perror", NL_CAT_LOCALE);
        const char *errmsg =
          (catd != (nl_catd)-1
           ? catgets (catd, 1, 1 + errnum, sys_errlist[errnum])
           : sys_errlist[errnum]);
#   endif
#  else
        const char *errmsg = sys_errlist[errnum];
#  endif
        if (errmsg == NULL || *errmsg == '\0')
          ret = EINVAL;
        else
          {
            size_t len = strlen (errmsg);

            if (len < buflen)
              {
                memcpy (buf, errmsg, len + 1);
                ret = 0;
              }
            else
              ret = ERANGE;
          }
#  if HAVE_CATGETS && (defined __NetBSD__ || defined __hpux)
        if (catd != (nl_catd)-1)
          catclose (catd);
        errno = saved_errno;
#  endif
      }
    else
      ret = EINVAL;

# elif defined __sgi || (defined __sun && !defined _LP64) /* IRIX, Solaris <= 9 32-bit */

    /* For a valid error number, the system's strerror() function returns
       a pointer to a not copied string, not to a buffer.  */
    if (errnum >= 0 && errnum < sys_nerr)
      {
        char *errmsg = strerror (errnum);

        if (errmsg == NULL || *errmsg == '\0')
          ret = EINVAL;
        else
          {
            size_t len = strlen (errmsg);

            if (len < buflen)
              {
                memcpy (buf, errmsg, len + 1);
                ret = 0;
              }
            else
              ret = ERANGE;
          }
      }
    else
      ret = EINVAL;

# else

    gl_lock_lock (strerror_lock);

    {
      char *errmsg = strerror (errnum);

      /* For invalid error numbers, strerror() on
           - IRIX 6.5 returns NULL,
           - HP-UX 11 returns an empty string.  */
      if (errmsg == NULL || *errmsg == '\0')
        ret = EINVAL;
      else
        {
          size_t len = strlen (errmsg);

          if (len < buflen)
            {
              memcpy (buf, errmsg, len + 1);
              ret = 0;
            }
          else
            ret = ERANGE;
        }
    }

    gl_lock_unlock (strerror_lock);

# endif

#endif

    return ret;
  }
}