xref: /netbsd-src/external/gpl3/gdb/dist/libiberty/xstrerror.c (revision 98b9484c67cdf05a7e01fa6a65d1f4f959a3633e)
1*98b9484cSchristos /* xstrerror.c -- jacket routine for more robust strerror() usage.
2*98b9484cSchristos    Fri Jun 16 18:30:00 1995  Pat Rankin  <rankin@eql.caltech.edu>
3*98b9484cSchristos    This code is in the public domain.  */
4*98b9484cSchristos 
5*98b9484cSchristos /*
6*98b9484cSchristos 
7*98b9484cSchristos @deftypefn Replacement char* xstrerror (int @var{errnum})
8*98b9484cSchristos 
9*98b9484cSchristos Behaves exactly like the standard @code{strerror} function, but
10*98b9484cSchristos will never return a @code{NULL} pointer.
11*98b9484cSchristos 
12*98b9484cSchristos @end deftypefn
13*98b9484cSchristos 
14*98b9484cSchristos */
15*98b9484cSchristos 
16*98b9484cSchristos #include <stdio.h>
17*98b9484cSchristos 
18*98b9484cSchristos #include "config.h"
19*98b9484cSchristos #include "libiberty.h"
20*98b9484cSchristos 
21*98b9484cSchristos #ifdef VMS
22*98b9484cSchristos #  include <errno.h>
23*98b9484cSchristos #  if !defined (__STRICT_ANSI__) && !defined (__HIDE_FORBIDDEN_NAMES)
24*98b9484cSchristos #    ifdef __cplusplus
25*98b9484cSchristos extern "C" {
26*98b9484cSchristos #    endif /* __cplusplus */
27*98b9484cSchristos extern char *strerror (int,...);
28*98b9484cSchristos #    define DONT_DECLARE_STRERROR
29*98b9484cSchristos #    ifdef __cplusplus
30*98b9484cSchristos }
31*98b9484cSchristos #    endif /* __cplusplus */
32*98b9484cSchristos #  endif
33*98b9484cSchristos #endif  /* VMS */
34*98b9484cSchristos 
35*98b9484cSchristos 
36*98b9484cSchristos #ifndef DONT_DECLARE_STRERROR
37*98b9484cSchristos #  ifdef __cplusplus
38*98b9484cSchristos extern "C" {
39*98b9484cSchristos #  endif /* __cplusplus */
40*98b9484cSchristos extern char *strerror (int);
41*98b9484cSchristos #  ifdef __cplusplus
42*98b9484cSchristos }
43*98b9484cSchristos #  endif /* __cplusplus */
44*98b9484cSchristos #endif
45*98b9484cSchristos 
46*98b9484cSchristos /* If strerror returns NULL, we'll format the number into a static buffer.  */
47*98b9484cSchristos 
48*98b9484cSchristos #define ERRSTR_FMT "undocumented error #%d"
49*98b9484cSchristos static char xstrerror_buf[sizeof ERRSTR_FMT + 20];
50*98b9484cSchristos 
51*98b9484cSchristos /* Like strerror, but result is never a null pointer.  */
52*98b9484cSchristos 
53*98b9484cSchristos char *
xstrerror(int errnum)54*98b9484cSchristos xstrerror (int errnum)
55*98b9484cSchristos {
56*98b9484cSchristos   char *errstr;
57*98b9484cSchristos #ifdef VMS
58*98b9484cSchristos   char *(*vmslib_strerror) (int,...);
59*98b9484cSchristos 
60*98b9484cSchristos   /* Override any possibly-conflicting declaration from system header.  */
61*98b9484cSchristos   vmslib_strerror = (char *(*) (int,...)) strerror;
62*98b9484cSchristos   /* Second argument matters iff first is EVMSERR, but it's simpler to
63*98b9484cSchristos      pass it unconditionally.  `vaxc$errno' is declared in <errno.h>
64*98b9484cSchristos      and maintained by the run-time library in parallel to `errno'.
65*98b9484cSchristos      We assume that `errnum' corresponds to the last value assigned to
66*98b9484cSchristos      errno by the run-time library, hence vaxc$errno will be relevant.  */
67*98b9484cSchristos   errstr = (*vmslib_strerror) (errnum, vaxc$errno);
68*98b9484cSchristos #else
69*98b9484cSchristos   errstr = strerror (errnum);
70*98b9484cSchristos #endif
71*98b9484cSchristos 
72*98b9484cSchristos   /* If `errnum' is out of range, result might be NULL.  We'll fix that.  */
73*98b9484cSchristos   if (!errstr)
74*98b9484cSchristos     {
75*98b9484cSchristos       sprintf (xstrerror_buf, ERRSTR_FMT, errnum);
76*98b9484cSchristos       errstr = xstrerror_buf;
77*98b9484cSchristos     }
78*98b9484cSchristos   return errstr;
79*98b9484cSchristos }
80