binutils-gdb/libiberty/xstrerror.c

80 lines
2.0 KiB
C
Raw Normal View History

1999-05-03 09:29:11 +02:00
/* xstrerror.c -- jacket routine for more robust strerror() usage.
Fri Jun 16 18:30:00 1995 Pat Rankin <rankin@eql.caltech.edu>
This code is in the public domain. */
2001-09-26 20:45:50 +02:00
/*
@deftypefn Replacement char* xstrerror (int @var{errnum})
Behaves exactly like the standard @code{strerror} function, but
2001-10-08 00:42:23 +02:00
will never return a @code{NULL} pointer.
2001-09-26 20:45:50 +02:00
@end deftypefn
*/
1999-05-03 09:29:11 +02:00
#include <stdio.h>
#include "config.h"
2004-11-20 04:00:47 +01:00
#include "libiberty.h"
1999-05-03 09:29:11 +02:00
#ifdef VMS
2005-05-24 23:01:33 +02:00
# include <errno.h>
# if !defined (__STRICT_ANSI__) && !defined (__HIDE_FORBIDDEN_NAMES)
# ifdef __cplusplus
extern "C" {
# endif /* __cplusplus */
2005-03-28 04:09:01 +02:00
extern char *strerror (int,...);
2005-05-24 23:01:33 +02:00
# define DONT_DECLARE_STRERROR
# ifdef __cplusplus
}
# endif /* __cplusplus */
# endif
#endif /* VMS */
1999-05-03 09:29:11 +02:00
#ifndef DONT_DECLARE_STRERROR
2005-05-24 23:01:33 +02:00
# ifdef __cplusplus
extern "C" {
# endif /* __cplusplus */
2005-03-28 04:09:01 +02:00
extern char *strerror (int);
2005-05-24 23:01:33 +02:00
# ifdef __cplusplus
}
# endif /* __cplusplus */
1999-05-03 09:29:11 +02:00
#endif
/* If strerror returns NULL, we'll format the number into a static buffer. */
#define ERRSTR_FMT "undocumented error #%d"
static char xstrerror_buf[sizeof ERRSTR_FMT + 20];
/* Like strerror, but result is never a null pointer. */
char *
2005-03-28 04:09:01 +02:00
xstrerror (int errnum)
1999-05-03 09:29:11 +02:00
{
char *errstr;
#ifdef VMS
2005-03-28 04:09:01 +02:00
char *(*vmslib_strerror) (int,...);
1999-05-03 09:29:11 +02:00
/* Override any possibly-conflicting declaration from system header. */
2005-03-28 04:09:01 +02:00
vmslib_strerror = (char *(*) (int,...)) strerror;
1999-05-03 09:29:11 +02:00
/* Second argument matters iff first is EVMSERR, but it's simpler to
pass it unconditionally. `vaxc$errno' is declared in <errno.h>
and maintained by the run-time library in parallel to `errno'.
We assume that `errnum' corresponds to the last value assigned to
errno by the run-time library, hence vaxc$errno will be relevant. */
errstr = (*vmslib_strerror) (errnum, vaxc$errno);
#else
errstr = strerror (errnum);
#endif
/* If `errnum' is out of range, result might be NULL. We'll fix that. */
if (!errstr)
{
sprintf (xstrerror_buf, ERRSTR_FMT, errnum);
errstr = xstrerror_buf;
}
return errstr;
}