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