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
8 * This code is derived from software contributed to Berkeley by
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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
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 $
45 * General utilitarian routines for make(1).
48 #include <sys/types.h>
65 * Print a debugging message given its format.
71 * The message is printed.
75 Debug(const char *fmt
, ...)
80 vfprintf(stderr
, fmt
, ap
);
86 * Print a debugging message given its format and append the current
87 * errno description. Terminate with a newline.
91 DebugM(const char *fmt
, ...)
97 vfprintf(stderr
, fmt
, ap
);
98 fprintf(stderr
, ": %s\n", strerror(e
));
105 * Print an error message given its format.
111 * The message is printed.
115 Error(const char *fmt
, ...)
120 vfprintf(stderr
, fmt
, ap
);
122 fprintf(stderr
, "\n");
128 * Produce a Fatal error message. If jobs are running, waits for them
139 Fatal(const char *fmt
, ...)
147 vfprintf(stderr
, fmt
, ap
);
149 fprintf(stderr
, "\n");
154 exit(2); /* Not 1 so -q can distinguish error */
159 * Major exception once jobs are being created. Kills all jobs, prints
160 * a message and exits.
166 * All children are killed indiscriminately and the program Lib_Exits
170 Punt(const char *fmt
, ...)
175 fprintf(stderr
, "make: ");
176 vfprintf(stderr
, fmt
, ap
);
178 fprintf(stderr
, "\n");
186 * Exit without giving a message.
201 exit(2); /* Not 1, so -q can distinguish error */
206 * Called when aborting due to errors in child shell to signal
207 * abnormal exit, with the number of errors encountered in Make_Make.
219 Fatal("%d error%s", errors
, errors
== 1 ? "" : "s");
224 * malloc, but die on error.
231 if ((p
= malloc(size
)) == NULL
)
232 err(2, "malloc(%d)", size
);
238 * strdup, but die on error.
241 estrdup(const char *str
)
245 if ((p
= strdup(str
)) == NULL
)
246 err(2, "strdup(%s)", str
);
252 * realloc, but die on error.
255 erealloc(void *ptr
, size_t size
)
259 if ((p
= realloc(ptr
, size
)) == NULL
)
260 err(2, "realloc(%d)", size
);
266 * Remove a file carefully, avoiding directories.
269 eunlink(const char *file
)
273 if (lstat(file
, &st
) == -1)
276 if (S_ISDIR(st
.st_mode
)) {
280 return (unlink(file
));
284 * Convert a flag word to a printable thing and print it
287 print_flags(FILE *fp
, const struct flag2str
*tab
, u_int flags
, int par
)
293 while (tab
->str
!= NULL
) {
294 if (flags
& tab
->flag
) {
296 fprintf(fp
, par
? "|" : " ");
298 fprintf(fp
, "%s", tab
->str
);
307 * Create a fifo file with a uniq filename, and returns a file
308 * descriptor to that fifo.
311 mkfifotemp(char *template)
316 const char padchar
[] =
317 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
319 if (template[0] == '\0') {
320 errno
= EINVAL
; /* bad input string */
324 /* Find end of template string. */
325 pathend
= strchr(template, '\0');
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
];
338 /* Check the target directory. */
339 for (; ptr
> template; --ptr
) {
344 if (stat(template, &sbuf
) != 0)
347 if (!S_ISDIR(sbuf
.st_mode
)) {
357 if (mkfifo(template, 0600) == 0) {
360 if ((fd
= open(template, O_RDWR
, 0600)) < 0) {
367 if (errno
!= EEXIST
) {
373 * If we have a collision, cycle through the space of
376 for (ptr
= start
;;) {
379 if (*ptr
== '\0' || ptr
== pathend
)
382 pad
= strchr(padchar
, *ptr
);
383 if (pad
== NULL
|| *++pad
== '\0') {