Initialize intrcnt[] with 0 before using it, because hw.intrcnt
[dragonfly.git] / usr.bin / make / util.c
blob9da2c51e7742e996e41440248e707b4e7e774504
1 /*-
2 * Copyright (c) 2002 Juli Mallett. All rights reserved.
3 * Copyright (c) 1988, 1989, 1990, 1993
4 * The Regents of the University of California. All rights reserved.
5 * Copyright (c) 1989 by Berkeley Softworks
6 * All rights reserved.
8 * This code is derived from software contributed to Berkeley by
9 * Adam de Boor.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
39 * $FreeBSD: src/usr.bin/make/util.c,v 1.16 2005/02/04 13:23:39 harti Exp $
40 * $DragonFly: src/usr.bin/make/util.c,v 1.24 2005/07/13 20:40:52 okumoto Exp $
43 /*-
44 * util.c --
45 * General utilitarian routines for make(1).
48 #include <sys/types.h>
49 #include <sys/stat.h>
50 #include <err.h>
51 #include <errno.h>
52 #include <stdarg.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <unistd.h>
56 #include <fcntl.h>
58 #include "globals.h"
59 #include "job.h"
60 #include "targ.h"
61 #include "util.h"
63 /*-
64 * Debug --
65 * Print a debugging message given its format.
67 * Results:
68 * None.
70 * Side Effects:
71 * The message is printed.
73 /* VARARGS */
74 void
75 Debug(const char *fmt, ...)
77 va_list ap;
79 va_start(ap, fmt);
80 vfprintf(stderr, fmt, ap);
81 va_end(ap);
82 fflush(stderr);
85 /*-
86 * Print a debugging message given its format and append the current
87 * errno description. Terminate with a newline.
89 /* VARARGS */
90 void
91 DebugM(const char *fmt, ...)
93 va_list ap;
94 int e = errno;
96 va_start(ap, fmt);
97 vfprintf(stderr, fmt, ap);
98 fprintf(stderr, ": %s\n", strerror(e));
99 va_end(ap);
100 fflush(stderr);
104 * Error --
105 * Print an error message given its format.
107 * Results:
108 * None.
110 * Side Effects:
111 * The message is printed.
113 /* VARARGS */
114 void
115 Error(const char *fmt, ...)
117 va_list ap;
119 va_start(ap, fmt);
120 vfprintf(stderr, fmt, ap);
121 va_end(ap);
122 fprintf(stderr, "\n");
123 fflush(stderr);
127 * Fatal --
128 * Produce a Fatal error message. If jobs are running, waits for them
129 * to finish.
131 * Results:
132 * None
134 * Side Effects:
135 * The program exits
137 /* VARARGS */
138 void
139 Fatal(const char *fmt, ...)
141 va_list ap;
143 va_start(ap, fmt);
144 if (jobsRunning)
145 Job_Wait();
147 vfprintf(stderr, fmt, ap);
148 va_end(ap);
149 fprintf(stderr, "\n");
150 fflush(stderr);
152 if (DEBUG(GRAPH2))
153 Targ_PrintGraph(2);
154 exit(2); /* Not 1 so -q can distinguish error */
158 * Punt --
159 * Major exception once jobs are being created. Kills all jobs, prints
160 * a message and exits.
162 * Results:
163 * None
165 * Side Effects:
166 * All children are killed indiscriminately and the program Lib_Exits
168 /* VARARGS */
169 void
170 Punt(const char *fmt, ...)
172 va_list ap;
174 va_start(ap, fmt);
175 fprintf(stderr, "make: ");
176 vfprintf(stderr, fmt, ap);
177 va_end(ap);
178 fprintf(stderr, "\n");
179 fflush(stderr);
181 DieHorribly();
185 * DieHorribly --
186 * Exit without giving a message.
188 * Results:
189 * None
191 * Side Effects:
192 * A big one...
194 void
195 DieHorribly(void)
197 if (jobsRunning)
198 Job_AbortAll();
199 if (DEBUG(GRAPH2))
200 Targ_PrintGraph(2);
201 exit(2); /* Not 1, so -q can distinguish error */
205 * Finish --
206 * Called when aborting due to errors in child shell to signal
207 * abnormal exit, with the number of errors encountered in Make_Make.
209 * Results:
210 * None
212 * Side Effects:
213 * The program exits
215 void
216 Finish(int errors)
219 Fatal("%d error%s", errors, errors == 1 ? "" : "s");
223 * emalloc --
224 * malloc, but die on error.
226 void *
227 emalloc(size_t size)
229 void *p;
231 if ((p = malloc(size)) == NULL)
232 err(2, "malloc(%d)", size);
233 return (p);
237 * estrdup --
238 * strdup, but die on error.
240 char *
241 estrdup(const char *str)
243 char *p;
245 if ((p = strdup(str)) == NULL)
246 err(2, "strdup(%s)", str);
247 return (p);
251 * erealloc --
252 * realloc, but die on error.
254 void *
255 erealloc(void *ptr, size_t size)
257 char *p;
259 if ((p = realloc(ptr, size)) == NULL)
260 err(2, "realloc(%d)", size);
261 return (p);
265 * enunlink --
266 * Remove a file carefully, avoiding directories.
269 eunlink(const char *file)
271 struct stat st;
273 if (lstat(file, &st) == -1)
274 return (-1);
276 if (S_ISDIR(st.st_mode)) {
277 errno = EISDIR;
278 return (-1);
280 return (unlink(file));
284 * Convert a flag word to a printable thing and print it
286 void
287 print_flags(FILE *fp, const struct flag2str *tab, u_int flags, int par)
289 int first = 1;
291 if (par)
292 fprintf(fp, "(");
293 while (tab->str != NULL) {
294 if (flags & tab->flag) {
295 if (!first)
296 fprintf(fp, par ? "|" : " ");
297 first = 0;
298 fprintf(fp, "%s", tab->str);
300 tab++;
302 if (par)
303 fprintf(fp, ")");
307 * Create a fifo file with a uniq filename, and returns a file
308 * descriptor to that fifo.
311 mkfifotemp(char *template)
313 char *start;
314 char *pathend;
315 char *ptr;
316 const char padchar[] =
317 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
319 if (template[0] == '\0') {
320 errno = EINVAL; /* bad input string */
321 return (-1);
324 /* Find end of template string. */
325 pathend = strchr(template, '\0');
326 ptr = pathend - 1;
329 * Starting from the end of the template replace spaces with 'X' in
330 * them with random characters until there are no more 'X'.
332 while (ptr >= template && *ptr == 'X') {
333 uint32_t rand_num = arc4random() % (sizeof(padchar) - 1);
334 *ptr-- = padchar[rand_num];
336 start = ptr + 1;
338 /* Check the target directory. */
339 for (; ptr > template; --ptr) {
340 if (*ptr == '/') {
341 struct stat sbuf;
343 *ptr = '\0';
344 if (stat(template, &sbuf) != 0)
345 return (-1);
347 if (!S_ISDIR(sbuf.st_mode)) {
348 errno = ENOTDIR;
349 return (-1);
351 *ptr = '/';
352 break;
356 for (;;) {
357 if (mkfifo(template, 0600) == 0) {
358 int fd;
360 if ((fd = open(template, O_RDWR, 0600)) < 0) {
361 unlink(template);
362 return (-1);
363 } else {
364 return (fd);
366 } else {
367 if (errno != EEXIST) {
368 return (-1);
373 * If we have a collision, cycle through the space of
374 * filenames.
376 for (ptr = start;;) {
377 char *pad;
379 if (*ptr == '\0' || ptr == pathend)
380 return (-1);
382 pad = strchr(padchar, *ptr);
383 if (pad == NULL || *++pad == '\0') {
384 *ptr++ = padchar[0];
385 } else {
386 *ptr++ = *pad;
387 break;
391 /*NOTREACHED*/