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.18 2005/03/12 09:52:54 okumoto Exp $
45 * General utilitarian routines for make(1).
48 #include <sys/types.h>
62 static void enomem(void) __dead2;
66 * Print a debugging message given its format.
72 * The message is printed.
76 Debug(const char *fmt, ...)
81 vfprintf(stderr, fmt, ap);
87 * Print a debugging message given its format and append the current
88 * errno description. Terminate with a newline.
92 DebugM(const char *fmt, ...)
98 vfprintf(stderr, fmt, ap);
99 fprintf(stderr, ": %s\n", strerror(e));
106 * Print an error message given its format.
112 * The message is printed.
116 Error(const char *fmt, ...)
121 vfprintf(stderr, fmt, ap);
123 fprintf(stderr, "\n");
129 * Produce a Fatal error message. If jobs are running, waits for them
140 Fatal(const char *fmt, ...)
148 vfprintf(stderr, fmt, ap);
150 fprintf(stderr, "\n");
155 exit(2); /* Not 1 so -q can distinguish error */
160 * Major exception once jobs are being created. Kills all jobs, prints
161 * a message and exits.
167 * All children are killed indiscriminately and the program Lib_Exits
171 Punt(const char *fmt, ...)
176 fprintf(stderr, "make: ");
177 vfprintf(stderr, fmt, ap);
179 fprintf(stderr, "\n");
187 * Exit without giving a message.
202 exit(2); /* Not 1, so -q can distinguish error */
207 * Called when aborting due to errors in child shell to signal
208 * abnormal exit, with the number of errors encountered in Make_Make.
220 Fatal("%d error%s", errors, errors == 1 ? "" : "s");
225 * malloc, but die on error.
232 if ((p = malloc(len)) == NULL)
239 * strdup, but die on error.
242 estrdup(const char *str)
246 if ((p = strdup(str)) == NULL)
253 * realloc, but die on error.
256 erealloc(void *ptr, size_t size)
259 if ((ptr = realloc(ptr, size)) == NULL)
266 * die when out of memory.
276 * Remove a file carefully, avoiding directories.
279 eunlink(const char *file)
283 if (lstat(file, &st) == -1)
286 if (S_ISDIR(st.st_mode)) {
290 return (unlink(file));