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