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