xstrerror.c revision 33965
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#include <stdio.h>
61573Srgrimes
71573Srgrimes#include "libiberty.h"
81573Srgrimes#include "config.h"
91573Srgrimes
101573Srgrimes#ifdef VMS
111573Srgrimes#include <errno.h>
121573Srgrimes#if !defined (__STRICT_ANSI__) && !defined (__HIDE_FORBIDDEN_NAMES)
131573Srgrimesextern char *strerror PARAMS ((int,...));
141573Srgrimes#define DONT_DECLARE_STRERROR
151573Srgrimes#endif
161573Srgrimes#endif	/* VMS */
171573Srgrimes
181573Srgrimes#ifndef DONT_DECLARE_STRERROR
191573Srgrimesextern char *strerror PARAMS ((int));
201573Srgrimes#endif
211573Srgrimes
221573Srgrimes/* If strerror returns NULL, we'll format the number into a static buffer.  */
231573Srgrimes
241573Srgrimes#define ERRSTR_FMT "undocumented error #%d"
251573Srgrimesstatic char xstrerror_buf[sizeof ERRSTR_FMT + 20];
261573Srgrimes
271573Srgrimes/* Like strerror, but result is never a null pointer.  */
281573Srgrimes
291573Srgrimeschar *
301573Srgrimesxstrerror (errnum)
311573Srgrimes     int errnum;
321573Srgrimes{
3390039Sobrien  char *errstr;
3490039Sobrien#ifdef VMS
351573Srgrimes  char *(*vmslib_strerror) PARAMS ((int,...));
361573Srgrimes
371573Srgrimes  /* Override any possibly-conflicting declaration from system header.  */
381573Srgrimes  vmslib_strerror = (char *(*) PARAMS ((int,...))) strerror;
391573Srgrimes  /* Second argument matters iff first is EVMSERR, but it's simpler to
401573Srgrimes     pass it unconditionally.  `vaxc$errno' is declared in <errno.h>
411573Srgrimes     and maintained by the run-time library in parallel to `errno'.
421573Srgrimes     We assume that `errnum' corresponds to the last value assigned to
431573Srgrimes     errno by the run-time library, hence vaxc$errno will be relevant.  */
441573Srgrimes  errstr = (*vmslib_strerror) (errnum, vaxc$errno);
451573Srgrimes#else
461573Srgrimes  errstr = strerror (errnum);
471573Srgrimes#endif
481573Srgrimes
491573Srgrimes  /* If `errnum' is out of range, result might be NULL.  We'll fix that.  */
501573Srgrimes  if (!errstr)
511573Srgrimes    {
521573Srgrimes      sprintf (xstrerror_buf, ERRSTR_FMT, errnum);
531573Srgrimes      errstr = xstrerror_buf;
541573Srgrimes    }
551573Srgrimes  return errstr;
561573Srgrimes}
571573Srgrimes