Change to the linux kernel coding style
[wmaker-crm.git] / WINGs / error.c
Commit [+]AuthorDateLineData
9d2e6ef9 scottc1998-09-29 22:36:29 +00001/*
9af1c6c4 dan1998-10-21 14:43:47 +00002 * Window Maker miscelaneous function library
6830b057 dan2004-10-12 21:28:27 +00003 *
4153e2fd dan2003-01-16 23:30:45 +00004 * Copyright (c) 1997-2003 Alfredo K. Kojima
6830b057 dan2004-10-12 21:28:27 +00005 *
9d2e6ef9 scottc1998-09-29 22:36:29 +00006 * 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
882b9a8e kojima2001-07-23 20:31:32 +000021#include "wconfig.h"
22
9d2e6ef9 scottc1998-09-29 22:36:29 +000023#include <stdio.h>
24#include <stdlib.h>
25#include <stdarg.h>
26#include <string.h>
27#include <errno.h>
28
9d2e6ef9 scottc1998-09-29 22:36:29 +000029extern char *_WINGS_progname;
30
31#define MAXLINE 1024
32
bb75616a dan1999-12-11 00:39:57 +000033/*********************************************************************
34 * Returns the system error message associated with error code 'errnum'
35 *********************************************************************/
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020036char *wstrerror(int errnum)
bb75616a dan1999-12-11 00:39:57 +000037{
38#if defined(HAVE_STRERROR)
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020039 return strerror(errnum);
bb75616a dan1999-12-11 00:39:57 +000040#elif !defined(HAVE_STRERROR) && defined(BSD)
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020041 extern int errno, sys_nerr;
bb75616a dan1999-12-11 00:39:57 +000042# ifndef __DECC
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020043 extern char *sys_errlist[];
bb75616a dan1999-12-11 00:39:57 +000044# endif
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020045 static char buf[] = "Unknown error 12345678901234567890";
bb75616a dan1999-12-11 00:39:57 +000046
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020047 if (errno < sys_nerr)
48 return sys_errlist[errnum];
bb75616a dan1999-12-11 00:39:57 +000049
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020050 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";
bb75616a dan1999-12-11 00:39:57 +000054
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020055 snprintf(buf, sizeof(buf), _("Error %d"), errnum);
56 return buf;
bb75616a dan1999-12-11 00:39:57 +000057#endif
58}
59
debd7ef5 dan2000-11-06 23:13:14 +000060/*********************************************************************
61 * Prints a message with variable arguments
6830b057 dan2004-10-12 21:28:27 +000062 *
9d2e6ef9 scottc1998-09-29 22:36:29 +000063 * msg - message to print with optional formatting
debd7ef5 dan2000-11-06 23:13:14 +000064 * ... - arguments to use on formatting
65 *********************************************************************/
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020066void wmessage(const char *msg, ...)
9d2e6ef9 scottc1998-09-29 22:36:29 +000067{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020068 va_list args;
69 char buf[MAXLINE];
9d2e6ef9 scottc1998-09-29 22:36:29 +000070
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020071 va_start(args, msg);
9d2e6ef9 scottc1998-09-29 22:36:29 +000072
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020073 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);
9d2e6ef9 scottc1998-09-29 22:36:29 +000081
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020082 va_end(args);
9d2e6ef9 scottc1998-09-29 22:36:29 +000083}
84
9d2e6ef9 scottc1998-09-29 22:36:29 +000085/*********************************************************************
6830b057 dan2004-10-12 21:28:27 +000086 * Prints a warning message with variable arguments
87 *
9d2e6ef9 scottc1998-09-29 22:36:29 +000088 * msg - message to print with optional formatting
89 * ... - arguments to use on formatting
90 *********************************************************************/
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020091void wwarning(const char *msg, ...)
9d2e6ef9 scottc1998-09-29 22:36:29 +000092{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020093 va_list args;
94 char buf[MAXLINE];
6830b057 dan2004-10-12 21:28:27 +000095
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020096 va_start(args, msg);
6830b057 dan2004-10-12 21:28:27 +000097
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020098 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);
6830b057 dan2004-10-12 21:28:27 +0000106
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200107 va_end(args);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000108}
109
debd7ef5 dan2000-11-06 23:13:14 +0000110/**************************************************************************
111 * Prints a fatal error message with variable arguments and terminates
6830b057 dan2004-10-12 21:28:27 +0000112 *
debd7ef5 dan2000-11-06 23:13:14 +0000113 * msg - message to print with optional formatting
6830b057 dan2004-10-12 21:28:27 +0000114 * ... - arguments to use on formatting
debd7ef5 dan2000-11-06 23:13:14 +0000115 **************************************************************************/
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200116void wfatal(const char *msg, ...)
debd7ef5 dan2000-11-06 23:13:14 +0000117{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200118 va_list args;
119 char buf[MAXLINE];
debd7ef5 dan2000-11-06 23:13:14 +0000120
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200121 va_start(args, msg);
debd7ef5 dan2000-11-06 23:13:14 +0000122
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200123 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);
debd7ef5 dan2000-11-06 23:13:14 +0000131
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200132 va_end(args);
debd7ef5 dan2000-11-06 23:13:14 +0000133}
134
9d2e6ef9 scottc1998-09-29 22:36:29 +0000135/*********************************************************************
6830b057 dan2004-10-12 21:28:27 +0000136 * Prints a system error message with variable arguments
137 *
9d2e6ef9 scottc1998-09-29 22:36:29 +0000138 * msg - message to print with optional formatting
139 * ... - arguments to use on formatting
140 *********************************************************************/
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200141void wsyserror(const char *msg, ...)
9d2e6ef9 scottc1998-09-29 22:36:29 +0000142{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200143 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);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000159}
160
9e47eba1 dan1999-12-12 04:17:18 +0000161/*********************************************************************
162 * Prints a system error message with variable arguments, being given
163 * the error code.
6830b057 dan2004-10-12 21:28:27 +0000164 *
9e47eba1 dan1999-12-12 04:17:18 +0000165 * 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 *********************************************************************/
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200169void wsyserrorwithcode(int error, const char *msg, ...)
9e47eba1 dan1999-12-12 04:17:18 +0000170{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200171 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);
9e47eba1 dan1999-12-12 04:17:18 +0000186}