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