Back to index

cell-binutils  2.17cvs20070401
xstrerror.c
Go to the documentation of this file.
00001 /* xstrerror.c -- jacket routine for more robust strerror() usage.
00002    Fri Jun 16 18:30:00 1995  Pat Rankin  <rankin@eql.caltech.edu>
00003    This code is in the public domain.  */
00004 
00005 /*
00006 
00007 @deftypefn Replacement char* xstrerror (int @var{errnum})
00008 
00009 Behaves exactly like the standard @code{strerror} function, but
00010 will never return a @code{NULL} pointer.
00011 
00012 @end deftypefn
00013 
00014 */
00015 
00016 #include <stdio.h>
00017 
00018 #include "config.h"
00019 #include "libiberty.h"
00020 
00021 #ifdef VMS
00022 #  include <errno.h>
00023 #  if !defined (__STRICT_ANSI__) && !defined (__HIDE_FORBIDDEN_NAMES)
00024 #    ifdef __cplusplus
00025 extern "C" {
00026 #    endif /* __cplusplus */
00027 extern char *strerror (int,...);
00028 #    define DONT_DECLARE_STRERROR
00029 #    ifdef __cplusplus
00030 }
00031 #    endif /* __cplusplus */
00032 #  endif
00033 #endif  /* VMS */
00034 
00035 
00036 #ifndef DONT_DECLARE_STRERROR
00037 #  ifdef __cplusplus
00038 extern "C" {
00039 #  endif /* __cplusplus */
00040 extern char *strerror (int);
00041 #  ifdef __cplusplus
00042 }
00043 #  endif /* __cplusplus */
00044 #endif
00045 
00046 /* If strerror returns NULL, we'll format the number into a static buffer.  */
00047 
00048 #define ERRSTR_FMT "undocumented error #%d"
00049 static char xstrerror_buf[sizeof ERRSTR_FMT + 20];
00050 
00051 /* Like strerror, but result is never a null pointer.  */
00052 
00053 char *
00054 xstrerror (int errnum)
00055 {
00056   char *errstr;
00057 #ifdef VMS
00058   char *(*vmslib_strerror) (int,...);
00059 
00060   /* Override any possibly-conflicting declaration from system header.  */
00061   vmslib_strerror = (char *(*) (int,...)) strerror;
00062   /* Second argument matters iff first is EVMSERR, but it's simpler to
00063      pass it unconditionally.  `vaxc$errno' is declared in <errno.h>
00064      and maintained by the run-time library in parallel to `errno'.
00065      We assume that `errnum' corresponds to the last value assigned to
00066      errno by the run-time library, hence vaxc$errno will be relevant.  */
00067   errstr = (*vmslib_strerror) (errnum, vaxc$errno);
00068 #else
00069   errstr = strerror (errnum);
00070 #endif
00071 
00072   /* If `errnum' is out of range, result might be NULL.  We'll fix that.  */
00073   if (!errstr)
00074     {
00075       sprintf (xstrerror_buf, ERRSTR_FMT, errnum);
00076       errstr = xstrerror_buf;
00077     }
00078   return errstr;
00079 }