darling-gdb/libiberty/xstrerror.c

80 lines
2.0 KiB
C
Raw Normal View History

1999-05-03 07:29:11 +00: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 18:45:50 +00:00
/*
@deftypefn Replacement char* xstrerror (int @var{errnum})
Behaves exactly like the standard @code{strerror} function, but
2001-10-07 22:42:23 +00:00
will never return a @code{NULL} pointer.
2001-09-26 18:45:50 +00:00
@end deftypefn
*/
1999-05-03 07:29:11 +00:00
#include <stdio.h>
#include "config.h"
2004-11-20 03:00:47 +00:00
#include "libiberty.h"
1999-05-03 07:29:11 +00:00
#ifdef VMS
2005-05-24 21:01:33 +00:00
# include <errno.h>
# if !defined (__STRICT_ANSI__) && !defined (__HIDE_FORBIDDEN_NAMES)
# ifdef __cplusplus
extern "C" {
# endif /* __cplusplus */
2005-03-28 02:09:01 +00:00
extern char *strerror (int,...);
2005-05-24 21:01:33 +00:00
# define DONT_DECLARE_STRERROR
# ifdef __cplusplus
}
# endif /* __cplusplus */
# endif
#endif /* VMS */
1999-05-03 07:29:11 +00:00
#ifndef DONT_DECLARE_STRERROR
2005-05-24 21:01:33 +00:00
# ifdef __cplusplus
extern "C" {
# endif /* __cplusplus */
2005-03-28 02:09:01 +00:00
extern char *strerror (int);
2005-05-24 21:01:33 +00:00
# ifdef __cplusplus
}
# endif /* __cplusplus */
1999-05-03 07:29:11 +00: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 02:09:01 +00:00
xstrerror (int errnum)
1999-05-03 07:29:11 +00:00
{
char *errstr;
#ifdef VMS
2005-03-28 02:09:01 +00:00
char *(*vmslib_strerror) (int,...);
1999-05-03 07:29:11 +00:00
/* Override any possibly-conflicting declaration from system header. */
2005-03-28 02:09:01 +00:00
vmslib_strerror = (char *(*) (int,...)) strerror;
1999-05-03 07:29:11 +00: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;
}