1 /* Miscellaneous generic support functions for GNU Make.
2 Copyright (C) 1988-2013 Free Software Foundation, Inc.
3 This file is part of GNU Make.
5 GNU Make is free software; you can redistribute it and/or modify it under the
6 terms of the GNU General Public License as published by the Free Software
7 Foundation; either version 3 of the License, or (at your option) any later
10 GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
11 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License along with
15 this program. If not, see <http://www.gnu.org/licenses/>. */
22 /* GNU make no longer supports pre-ANSI89 environments. */
29 # include <sys/file.h>
32 /* Compare strings *S1 and *S2.
33 Return negative if the first is less, positive if it is greater,
34 zero if they are equal. */
37 alpha_compare (const void *v1, const void *v2)
39 const char *s1 = *((char **)v1);
40 const char *s2 = *((char **)v2);
44 return strcmp (s1, s2);
47 /* Discard each backslash-newline combination from LINE.
48 Backslash-backslash-newline combinations become backslash-newlines.
49 This is done by copying the text at LINE into itself. */
52 collapse_continuations (char *line)
56 in = strchr (line, '\n');
61 while (out > line && out[-1] == '\\')
66 /* BS_WRITE gets the number of quoted backslashes at
67 the end just before IN, and BACKSLASH gets nonzero
68 if the next character is quoted. */
69 unsigned int backslash = 0;
70 unsigned int bs_write = 0;
71 for (p = in - 1; p >= line && *p == '\\'; --p)
75 backslash = !backslash;
77 /* It should be impossible to go back this far without exiting,
78 but if we do, we can't get the right answer. */
83 /* Output the appropriate number of backslashes. */
84 while (bs_write-- > 0)
87 /* Skip the newline. */
92 /* Backslash/newline handling:
93 In traditional GNU make all trailing whitespace, consecutive
94 backslash/newlines, and any leading whitespace on the next line
95 is reduced to a single space.
96 In POSIX, each backslash/newline and is replaced by a space. */
99 while (out > line && isblank ((unsigned char)out[-1]))
104 /* If the newline isn't quoted, put it in the output. */
107 /* Now copy the following line to the output.
108 Stop when we find backslashes followed by a newline. */
130 /* Print N spaces (used in debug for target-depth). */
133 print_spaces (unsigned int n)
140 /* Return a string whose contents concatenate the NUM strings provided
141 This string lives in static, re-used memory. */
144 concat (unsigned int num, ...)
146 static unsigned int rlen = 0;
147 static char *result = NULL;
151 va_start (args, num);
155 const char *s = va_arg (args, const char *);
156 unsigned int l = xstrlen (s);
163 rlen = ((rlen ? rlen : 60) + l) * 2;
164 result = xrealloc (result, rlen);
167 memcpy (result + ri, s, l);
173 /* Get some more memory if we don't have enough space for the
177 rlen = (rlen ? rlen : 60) * 2;
178 result = xrealloc (result, rlen);
187 #ifndef HAVE_STRERROR
190 strerror (int errnum)
192 extern int errno, sys_nerr;
194 extern char *sys_errlist[];
196 static char buf[] = "Unknown error 12345678901234567890";
198 if (errno < sys_nerr)
199 return sys_errlist[errnum];
201 sprintf (buf, _("Unknown error %d"), errnum);
206 /* Like malloc but get fatal error if memory is exhausted. */
207 /* Don't bother if we're using dmalloc; it provides these for us. */
209 #ifndef HAVE_DMALLOC_H
217 xmalloc (unsigned int size)
219 /* Make sure we don't allocate 0, for pre-ISO implementations. */
220 void *result = malloc (size ? size : 1);
222 fatal (NILF, _("virtual memory exhausted"));
228 xcalloc (unsigned int size)
230 /* Make sure we don't allocate 0, for pre-ISO implementations. */
231 void *result = calloc (size ? size : 1, 1);
233 fatal (NILF, _("virtual memory exhausted"));
239 xrealloc (void *ptr, unsigned int size)
243 /* Some older implementations of realloc() don't conform to ISO. */
246 result = ptr ? realloc (ptr, size) : malloc (size);
248 fatal (NILF, _("virtual memory exhausted"));
254 xstrdup (const char *ptr)
259 result = strdup (ptr);
261 result = malloc (strlen (ptr) + 1);
265 fatal (NILF, _("virtual memory exhausted"));
270 return strcpy (result, ptr);
274 #endif /* HAVE_DMALLOC_H */
277 xstrndup (const char *str, unsigned int length)
282 result = strndup (str, length);
284 fatal (NILF, _("virtual memory exhausted"));
286 result = xmalloc (length + 1);
288 strncpy (result, str, length);
289 result[length] = '\0';
297 Search through the string STRING, which ends at LIMIT, for the character C.
298 Returns a pointer to the first occurrence, or nil if none is found.
299 Like INDEX except that the string searched ends where specified
300 instead of at the first null. */
303 lindex (const char *s, const char *limit, int c)
307 return (char *)(s - 1);
312 /* Return the address of the first whitespace or null in the string S. */
315 end_of_token (const char *s)
317 while (! STOP_SET (*s, MAP_BLANK|MAP_NUL))
322 /* Return the address of the first nonwhitespace or null in the string S. */
325 next_token (const char *s)
327 while (isblank ((unsigned char)*s))
332 /* Find the next token in PTR; return the address of it, and store the length
333 of the token into *LENGTHPTR if LENGTHPTR is not nil. Set *PTR to the end
334 of the token, so this function can be called repeatedly in a loop. */
337 find_next_token (const char **ptr, unsigned int *lengthptr)
339 const char *p = next_token (*ptr);
344 *ptr = end_of_token (p);
346 *lengthptr = *ptr - p;
352 /* Copy a chain of 'struct dep'. For 2nd expansion deps, dup the name. */
355 copy_dep_chain (const struct dep *d)
357 struct dep *firstnew = 0;
358 struct dep *lastnew = 0;
362 struct dep *c = xmalloc (sizeof (struct dep));
363 memcpy (c, d, sizeof (struct dep));
365 if (c->need_2nd_expansion)
366 c->name = xstrdup (c->name);
370 firstnew = lastnew = c;
372 lastnew = lastnew->next = c;
380 /* Free a chain of 'struct dep'. */
383 free_dep_chain (struct dep *d)
393 /* Free a chain of struct nameseq.
394 For struct dep chains use free_dep_chain. */
397 free_ns_chain (struct nameseq *ns)
401 struct nameseq *t = ns;
408 #if !HAVE_STRCASECMP && !HAVE_STRICMP && !HAVE_STRCMPI
409 /* If we don't have strcasecmp() (from POSIX), or anything that can substitute
410 for it, define our own version. */
413 strcasecmp (const char *s1, const char *s2)
417 int c1 = (int) *(s1++);
418 int c2 = (int) *(s2++);
425 if (c1 != '\0' && c1 == c2)
433 #if !HAVE_STRNCASECMP && !HAVE_STRNICMP && !HAVE_STRNCMPI
434 /* If we don't have strncasecmp() (from POSIX), or anything that can
435 substitute for it, define our own version. */
438 strncasecmp (const char *s1, const char *s2, int n)
442 int c1 = (int) *(s1++);
443 int c2 = (int) *(s2++);
450 if (c1 != '\0' && c1 == c2)
460 #ifdef GETLOADAVG_PRIVILEGED
464 /* Hopefully if a system says it's POSIX.1 and has the setuid and setgid
465 functions, they work as POSIX.1 says. Some systems (Alpha OSF/1 1.2,
466 for example) which claim to be POSIX.1 also have the BSD setreuid and
467 setregid functions, but they don't work as in BSD and only the POSIX.1
473 #else /* Not POSIX. */
475 /* Some POSIX.1 systems have the seteuid and setegid functions. In a
476 POSIX-like system, they are the best thing to use. However, some
477 non-POSIX systems have them too but they do not work in the POSIX style
478 and we must use setreuid and setregid instead. */
485 #ifndef HAVE_UNISTD_H
486 extern int getuid (), getgid (), geteuid (), getegid ();
487 extern int setuid (), setgid ();
489 extern int seteuid ();
492 extern int setreuid ();
493 #endif /* Have setreuid. */
494 #endif /* Have seteuid. */
496 extern int setegid ();
499 extern int setregid ();
500 #endif /* Have setregid. */
501 #endif /* Have setegid. */
502 #endif /* No <unistd.h>. */
504 /* Keep track of the user and group IDs for user- and make- access. */
505 static int user_uid = -1, user_gid = -1, make_uid = -1, make_gid = -1;
506 #define access_inited (user_uid != -1)
507 static enum { make, user } current_access;
510 /* Under -d, write a message describing the current IDs. */
513 log_access (const char *flavor)
515 if (! ISDB (DB_JOBS))
518 /* All the other debugging messages go to stdout,
519 but we write this one to stderr because it might be
520 run in a child fork whose stdout is piped. */
522 fprintf (stderr, _("%s: user %lu (real %lu), group %lu (real %lu)\n"),
523 flavor, (unsigned long) geteuid (), (unsigned long) getuid (),
524 (unsigned long) getegid (), (unsigned long) getgid ());
533 user_uid = getuid ();
534 user_gid = getgid ();
536 make_uid = geteuid ();
537 make_gid = getegid ();
539 /* Do these ever fail? */
540 if (user_uid == -1 || user_gid == -1 || make_uid == -1 || make_gid == -1)
541 pfatal_with_name ("get{e}[gu]id");
543 log_access (_("Initialized access"));
545 current_access = make;
549 #endif /* GETLOADAVG_PRIVILEGED */
551 /* Give the process appropriate permissions for access to
552 user data (i.e., to stat files, or to spawn a child process). */
556 #ifdef GETLOADAVG_PRIVILEGED
561 if (current_access == user)
564 /* We are in "make access" mode. This means that the effective user and
565 group IDs are those of make (if it was installed setuid or setgid).
566 We now want to set the effective user and group IDs to the real IDs,
567 which are the IDs of the process that exec'd make. */
571 /* Modern systems have the seteuid/setegid calls which set only the
572 effective IDs, which is ideal. */
574 if (seteuid (user_uid) < 0)
575 pfatal_with_name ("user_access: seteuid");
577 #else /* Not HAVE_SETEUID. */
579 #ifndef HAVE_SETREUID
581 /* System V has only the setuid/setgid calls to set user/group IDs.
582 There is an effective ID, which can be set by setuid/setgid.
583 It can be set (unless you are root) only to either what it already is
584 (returned by geteuid/getegid, now in make_uid/make_gid),
585 the real ID (return by getuid/getgid, now in user_uid/user_gid),
586 or the saved set ID (what the effective ID was before this set-ID
587 executable (make) was exec'd). */
589 if (setuid (user_uid) < 0)
590 pfatal_with_name ("user_access: setuid");
592 #else /* HAVE_SETREUID. */
594 /* In 4BSD, the setreuid/setregid calls set both the real and effective IDs.
595 They may be set to themselves or each other. So you have two alternatives
596 at any one time. If you use setuid/setgid, the effective will be set to
597 the real, leaving only one alternative. Using setreuid/setregid, however,
598 you can toggle between your two alternatives by swapping the values in a
599 single setreuid or setregid call. */
601 if (setreuid (make_uid, user_uid) < 0)
602 pfatal_with_name ("user_access: setreuid");
604 #endif /* Not HAVE_SETREUID. */
605 #endif /* HAVE_SETEUID. */
608 if (setegid (user_gid) < 0)
609 pfatal_with_name ("user_access: setegid");
611 #ifndef HAVE_SETREGID
612 if (setgid (user_gid) < 0)
613 pfatal_with_name ("user_access: setgid");
615 if (setregid (make_gid, user_gid) < 0)
616 pfatal_with_name ("user_access: setregid");
620 current_access = user;
622 log_access (_("User access"));
624 #endif /* GETLOADAVG_PRIVILEGED */
627 /* Give the process appropriate permissions for access to
628 make data (i.e., the load average). */
632 #ifdef GETLOADAVG_PRIVILEGED
637 if (current_access == make)
640 /* See comments in user_access, above. */
643 if (seteuid (make_uid) < 0)
644 pfatal_with_name ("make_access: seteuid");
646 #ifndef HAVE_SETREUID
647 if (setuid (make_uid) < 0)
648 pfatal_with_name ("make_access: setuid");
650 if (setreuid (user_uid, make_uid) < 0)
651 pfatal_with_name ("make_access: setreuid");
656 if (setegid (make_gid) < 0)
657 pfatal_with_name ("make_access: setegid");
659 #ifndef HAVE_SETREGID
660 if (setgid (make_gid) < 0)
661 pfatal_with_name ("make_access: setgid");
663 if (setregid (user_gid, make_gid) < 0)
664 pfatal_with_name ("make_access: setregid");
668 current_access = make;
670 log_access (_("Make access"));
672 #endif /* GETLOADAVG_PRIVILEGED */
675 /* Give the process appropriate permissions for a child process.
676 This is like user_access, but you can't get back to make_access. */
680 #ifdef GETLOADAVG_PRIVILEGED
685 /* Set both the real and effective UID and GID to the user's.
686 They cannot be changed back to make's. */
688 #ifndef HAVE_SETREUID
689 if (setuid (user_uid) < 0)
690 pfatal_with_name ("child_access: setuid");
692 if (setreuid (user_uid, user_uid) < 0)
693 pfatal_with_name ("child_access: setreuid");
696 #ifndef HAVE_SETREGID
697 if (setgid (user_gid) < 0)
698 pfatal_with_name ("child_access: setgid");
700 if (setregid (user_gid, user_gid) < 0)
701 pfatal_with_name ("child_access: setregid");
704 log_access (_("Child access"));
706 #endif /* GETLOADAVG_PRIVILEGED */
709 #ifdef NEED_GET_PATH_MAX
713 static unsigned int value;
717 long int x = pathconf ("/", _PC_PATH_MAX);