1 /* Miscellaneous generic support functions for GNU Make.
2 Copyright (C) 1988,89,90,91,92,93,94,95,97 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
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
10 GNU Make is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNU Make; see the file COPYING. If not, write to
17 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
23 /* Compare strings *S1 and *S2.
24 Return negative if the first is less, positive if it is greater,
25 zero if they are equal. */
28 alpha_compare (s1
, s2
)
33 return strcmp (*s1
, *s2
);
36 /* Discard each backslash-newline combination from LINE.
37 Backslash-backslash-newline combinations become backslash-newlines.
38 This is done by copying the text at LINE into itself. */
41 collapse_continuations (line
)
44 register char *in
, *out
, *p
;
45 register int backslash
;
46 register unsigned int bs_write
;
48 in
= index (line
, '\n');
53 while (out
> line
&& out
[-1] == '\\')
58 /* BS_WRITE gets the number of quoted backslashes at
59 the end just before IN, and BACKSLASH gets nonzero
60 if the next character is quoted. */
63 for (p
= in
- 1; p
>= line
&& *p
== '\\'; --p
)
67 backslash
= !backslash
;
69 /* It should be impossible to go back this far without exiting,
70 but if we do, we can't get the right answer. */
75 /* Output the appropriate number of backslashes. */
76 while (bs_write
-- > 0)
79 /* Skip the newline. */
82 /* If the newline is quoted, discard following whitespace
83 and any preceding whitespace; leave just one space. */
87 while (out
> line
&& isblank (out
[-1]))
92 /* If the newline isn't quoted, put it in the output. */
95 /* Now copy the following line to the output.
96 Stop when we find backslashes followed by a newline. */
119 /* Remove comments from LINE.
120 This is done by copying the text at LINE onto itself. */
123 remove_comments (line
)
128 comment
= find_char_unquote (line
, "#", 0);
131 /* Cut off the line at the #. */
135 /* Print N spaces (used by DEBUGPR for target-depth). */
139 register unsigned int n
;
146 /* Return a newly-allocated string whose contents
147 concatenate those of s1, s2, s3. */
151 register char *s1
, *s2
, *s3
;
153 register unsigned int len1
, len2
, len3
;
154 register char *result
;
156 len1
= *s1
!= '\0' ? strlen (s1
) : 0;
157 len2
= *s2
!= '\0' ? strlen (s2
) : 0;
158 len3
= *s3
!= '\0' ? strlen (s3
) : 0;
160 result
= (char *) xmalloc (len1
+ len2
+ len3
+ 1);
163 bcopy (s1
, result
, len1
);
165 bcopy (s2
, result
+ len1
, len2
);
167 bcopy (s3
, result
+ len1
+ len2
, len3
);
168 *(result
+ len1
+ len2
+ len3
) = '\0';
173 /* Print a message on stdout. */
176 message (prefix
, s1
, s2
, s3
, s4
, s5
, s6
)
178 char *s1
, *s2
, *s3
, *s4
, *s5
, *s6
;
180 log_working_directory (1);
187 printf ("%s: ", program
);
189 printf ("%s[%u]: ", program
, makelevel
);
191 printf (s1
, s2
, s3
, s4
, s5
, s6
);
198 /* Print an error message and exit. */
202 fatal (s1
, s2
, s3
, s4
, s5
, s6
)
203 char *s1
, *s2
, *s3
, *s4
, *s5
, *s6
;
205 log_working_directory (1);
208 fprintf (stderr
, "%s: *** ", program
);
210 fprintf (stderr
, "%s[%u]: *** ", program
, makelevel
);
211 fprintf (stderr
, s1
, s2
, s3
, s4
, s5
, s6
);
212 fputs (". Stop.\n", stderr
);
217 /* Print error message. `s1' is printf control string, `s2' is arg for it. */
222 error (s1
, s2
, s3
, s4
, s5
, s6
)
223 char *s1
, *s2
, *s3
, *s4
, *s5
, *s6
;
225 log_working_directory (1);
228 fprintf (stderr
, "%s: ", program
);
230 fprintf (stderr
, "%s[%u]: ", program
, makelevel
);
231 fprintf (stderr
, s1
, s2
, s3
, s4
, s5
, s6
);
237 makefile_error (file
, lineno
, s1
, s2
, s3
, s4
, s5
, s6
)
240 char *s1
, *s2
, *s3
, *s4
, *s5
, *s6
;
242 log_working_directory (1);
244 fprintf (stderr
, "%s:%u: ", file
, lineno
);
245 fprintf (stderr
, s1
, s2
, s3
, s4
, s5
, s6
);
251 makefile_fatal (file
, lineno
, s1
, s2
, s3
, s4
, s5
, s6
)
254 char *s1
, *s2
, *s3
, *s4
, *s5
, *s6
;
257 fatal(s1
, s2
, s3
, s4
, s5
, s6
);
259 log_working_directory (1);
261 fprintf (stderr
, "%s:%u: *** ", file
, lineno
);
262 fprintf (stderr
, s1
, s2
, s3
, s4
, s5
, s6
);
263 fputs (". Stop.\n", stderr
);
268 #ifndef HAVE_STRERROR
276 extern int errno
, sys_nerr
;
278 extern char *sys_errlist
[];
280 static char buf
[] = "Unknown error 12345678901234567890";
282 if (errno
< sys_nerr
)
283 return sys_errlist
[errnum
];
285 sprintf (buf
, "Unknown error %d", errnum
);
290 /* Print an error message from errno. */
293 perror_with_name (str
, name
)
296 error ("%s%s: %s", str
, name
, strerror (errno
));
299 /* Print an error message from errno and exit. */
302 pfatal_with_name (name
)
305 fatal ("%s: %s", name
, strerror (errno
));
310 /* Like malloc but get fatal error if memory is exhausted. */
319 char *result
= (char *) malloc (size
);
321 fatal ("virtual memory exhausted");
331 char *result
= (char *) realloc (ptr
, size
);
333 fatal ("virtual memory exhausted");
338 savestring (str
, length
)
342 register char *out
= (char *) xmalloc (length
+ 1);
344 bcopy (str
, out
, length
);
349 /* Search string BIG (length BLEN) for an occurrence of
350 string SMALL (length SLEN). Return a pointer to the
351 beginning of the first occurrence, or return nil if none found. */
354 sindex (big
, blen
, small
, slen
)
360 register unsigned int b
;
365 slen
= strlen (small
);
367 for (b
= 0; b
< blen
; ++b
)
368 if (big
[b
] == *small
&& !strncmp (&big
[b
+ 1], small
+ 1, slen
- 1))
375 Search through the string STRING, which ends at LIMIT, for the character C.
376 Returns a pointer to the first occurrence, or nil if none is found.
377 Like INDEX except that the string searched ends where specified
378 instead of at the first null. */
382 register char *s
, *limit
;
392 /* Return the address of the first whitespace or null in the string S. */
398 while (*s
!= '\0' && !isblank (*s
))
405 * Same as end_of_token, but take into account a stop character
408 end_of_token_w32 (s
, stopchar
)
412 register char *p
= s
;
413 register int backslash
= 0;
415 while (*p
!= '\0' && *p
!= stopchar
&& (backslash
|| !isblank (*p
)))
419 backslash
= !backslash
;
422 backslash
= !backslash
;
434 /* Return the address of the first nonwhitespace or null in the string S. */
440 register char *p
= s
;
447 /* Find the next token in PTR; return the address of it, and store the
448 length of the token into *LENGTHPTR if LENGTHPTR is not nil. */
451 find_next_token (ptr
, lengthptr
)
453 unsigned int *lengthptr
;
455 char *p
= next_token (*ptr
);
461 *ptr
= end
= end_of_token (p
);
463 *lengthptr
= end
- p
;
467 /* Copy a chain of `struct dep', making a new chain
468 with the same contents as the old one. */
472 register struct dep
*d
;
474 register struct dep
*c
;
475 struct dep
*firstnew
= 0;
476 struct dep
*lastnew
= 0;
480 c
= (struct dep
*) xmalloc (sizeof (struct dep
));
481 bcopy ((char *) d
, (char *) c
, sizeof (struct dep
));
483 c
->name
= savestring (c
->name
, strlen (c
->name
));
486 firstnew
= lastnew
= c
;
488 lastnew
= lastnew
->next
= c
;
497 /* The losing compiler on this machine can't handle this macro. */
503 return dep
->name
== 0 ? dep
->file
->name
: dep
->name
;
507 #ifdef GETLOADAVG_PRIVILEGED
511 /* Hopefully if a system says it's POSIX.1 and has the setuid and setgid
512 functions, they work as POSIX.1 says. Some systems (Alpha OSF/1 1.2,
513 for example) which claim to be POSIX.1 also have the BSD setreuid and
514 setregid functions, but they don't work as in BSD and only the POSIX.1
520 #else /* Not POSIX. */
522 /* Some POSIX.1 systems have the seteuid and setegid functions. In a
523 POSIX-like system, they are the best thing to use. However, some
524 non-POSIX systems have them too but they do not work in the POSIX style
525 and we must use setreuid and setregid instead. */
532 #ifndef HAVE_UNISTD_H
533 extern int getuid (), getgid (), geteuid (), getegid ();
534 extern int setuid (), setgid ();
536 extern int seteuid ();
539 extern int setreuid ();
540 #endif /* Have setreuid. */
541 #endif /* Have seteuid. */
543 extern int setegid ();
546 extern int setregid ();
547 #endif /* Have setregid. */
548 #endif /* Have setegid. */
549 #endif /* No <unistd.h>. */
551 /* Keep track of the user and group IDs for user- and make- access. */
552 static int user_uid
= -1, user_gid
= -1, make_uid
= -1, make_gid
= -1;
553 #define access_inited (user_uid != -1)
554 static enum { make
, user
} current_access
;
557 /* Under -d, write a message describing the current IDs. */
566 /* All the other debugging messages go to stdout,
567 but we write this one to stderr because it might be
568 run in a child fork whose stdout is piped. */
570 fprintf (stderr
, "%s access: user %lu (real %lu), group %lu (real %lu)\n",
571 flavor
, (unsigned long) geteuid (), (unsigned long) getuid (),
572 (unsigned long) getegid (), (unsigned long) getgid ());
581 user_uid
= getuid ();
582 user_gid
= getgid ();
584 make_uid
= geteuid ();
585 make_gid
= getegid ();
587 /* Do these ever fail? */
588 if (user_uid
== -1 || user_gid
== -1 || make_uid
== -1 || make_gid
== -1)
589 pfatal_with_name ("get{e}[gu]id");
591 log_access ("Initialized");
593 current_access
= make
;
597 #endif /* GETLOADAVG_PRIVILEGED */
599 /* Give the process appropriate permissions for access to
600 user data (i.e., to stat files, or to spawn a child process). */
604 #ifdef GETLOADAVG_PRIVILEGED
609 if (current_access
== user
)
612 /* We are in "make access" mode. This means that the effective user and
613 group IDs are those of make (if it was installed setuid or setgid).
614 We now want to set the effective user and group IDs to the real IDs,
615 which are the IDs of the process that exec'd make. */
619 /* Modern systems have the seteuid/setegid calls which set only the
620 effective IDs, which is ideal. */
622 if (seteuid (user_uid
) < 0)
623 pfatal_with_name ("user_access: seteuid");
625 #else /* Not HAVE_SETEUID. */
627 #ifndef HAVE_SETREUID
629 /* System V has only the setuid/setgid calls to set user/group IDs.
630 There is an effective ID, which can be set by setuid/setgid.
631 It can be set (unless you are root) only to either what it already is
632 (returned by geteuid/getegid, now in make_uid/make_gid),
633 the real ID (return by getuid/getgid, now in user_uid/user_gid),
634 or the saved set ID (what the effective ID was before this set-ID
635 executable (make) was exec'd). */
637 if (setuid (user_uid
) < 0)
638 pfatal_with_name ("user_access: setuid");
640 #else /* HAVE_SETREUID. */
642 /* In 4BSD, the setreuid/setregid calls set both the real and effective IDs.
643 They may be set to themselves or each other. So you have two alternatives
644 at any one time. If you use setuid/setgid, the effective will be set to
645 the real, leaving only one alternative. Using setreuid/setregid, however,
646 you can toggle between your two alternatives by swapping the values in a
647 single setreuid or setregid call. */
649 if (setreuid (make_uid
, user_uid
) < 0)
650 pfatal_with_name ("user_access: setreuid");
652 #endif /* Not HAVE_SETREUID. */
653 #endif /* HAVE_SETEUID. */
656 if (setegid (user_gid
) < 0)
657 pfatal_with_name ("user_access: setegid");
659 #ifndef HAVE_SETREGID
660 if (setgid (user_gid
) < 0)
661 pfatal_with_name ("user_access: setgid");
663 if (setregid (make_gid
, user_gid
) < 0)
664 pfatal_with_name ("user_access: setregid");
668 current_access
= user
;
672 #endif /* GETLOADAVG_PRIVILEGED */
675 /* Give the process appropriate permissions for access to
676 make data (i.e., the load average). */
680 #ifdef GETLOADAVG_PRIVILEGED
685 if (current_access
== make
)
688 /* See comments in user_access, above. */
691 if (seteuid (make_uid
) < 0)
692 pfatal_with_name ("make_access: seteuid");
694 #ifndef HAVE_SETREUID
695 if (setuid (make_uid
) < 0)
696 pfatal_with_name ("make_access: setuid");
698 if (setreuid (user_uid
, make_uid
) < 0)
699 pfatal_with_name ("make_access: setreuid");
704 if (setegid (make_gid
) < 0)
705 pfatal_with_name ("make_access: setegid");
707 #ifndef HAVE_SETREGID
708 if (setgid (make_gid
) < 0)
709 pfatal_with_name ("make_access: setgid");
711 if (setregid (user_gid
, make_gid
) < 0)
712 pfatal_with_name ("make_access: setregid");
716 current_access
= make
;
720 #endif /* GETLOADAVG_PRIVILEGED */
723 /* Give the process appropriate permissions for a child process.
724 This is like user_access, but you can't get back to make_access. */
728 #ifdef GETLOADAVG_PRIVILEGED
733 /* Set both the real and effective UID and GID to the user's.
734 They cannot be changed back to make's. */
736 #ifndef HAVE_SETREUID
737 if (setuid (user_uid
) < 0)
738 pfatal_with_name ("child_access: setuid");
740 if (setreuid (user_uid
, user_uid
) < 0)
741 pfatal_with_name ("child_access: setreuid");
744 #ifndef HAVE_SETREGID
745 if (setgid (user_gid
) < 0)
746 pfatal_with_name ("child_access: setgid");
748 if (setregid (user_gid
, user_gid
) < 0)
749 pfatal_with_name ("child_access: setregid");
752 log_access ("Child");
754 #endif /* GETLOADAVG_PRIVILEGED */
757 #ifdef NEED_GET_PATH_MAX
761 static unsigned int value
;
765 long int x
= pathconf ("/", _PC_PATH_MAX
);