From bb75616aad98e248ab92be4a9a8778c81819352e Mon Sep 17 00:00:00 2001 From: dan Date: Sat, 11 Dec 1999 00:39:57 +0000 Subject: [PATCH] Added char* wsterrror(int errnum) to return the string associated with errnum This works even on platforms that don't provide strerror(). --- WINGs/ChangeLog | 1 + WINGs/WUtil.h | 4 ++++ WINGs/error.c | 58 ++++++++++++++++++++++++++----------------------- 3 files changed, 36 insertions(+), 27 deletions(-) diff --git a/WINGs/ChangeLog b/WINGs/ChangeLog index e25e64de..8e548d9d 100644 --- a/WINGs/ChangeLog +++ b/WINGs/ChangeLog @@ -33,6 +33,7 @@ changes since wmaker 0.61.1: 1. if old is NULL, return wmalloc(new_size). 2. if new_size is 0, call wfree(old), and return NULL. 3. if both old is a valid pointer and new_size>0, call realloc. +- added wstrerror(int errnum) to return the string associated with errnum. changes since wmaker 0.61.0: ............................ diff --git a/WINGs/WUtil.h b/WINGs/WUtil.h index 09ed25f0..066d8f62 100644 --- a/WINGs/WUtil.h +++ b/WINGs/WUtil.h @@ -142,6 +142,9 @@ typedef void (waborthandler)(int); waborthandler *wsetabort(waborthandler*); +/* don't free the returned string */ +char *wstrerror(int errnum); + void wfatal(const char *msg, ...); void wwarning(const char *msg, ...); void wsyserror(const char *msg, ...); @@ -156,6 +159,7 @@ char *wexpandpath(char *path); /* don't free the returned string */ char *wgethomedir(); + void *wmalloc(size_t size); void *wrealloc(void *ptr, size_t newsize); void wfree(void *ptr); diff --git a/WINGs/error.c b/WINGs/error.c index a873b3d0..ab5e0c0d 100644 --- a/WINGs/error.c +++ b/WINGs/error.c @@ -27,31 +27,40 @@ #include #include -#if !defined(HAVE_STRERROR) && defined(BSD) -#define HAVE_STRERROR -char * -strerror(int errnum) -{ - extern int errno, sys_nerr; -#ifndef __DECC - extern char *sys_errlist[]; -#endif - static char buf[] = "Unknown error 12345678901234567890"; - - if (errno < sys_nerr) - return sys_errlist[errnum]; - - sprintf (buf, "Unknown error %d", errnum); - return buf; -} -#endif - - extern char *_WINGS_progname; #define MAXLINE 1024 +/********************************************************************* + * Returns the system error message associated with error code 'errnum' + *********************************************************************/ +char* +wstrerror(int errnum) +{ +#if defined(HAVE_STRERROR) + return strerror(errnum); +#elif !defined(HAVE_STRERROR) && defined(BSD) + extern int errno, sys_nerr; +# ifndef __DECC + extern char *sys_errlist[]; +# endif + static char buf[] = "Unknown error number 12345678901234567890"; + + if (errno < sys_nerr) + return sys_errlist[errnum]; + + sprintf (buf, "Unknown error number %d", errnum); + return buf; +#else /* no strerror() and no sys_errlist[] */ + static char buf[] = "Error number 12345678901234567890"; + + sprintf(buf, "Error number %d", errnum); + return buf; +#endif +} + + /************************************************************************** * Prints a fatal error message with variable arguments and terminates * @@ -117,24 +126,19 @@ wsyserror(const char *msg, ...) { va_list args; char buf[MAXLINE]; -#ifdef HAVE_STRERROR int error=errno; -#endif + va_start(args, msg); vsprintf(buf, msg, args); fflush(stdout); fputs(_WINGS_progname, stderr); fputs(" error: ", stderr); strcat(buf, ": "); -#ifdef HAVE_STRERROR - strcat(buf, strerror(error)); + strcat(buf, wstrerror(error)); strcat(buf,"\n"); fputs(buf, stderr); fflush(stderr); fflush(stdout); -#else - perror(buf); -#endif va_end(args); }