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