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