xstrerror.c revision 218822
11573Srgrimes/* xstrerror.c -- jacket routine for more robust strerror() usage.
21573Srgrimes   Fri Jun 16 18:30:00 1995  Pat Rankin  <rankin@eql.caltech.edu>
31573Srgrimes   This code is in the public domain.  */
41573Srgrimes
51573Srgrimes/*
61573Srgrimes
71573Srgrimes@deftypefn Replacement char* xstrerror (int @var{errnum})
81573Srgrimes
91573SrgrimesBehaves exactly like the standard @code{strerror} function, but
101573Srgrimeswill never return a @code{NULL} pointer.
111573Srgrimes
121573Srgrimes@end deftypefn
131573Srgrimes
141573Srgrimes*/
151573Srgrimes
16148834Sstefanf#include <stdio.h>
171573Srgrimes
181573Srgrimes#include "config.h"
191573Srgrimes#include "libiberty.h"
201573Srgrimes
211573Srgrimes#ifdef VMS
221573Srgrimes#  include <errno.h>
231573Srgrimes#  if !defined (__STRICT_ANSI__) && !defined (__HIDE_FORBIDDEN_NAMES)
241573Srgrimes#    ifdef __cplusplus
251573Srgrimesextern "C" {
261573Srgrimes#    endif /* __cplusplus */
271573Srgrimesextern char *strerror (int,...);
281573Srgrimes#    define DONT_DECLARE_STRERROR
291573Srgrimes#    ifdef __cplusplus
301573Srgrimes}
311573Srgrimes#    endif /* __cplusplus */
321573Srgrimes#  endif
33148834Sstefanf#endif  /* VMS */
3484260Sobrien
351573Srgrimes
361573Srgrimes#ifndef DONT_DECLARE_STRERROR
371573Srgrimes#  ifdef __cplusplus
381573Srgrimesextern "C" {
391573Srgrimes#  endif /* __cplusplus */
408870Srgrimesextern char *strerror (int);
4184260Sobrien#  ifdef __cplusplus
421573Srgrimes}
431573Srgrimes#  endif /* __cplusplus */
441573Srgrimes#endif
451573Srgrimes
4684260Sobrien/* If strerror returns NULL, we'll format the number into a static buffer.  */
4784260Sobrien
4884260Sobrien#define ERRSTR_FMT "undocumented error #%d"
4984260Sobrienstatic char xstrerror_buf[sizeof ERRSTR_FMT + 20];
5084260Sobrien
51148834Sstefanf/* Like strerror, but result is never a null pointer.  */
521573Srgrimes
531573Srgrimeschar *
541573Srgrimesxstrerror (int errnum)
5584260Sobrien{
5684260Sobrien  char *errstr;
5784260Sobrien#ifdef VMS
5884260Sobrien  char *(*vmslib_strerror) (int,...);
5984260Sobrien
6084260Sobrien  /* Override any possibly-conflicting declaration from system header.  */
6184260Sobrien  vmslib_strerror = (char *(*) (int,...)) strerror;
62148834Sstefanf  /* Second argument matters iff first is EVMSERR, but it's simpler to
6384260Sobrien     pass it unconditionally.  `vaxc$errno' is declared in <errno.h>
64148834Sstefanf     and maintained by the run-time library in parallel to `errno'.
651573Srgrimes     We assume that `errnum' corresponds to the last value assigned to
661573Srgrimes     errno by the run-time library, hence vaxc$errno will be relevant.  */
67  errstr = (*vmslib_strerror) (errnum, vaxc$errno);
68#else
69  errstr = strerror (errnum);
70#endif
71
72  /* If `errnum' is out of range, result might be NULL.  We'll fix that.  */
73  if (!errstr)
74    {
75      sprintf (xstrerror_buf, ERRSTR_FMT, errnum);
76      errstr = xstrerror_buf;
77    }
78  return errstr;
79}
80