-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy patherrormesg.c
117 lines (102 loc) · 2.87 KB
/
errormesg.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
* Copyright (C) 2000-2005 Erik Edelmann <[email protected]>
*
* This program is free software; you can redistribute it
* and/or modify it under the terms of the GNU General Public
* License version 2 as published by the Free Software
* Foundation;
*
* This program is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the Free
* Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307 USA
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <assert.h>
#include "config.h"
static const char *progname;
void set_progname (const char *prog)
{
progname = prog;
}
static void print_errmesg (const char *kind, const char *fmt, va_list ap)
{
char *s;
int n;
unsigned u;
double d;
char c;
fprintf (stderr, "\n%s: %s: ", progname, kind);
for (; *fmt; fmt++) {
if (*fmt != '%') {
fputc (*fmt, stderr);
} else {
fmt++;
switch (*fmt) {
case 's':
s = va_arg (ap, char *);
fputs (s, stderr);
break;
case 'c':
c = va_arg (ap, int);
fputc(c, stderr);
break;
case 'd':
case 'i':
n = va_arg (ap, int);
fprintf (stderr,"%i", n);
break;
case 'u':
u = va_arg (ap, unsigned);
fprintf (stderr, "%u", u);
break;
case 'f':
d = va_arg (ap, double);
fprintf (stderr, "%f", d);
break;
case 'g':
d = va_arg (ap, double);
fprintf (stderr, "%g", d);
break;
case '%':
fputc ('%', stderr);
break;
case '\0':
break;
default:
assert (0);
break;
}
}
}
fputc ('\n', stderr);
}
void fatal_error (const char *fmt, ...)
/*
* Print a message on stderr and exit with exitcode 'EXIT_FAILURE'
*/
{
va_list ap;
va_start (ap, fmt);
print_errmesg ("ERROR", fmt, ap);
va_end(ap);
exit (EXIT_FAILURE);
}
void warning (const char *fmt, ...)
/*
* Print a message on stderr.
*/
{
va_list ap;
va_start (ap, fmt);
print_errmesg ("WARNING", fmt, ap);
va_end(ap);
}