Added char* wsterrror(int errnum) to return the string associated with errnum
[wmaker-crm.git] / WINGs / error.c
blobab5e0c0df10d49a799f5fd6df63db581a14abb95
1 /*
2 * Window Maker miscelaneous function library
3 *
4 * Copyright (c) 1997 Alfredo K. Kojima
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include "../src/config.h"
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <stdarg.h>
27 #include <string.h>
28 #include <errno.h>
30 extern char *_WINGS_progname;
32 #define MAXLINE 1024
35 /*********************************************************************
36 * Returns the system error message associated with error code 'errnum'
37 *********************************************************************/
38 char*
39 wstrerror(int errnum)
41 #if defined(HAVE_STRERROR)
42 return strerror(errnum);
43 #elif !defined(HAVE_STRERROR) && defined(BSD)
44 extern int errno, sys_nerr;
45 # ifndef __DECC
46 extern char *sys_errlist[];
47 # endif
48 static char buf[] = "Unknown error number 12345678901234567890";
50 if (errno < sys_nerr)
51 return sys_errlist[errnum];
53 sprintf (buf, "Unknown error number %d", errnum);
54 return buf;
55 #else /* no strerror() and no sys_errlist[] */
56 static char buf[] = "Error number 12345678901234567890";
58 sprintf(buf, "Error number %d", errnum);
59 return buf;
60 #endif
64 /**************************************************************************
65 * Prints a fatal error message with variable arguments and terminates
67 * msg - message to print with optional formatting
68 * ... - arguments to use on formatting
69 **************************************************************************/
70 void
71 wfatal(const char *msg, ...)
73 va_list args;
74 char buf[MAXLINE];
76 va_start(args, msg);
78 vsprintf(buf, msg, args);
79 strcat(buf,"\n");
80 fflush(stdout);
81 fputs(_WINGS_progname, stderr);
82 fputs(" fatal error: ",stderr);
83 fputs(buf, stderr);
84 fflush(stdout);
85 fflush(stderr);
87 va_end(args);
91 /*********************************************************************
92 * Prints a warning message with variable arguments
94 * msg - message to print with optional formatting
95 * ... - arguments to use on formatting
96 *********************************************************************/
97 void
98 wwarning(const char *msg, ...)
100 va_list args;
101 char buf[MAXLINE];
103 va_start(args, msg);
105 vsprintf(buf, msg, args);
106 strcat(buf,"\n");
107 fflush(stdout);
108 fputs(_WINGS_progname, stderr);
109 fputs(" warning: ",stderr);
110 fputs(buf, stderr);
111 fflush(stdout);
112 fflush(stderr);
114 va_end(args);
118 /*********************************************************************
119 * Prints a system error message with variable arguments
121 * msg - message to print with optional formatting
122 * ... - arguments to use on formatting
123 *********************************************************************/
124 void
125 wsyserror(const char *msg, ...)
127 va_list args;
128 char buf[MAXLINE];
129 int error=errno;
131 va_start(args, msg);
132 vsprintf(buf, msg, args);
133 fflush(stdout);
134 fputs(_WINGS_progname, stderr);
135 fputs(" error: ", stderr);
136 strcat(buf, ": ");
137 strcat(buf, wstrerror(error));
138 strcat(buf,"\n");
139 fputs(buf, stderr);
140 fflush(stderr);
141 fflush(stdout);
142 va_end(args);