Change to the linux kernel coding style
[wmaker-crm.git] / WINGs / error.c
blobe0aa4e86790837fa588383d43e5dd1f1a0370d4d
1 /*
2 * Window Maker miscelaneous function library
4 * Copyright (c) 1997-2003 Alfredo K. Kojima
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.
21 #include "wconfig.h"
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <stdarg.h>
26 #include <string.h>
27 #include <errno.h>
29 extern char *_WINGS_progname;
31 #define MAXLINE 1024
33 /*********************************************************************
34 * Returns the system error message associated with error code 'errnum'
35 *********************************************************************/
36 char *wstrerror(int errnum)
38 #if defined(HAVE_STRERROR)
39 return strerror(errnum);
40 #elif !defined(HAVE_STRERROR) && defined(BSD)
41 extern int errno, sys_nerr;
42 # ifndef __DECC
43 extern char *sys_errlist[];
44 # endif
45 static char buf[] = "Unknown error 12345678901234567890";
47 if (errno < sys_nerr)
48 return sys_errlist[errnum];
50 snprintf(buf, sizeof(buf), _("Unknown error %d"), errnum);
51 return buf;
52 #else /* no strerror() and no sys_errlist[] */
53 static char buf[] = "Error 12345678901234567890";
55 snprintf(buf, sizeof(buf), _("Error %d"), errnum);
56 return buf;
57 #endif
60 /*********************************************************************
61 * Prints a message with variable arguments
63 * msg - message to print with optional formatting
64 * ... - arguments to use on formatting
65 *********************************************************************/
66 void wmessage(const char *msg, ...)
68 va_list args;
69 char buf[MAXLINE];
71 va_start(args, msg);
73 vsnprintf(buf, MAXLINE - 3, msg, args);
74 strcat(buf, "\n");
75 fflush(stdout);
76 fputs(_WINGS_progname ? _WINGS_progname : "WINGs", stderr);
77 fputs(": ", stderr);
78 fputs(buf, stderr);
79 fflush(stdout);
80 fflush(stderr);
82 va_end(args);
85 /*********************************************************************
86 * Prints a warning message with variable arguments
88 * msg - message to print with optional formatting
89 * ... - arguments to use on formatting
90 *********************************************************************/
91 void wwarning(const char *msg, ...)
93 va_list args;
94 char buf[MAXLINE];
96 va_start(args, msg);
98 vsnprintf(buf, MAXLINE - 3, msg, args);
99 strcat(buf, "\n");
100 fflush(stdout);
101 fputs(_WINGS_progname ? _WINGS_progname : "WINGs", stderr);
102 fputs(_(" warning: "), stderr);
103 fputs(buf, stderr);
104 fflush(stdout);
105 fflush(stderr);
107 va_end(args);
110 /**************************************************************************
111 * Prints a fatal error message with variable arguments and terminates
113 * msg - message to print with optional formatting
114 * ... - arguments to use on formatting
115 **************************************************************************/
116 void wfatal(const char *msg, ...)
118 va_list args;
119 char buf[MAXLINE];
121 va_start(args, msg);
123 vsnprintf(buf, MAXLINE - 3, msg, args);
124 strcat(buf, "\n");
125 fflush(stdout);
126 fputs(_WINGS_progname ? _WINGS_progname : "WINGs", stderr);
127 fputs(_(" fatal error: "), stderr);
128 fputs(buf, stderr);
129 fflush(stdout);
130 fflush(stderr);
132 va_end(args);
135 /*********************************************************************
136 * Prints a system error message with variable arguments
138 * msg - message to print with optional formatting
139 * ... - arguments to use on formatting
140 *********************************************************************/
141 void wsyserror(const char *msg, ...)
143 va_list args;
144 char buf[MAXLINE];
145 int error = errno;
147 va_start(args, msg);
148 vsnprintf(buf, MAXLINE - 3, msg, args);
149 fflush(stdout);
150 fputs(_WINGS_progname ? _WINGS_progname : "WINGs", stderr);
151 fputs(_(" error: "), stderr);
152 fputs(buf, stderr);
153 fputs(": ", stderr);
154 fputs(wstrerror(error), stderr);
155 fputs("\n", stderr);
156 fflush(stderr);
157 fflush(stdout);
158 va_end(args);
161 /*********************************************************************
162 * Prints a system error message with variable arguments, being given
163 * the error code.
165 * error - the error code foe which to print the message
166 * msg - message to print with optional formatting
167 * ... - arguments to use on formatting
168 *********************************************************************/
169 void wsyserrorwithcode(int error, const char *msg, ...)
171 va_list args;
172 char buf[MAXLINE];
174 va_start(args, msg);
175 vsnprintf(buf, MAXLINE - 3, msg, args);
176 fflush(stdout);
177 fputs(_WINGS_progname ? _WINGS_progname : "WINGs", stderr);
178 fputs(_(" error: "), stderr);
179 fputs(buf, stderr);
180 fputs(": ", stderr);
181 fputs(wstrerror(error), stderr);
182 fputs("\n", stderr);
183 fflush(stderr);
184 fflush(stdout);
185 va_end(args);