Fix some off-by-ones
[wmaker-crm.git] / WINGs / error.c
blob5661a0414555090216c45869f19979b215aa6ab5
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 #include <WUtil.h>
31 extern char *_WINGS_progname;
33 #define MAXLINE 1024
35 void __wmessage(int type, void *extra, const char *msg, ...)
37 va_list args;
38 char buf[MAXLINE];
40 fflush(stdout);
41 /* message format: <wings_progname>: <qualifier>: <message>[: <extra info>]"\n" */
42 snprintf(buf, sizeof(buf), "%s: ", _WINGS_progname ? _WINGS_progname : "WINGs");
43 va_start(args, msg);
44 switch (type) {
45 case WMESSAGE_TYPE_WARNING:
46 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf) - 1,
47 _("warning: "));
48 vsnprintf(buf + strlen(buf), sizeof(buf) - strlen(buf) - 1,
49 msg, args);
50 break;
51 case WMESSAGE_TYPE_FATAL:
52 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf) - 1,
53 _("fatal error: "));
54 vsnprintf(buf + strlen(buf), sizeof(buf) - strlen(buf) - 1,
55 msg, args);
56 break;
57 case WMESSAGE_TYPE_WSYSERROR:
58 case WMESSAGE_TYPE_WSYSERRORWITHCODE:
59 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf) - 1,
60 _("error: "));
61 vsnprintf(buf + strlen(buf), sizeof(buf) - strlen(buf) - 1,
62 msg, args);
63 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf) - 1,
64 ": %s", type == WMESSAGE_TYPE_WSYSERROR ?
65 strerror(errno) : strerror(*(int *)extra));
66 break;
67 case WMESSAGE_TYPE_MESSAGE:
68 /* FALLTHROUGH */
69 default:
70 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf) - 1, ": ");
71 vsnprintf(buf + strlen(buf), sizeof(buf) - strlen(buf) - 1, msg, args);
72 break;
74 va_end(args);
75 strncat(buf + strlen(buf), "\n", sizeof(buf) - strlen(buf));
76 fputs(buf, stderr);