xstrerror.c revision 89857
133965Sjdp/* xstrerror.c -- jacket routine for more robust strerror() usage.
233965Sjdp   Fri Jun 16 18:30:00 1995  Pat Rankin  <rankin@eql.caltech.edu>
333965Sjdp   This code is in the public domain.  */
433965Sjdp
589857Sobrien/*
689857Sobrien
789857Sobrien@deftypefn Replacement char* xstrerror (int @var{errnum})
889857Sobrien
989857SobrienBehaves exactly like the standard @code{strerror} function, but
1089857Sobrienwill never return a @code{NULL} pointer.
1189857Sobrien
1289857Sobrien@end deftypefn
1389857Sobrien
1489857Sobrien*/
1589857Sobrien
1633965Sjdp#include <stdio.h>
1733965Sjdp
1833965Sjdp#include "libiberty.h"
1933965Sjdp#include "config.h"
2033965Sjdp
2133965Sjdp#ifdef VMS
2233965Sjdp#include <errno.h>
2333965Sjdp#if !defined (__STRICT_ANSI__) && !defined (__HIDE_FORBIDDEN_NAMES)
2433965Sjdpextern char *strerror PARAMS ((int,...));
2533965Sjdp#define DONT_DECLARE_STRERROR
2633965Sjdp#endif
2733965Sjdp#endif	/* VMS */
2833965Sjdp
2933965Sjdp#ifndef DONT_DECLARE_STRERROR
3033965Sjdpextern char *strerror PARAMS ((int));
3133965Sjdp#endif
3233965Sjdp
3333965Sjdp/* If strerror returns NULL, we'll format the number into a static buffer.  */
3433965Sjdp
3533965Sjdp#define ERRSTR_FMT "undocumented error #%d"
3633965Sjdpstatic char xstrerror_buf[sizeof ERRSTR_FMT + 20];
3733965Sjdp
3833965Sjdp/* Like strerror, but result is never a null pointer.  */
3933965Sjdp
4033965Sjdpchar *
4133965Sjdpxstrerror (errnum)
4233965Sjdp     int errnum;
4333965Sjdp{
4433965Sjdp  char *errstr;
4533965Sjdp#ifdef VMS
4633965Sjdp  char *(*vmslib_strerror) PARAMS ((int,...));
4733965Sjdp
4833965Sjdp  /* Override any possibly-conflicting declaration from system header.  */
4933965Sjdp  vmslib_strerror = (char *(*) PARAMS ((int,...))) strerror;
5033965Sjdp  /* Second argument matters iff first is EVMSERR, but it's simpler to
5133965Sjdp     pass it unconditionally.  `vaxc$errno' is declared in <errno.h>
5233965Sjdp     and maintained by the run-time library in parallel to `errno'.
5333965Sjdp     We assume that `errnum' corresponds to the last value assigned to
5433965Sjdp     errno by the run-time library, hence vaxc$errno will be relevant.  */
5533965Sjdp  errstr = (*vmslib_strerror) (errnum, vaxc$errno);
5633965Sjdp#else
5733965Sjdp  errstr = strerror (errnum);
5833965Sjdp#endif
5933965Sjdp
6033965Sjdp  /* If `errnum' is out of range, result might be NULL.  We'll fix that.  */
6133965Sjdp  if (!errstr)
6233965Sjdp    {
6333965Sjdp      sprintf (xstrerror_buf, ERRSTR_FMT, errnum);
6433965Sjdp      errstr = xstrerror_buf;
6533965Sjdp    }
6633965Sjdp  return errstr;
6733965Sjdp}
68