xstrerror.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /* xstrerror.c -- jacket routine for more robust strerror() usage.
  2. Fri Jun 16 18:30:00 1995 Pat Rankin <rankin@eql.caltech.edu>
  3. This code is in the public domain. */
  4. /*
  5. @deftypefn Replacement char* xstrerror (int @var{errnum})
  6. Behaves exactly like the standard @code{strerror} function, but
  7. will never return a @code{NULL} pointer.
  8. @end deftypefn
  9. */
  10. #include <stdio.h>
  11. #include "config.h"
  12. #include "libiberty.h"
  13. #ifdef VMS
  14. # include <errno.h>
  15. # if !defined (__STRICT_ANSI__) && !defined (__HIDE_FORBIDDEN_NAMES)
  16. # ifdef __cplusplus
  17. extern "C" {
  18. # endif /* __cplusplus */
  19. extern char *strerror (int,...);
  20. # define DONT_DECLARE_STRERROR
  21. # ifdef __cplusplus
  22. }
  23. # endif /* __cplusplus */
  24. # endif
  25. #endif /* VMS */
  26. #ifndef DONT_DECLARE_STRERROR
  27. # ifdef __cplusplus
  28. extern "C" {
  29. # endif /* __cplusplus */
  30. extern char *strerror (int);
  31. # ifdef __cplusplus
  32. }
  33. # endif /* __cplusplus */
  34. #endif
  35. /* If strerror returns NULL, we'll format the number into a static buffer. */
  36. #define ERRSTR_FMT "undocumented error #%d"
  37. static char xstrerror_buf[sizeof ERRSTR_FMT + 20];
  38. /* Like strerror, but result is never a null pointer. */
  39. char *
  40. xstrerror (int errnum)
  41. {
  42. char *errstr;
  43. #ifdef VMS
  44. char *(*vmslib_strerror) (int,...);
  45. /* Override any possibly-conflicting declaration from system header. */
  46. vmslib_strerror = (char *(*) (int,...)) strerror;
  47. /* Second argument matters iff first is EVMSERR, but it's simpler to
  48. pass it unconditionally. `vaxc$errno' is declared in <errno.h>
  49. and maintained by the run-time library in parallel to `errno'.
  50. We assume that `errnum' corresponds to the last value assigned to
  51. errno by the run-time library, hence vaxc$errno will be relevant. */
  52. errstr = (*vmslib_strerror) (errnum, vaxc$errno);
  53. #else
  54. errstr = strerror (errnum);
  55. #endif
  56. /* If `errnum' is out of range, result might be NULL. We'll fix that. */
  57. if (!errstr)
  58. {
  59. sprintf (xstrerror_buf, ERRSTR_FMT, errnum);
  60. errstr = xstrerror_buf;
  61. }
  62. return errstr;
  63. }