util.c revision 141139
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.
7 *
8 * This code is derived from software contributed to Berkeley by
9 * Adam de Boor.
10 *
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.
26 *
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.
38 *
39 * @(#)main.c      8.3 (Berkeley) 3/19/94
40 */
41
42#include <sys/cdefs.h>
43__FBSDID("$FreeBSD: head/usr.bin/make/util.c 141139 2005-02-02 11:25:05Z harti $");
44
45/*-
46 * util.c --
47 *	General utilitarian routines for make(1).
48 */
49
50#include <sys/types.h>
51#include <sys/stat.h>
52#include <err.h>
53#include <errno.h>
54#include <stdarg.h>
55#include <stdlib.h>
56#include <string.h>
57#include <unistd.h>
58
59#include "globals.h"
60#include "job.h"
61#include "targ.h"
62#include "util.h"
63
64static void enomem(void) __dead2;
65
66/*-
67 * Debug --
68 *	Print a debugging message given its format.
69 *
70 * Results:
71 *	None.
72 *
73 * Side Effects:
74 *	The message is printed.
75 */
76/* VARARGS */
77void
78Debug(const char *fmt, ...)
79{
80	va_list ap;
81
82	va_start(ap, fmt);
83	vfprintf(stderr, fmt, ap);
84	va_end(ap);
85	fflush(stderr);
86}
87
88/*-
89 * Error --
90 *	Print an error message given its format.
91 *
92 * Results:
93 *	None.
94 *
95 * Side Effects:
96 *	The message is printed.
97 */
98/* VARARGS */
99void
100Error(const char *fmt, ...)
101{
102	va_list ap;
103
104	va_start(ap, fmt);
105	vfprintf(stderr, fmt, ap);
106	va_end(ap);
107	fprintf(stderr, "\n");
108	fflush(stderr);
109}
110
111/*-
112 * Fatal --
113 *	Produce a Fatal error message. If jobs are running, waits for them
114 *	to finish.
115 *
116 * Results:
117 *	None
118 *
119 * Side Effects:
120 *	The program exits
121 */
122/* VARARGS */
123void
124Fatal(const char *fmt, ...)
125{
126	va_list ap;
127
128	va_start(ap, fmt);
129	if (jobsRunning)
130		Job_Wait();
131
132	vfprintf(stderr, fmt, ap);
133	va_end(ap);
134	fprintf(stderr, "\n");
135	fflush(stderr);
136
137	if (DEBUG(GRAPH2))
138		Targ_PrintGraph(2);
139	exit(2);		/* Not 1 so -q can distinguish error */
140}
141
142/*
143 * Punt --
144 *	Major exception once jobs are being created. Kills all jobs, prints
145 *	a message and exits.
146 *
147 * Results:
148 *	None
149 *
150 * Side Effects:
151 *	All children are killed indiscriminately and the program Lib_Exits
152 */
153/* VARARGS */
154void
155Punt(const char *fmt, ...)
156{
157	va_list ap;
158
159	va_start(ap, fmt);
160	fprintf(stderr, "make: ");
161	vfprintf(stderr, fmt, ap);
162	va_end(ap);
163	fprintf(stderr, "\n");
164	fflush(stderr);
165
166	DieHorribly();
167}
168
169/*-
170 * DieHorribly --
171 *	Exit without giving a message.
172 *
173 * Results:
174 *	None
175 *
176 * Side Effects:
177 *	A big one...
178 */
179void
180DieHorribly(void)
181{
182	if (jobsRunning)
183		Job_AbortAll();
184	if (DEBUG(GRAPH2))
185		Targ_PrintGraph(2);
186	exit(2);		/* Not 1, so -q can distinguish error */
187}
188
189/*
190 * Finish --
191 *	Called when aborting due to errors in child shell to signal
192 *	abnormal exit, with the number of errors encountered in Make_Make.
193 *
194 * Results:
195 *	None
196 *
197 * Side Effects:
198 *	The program exits
199 */
200void
201Finish(int errors)
202{
203
204	Fatal("%d error%s", errors, errors == 1 ? "" : "s");
205}
206
207/*
208 * emalloc --
209 *	malloc, but die on error.
210 */
211void *
212emalloc(size_t len)
213{
214	void *p;
215
216	if ((p = malloc(len)) == NULL)
217		enomem();
218	return (p);
219}
220
221/*
222 * estrdup --
223 *	strdup, but die on error.
224 */
225char *
226estrdup(const char *str)
227{
228	char *p;
229
230	if ((p = strdup(str)) == NULL)
231		enomem();
232	return (p);
233}
234
235/*
236 * erealloc --
237 *	realloc, but die on error.
238 */
239void *
240erealloc(void *ptr, size_t size)
241{
242
243	if ((ptr = realloc(ptr, size)) == NULL)
244		enomem();
245	return (ptr);
246}
247
248/*
249 * enomem --
250 *	die when out of memory.
251 */
252static void
253enomem(void)
254{
255	err(2, NULL);
256}
257
258/*
259 * enunlink --
260 *	Remove a file carefully, avoiding directories.
261 */
262int
263eunlink(const char *file)
264{
265	struct stat st;
266
267	if (lstat(file, &st) == -1)
268		return (-1);
269
270	if (S_ISDIR(st.st_mode)) {
271		errno = EISDIR;
272		return (-1);
273	}
274	return (unlink(file));
275}
276
277/*
278 * Printaddr --
279 * 	Print the address of a node, used as an interative function.
280 */
281int
282PrintAddr(void *a, void *b __unused)
283{
284
285    printf("%p ", a);
286    return (0);
287}
288