[2579] | 1 | /* Miscellaneous generic support functions for GNU Make.
|
---|
| 2 | Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
|
---|
| 3 | 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
|
---|
| 4 | 2010 Free Software Foundation, Inc.
|
---|
| 5 | This file is part of GNU Make.
|
---|
| 6 |
|
---|
| 7 | GNU Make is free software; you can redistribute it and/or modify it under the
|
---|
| 8 | terms of the GNU General Public License as published by the Free Software
|
---|
| 9 | Foundation; either version 3 of the License, or (at your option) any later
|
---|
| 10 | version.
|
---|
| 11 |
|
---|
| 12 | GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
|
---|
| 13 | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
|
---|
| 14 | A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
---|
| 15 |
|
---|
| 16 | You should have received a copy of the GNU General Public License along with
|
---|
| 17 | this program. If not, see <http://www.gnu.org/licenses/>. */
|
---|
| 18 |
|
---|
| 19 | #include "make.h"
|
---|
| 20 | #include "dep.h"
|
---|
| 21 | #include "debug.h"
|
---|
| 22 |
|
---|
| 23 | /* Variadic functions. We go through contortions to allow proper function
|
---|
| 24 | prototypes for both ANSI and pre-ANSI C compilers, and also for those
|
---|
| 25 | which support stdarg.h vs. varargs.h, and finally those which have
|
---|
| 26 | vfprintf(), etc. and those who have _doprnt... or nothing.
|
---|
| 27 |
|
---|
| 28 | This fancy stuff all came from GNU fileutils, except for the VA_PRINTF and
|
---|
| 29 | VA_END macros used here since we have multiple print functions. */
|
---|
| 30 |
|
---|
| 31 | #if USE_VARIADIC
|
---|
| 32 | # if HAVE_STDARG_H
|
---|
| 33 | # include <stdarg.h>
|
---|
| 34 | # define VA_START(args, lastarg) va_start(args, lastarg)
|
---|
| 35 | # else
|
---|
| 36 | # include <varargs.h>
|
---|
| 37 | # define VA_START(args, lastarg) va_start(args)
|
---|
| 38 | # endif
|
---|
| 39 | # if HAVE_VPRINTF
|
---|
| 40 | # define VA_PRINTF(fp, lastarg, args) vfprintf((fp), (lastarg), (args))
|
---|
| 41 | # else
|
---|
| 42 | # define VA_PRINTF(fp, lastarg, args) _doprnt((lastarg), (args), (fp))
|
---|
| 43 | # endif
|
---|
| 44 | # define VA_END(args) va_end(args)
|
---|
| 45 | #else
|
---|
| 46 | /* We can't use any variadic interface! */
|
---|
| 47 | # define va_alist a1, a2, a3, a4, a5, a6, a7, a8
|
---|
| 48 | # define va_dcl char *a1, *a2, *a3, *a4, *a5, *a6, *a7, *a8;
|
---|
| 49 | # define VA_START(args, lastarg)
|
---|
| 50 | # define VA_PRINTF(fp, lastarg, args) fprintf((fp), (lastarg), va_alist)
|
---|
| 51 | # define VA_END(args)
|
---|
| 52 | #endif
|
---|
| 53 |
|
---|
| 54 |
|
---|
| 55 | /* Compare strings *S1 and *S2.
|
---|
| 56 | Return negative if the first is less, positive if it is greater,
|
---|
| 57 | zero if they are equal. */
|
---|
| 58 |
|
---|
| 59 | int
|
---|
| 60 | alpha_compare (const void *v1, const void *v2)
|
---|
| 61 | {
|
---|
| 62 | const char *s1 = *((char **)v1);
|
---|
| 63 | const char *s2 = *((char **)v2);
|
---|
| 64 |
|
---|
| 65 | if (*s1 != *s2)
|
---|
| 66 | return *s1 - *s2;
|
---|
| 67 | return strcmp (s1, s2);
|
---|
| 68 | }
|
---|
| 69 | |
---|
| 70 |
|
---|
| 71 | /* Discard each backslash-newline combination from LINE.
|
---|
| 72 | Backslash-backslash-newline combinations become backslash-newlines.
|
---|
| 73 | This is done by copying the text at LINE into itself. */
|
---|
| 74 |
|
---|
| 75 | void
|
---|
| 76 | collapse_continuations (char *line)
|
---|
| 77 | {
|
---|
| 78 | register char *in, *out, *p;
|
---|
| 79 | register int backslash;
|
---|
| 80 | register unsigned int bs_write;
|
---|
| 81 |
|
---|
| 82 | in = strchr (line, '\n');
|
---|
| 83 | if (in == 0)
|
---|
| 84 | return;
|
---|
| 85 |
|
---|
| 86 | out = in;
|
---|
| 87 | while (out > line && out[-1] == '\\')
|
---|
| 88 | --out;
|
---|
| 89 |
|
---|
| 90 | while (*in != '\0')
|
---|
| 91 | {
|
---|
| 92 | /* BS_WRITE gets the number of quoted backslashes at
|
---|
| 93 | the end just before IN, and BACKSLASH gets nonzero
|
---|
| 94 | if the next character is quoted. */
|
---|
| 95 | backslash = 0;
|
---|
| 96 | bs_write = 0;
|
---|
| 97 | for (p = in - 1; p >= line && *p == '\\'; --p)
|
---|
| 98 | {
|
---|
| 99 | if (backslash)
|
---|
| 100 | ++bs_write;
|
---|
| 101 | backslash = !backslash;
|
---|
| 102 |
|
---|
| 103 | /* It should be impossible to go back this far without exiting,
|
---|
| 104 | but if we do, we can't get the right answer. */
|
---|
| 105 | if (in == out - 1)
|
---|
| 106 | abort ();
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | /* Output the appropriate number of backslashes. */
|
---|
| 110 | while (bs_write-- > 0)
|
---|
| 111 | *out++ = '\\';
|
---|
| 112 |
|
---|
| 113 | /* Skip the newline. */
|
---|
| 114 | ++in;
|
---|
| 115 |
|
---|
| 116 | /* If the newline is escaped, discard following whitespace leaving just
|
---|
| 117 | one space. POSIX requires that each backslash/newline/following
|
---|
| 118 | whitespace sequence be reduced to a single space. */
|
---|
| 119 | if (backslash)
|
---|
| 120 | {
|
---|
| 121 | in = next_token (in);
|
---|
| 122 | /* Removing this loop will fix Savannah bug #16670: do we want to? */
|
---|
| 123 | while (out > line && isblank ((unsigned char)out[-1]))
|
---|
| 124 | --out;
|
---|
| 125 | *out++ = ' ';
|
---|
| 126 | }
|
---|
| 127 | else
|
---|
| 128 | /* If the newline isn't quoted, put it in the output. */
|
---|
| 129 | *out++ = '\n';
|
---|
| 130 |
|
---|
| 131 | /* Now copy the following line to the output.
|
---|
| 132 | Stop when we find backslashes followed by a newline. */
|
---|
| 133 | while (*in != '\0')
|
---|
| 134 | if (*in == '\\')
|
---|
| 135 | {
|
---|
| 136 | p = in + 1;
|
---|
| 137 | while (*p == '\\')
|
---|
| 138 | ++p;
|
---|
| 139 | if (*p == '\n')
|
---|
| 140 | {
|
---|
| 141 | in = p;
|
---|
| 142 | break;
|
---|
| 143 | }
|
---|
| 144 | while (in < p)
|
---|
| 145 | *out++ = *in++;
|
---|
| 146 | }
|
---|
| 147 | else
|
---|
| 148 | *out++ = *in++;
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | *out = '\0';
|
---|
| 152 | }
|
---|
| 153 | |
---|
| 154 |
|
---|
| 155 | /* Print N spaces (used in debug for target-depth). */
|
---|
| 156 |
|
---|
| 157 | void
|
---|
| 158 | print_spaces (unsigned int n)
|
---|
| 159 | {
|
---|
| 160 | while (n-- > 0)
|
---|
| 161 | putchar (' ');
|
---|
| 162 | }
|
---|
| 163 |
|
---|
| 164 | |
---|
| 165 |
|
---|
| 166 | /* Return a string whose contents concatenate the NUM strings provided
|
---|
| 167 | This string lives in static, re-used memory. */
|
---|
| 168 |
|
---|
| 169 | const char *
|
---|
| 170 | #if HAVE_ANSI_COMPILER && USE_VARIADIC && HAVE_STDARG_H
|
---|
| 171 | concat (unsigned int num, ...)
|
---|
| 172 | #else
|
---|
| 173 | concat (num, va_alist)
|
---|
| 174 | unsigned int num;
|
---|
| 175 | va_dcl
|
---|
| 176 | #endif
|
---|
| 177 | {
|
---|
| 178 | static unsigned int rlen = 0;
|
---|
| 179 | static char *result = NULL;
|
---|
| 180 | int ri = 0;
|
---|
| 181 |
|
---|
| 182 | #if USE_VARIADIC
|
---|
| 183 | va_list args;
|
---|
| 184 | #endif
|
---|
| 185 |
|
---|
| 186 | VA_START (args, num);
|
---|
| 187 |
|
---|
| 188 | while (num-- > 0)
|
---|
| 189 | {
|
---|
| 190 | const char *s = va_arg (args, const char *);
|
---|
| 191 | unsigned int l = s ? strlen (s) : 0;
|
---|
| 192 |
|
---|
| 193 | if (l == 0)
|
---|
| 194 | continue;
|
---|
| 195 |
|
---|
| 196 | if (ri + l > rlen)
|
---|
| 197 | {
|
---|
| 198 | rlen = ((rlen ? rlen : 60) + l) * 2;
|
---|
| 199 | result = xrealloc (result, rlen);
|
---|
| 200 | }
|
---|
| 201 |
|
---|
| 202 | memcpy (result + ri, s, l);
|
---|
| 203 | ri += l;
|
---|
| 204 | }
|
---|
| 205 |
|
---|
| 206 | VA_END (args);
|
---|
| 207 |
|
---|
| 208 | /* Get some more memory if we don't have enough space for the
|
---|
| 209 | terminating '\0'. */
|
---|
| 210 | if (ri == rlen)
|
---|
| 211 | {
|
---|
| 212 | rlen = (rlen ? rlen : 60) * 2;
|
---|
| 213 | result = xrealloc (result, rlen);
|
---|
| 214 | }
|
---|
| 215 |
|
---|
| 216 | result[ri] = '\0';
|
---|
| 217 |
|
---|
| 218 | return result;
|
---|
| 219 | }
|
---|
| 220 | |
---|
| 221 |
|
---|
| 222 | /* Print a message on stdout. */
|
---|
| 223 |
|
---|
| 224 | void
|
---|
| 225 | #if HAVE_ANSI_COMPILER && USE_VARIADIC && HAVE_STDARG_H
|
---|
| 226 | message (int prefix, const char *fmt, ...)
|
---|
| 227 | #else
|
---|
| 228 | message (prefix, fmt, va_alist)
|
---|
| 229 | int prefix;
|
---|
| 230 | const char *fmt;
|
---|
| 231 | va_dcl
|
---|
| 232 | #endif
|
---|
| 233 | {
|
---|
| 234 | #if USE_VARIADIC
|
---|
| 235 | va_list args;
|
---|
| 236 | #endif
|
---|
| 237 |
|
---|
| 238 | log_working_directory (1);
|
---|
| 239 |
|
---|
| 240 | if (fmt != 0)
|
---|
| 241 | {
|
---|
| 242 | if (prefix)
|
---|
| 243 | {
|
---|
| 244 | if (makelevel == 0)
|
---|
| 245 | printf ("%s: ", program);
|
---|
| 246 | else
|
---|
| 247 | printf ("%s[%u]: ", program, makelevel);
|
---|
| 248 | }
|
---|
| 249 | VA_START (args, fmt);
|
---|
| 250 | VA_PRINTF (stdout, fmt, args);
|
---|
| 251 | VA_END (args);
|
---|
| 252 | putchar ('\n');
|
---|
| 253 | }
|
---|
| 254 |
|
---|
| 255 | fflush (stdout);
|
---|
| 256 | }
|
---|
| 257 |
|
---|
| 258 | /* Print an error message. */
|
---|
| 259 |
|
---|
| 260 | void
|
---|
| 261 | #if HAVE_ANSI_COMPILER && USE_VARIADIC && HAVE_STDARG_H
|
---|
| 262 | error (const struct floc *flocp, const char *fmt, ...)
|
---|
| 263 | #else
|
---|
| 264 | error (flocp, fmt, va_alist)
|
---|
| 265 | const struct floc *flocp;
|
---|
| 266 | const char *fmt;
|
---|
| 267 | va_dcl
|
---|
| 268 | #endif
|
---|
| 269 | {
|
---|
| 270 | #if USE_VARIADIC
|
---|
| 271 | va_list args;
|
---|
| 272 | #endif
|
---|
| 273 |
|
---|
| 274 | log_working_directory (1);
|
---|
| 275 |
|
---|
| 276 | if (flocp && flocp->filenm)
|
---|
| 277 | fprintf (stderr, "%s:%lu: ", flocp->filenm, flocp->lineno);
|
---|
| 278 | else if (makelevel == 0)
|
---|
| 279 | fprintf (stderr, "%s: ", program);
|
---|
| 280 | else
|
---|
| 281 | fprintf (stderr, "%s[%u]: ", program, makelevel);
|
---|
| 282 |
|
---|
| 283 | VA_START(args, fmt);
|
---|
| 284 | VA_PRINTF (stderr, fmt, args);
|
---|
| 285 | VA_END (args);
|
---|
| 286 |
|
---|
| 287 | putc ('\n', stderr);
|
---|
| 288 | fflush (stderr);
|
---|
| 289 | }
|
---|
| 290 |
|
---|
| 291 | /* Print an error message and exit. */
|
---|
| 292 |
|
---|
| 293 | void
|
---|
| 294 | #if HAVE_ANSI_COMPILER && USE_VARIADIC && HAVE_STDARG_H
|
---|
| 295 | fatal (const struct floc *flocp, const char *fmt, ...)
|
---|
| 296 | #else
|
---|
| 297 | fatal (flocp, fmt, va_alist)
|
---|
| 298 | const struct floc *flocp;
|
---|
| 299 | const char *fmt;
|
---|
| 300 | va_dcl
|
---|
| 301 | #endif
|
---|
| 302 | {
|
---|
| 303 | #if USE_VARIADIC
|
---|
| 304 | va_list args;
|
---|
| 305 | #endif
|
---|
| 306 |
|
---|
| 307 | log_working_directory (1);
|
---|
| 308 |
|
---|
| 309 | if (flocp && flocp->filenm)
|
---|
| 310 | fprintf (stderr, "%s:%lu: *** ", flocp->filenm, flocp->lineno);
|
---|
| 311 | else if (makelevel == 0)
|
---|
| 312 | fprintf (stderr, "%s: *** ", program);
|
---|
| 313 | else
|
---|
| 314 | fprintf (stderr, "%s[%u]: *** ", program, makelevel);
|
---|
| 315 |
|
---|
| 316 | VA_START(args, fmt);
|
---|
| 317 | VA_PRINTF (stderr, fmt, args);
|
---|
| 318 | VA_END (args);
|
---|
| 319 |
|
---|
| 320 | fputs (_(". Stop.\n"), stderr);
|
---|
| 321 |
|
---|
| 322 | die (2);
|
---|
| 323 | }
|
---|
| 324 |
|
---|
| 325 | #ifndef HAVE_STRERROR
|
---|
| 326 |
|
---|
| 327 | #undef strerror
|
---|
| 328 |
|
---|
| 329 | char *
|
---|
| 330 | strerror (int errnum)
|
---|
| 331 | {
|
---|
| 332 | extern int errno, sys_nerr;
|
---|
| 333 | #ifndef __DECC
|
---|
| 334 | extern char *sys_errlist[];
|
---|
| 335 | #endif
|
---|
| 336 | static char buf[] = "Unknown error 12345678901234567890";
|
---|
| 337 |
|
---|
| 338 | if (errno < sys_nerr)
|
---|
| 339 | return sys_errlist[errnum];
|
---|
| 340 |
|
---|
| 341 | sprintf (buf, _("Unknown error %d"), errnum);
|
---|
| 342 | return buf;
|
---|
| 343 | }
|
---|
| 344 | #endif
|
---|
| 345 |
|
---|
| 346 | /* Print an error message from errno. */
|
---|
| 347 |
|
---|
| 348 | void
|
---|
| 349 | perror_with_name (const char *str, const char *name)
|
---|
| 350 | {
|
---|
| 351 | error (NILF, _("%s%s: %s"), str, name, strerror (errno));
|
---|
| 352 | }
|
---|
| 353 |
|
---|
| 354 | /* Print an error message from errno and exit. */
|
---|
| 355 |
|
---|
| 356 | void
|
---|
| 357 | pfatal_with_name (const char *name)
|
---|
| 358 | {
|
---|
| 359 | fatal (NILF, _("%s: %s"), name, strerror (errno));
|
---|
| 360 |
|
---|
| 361 | /* NOTREACHED */
|
---|
| 362 | }
|
---|
| 363 | |
---|
| 364 |
|
---|
| 365 | /* Like malloc but get fatal error if memory is exhausted. */
|
---|
| 366 | /* Don't bother if we're using dmalloc; it provides these for us. */
|
---|
| 367 |
|
---|
| 368 | #ifndef HAVE_DMALLOC_H
|
---|
| 369 |
|
---|
| 370 | #undef xmalloc
|
---|
| 371 | #undef xcalloc
|
---|
| 372 | #undef xrealloc
|
---|
| 373 | #undef xstrdup
|
---|
| 374 |
|
---|
| 375 | void *
|
---|
| 376 | xmalloc (unsigned int size)
|
---|
| 377 | {
|
---|
| 378 | /* Make sure we don't allocate 0, for pre-ISO implementations. */
|
---|
| 379 | void *result = malloc (size ? size : 1);
|
---|
| 380 | if (result == 0)
|
---|
| 381 | fatal (NILF, _("virtual memory exhausted"));
|
---|
| 382 | return result;
|
---|
| 383 | }
|
---|
| 384 |
|
---|
| 385 |
|
---|
| 386 | void *
|
---|
| 387 | xcalloc (unsigned int size)
|
---|
| 388 | {
|
---|
| 389 | /* Make sure we don't allocate 0, for pre-ISO implementations. */
|
---|
| 390 | void *result = calloc (size ? size : 1, 1);
|
---|
| 391 | if (result == 0)
|
---|
| 392 | fatal (NILF, _("virtual memory exhausted"));
|
---|
| 393 | return result;
|
---|
| 394 | }
|
---|
| 395 |
|
---|
| 396 |
|
---|
| 397 | void *
|
---|
| 398 | xrealloc (void *ptr, unsigned int size)
|
---|
| 399 | {
|
---|
| 400 | void *result;
|
---|
| 401 |
|
---|
| 402 | /* Some older implementations of realloc() don't conform to ISO. */
|
---|
| 403 | if (! size)
|
---|
| 404 | size = 1;
|
---|
| 405 | result = ptr ? realloc (ptr, size) : malloc (size);
|
---|
| 406 | if (result == 0)
|
---|
| 407 | fatal (NILF, _("virtual memory exhausted"));
|
---|
| 408 | return result;
|
---|
| 409 | }
|
---|
| 410 |
|
---|
| 411 |
|
---|
| 412 | char *
|
---|
| 413 | xstrdup (const char *ptr)
|
---|
| 414 | {
|
---|
| 415 | char *result;
|
---|
| 416 |
|
---|
| 417 | #ifdef HAVE_STRDUP
|
---|
| 418 | result = strdup (ptr);
|
---|
| 419 | #else
|
---|
| 420 | result = malloc (strlen (ptr) + 1);
|
---|
| 421 | #endif
|
---|
| 422 |
|
---|
| 423 | if (result == 0)
|
---|
| 424 | fatal (NILF, _("virtual memory exhausted"));
|
---|
| 425 |
|
---|
| 426 | #ifdef HAVE_STRDUP
|
---|
| 427 | return result;
|
---|
| 428 | #else
|
---|
| 429 | return strcpy (result, ptr);
|
---|
| 430 | #endif
|
---|
| 431 | }
|
---|
| 432 |
|
---|
| 433 | #endif /* HAVE_DMALLOC_H */
|
---|
| 434 |
|
---|
| 435 | char *
|
---|
| 436 | xstrndup (const char *str, unsigned int length)
|
---|
| 437 | {
|
---|
| 438 | char *result;
|
---|
| 439 |
|
---|
| 440 | #ifdef HAVE_STRNDUP
|
---|
| 441 | result = strndup (str, length);
|
---|
| 442 | if (result == 0)
|
---|
| 443 | fatal (NILF, _("virtual memory exhausted"));
|
---|
| 444 | #else
|
---|
| 445 | result = xmalloc (length + 1);
|
---|
| 446 | if (length > 0)
|
---|
| 447 | strncpy (result, str, length);
|
---|
| 448 | result[length] = '\0';
|
---|
| 449 | #endif
|
---|
| 450 |
|
---|
| 451 | return result;
|
---|
| 452 | }
|
---|
| 453 | |
---|
| 454 |
|
---|
| 455 |
|
---|
| 456 | /* Limited INDEX:
|
---|
| 457 | Search through the string STRING, which ends at LIMIT, for the character C.
|
---|
| 458 | Returns a pointer to the first occurrence, or nil if none is found.
|
---|
| 459 | Like INDEX except that the string searched ends where specified
|
---|
| 460 | instead of at the first null. */
|
---|
| 461 |
|
---|
| 462 | char *
|
---|
| 463 | lindex (const char *s, const char *limit, int c)
|
---|
| 464 | {
|
---|
| 465 | while (s < limit)
|
---|
| 466 | if (*s++ == c)
|
---|
| 467 | return (char *)(s - 1);
|
---|
| 468 |
|
---|
| 469 | return 0;
|
---|
| 470 | }
|
---|
| 471 | |
---|
| 472 |
|
---|
| 473 | /* Return the address of the first whitespace or null in the string S. */
|
---|
| 474 |
|
---|
| 475 | char *
|
---|
| 476 | end_of_token (const char *s)
|
---|
| 477 | {
|
---|
| 478 | while (*s != '\0' && !isblank ((unsigned char)*s))
|
---|
| 479 | ++s;
|
---|
| 480 | return (char *)s;
|
---|
| 481 | }
|
---|
| 482 |
|
---|
| 483 | #ifdef WINDOWS32
|
---|
| 484 | /*
|
---|
| 485 | * Same as end_of_token, but take into account a stop character
|
---|
| 486 | */
|
---|
| 487 | char *
|
---|
| 488 | end_of_token_w32 (const char *s, char stopchar)
|
---|
| 489 | {
|
---|
| 490 | const char *p = s;
|
---|
| 491 | int backslash = 0;
|
---|
| 492 |
|
---|
| 493 | while (*p != '\0' && *p != stopchar
|
---|
| 494 | && (backslash || !isblank ((unsigned char)*p)))
|
---|
| 495 | {
|
---|
| 496 | if (*p++ == '\\')
|
---|
| 497 | {
|
---|
| 498 | backslash = !backslash;
|
---|
| 499 | while (*p == '\\')
|
---|
| 500 | {
|
---|
| 501 | backslash = !backslash;
|
---|
| 502 | ++p;
|
---|
| 503 | }
|
---|
| 504 | }
|
---|
| 505 | else
|
---|
| 506 | backslash = 0;
|
---|
| 507 | }
|
---|
| 508 |
|
---|
| 509 | return (char *)p;
|
---|
| 510 | }
|
---|
| 511 | #endif
|
---|
| 512 |
|
---|
| 513 | /* Return the address of the first nonwhitespace or null in the string S. */
|
---|
| 514 |
|
---|
| 515 | char *
|
---|
| 516 | next_token (const char *s)
|
---|
| 517 | {
|
---|
| 518 | while (isblank ((unsigned char)*s))
|
---|
| 519 | ++s;
|
---|
| 520 | return (char *)s;
|
---|
| 521 | }
|
---|
| 522 |
|
---|
| 523 | /* Find the next token in PTR; return the address of it, and store the length
|
---|
| 524 | of the token into *LENGTHPTR if LENGTHPTR is not nil. Set *PTR to the end
|
---|
| 525 | of the token, so this function can be called repeatedly in a loop. */
|
---|
| 526 |
|
---|
| 527 | char *
|
---|
| 528 | find_next_token (const char **ptr, unsigned int *lengthptr)
|
---|
| 529 | {
|
---|
| 530 | const char *p = next_token (*ptr);
|
---|
| 531 |
|
---|
| 532 | if (*p == '\0')
|
---|
| 533 | return 0;
|
---|
| 534 |
|
---|
| 535 | *ptr = end_of_token (p);
|
---|
| 536 | if (lengthptr != 0)
|
---|
| 537 | *lengthptr = *ptr - p;
|
---|
| 538 |
|
---|
| 539 | return (char *)p;
|
---|
| 540 | }
|
---|
| 541 | |
---|
| 542 |
|
---|
| 543 |
|
---|
| 544 | /* Copy a chain of `struct dep'. For 2nd expansion deps, dup the name. */
|
---|
| 545 |
|
---|
| 546 | struct dep *
|
---|
| 547 | copy_dep_chain (const struct dep *d)
|
---|
| 548 | {
|
---|
| 549 | struct dep *firstnew = 0;
|
---|
| 550 | struct dep *lastnew = 0;
|
---|
| 551 |
|
---|
| 552 | while (d != 0)
|
---|
| 553 | {
|
---|
| 554 | struct dep *c = xmalloc (sizeof (struct dep));
|
---|
| 555 | memcpy (c, d, sizeof (struct dep));
|
---|
| 556 |
|
---|
| 557 | if (c->need_2nd_expansion)
|
---|
| 558 | c->name = xstrdup (c->name);
|
---|
| 559 |
|
---|
| 560 | c->next = 0;
|
---|
| 561 | if (firstnew == 0)
|
---|
| 562 | firstnew = lastnew = c;
|
---|
| 563 | else
|
---|
| 564 | lastnew = lastnew->next = c;
|
---|
| 565 |
|
---|
| 566 | d = d->next;
|
---|
| 567 | }
|
---|
| 568 |
|
---|
| 569 | return firstnew;
|
---|
| 570 | }
|
---|
| 571 |
|
---|
| 572 | /* Free a chain of 'struct dep'. */
|
---|
| 573 |
|
---|
| 574 | void
|
---|
| 575 | free_dep_chain (struct dep *d)
|
---|
| 576 | {
|
---|
| 577 | while (d != 0)
|
---|
| 578 | {
|
---|
| 579 | struct dep *df = d;
|
---|
| 580 | d = d->next;
|
---|
| 581 | free_dep (df);
|
---|
| 582 | }
|
---|
| 583 | }
|
---|
| 584 |
|
---|
| 585 | /* Free a chain of struct nameseq.
|
---|
| 586 | For struct dep chains use free_dep_chain. */
|
---|
| 587 |
|
---|
| 588 | void
|
---|
| 589 | free_ns_chain (struct nameseq *ns)
|
---|
| 590 | {
|
---|
| 591 | while (ns != 0)
|
---|
| 592 | {
|
---|
| 593 | struct nameseq *t = ns;
|
---|
| 594 | ns = ns->next;
|
---|
| 595 | free (t);
|
---|
| 596 | }
|
---|
| 597 | }
|
---|
| 598 | |
---|
| 599 |
|
---|
| 600 |
|
---|
| 601 | #if !HAVE_STRCASECMP && !HAVE_STRICMP && !HAVE_STRCMPI
|
---|
| 602 |
|
---|
| 603 | /* If we don't have strcasecmp() (from POSIX), or anything that can substitute
|
---|
| 604 | for it, define our own version. */
|
---|
| 605 |
|
---|
| 606 | int
|
---|
| 607 | strcasecmp (const char *s1, const char *s2)
|
---|
| 608 | {
|
---|
| 609 | while (1)
|
---|
| 610 | {
|
---|
| 611 | int c1 = (int) *(s1++);
|
---|
| 612 | int c2 = (int) *(s2++);
|
---|
| 613 |
|
---|
| 614 | if (isalpha (c1))
|
---|
| 615 | c1 = tolower (c1);
|
---|
| 616 | if (isalpha (c2))
|
---|
| 617 | c2 = tolower (c2);
|
---|
| 618 |
|
---|
| 619 | if (c1 != '\0' && c1 == c2)
|
---|
| 620 | continue;
|
---|
| 621 |
|
---|
| 622 | return (c1 - c2);
|
---|
| 623 | }
|
---|
| 624 | }
|
---|
| 625 | #endif
|
---|
| 626 |
|
---|
| 627 | #if !HAVE_STRNCASECMP && !HAVE_STRNICMP && !HAVE_STRNCMPI
|
---|
| 628 |
|
---|
| 629 | /* If we don't have strncasecmp() (from POSIX), or anything that can
|
---|
| 630 | substitute for it, define our own version. */
|
---|
| 631 |
|
---|
| 632 | int
|
---|
| 633 | strncasecmp (const char *s1, const char *s2, int n)
|
---|
| 634 | {
|
---|
| 635 | while (n-- > 0)
|
---|
| 636 | {
|
---|
| 637 | int c1 = (int) *(s1++);
|
---|
| 638 | int c2 = (int) *(s2++);
|
---|
| 639 |
|
---|
| 640 | if (isalpha (c1))
|
---|
| 641 | c1 = tolower (c1);
|
---|
| 642 | if (isalpha (c2))
|
---|
| 643 | c2 = tolower (c2);
|
---|
| 644 |
|
---|
| 645 | if (c1 != '\0' && c1 == c2)
|
---|
| 646 | continue;
|
---|
| 647 |
|
---|
| 648 | return (c1 - c2);
|
---|
| 649 | }
|
---|
| 650 |
|
---|
| 651 | return 0;
|
---|
| 652 | }
|
---|
| 653 | #endif
|
---|
| 654 | |
---|
| 655 |
|
---|
| 656 | #ifdef GETLOADAVG_PRIVILEGED
|
---|
| 657 |
|
---|
| 658 | #ifdef POSIX
|
---|
| 659 |
|
---|
| 660 | /* Hopefully if a system says it's POSIX.1 and has the setuid and setgid
|
---|
| 661 | functions, they work as POSIX.1 says. Some systems (Alpha OSF/1 1.2,
|
---|
| 662 | for example) which claim to be POSIX.1 also have the BSD setreuid and
|
---|
| 663 | setregid functions, but they don't work as in BSD and only the POSIX.1
|
---|
| 664 | way works. */
|
---|
| 665 |
|
---|
| 666 | #undef HAVE_SETREUID
|
---|
| 667 | #undef HAVE_SETREGID
|
---|
| 668 |
|
---|
| 669 | #else /* Not POSIX. */
|
---|
| 670 |
|
---|
| 671 | /* Some POSIX.1 systems have the seteuid and setegid functions. In a
|
---|
| 672 | POSIX-like system, they are the best thing to use. However, some
|
---|
| 673 | non-POSIX systems have them too but they do not work in the POSIX style
|
---|
| 674 | and we must use setreuid and setregid instead. */
|
---|
| 675 |
|
---|
| 676 | #undef HAVE_SETEUID
|
---|
| 677 | #undef HAVE_SETEGID
|
---|
| 678 |
|
---|
| 679 | #endif /* POSIX. */
|
---|
| 680 |
|
---|
| 681 | #ifndef HAVE_UNISTD_H
|
---|
| 682 | extern int getuid (), getgid (), geteuid (), getegid ();
|
---|
| 683 | extern int setuid (), setgid ();
|
---|
| 684 | #ifdef HAVE_SETEUID
|
---|
| 685 | extern int seteuid ();
|
---|
| 686 | #else
|
---|
| 687 | #ifdef HAVE_SETREUID
|
---|
| 688 | extern int setreuid ();
|
---|
| 689 | #endif /* Have setreuid. */
|
---|
| 690 | #endif /* Have seteuid. */
|
---|
| 691 | #ifdef HAVE_SETEGID
|
---|
| 692 | extern int setegid ();
|
---|
| 693 | #else
|
---|
| 694 | #ifdef HAVE_SETREGID
|
---|
| 695 | extern int setregid ();
|
---|
| 696 | #endif /* Have setregid. */
|
---|
| 697 | #endif /* Have setegid. */
|
---|
| 698 | #endif /* No <unistd.h>. */
|
---|
| 699 |
|
---|
| 700 | /* Keep track of the user and group IDs for user- and make- access. */
|
---|
| 701 | static int user_uid = -1, user_gid = -1, make_uid = -1, make_gid = -1;
|
---|
| 702 | #define access_inited (user_uid != -1)
|
---|
| 703 | static enum { make, user } current_access;
|
---|
| 704 |
|
---|
| 705 |
|
---|
| 706 | /* Under -d, write a message describing the current IDs. */
|
---|
| 707 |
|
---|
| 708 | static void
|
---|
| 709 | log_access (const char *flavor)
|
---|
| 710 | {
|
---|
| 711 | if (! ISDB (DB_JOBS))
|
---|
| 712 | return;
|
---|
| 713 |
|
---|
| 714 | /* All the other debugging messages go to stdout,
|
---|
| 715 | but we write this one to stderr because it might be
|
---|
| 716 | run in a child fork whose stdout is piped. */
|
---|
| 717 |
|
---|
| 718 | fprintf (stderr, _("%s: user %lu (real %lu), group %lu (real %lu)\n"),
|
---|
| 719 | flavor, (unsigned long) geteuid (), (unsigned long) getuid (),
|
---|
| 720 | (unsigned long) getegid (), (unsigned long) getgid ());
|
---|
| 721 | fflush (stderr);
|
---|
| 722 | }
|
---|
| 723 |
|
---|
| 724 |
|
---|
| 725 | static void
|
---|
| 726 | init_access (void)
|
---|
| 727 | {
|
---|
| 728 | #ifndef VMS
|
---|
| 729 | user_uid = getuid ();
|
---|
| 730 | user_gid = getgid ();
|
---|
| 731 |
|
---|
| 732 | make_uid = geteuid ();
|
---|
| 733 | make_gid = getegid ();
|
---|
| 734 |
|
---|
| 735 | /* Do these ever fail? */
|
---|
| 736 | if (user_uid == -1 || user_gid == -1 || make_uid == -1 || make_gid == -1)
|
---|
| 737 | pfatal_with_name ("get{e}[gu]id");
|
---|
| 738 |
|
---|
| 739 | log_access (_("Initialized access"));
|
---|
| 740 |
|
---|
| 741 | current_access = make;
|
---|
| 742 | #endif
|
---|
| 743 | }
|
---|
| 744 |
|
---|
| 745 | #endif /* GETLOADAVG_PRIVILEGED */
|
---|
| 746 |
|
---|
| 747 | /* Give the process appropriate permissions for access to
|
---|
| 748 | user data (i.e., to stat files, or to spawn a child process). */
|
---|
| 749 | void
|
---|
| 750 | user_access (void)
|
---|
| 751 | {
|
---|
| 752 | #ifdef GETLOADAVG_PRIVILEGED
|
---|
| 753 |
|
---|
| 754 | if (!access_inited)
|
---|
| 755 | init_access ();
|
---|
| 756 |
|
---|
| 757 | if (current_access == user)
|
---|
| 758 | return;
|
---|
| 759 |
|
---|
| 760 | /* We are in "make access" mode. This means that the effective user and
|
---|
| 761 | group IDs are those of make (if it was installed setuid or setgid).
|
---|
| 762 | We now want to set the effective user and group IDs to the real IDs,
|
---|
| 763 | which are the IDs of the process that exec'd make. */
|
---|
| 764 |
|
---|
| 765 | #ifdef HAVE_SETEUID
|
---|
| 766 |
|
---|
| 767 | /* Modern systems have the seteuid/setegid calls which set only the
|
---|
| 768 | effective IDs, which is ideal. */
|
---|
| 769 |
|
---|
| 770 | if (seteuid (user_uid) < 0)
|
---|
| 771 | pfatal_with_name ("user_access: seteuid");
|
---|
| 772 |
|
---|
| 773 | #else /* Not HAVE_SETEUID. */
|
---|
| 774 |
|
---|
| 775 | #ifndef HAVE_SETREUID
|
---|
| 776 |
|
---|
| 777 | /* System V has only the setuid/setgid calls to set user/group IDs.
|
---|
| 778 | There is an effective ID, which can be set by setuid/setgid.
|
---|
| 779 | It can be set (unless you are root) only to either what it already is
|
---|
| 780 | (returned by geteuid/getegid, now in make_uid/make_gid),
|
---|
| 781 | the real ID (return by getuid/getgid, now in user_uid/user_gid),
|
---|
| 782 | or the saved set ID (what the effective ID was before this set-ID
|
---|
| 783 | executable (make) was exec'd). */
|
---|
| 784 |
|
---|
| 785 | if (setuid (user_uid) < 0)
|
---|
| 786 | pfatal_with_name ("user_access: setuid");
|
---|
| 787 |
|
---|
| 788 | #else /* HAVE_SETREUID. */
|
---|
| 789 |
|
---|
| 790 | /* In 4BSD, the setreuid/setregid calls set both the real and effective IDs.
|
---|
| 791 | They may be set to themselves or each other. So you have two alternatives
|
---|
| 792 | at any one time. If you use setuid/setgid, the effective will be set to
|
---|
| 793 | the real, leaving only one alternative. Using setreuid/setregid, however,
|
---|
| 794 | you can toggle between your two alternatives by swapping the values in a
|
---|
| 795 | single setreuid or setregid call. */
|
---|
| 796 |
|
---|
| 797 | if (setreuid (make_uid, user_uid) < 0)
|
---|
| 798 | pfatal_with_name ("user_access: setreuid");
|
---|
| 799 |
|
---|
| 800 | #endif /* Not HAVE_SETREUID. */
|
---|
| 801 | #endif /* HAVE_SETEUID. */
|
---|
| 802 |
|
---|
| 803 | #ifdef HAVE_SETEGID
|
---|
| 804 | if (setegid (user_gid) < 0)
|
---|
| 805 | pfatal_with_name ("user_access: setegid");
|
---|
| 806 | #else
|
---|
| 807 | #ifndef HAVE_SETREGID
|
---|
| 808 | if (setgid (user_gid) < 0)
|
---|
| 809 | pfatal_with_name ("user_access: setgid");
|
---|
| 810 | #else
|
---|
| 811 | if (setregid (make_gid, user_gid) < 0)
|
---|
| 812 | pfatal_with_name ("user_access: setregid");
|
---|
| 813 | #endif
|
---|
| 814 | #endif
|
---|
| 815 |
|
---|
| 816 | current_access = user;
|
---|
| 817 |
|
---|
| 818 | log_access (_("User access"));
|
---|
| 819 |
|
---|
| 820 | #endif /* GETLOADAVG_PRIVILEGED */
|
---|
| 821 | }
|
---|
| 822 |
|
---|
| 823 | /* Give the process appropriate permissions for access to
|
---|
| 824 | make data (i.e., the load average). */
|
---|
| 825 | void
|
---|
| 826 | make_access (void)
|
---|
| 827 | {
|
---|
| 828 | #ifdef GETLOADAVG_PRIVILEGED
|
---|
| 829 |
|
---|
| 830 | if (!access_inited)
|
---|
| 831 | init_access ();
|
---|
| 832 |
|
---|
| 833 | if (current_access == make)
|
---|
| 834 | return;
|
---|
| 835 |
|
---|
| 836 | /* See comments in user_access, above. */
|
---|
| 837 |
|
---|
| 838 | #ifdef HAVE_SETEUID
|
---|
| 839 | if (seteuid (make_uid) < 0)
|
---|
| 840 | pfatal_with_name ("make_access: seteuid");
|
---|
| 841 | #else
|
---|
| 842 | #ifndef HAVE_SETREUID
|
---|
| 843 | if (setuid (make_uid) < 0)
|
---|
| 844 | pfatal_with_name ("make_access: setuid");
|
---|
| 845 | #else
|
---|
| 846 | if (setreuid (user_uid, make_uid) < 0)
|
---|
| 847 | pfatal_with_name ("make_access: setreuid");
|
---|
| 848 | #endif
|
---|
| 849 | #endif
|
---|
| 850 |
|
---|
| 851 | #ifdef HAVE_SETEGID
|
---|
| 852 | if (setegid (make_gid) < 0)
|
---|
| 853 | pfatal_with_name ("make_access: setegid");
|
---|
| 854 | #else
|
---|
| 855 | #ifndef HAVE_SETREGID
|
---|
| 856 | if (setgid (make_gid) < 0)
|
---|
| 857 | pfatal_with_name ("make_access: setgid");
|
---|
| 858 | #else
|
---|
| 859 | if (setregid (user_gid, make_gid) < 0)
|
---|
| 860 | pfatal_with_name ("make_access: setregid");
|
---|
| 861 | #endif
|
---|
| 862 | #endif
|
---|
| 863 |
|
---|
| 864 | current_access = make;
|
---|
| 865 |
|
---|
| 866 | log_access (_("Make access"));
|
---|
| 867 |
|
---|
| 868 | #endif /* GETLOADAVG_PRIVILEGED */
|
---|
| 869 | }
|
---|
| 870 |
|
---|
| 871 | /* Give the process appropriate permissions for a child process.
|
---|
| 872 | This is like user_access, but you can't get back to make_access. */
|
---|
| 873 | void
|
---|
| 874 | child_access (void)
|
---|
| 875 | {
|
---|
| 876 | #ifdef GETLOADAVG_PRIVILEGED
|
---|
| 877 |
|
---|
| 878 | if (!access_inited)
|
---|
| 879 | abort ();
|
---|
| 880 |
|
---|
| 881 | /* Set both the real and effective UID and GID to the user's.
|
---|
| 882 | They cannot be changed back to make's. */
|
---|
| 883 |
|
---|
| 884 | #ifndef HAVE_SETREUID
|
---|
| 885 | if (setuid (user_uid) < 0)
|
---|
| 886 | pfatal_with_name ("child_access: setuid");
|
---|
| 887 | #else
|
---|
| 888 | if (setreuid (user_uid, user_uid) < 0)
|
---|
| 889 | pfatal_with_name ("child_access: setreuid");
|
---|
| 890 | #endif
|
---|
| 891 |
|
---|
| 892 | #ifndef HAVE_SETREGID
|
---|
| 893 | if (setgid (user_gid) < 0)
|
---|
| 894 | pfatal_with_name ("child_access: setgid");
|
---|
| 895 | #else
|
---|
| 896 | if (setregid (user_gid, user_gid) < 0)
|
---|
| 897 | pfatal_with_name ("child_access: setregid");
|
---|
| 898 | #endif
|
---|
| 899 |
|
---|
| 900 | log_access (_("Child access"));
|
---|
| 901 |
|
---|
| 902 | #endif /* GETLOADAVG_PRIVILEGED */
|
---|
| 903 | }
|
---|
| 904 | |
---|
| 905 |
|
---|
| 906 | #ifdef NEED_GET_PATH_MAX
|
---|
| 907 | unsigned int
|
---|
| 908 | get_path_max (void)
|
---|
| 909 | {
|
---|
| 910 | static unsigned int value;
|
---|
| 911 |
|
---|
| 912 | if (value == 0)
|
---|
| 913 | {
|
---|
| 914 | long int x = pathconf ("/", _PC_PATH_MAX);
|
---|
| 915 | if (x > 0)
|
---|
| 916 | value = x;
|
---|
| 917 | else
|
---|
| 918 | return MAXPATHLEN;
|
---|
| 919 | }
|
---|
| 920 |
|
---|
| 921 | return value;
|
---|
| 922 | }
|
---|
| 923 | #endif
|
---|
| 924 | |
---|
| 925 |
|
---|
| 926 |
|
---|
| 927 | /* This code is stolen from gnulib.
|
---|
| 928 | If/when we abandon the requirement to work with K&R compilers, we can
|
---|
| 929 | remove this (and perhaps other parts of GNU make!) and migrate to using
|
---|
| 930 | gnulib directly.
|
---|
| 931 |
|
---|
| 932 | This is called only through atexit(), which means die() has already been
|
---|
| 933 | invoked. So, call exit() here directly. Apparently that works...?
|
---|
| 934 | */
|
---|
| 935 |
|
---|
| 936 | /* Close standard output, exiting with status 'exit_failure' on failure.
|
---|
| 937 | If a program writes *anything* to stdout, that program should close
|
---|
| 938 | stdout and make sure that it succeeds before exiting. Otherwise,
|
---|
| 939 | suppose that you go to the extreme of checking the return status
|
---|
| 940 | of every function that does an explicit write to stdout. The last
|
---|
| 941 | printf can succeed in writing to the internal stream buffer, and yet
|
---|
| 942 | the fclose(stdout) could still fail (due e.g., to a disk full error)
|
---|
| 943 | when it tries to write out that buffered data. Thus, you would be
|
---|
| 944 | left with an incomplete output file and the offending program would
|
---|
| 945 | exit successfully. Even calling fflush is not always sufficient,
|
---|
| 946 | since some file systems (NFS and CODA) buffer written/flushed data
|
---|
| 947 | until an actual close call.
|
---|
| 948 |
|
---|
| 949 | Besides, it's wasteful to check the return value from every call
|
---|
| 950 | that writes to stdout -- just let the internal stream state record
|
---|
| 951 | the failure. That's what the ferror test is checking below.
|
---|
| 952 |
|
---|
| 953 | It's important to detect such failures and exit nonzero because many
|
---|
| 954 | tools (most notably `make' and other build-management systems) depend
|
---|
| 955 | on being able to detect failure in other tools via their exit status. */
|
---|
| 956 |
|
---|
| 957 | void
|
---|
| 958 | close_stdout (void)
|
---|
| 959 | {
|
---|
| 960 | int prev_fail = ferror (stdout);
|
---|
| 961 | int fclose_fail = fclose (stdout);
|
---|
| 962 |
|
---|
| 963 | if (prev_fail || fclose_fail)
|
---|
| 964 | {
|
---|
| 965 | if (fclose_fail)
|
---|
| 966 | error (NILF, _("write error: %s"), strerror (errno));
|
---|
| 967 | else
|
---|
| 968 | error (NILF, _("write error"));
|
---|
| 969 | exit (EXIT_FAILURE);
|
---|
| 970 | }
|
---|
| 971 | }
|
---|