1 | /* $Id: err.c 3065 2017-09-30 12:52:35Z bird $ */
|
---|
2 | /** @file
|
---|
3 | * Override err.h so we get the program name right.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (c) 2005-2016 knut st. osmundsen <[email protected]>
|
---|
8 | *
|
---|
9 | * This file is part of kBuild.
|
---|
10 | *
|
---|
11 | * kBuild is free software; you can redistribute it and/or modify
|
---|
12 | * it under the terms of the GNU General Public License as published by
|
---|
13 | * the Free Software Foundation; either version 3 of the License, or
|
---|
14 | * (at your option) any later version.
|
---|
15 | *
|
---|
16 | * kBuild is distributed in the hope that it will be useful,
|
---|
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
19 | * GNU General Public License for more details.
|
---|
20 | *
|
---|
21 | * You should have received a copy of the GNU General Public License
|
---|
22 | * along with kBuild. If not, see <http://www.gnu.org/licenses/>
|
---|
23 | *
|
---|
24 | */
|
---|
25 |
|
---|
26 | /*******************************************************************************
|
---|
27 | * Header Files *
|
---|
28 | *******************************************************************************/
|
---|
29 | #include "config.h"
|
---|
30 | #include <stdio.h>
|
---|
31 | #include <stdarg.h>
|
---|
32 | #include <string.h>
|
---|
33 | #include <errno.h>
|
---|
34 | #include "err.h"
|
---|
35 |
|
---|
36 | #ifdef KBUILD_OS_WINDOWS
|
---|
37 | /* This is a trick to speed up console output on windows. */
|
---|
38 | # undef fwrite
|
---|
39 | # define fwrite maybe_con_fwrite
|
---|
40 | extern size_t maybe_con_fwrite(void const *, size_t, size_t, FILE *);
|
---|
41 | #endif
|
---|
42 |
|
---|
43 |
|
---|
44 | /** The current program name. */
|
---|
45 | const char *g_progname = "kmk";
|
---|
46 |
|
---|
47 |
|
---|
48 | int err(int eval, const char *fmt, ...)
|
---|
49 | {
|
---|
50 | va_list args;
|
---|
51 | int error = errno;
|
---|
52 |
|
---|
53 | /* stderr is unbuffered, so try format the whole message and print it in
|
---|
54 | one go so it won't be split by other output. */
|
---|
55 | char szMsg[4096];
|
---|
56 | int cchMsg = snprintf(szMsg, sizeof(szMsg), "%s: ", g_progname);
|
---|
57 | if (cchMsg < (int)sizeof(szMsg) - 1 && cchMsg > 0)
|
---|
58 | {
|
---|
59 | int cchMsg2;
|
---|
60 | va_start(args, fmt);
|
---|
61 | cchMsg += cchMsg2 = vsnprintf(&szMsg[cchMsg], sizeof(szMsg) - cchMsg, fmt, args);
|
---|
62 | va_end(args);
|
---|
63 |
|
---|
64 | if ( cchMsg < (int)sizeof(szMsg) - 1
|
---|
65 | && cchMsg2 >= 0)
|
---|
66 | {
|
---|
67 | cchMsg += cchMsg2 = snprintf(&szMsg[cchMsg], sizeof(szMsg) - cchMsg, ": %s\n", strerror(error));
|
---|
68 | if ( cchMsg < (int)sizeof(szMsg) - 1
|
---|
69 | && cchMsg2 >= 0)
|
---|
70 | {
|
---|
71 | fwrite(szMsg, cchMsg, 1, stderr);
|
---|
72 | return eval;
|
---|
73 | }
|
---|
74 |
|
---|
75 | }
|
---|
76 |
|
---|
77 | }
|
---|
78 |
|
---|
79 | /* fallback */
|
---|
80 | fprintf(stderr, "%s: ", g_progname);
|
---|
81 | va_start(args, fmt);
|
---|
82 | vfprintf(stderr, fmt, args);
|
---|
83 | va_end(args);
|
---|
84 | fprintf(stderr, ": %s\n", strerror(error));
|
---|
85 |
|
---|
86 | return eval;
|
---|
87 | }
|
---|
88 |
|
---|
89 |
|
---|
90 | int errx(int eval, const char *fmt, ...)
|
---|
91 | {
|
---|
92 | va_list args;
|
---|
93 |
|
---|
94 | /* stderr is unbuffered, so try format the whole message and print it in
|
---|
95 | one go so it won't be split by other output. */
|
---|
96 | char szMsg[4096];
|
---|
97 | int cchMsg = snprintf(szMsg, sizeof(szMsg), "%s: ", g_progname);
|
---|
98 | if (cchMsg < (int)sizeof(szMsg) - 1 && cchMsg > 0)
|
---|
99 | {
|
---|
100 | int cchMsg2;
|
---|
101 | va_start(args, fmt);
|
---|
102 | cchMsg += cchMsg2 = vsnprintf(&szMsg[cchMsg], sizeof(szMsg) - cchMsg, fmt, args);
|
---|
103 | va_end(args);
|
---|
104 |
|
---|
105 | if ( cchMsg < (int)sizeof(szMsg) - 1
|
---|
106 | && cchMsg2 >= 0)
|
---|
107 | {
|
---|
108 | szMsg[cchMsg++] = '\n';
|
---|
109 | fwrite(szMsg, cchMsg, 1, stderr);
|
---|
110 | return eval;
|
---|
111 | }
|
---|
112 |
|
---|
113 | }
|
---|
114 |
|
---|
115 | /* fallback */
|
---|
116 | fprintf(stderr, "%s: ", g_progname);
|
---|
117 | va_start(args, fmt);
|
---|
118 | vfprintf(stderr, fmt, args);
|
---|
119 | va_end(args);
|
---|
120 | fprintf(stderr, "\n");
|
---|
121 |
|
---|
122 | return eval;
|
---|
123 | }
|
---|
124 |
|
---|
125 | void warn(const char *fmt, ...)
|
---|
126 | {
|
---|
127 | int error = errno;
|
---|
128 | va_list args;
|
---|
129 |
|
---|
130 | /* stderr is unbuffered, so try format the whole message and print it in
|
---|
131 | one go so it won't be split by other output. */
|
---|
132 | char szMsg[4096];
|
---|
133 | int cchMsg = snprintf(szMsg, sizeof(szMsg), "%s: ", g_progname);
|
---|
134 | if (cchMsg < (int)sizeof(szMsg) - 1 && cchMsg > 0)
|
---|
135 | {
|
---|
136 | int cchMsg2;
|
---|
137 | va_start(args, fmt);
|
---|
138 | cchMsg += cchMsg2 = vsnprintf(&szMsg[cchMsg], sizeof(szMsg) - cchMsg, fmt, args);
|
---|
139 | va_end(args);
|
---|
140 |
|
---|
141 | if ( cchMsg < (int)sizeof(szMsg) - 1
|
---|
142 | && cchMsg2 >= 0)
|
---|
143 | {
|
---|
144 | cchMsg += cchMsg2 = snprintf(&szMsg[cchMsg], sizeof(szMsg) - cchMsg, ": %s\n", strerror(error));
|
---|
145 | if ( cchMsg < (int)sizeof(szMsg) - 1
|
---|
146 | && cchMsg2 >= 0)
|
---|
147 | {
|
---|
148 | fwrite(szMsg, cchMsg, 1, stderr);
|
---|
149 | return;
|
---|
150 | }
|
---|
151 |
|
---|
152 | }
|
---|
153 | }
|
---|
154 |
|
---|
155 | /* fallback */
|
---|
156 | fprintf(stderr, "%s: ", g_progname);
|
---|
157 | va_start(args, fmt);
|
---|
158 | vfprintf(stderr, fmt, args);
|
---|
159 | va_end(args);
|
---|
160 | fprintf(stderr, ": %s\n", strerror(error));
|
---|
161 | }
|
---|
162 |
|
---|
163 | void warnx(const char *fmt, ...)
|
---|
164 | {
|
---|
165 | va_list args;
|
---|
166 |
|
---|
167 | /* stderr is unbuffered, so try format the whole message and print it in
|
---|
168 | one go so it won't be split by other output. */
|
---|
169 | char szMsg[4096];
|
---|
170 | int cchMsg = snprintf(szMsg, sizeof(szMsg), "%s: ", g_progname);
|
---|
171 | if (cchMsg < (int)sizeof(szMsg) - 1 && cchMsg > 0)
|
---|
172 | {
|
---|
173 | int cchMsg2;
|
---|
174 | va_start(args, fmt);
|
---|
175 | cchMsg += cchMsg2 = vsnprintf(&szMsg[cchMsg], sizeof(szMsg) - cchMsg, fmt, args);
|
---|
176 | va_end(args);
|
---|
177 |
|
---|
178 | if ( cchMsg < (int)sizeof(szMsg) - 1
|
---|
179 | && cchMsg2 >= 0)
|
---|
180 | {
|
---|
181 | szMsg[cchMsg++] = '\n';
|
---|
182 | fwrite(szMsg, cchMsg, 1, stderr);
|
---|
183 | return;
|
---|
184 | }
|
---|
185 |
|
---|
186 | }
|
---|
187 |
|
---|
188 | /* fallback */
|
---|
189 | fprintf(stderr, "%s: ", g_progname);
|
---|
190 | va_start(args, fmt);
|
---|
191 | vfprintf(stderr, fmt, args);
|
---|
192 | va_end(args);
|
---|
193 | fprintf(stderr, "\n");
|
---|
194 | }
|
---|
195 |
|
---|