Merge from vendor branch OPENSSH:
[dragonfly.git] / usr.bin / make / util.c
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  * $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.23 2005/05/15 17:50:09 okumoto Exp $
41  */
42
43 /*-
44  * util.c --
45  *      General utilitarian routines for make(1).
46  */
47
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
57 #include "globals.h"
58 #include "job.h"
59 #include "targ.h"
60 #include "util.h"
61
62 /*-
63  * Debug --
64  *      Print a debugging message given its format.
65  *
66  * Results:
67  *      None.
68  *
69  * Side Effects:
70  *      The message is printed.
71  */
72 /* VARARGS */
73 void
74 Debug(const char *fmt, ...)
75 {
76         va_list ap;
77
78         va_start(ap, fmt);
79         vfprintf(stderr, fmt, ap);
80         va_end(ap);
81         fflush(stderr);
82 }
83
84 /*-
85  * Print a debugging message given its format and append the current
86  * errno description. Terminate with a newline.
87  */
88 /* VARARGS */
89 void
90 DebugM(const char *fmt, ...)
91 {
92         va_list ap;
93         int e = errno;
94
95         va_start(ap, fmt);
96         vfprintf(stderr, fmt, ap);
97         fprintf(stderr, ": %s\n", strerror(e));
98         va_end(ap);
99         fflush(stderr);
100 }
101
102 /*-
103  * Error --
104  *      Print an error message given its format.
105  *
106  * Results:
107  *      None.
108  *
109  * Side Effects:
110  *      The message is printed.
111  */
112 /* VARARGS */
113 void
114 Error(const char *fmt, ...)
115 {
116         va_list ap;
117
118         va_start(ap, fmt);
119         vfprintf(stderr, fmt, ap);
120         va_end(ap);
121         fprintf(stderr, "\n");
122         fflush(stderr);
123 }
124
125 /*-
126  * Fatal --
127  *      Produce a Fatal error message. If jobs are running, waits for them
128  *      to finish.
129  *
130  * Results:
131  *      None
132  *
133  * Side Effects:
134  *      The program exits
135  */
136 /* VARARGS */
137 void
138 Fatal(const char *fmt, ...)
139 {
140         va_list ap;
141
142         va_start(ap, fmt);
143         if (jobsRunning)
144                 Job_Wait();
145
146         vfprintf(stderr, fmt, ap);
147         va_end(ap);
148         fprintf(stderr, "\n");
149         fflush(stderr);
150
151         if (DEBUG(GRAPH2))
152                 Targ_PrintGraph(2);
153         exit(2);                /* Not 1 so -q can distinguish error */
154 }
155
156 /*
157  * Punt --
158  *      Major exception once jobs are being created. Kills all jobs, prints
159  *      a message and exits.
160  *
161  * Results:
162  *      None
163  *
164  * Side Effects:
165  *      All children are killed indiscriminately and the program Lib_Exits
166  */
167 /* VARARGS */
168 void
169 Punt(const char *fmt, ...)
170 {
171         va_list ap;
172
173         va_start(ap, fmt);
174         fprintf(stderr, "make: ");
175         vfprintf(stderr, fmt, ap);
176         va_end(ap);
177         fprintf(stderr, "\n");
178         fflush(stderr);
179
180         DieHorribly();
181 }
182
183 /*-
184  * DieHorribly --
185  *      Exit without giving a message.
186  *
187  * Results:
188  *      None
189  *
190  * Side Effects:
191  *      A big one...
192  */
193 void
194 DieHorribly(void)
195 {
196         if (jobsRunning)
197                 Job_AbortAll();
198         if (DEBUG(GRAPH2))
199                 Targ_PrintGraph(2);
200         exit(2);                /* Not 1, so -q can distinguish error */
201 }
202
203 /*
204  * Finish --
205  *      Called when aborting due to errors in child shell to signal
206  *      abnormal exit, with the number of errors encountered in Make_Make.
207  *
208  * Results:
209  *      None
210  *
211  * Side Effects:
212  *      The program exits
213  */
214 void
215 Finish(int errors)
216 {
217
218         Fatal("%d error%s", errors, errors == 1 ? "" : "s");
219 }
220
221 /*
222  * emalloc --
223  *      malloc, but die on error.
224  */
225 void *
226 emalloc(size_t size)
227 {
228         void *p;
229
230         if ((p = malloc(size)) == NULL)
231                 err(2, "malloc(%d)", size);
232         return (p);
233 }
234
235 /*
236  * estrdup --
237  *      strdup, but die on error.
238  */
239 char *
240 estrdup(const char *str)
241 {
242         char *p;
243
244         if ((p = strdup(str)) == NULL)
245                 err(2, "strdup(%s)", str);
246         return (p);
247 }
248
249 /*
250  * erealloc --
251  *      realloc, but die on error.
252  */
253 void *
254 erealloc(void *ptr, size_t size)
255 {
256         char *p;
257
258         if ((p = realloc(ptr, size)) == NULL)
259                 err(2, "realloc(%d)", size);
260         return (p);
261 }
262
263 /*
264  * enunlink --
265  *      Remove a file carefully, avoiding directories.
266  */
267 int
268 eunlink(const char *file)
269 {
270         struct stat st;
271
272         if (lstat(file, &st) == -1)
273                 return (-1);
274
275         if (S_ISDIR(st.st_mode)) {
276                 errno = EISDIR;
277                 return (-1);
278         }
279         return (unlink(file));
280 }
281
282 /*
283  * Convert a flag word to a printable thing and print it
284  */
285 void
286 print_flags(FILE *fp, const struct flag2str *tab, u_int flags, int par)
287 {
288         int first = 1;
289
290         if (par)
291                 fprintf(fp, "(");
292         while (tab->str != NULL) {
293                 if (flags & tab->flag) {
294                         if (!first)
295                                 fprintf(fp, par ? "|" : " ");
296                         first = 0;
297                         fprintf(fp, "%s", tab->str);
298                 }
299                 tab++;
300         }
301         if (par)
302                 fprintf(fp, ")");
303 }