Changeset 3138 for vendor/gnumake/current/misc.c
- Timestamp:
- Mar 12, 2018, 8:32:29 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
vendor/gnumake/current/misc.c
r2596 r3138 1 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. 2 Copyright (C) 1988-2016 Free Software Foundation, Inc. 5 3 This file is part of GNU Make. 6 4 … … 17 15 this program. If not, see <http://www.gnu.org/licenses/>. */ 18 16 19 #include "make.h" 17 #include "makeint.h" 18 #include "filedef.h" 20 19 #include "dep.h" 21 20 #include "debug.h" 22 21 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 22 /* GNU make no longer supports pre-ANSI89 environments. */ 23 24 #include <stdarg.h> 25 26 #ifdef HAVE_FCNTL_H 27 # include <fcntl.h> 28 #else 29 # include <sys/file.h> 30 #endif 54 31 55 32 /* Compare strings *S1 and *S2. … … 76 53 collapse_continuations (char *line) 77 54 { 78 register char *in, *out, *p; 79 register int backslash; 80 register unsigned int bs_write; 55 char *in, *out, *p; 81 56 82 57 in = strchr (line, '\n'); … … 91 66 { 92 67 /* BS_WRITE gets the number of quoted backslashes at 93 94 95 backslash = 0;96 bs_write = 0;68 the end just before IN, and BACKSLASH gets nonzero 69 if the next character is quoted. */ 70 unsigned int backslash = 0; 71 unsigned int bs_write = 0; 97 72 for (p = in - 1; p >= line && *p == '\\'; --p) 98 99 100 101 102 103 104 105 106 107 73 { 74 if (backslash) 75 ++bs_write; 76 backslash = !backslash; 77 78 /* It should be impossible to go back this far without exiting, 79 but if we do, we can't get the right answer. */ 80 if (in == out - 1) 81 abort (); 82 } 108 83 109 84 /* Output the appropriate number of backslashes. */ 110 85 while (bs_write-- > 0) 111 86 *out++ = '\\'; 112 87 113 88 /* Skip the newline. */ 114 89 ++in; 115 90 116 /* If the newline is escaped, discard following whitespace leaving just117 one space. POSIX requires that each backslash/newline/following118 whitespace sequence be reduced to a single space. */119 91 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 } 92 { 93 /* Backslash/newline handling: 94 In traditional GNU make all trailing whitespace, consecutive 95 backslash/newlines, and any leading non-newline whitespace on the 96 next line is reduced to a single space. 97 In POSIX, each backslash/newline and is replaced by a space. */ 98 while (ISBLANK (*in)) 99 ++in; 100 if (! posix_pedantic) 101 while (out > line && ISBLANK (out[-1])) 102 --out; 103 *out++ = ' '; 104 } 127 105 else 128 129 106 /* If the newline isn't quoted, put it in the output. */ 107 *out++ = '\n'; 130 108 131 109 /* Now copy the following line to the output. 132 110 Stop when we find backslashes followed by a newline. */ 133 111 while (*in != '\0') 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 112 if (*in == '\\') 113 { 114 p = in + 1; 115 while (*p == '\\') 116 ++p; 117 if (*p == '\n') 118 { 119 in = p; 120 break; 121 } 122 while (in < p) 123 *out++ = *in++; 124 } 125 else 126 *out++ = *in++; 149 127 } 150 128 … … 168 146 169 147 const char * 170 #if HAVE_ANSI_COMPILER && USE_VARIADIC && HAVE_STDARG_H171 148 concat (unsigned int num, ...) 172 #else173 concat (num, va_alist)174 unsigned int num;175 va_dcl176 #endif177 149 { 178 150 static unsigned int rlen = 0; 179 151 static char *result = NULL; 180 int ri = 0; 181 182 #if USE_VARIADIC 152 unsigned int ri = 0; 183 153 va_list args; 184 #endif 185 186 VA_START (args, num); 154 155 va_start (args, num); 187 156 188 157 while (num-- > 0) 189 158 { 190 159 const char *s = va_arg (args, const char *); 191 unsigned int l = s ? strlen (s) : 0;160 unsigned int l = xstrlen (s); 192 161 193 162 if (l == 0) … … 204 173 } 205 174 206 VA_END(args);175 va_end (args); 207 176 208 177 /* Get some more memory if we don't have enough space for the … … 220 189 221 190 222 /* Print a message on stdout. */223 224 void225 #if HAVE_ANSI_COMPILER && USE_VARIADIC && HAVE_STDARG_H226 message (int prefix, const char *fmt, ...)227 #else228 message (prefix, fmt, va_alist)229 int prefix;230 const char *fmt;231 va_dcl232 #endif233 {234 #if USE_VARIADIC235 va_list args;236 #endif237 238 log_working_directory (1);239 240 if (fmt != 0)241 {242 if (prefix)243 {244 if (makelevel == 0)245 printf ("%s: ", program);246 else247 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 void261 #if HAVE_ANSI_COMPILER && USE_VARIADIC && HAVE_STDARG_H262 error (const struct floc *flocp, const char *fmt, ...)263 #else264 error (flocp, fmt, va_alist)265 const struct floc *flocp;266 const char *fmt;267 va_dcl268 #endif269 {270 #if USE_VARIADIC271 va_list args;272 #endif273 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 else281 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 void294 #if HAVE_ANSI_COMPILER && USE_VARIADIC && HAVE_STDARG_H295 fatal (const struct floc *flocp, const char *fmt, ...)296 #else297 fatal (flocp, fmt, va_alist)298 const struct floc *flocp;299 const char *fmt;300 va_dcl301 #endif302 {303 #if USE_VARIADIC304 va_list args;305 #endif306 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 else314 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 191 325 192 #ifndef HAVE_STRERROR 326 327 #undef strerror 328 193 #undef strerror 329 194 char * 330 195 strerror (int errnum) … … 343 208 } 344 209 #endif 345 346 /* Print an error message from errno. */347 348 void349 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 void357 pfatal_with_name (const char *name)358 {359 fatal (NILF, _("%s: %s"), name, strerror (errno));360 361 /* NOTREACHED */362 }363 210 364 211 … … 379 226 void *result = malloc (size ? size : 1); 380 227 if (result == 0) 381 fatal (NILF, _("virtual memory exhausted"));228 OUT_OF_MEM(); 382 229 return result; 383 230 } … … 390 237 void *result = calloc (size ? size : 1, 1); 391 238 if (result == 0) 392 fatal (NILF, _("virtual memory exhausted"));239 OUT_OF_MEM(); 393 240 return result; 394 241 } … … 405 252 result = ptr ? realloc (ptr, size) : malloc (size); 406 253 if (result == 0) 407 fatal (NILF, _("virtual memory exhausted"));254 OUT_OF_MEM(); 408 255 return result; 409 256 } … … 422 269 423 270 if (result == 0) 424 fatal (NILF, _("virtual memory exhausted"));271 OUT_OF_MEM(); 425 272 426 273 #ifdef HAVE_STRDUP … … 441 288 result = strndup (str, length); 442 289 if (result == 0) 443 fatal (NILF, _("virtual memory exhausted"));290 OUT_OF_MEM(); 444 291 #else 445 292 result = xmalloc (length + 1); … … 476 323 end_of_token (const char *s) 477 324 { 478 while (*s != '\0' && !isblank ((unsigned char)*s)) 479 ++s; 325 END_OF_TOKEN (s); 480 326 return (char *)s; 481 327 } 482 483 #ifdef WINDOWS32484 /*485 * Same as end_of_token, but take into account a stop character486 */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 != stopchar494 && (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 else506 backslash = 0;507 }508 509 return (char *)p;510 }511 #endif512 328 513 329 /* Return the address of the first nonwhitespace or null in the string S. */ … … 516 332 next_token (const char *s) 517 333 { 518 while (isblank ((unsigned char)*s)) 519 ++s; 334 NEXT_TOKEN (s); 520 335 return (char *)s; 521 336 } … … 542 357 543 358 544 /* Copy a chain of `struct dep'. For 2nd expansion deps, dup the name. */359 /* Copy a chain of 'struct dep'. For 2nd expansion deps, dup the name. */ 545 360 546 361 struct dep * … … 560 375 c->next = 0; 561 376 if (firstnew == 0) 562 377 firstnew = lastnew = c; 563 378 else 564 379 lastnew = lastnew->next = c; 565 380 566 381 d = d->next; … … 568 383 569 384 return firstnew; 570 }571 572 /* Free a chain of 'struct dep'. */573 574 void575 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 385 } 584 386 … … 593 395 struct nameseq *t = ns; 594 396 ns = ns->next; 595 free (t);397 free_ns (t); 596 398 } 597 399 } … … 600 402 601 403 #if !HAVE_STRCASECMP && !HAVE_STRICMP && !HAVE_STRCMPI 602 603 404 /* If we don't have strcasecmp() (from POSIX), or anything that can substitute 604 405 for it, define our own version. */ … … 626 427 627 428 #if !HAVE_STRNCASECMP && !HAVE_STRNICMP && !HAVE_STRNCMPI 628 629 429 /* If we don't have strncasecmp() (from POSIX), or anything that can 630 430 substitute for it, define our own version. */ … … 654 454 655 455 656 #ifdef 456 #ifdef GETLOADAVG_PRIVILEGED 657 457 658 458 #ifdef POSIX … … 667 467 #undef HAVE_SETREGID 668 468 669 #else 469 #else /* Not POSIX. */ 670 470 671 471 /* Some POSIX.1 systems have the seteuid and setegid functions. In a … … 677 477 #undef HAVE_SETEGID 678 478 679 #endif 680 681 #ifndef 479 #endif /* POSIX. */ 480 481 #ifndef HAVE_UNISTD_H 682 482 extern int getuid (), getgid (), geteuid (), getegid (); 683 483 extern int setuid (), setgid (); … … 685 485 extern int seteuid (); 686 486 #else 687 #ifdef 487 #ifdef HAVE_SETREUID 688 488 extern int setreuid (); 689 #endif 690 #endif 489 #endif /* Have setreuid. */ 490 #endif /* Have seteuid. */ 691 491 #ifdef HAVE_SETEGID 692 492 extern int setegid (); 693 493 #else 694 #ifdef 494 #ifdef HAVE_SETREGID 695 495 extern int setregid (); 696 #endif 697 #endif 698 #endif 496 #endif /* Have setregid. */ 497 #endif /* Have setegid. */ 498 #endif /* No <unistd.h>. */ 699 499 700 500 /* Keep track of the user and group IDs for user- and make- access. */ 701 501 static int user_uid = -1, user_gid = -1, make_uid = -1, make_gid = -1; 702 #define access_inited(user_uid != -1)502 #define access_inited (user_uid != -1) 703 503 static enum { make, user } current_access; 704 504 … … 717 517 718 518 fprintf (stderr, _("%s: user %lu (real %lu), group %lu (real %lu)\n"), 719 519 flavor, (unsigned long) geteuid (), (unsigned long) getuid (), 720 520 (unsigned long) getegid (), (unsigned long) getgid ()); 721 521 fflush (stderr); … … 743 543 } 744 544 745 #endif 545 #endif /* GETLOADAVG_PRIVILEGED */ 746 546 747 547 /* Give the process appropriate permissions for access to … … 750 550 user_access (void) 751 551 { 752 #ifdef 552 #ifdef GETLOADAVG_PRIVILEGED 753 553 754 554 if (!access_inited) … … 763 563 which are the IDs of the process that exec'd make. */ 764 564 765 #ifdef 565 #ifdef HAVE_SETEUID 766 566 767 567 /* Modern systems have the seteuid/setegid calls which set only the … … 771 571 pfatal_with_name ("user_access: seteuid"); 772 572 773 #else 774 775 #ifndef 573 #else /* Not HAVE_SETEUID. */ 574 575 #ifndef HAVE_SETREUID 776 576 777 577 /* System V has only the setuid/setgid calls to set user/group IDs. … … 786 586 pfatal_with_name ("user_access: setuid"); 787 587 788 #else 588 #else /* HAVE_SETREUID. */ 789 589 790 590 /* In 4BSD, the setreuid/setregid calls set both the real and effective IDs. … … 798 598 pfatal_with_name ("user_access: setreuid"); 799 599 800 #endif 801 #endif 802 803 #ifdef 600 #endif /* Not HAVE_SETREUID. */ 601 #endif /* HAVE_SETEUID. */ 602 603 #ifdef HAVE_SETEGID 804 604 if (setegid (user_gid) < 0) 805 605 pfatal_with_name ("user_access: setegid"); 806 606 #else 807 #ifndef 607 #ifndef HAVE_SETREGID 808 608 if (setgid (user_gid) < 0) 809 609 pfatal_with_name ("user_access: setgid"); … … 818 618 log_access (_("User access")); 819 619 820 #endif 620 #endif /* GETLOADAVG_PRIVILEGED */ 821 621 } 822 622 … … 826 626 make_access (void) 827 627 { 828 #ifdef 628 #ifdef GETLOADAVG_PRIVILEGED 829 629 830 630 if (!access_inited) … … 836 636 /* See comments in user_access, above. */ 837 637 838 #ifdef 638 #ifdef HAVE_SETEUID 839 639 if (seteuid (make_uid) < 0) 840 640 pfatal_with_name ("make_access: seteuid"); 841 641 #else 842 #ifndef 642 #ifndef HAVE_SETREUID 843 643 if (setuid (make_uid) < 0) 844 644 pfatal_with_name ("make_access: setuid"); … … 849 649 #endif 850 650 851 #ifdef 651 #ifdef HAVE_SETEGID 852 652 if (setegid (make_gid) < 0) 853 653 pfatal_with_name ("make_access: setegid"); 854 654 #else 855 #ifndef 655 #ifndef HAVE_SETREGID 856 656 if (setgid (make_gid) < 0) 857 657 pfatal_with_name ("make_access: setgid"); … … 866 666 log_access (_("Make access")); 867 667 868 #endif 668 #endif /* GETLOADAVG_PRIVILEGED */ 869 669 } 870 670 … … 874 674 child_access (void) 875 675 { 876 #ifdef 676 #ifdef GETLOADAVG_PRIVILEGED 877 677 878 678 if (!access_inited) … … 882 682 They cannot be changed back to make's. */ 883 683 884 #ifndef 684 #ifndef HAVE_SETREUID 885 685 if (setuid (user_uid) < 0) 886 686 pfatal_with_name ("child_access: setuid"); … … 890 690 #endif 891 691 892 #ifndef 692 #ifndef HAVE_SETREGID 893 693 if (setgid (user_gid) < 0) 894 694 pfatal_with_name ("child_access: setgid"); … … 900 700 log_access (_("Child access")); 901 701 902 #endif /* GETLOADAVG_PRIVILEGED */ 903 } 904 702 #endif /* GETLOADAVG_PRIVILEGED */ 703 } 905 704 906 705 #ifdef NEED_GET_PATH_MAX … … 914 713 long int x = pathconf ("/", _PC_PATH_MAX); 915 714 if (x > 0) 916 715 value = x; 917 716 else 918 717 return MAXPATHLEN; 919 718 } 920 719 … … 922 721 } 923 722 #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 can929 remove this (and perhaps other parts of GNU make!) and migrate to using930 gnulib directly.931 932 This is called only through atexit(), which means die() has already been933 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 close938 stdout and make sure that it succeeds before exiting. Otherwise,939 suppose that you go to the extreme of checking the return status940 of every function that does an explicit write to stdout. The last941 printf can succeed in writing to the internal stream buffer, and yet942 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 be944 left with an incomplete output file and the offending program would945 exit successfully. Even calling fflush is not always sufficient,946 since some file systems (NFS and CODA) buffer written/flushed data947 until an actual close call.948 949 Besides, it's wasteful to check the return value from every call950 that writes to stdout -- just let the internal stream state record951 the failure. That's what the ferror test is checking below.952 953 It's important to detect such failures and exit nonzero because many954 tools (most notably `make' and other build-management systems) depend955 on being able to detect failure in other tools via their exit status. */956 957 void958 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 else968 error (NILF, _("write error"));969 exit (EXIT_FAILURE);970 }971 }
Note:
See TracChangeset
for help on using the changeset viewer.