Change to the linux kernel coding style
[wmaker-crm.git] / WINGs / error.c
1 /*
2  *  Window Maker miscelaneous function library
3  *
4  *  Copyright (c) 1997-2003 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.
10  *
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.
15  *
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.
19  */
20
21 #include "wconfig.h"
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <stdarg.h>
26 #include <string.h>
27 #include <errno.h>
28
29 extern char *_WINGS_progname;
30
31 #define MAXLINE 1024
32
33 /*********************************************************************
34  * Returns the system error message associated with error code 'errnum'
35  *********************************************************************/
36 char *wstrerror(int errnum)
37 {
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";
46
47         if (errno < sys_nerr)
48                 return sys_errlist[errnum];
49
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";
54
55         snprintf(buf, sizeof(buf), _("Error %d"), errnum);
56         return buf;
57 #endif
58 }
59
60 /*********************************************************************
61  * Prints a message with variable arguments
62  *
63  * msg - message to print with optional formatting
64  * ... - arguments to use on formatting
65  *********************************************************************/
66 void wmessage(const char *msg, ...)
67 {
68         va_list args;
69         char buf[MAXLINE];
70
71         va_start(args, msg);
72
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);
81
82         va_end(args);
83 }
84
85 /*********************************************************************
86  * Prints a warning message with variable arguments
87  *
88  * msg - message to print with optional formatting
89  * ... - arguments to use on formatting
90  *********************************************************************/
91 void wwarning(const char *msg, ...)
92 {
93         va_list args;
94         char buf[MAXLINE];
95
96         va_start(args, msg);
97
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);
106
107         va_end(args);
108 }
109
110 /**************************************************************************
111  * Prints a fatal error message with variable arguments and terminates
112  *
113  * msg - message to print with optional formatting
114  * ... - arguments to use on formatting
115  **************************************************************************/
116 void wfatal(const char *msg, ...)
117 {
118         va_list args;
119         char buf[MAXLINE];
120
121         va_start(args, msg);
122
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);
131
132         va_end(args);
133 }
134
135 /*********************************************************************
136  * Prints a system error message with variable arguments
137  *
138  * msg - message to print with optional formatting
139  * ... - arguments to use on formatting
140  *********************************************************************/
141 void wsyserror(const char *msg, ...)
142 {
143         va_list args;
144         char buf[MAXLINE];
145         int error = errno;
146
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);
159 }
160
161 /*********************************************************************
162  * Prints a system error message with variable arguments, being given
163  * the error code.
164  *
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, ...)
170 {
171         va_list args;
172         char buf[MAXLINE];
173
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);
186 }