1 /* Miscellaneous generic support functions for GNU Make.
2 Copyright (C) 1988, 89, 90, 91, 92, 93, 94, 1995 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');
54 while (out
[-1] == '\\')
59 /* BS_WRITE gets the number of quoted backslashes at
60 the end just before IN, and BACKSLASH gets nonzero
61 if the next character is quoted. */
64 for (p
= in
- 1; p
>= line
&& *p
== '\\'; --p
)
68 backslash
= !backslash
;
70 /* It should be impossible to go back this far without exiting,
71 but if we do, we can't get the right answer. */
76 /* Output the appropriate number of backslashes. */
77 while (bs_write
-- > 0)
80 /* Skip the newline. */
83 /* If the newline is quoted, discard following whitespace
84 and any preceding whitespace; leave just one space. */
88 while (out
> line
&& isblank (out
[-1]))
93 /* If the newline isn't quoted, put it in the output. */
96 /* Now copy the following line to the output.
97 Stop when we find backslashes followed by a newline. */
120 /* Remove comments from LINE.
121 This is done by copying the text at LINE onto itself. */
124 remove_comments (line
)
129 comment
= find_char_unquote (line
, '#', 0);
132 /* Cut off the line at the #. */
136 /* Print N spaces (used by DEBUGPR for target-depth). */
140 register unsigned int n
;
147 /* Return a newly-allocated string whose contents
148 concatenate those of s1, s2, s3. */
152 register char *s1
, *s2
, *s3
;
154 register unsigned int len1
, len2
, len3
;
155 register char *result
;
157 len1
= *s1
!= '\0' ? strlen (s1
) : 0;
158 len2
= *s2
!= '\0' ? strlen (s2
) : 0;
159 len3
= *s3
!= '\0' ? strlen (s3
) : 0;
161 result
= (char *) xmalloc (len1
+ len2
+ len3
+ 1);
164 bcopy (s1
, result
, len1
);
166 bcopy (s2
, result
+ len1
, len2
);
168 bcopy (s3
, result
+ len1
+ len2
, len3
);
169 *(result
+ len1
+ len2
+ len3
) = '\0';
174 /* Print a message on stdout. */
177 message (s1
, s2
, s3
, s4
, s5
, s6
)
178 char *s1
, *s2
, *s3
, *s4
, *s5
, *s6
;
181 printf ("%s: ", program
);
183 printf ("%s[%u]: ", program
, makelevel
);
184 printf (s1
, s2
, s3
, s4
, s5
, s6
);
189 /* Print an error message and exit. */
193 fatal (s1
, s2
, s3
, s4
, s5
, s6
)
194 char *s1
, *s2
, *s3
, *s4
, *s5
, *s6
;
197 fprintf (stderr
, "%s: *** ", program
);
199 fprintf (stderr
, "%s[%u]: *** ", program
, makelevel
);
200 fprintf (stderr
, s1
, s2
, s3
, s4
, s5
, s6
);
201 fputs (". Stop.\n", stderr
);
206 /* Print error message. `s1' is printf control string, `s2' is arg for it. */
211 error (s1
, s2
, s3
, s4
, s5
, s6
)
212 char *s1
, *s2
, *s3
, *s4
, *s5
, *s6
;
215 fprintf (stderr
, "%s: ", program
);
217 fprintf (stderr
, "%s[%u]: ", program
, makelevel
);
218 fprintf (stderr
, s1
, s2
, s3
, s4
, s5
, s6
);
224 makefile_error (file
, lineno
, s1
, s2
, s3
, s4
, s5
, s6
)
227 char *s1
, *s2
, *s3
, *s4
, *s5
, *s6
;
229 fprintf (stderr
, "%s:%u: ", file
, lineno
);
230 fprintf (stderr
, s1
, s2
, s3
, s4
, s5
, s6
);
236 makefile_fatal (file
, lineno
, s1
, s2
, s3
, s4
, s5
, s6
)
239 char *s1
, *s2
, *s3
, *s4
, *s5
, *s6
;
241 fprintf (stderr
, "%s:%u: *** ", file
, lineno
);
242 fprintf (stderr
, s1
, s2
, s3
, s4
, s5
, s6
);
243 fputs (". Stop.\n", stderr
);
248 #ifndef HAVE_STRERROR
256 extern int errno
, sys_nerr
;
257 extern char *sys_errlist
[];
258 static char buf
[] = "Unknown error 12345678901234567890";
260 if (errno
< sys_nerr
)
261 return sys_errlist
[errnum
];
263 sprintf (buf
, "Unknown error %d", errnum
);
268 /* Print an error message from errno. */
271 perror_with_name (str
, name
)
274 error ("%s%s: %s", str
, name
, strerror (errno
));
277 /* Print an error message from errno and exit. */
280 pfatal_with_name (name
)
283 fatal ("%s: %s", name
, strerror (errno
));
288 /* Like malloc but get fatal error if memory is exhausted. */
297 char *result
= (char *) malloc (size
);
299 fatal ("virtual memory exhausted");
309 char *result
= (char *) realloc (ptr
, size
);
311 fatal ("virtual memory exhausted");
316 savestring (str
, length
)
320 register char *out
= (char *) xmalloc (length
+ 1);
322 bcopy (str
, out
, length
);
327 /* Search string BIG (length BLEN) for an occurrence of
328 string SMALL (length SLEN). Return a pointer to the
329 beginning of the first occurrence, or return nil if none found. */
332 sindex (big
, blen
, small
, slen
)
338 register unsigned int b
;
343 slen
= strlen (small
);
345 for (b
= 0; b
< blen
; ++b
)
346 if (big
[b
] == *small
&& !strncmp (&big
[b
+ 1], small
+ 1, slen
- 1))
353 Search through the string STRING, which ends at LIMIT, for the character C.
354 Returns a pointer to the first occurrence, or nil if none is found.
355 Like INDEX except that the string searched ends where specified
356 instead of at the first null. */
360 register char *s
, *limit
;
370 /* Return the address of the first whitespace or null in the string S. */
376 while (*s
!= '\0' && !isblank (*s
))
381 /* Return the address of the first nonwhitespace or null in the string S. */
387 register char *p
= s
;
394 /* Find the next token in PTR; return the address of it, and store the
395 length of the token into *LENGTHPTR if LENGTHPTR is not nil. */
398 find_next_token (ptr
, lengthptr
)
400 unsigned int *lengthptr
;
402 char *p
= next_token (*ptr
);
408 *ptr
= end
= end_of_token (p
);
410 *lengthptr
= end
- p
;
414 /* Copy a chain of `struct dep', making a new chain
415 with the same contents as the old one. */
419 register struct dep
*d
;
421 register struct dep
*c
;
422 struct dep
*firstnew
= 0;
427 c
= (struct dep
*) xmalloc (sizeof (struct dep
));
428 bcopy ((char *) d
, (char *) c
, sizeof (struct dep
));
430 c
->name
= savestring (c
->name
, strlen (c
->name
));
433 firstnew
= lastnew
= c
;
435 lastnew
= lastnew
->next
= c
;
444 /* The losing compiler on this machine can't handle this macro. */
450 return dep
->name
== 0 ? dep
->file
->name
: dep
->name
;
454 #ifdef GETLOADAVG_PRIVILEGED
458 /* Hopefully if a system says it's POSIX.1 and has the setuid and setgid
459 functions, they work as POSIX.1 says. Some systems (Alpha OSF/1 1.2,
460 for example) which claim to be POSIX.1 also have the BSD setreuid and
461 setregid functions, but they don't work as in BSD and only the POSIX.1
467 #else /* Not POSIX. */
469 /* Some POSIX.1 systems have the seteuid and setegid functions. In a
470 POSIX-like system, they are the best thing to use. However, some
471 non-POSIX systems have them too but they do not work in the POSIX style
472 and we must use setreuid and setregid instead. */
479 #ifndef HAVE_UNISTD_H
480 extern int getuid (), getgid (), geteuid (), getegid ();
481 extern int setuid (), setgid ();
483 extern int seteuid ();
486 extern int setreuid ();
487 #endif /* Have setreuid. */
488 #endif /* Have seteuid. */
490 extern int setegid ();
493 extern int setregid ();
494 #endif /* Have setregid. */
495 #endif /* Have setegid. */
496 #endif /* No <unistd.h>. */
498 /* Keep track of the user and group IDs for user- and make- access. */
499 static int user_uid
= -1, user_gid
= -1, make_uid
= -1, make_gid
= -1;
500 #define access_inited (user_uid != -1)
501 static enum { make
, user
} current_access
;
504 /* Under -d, write a message describing the current IDs. */
513 /* All the other debugging messages go to stdout,
514 but we write this one to stderr because it might be
515 run in a child fork whose stdout is piped. */
517 fprintf (stderr
, "%s access: user %d (real %d), group %d (real %d)\n",
518 flavor
, geteuid (), getuid (), getegid (), getgid ());
526 user_uid
= getuid ();
527 user_gid
= getgid ();
529 make_uid
= geteuid ();
530 make_gid
= getegid ();
532 /* Do these ever fail? */
533 if (user_uid
== -1 || user_gid
== -1 || make_uid
== -1 || make_gid
== -1)
534 pfatal_with_name ("get{e}[gu]id");
536 log_access ("Initialized");
538 current_access
= make
;
541 #endif /* GETLOADAVG_PRIVILEGED */
543 /* Give the process appropriate permissions for access to
544 user data (i.e., to stat files, or to spawn a child process). */
548 #ifdef GETLOADAVG_PRIVILEGED
553 if (current_access
== user
)
556 /* We are in "make access" mode. This means that the effective user and
557 group IDs are those of make (if it was installed setuid or setgid).
558 We now want to set the effective user and group IDs to the real IDs,
559 which are the IDs of the process that exec'd make. */
563 /* Modern systems have the seteuid/setegid calls which set only the
564 effective IDs, which is ideal. */
566 if (seteuid (user_uid
) < 0)
567 pfatal_with_name ("user_access: seteuid");
569 #else /* Not HAVE_SETEUID. */
571 #ifndef HAVE_SETREUID
573 /* System V has only the setuid/setgid calls to set user/group IDs.
574 There is an effective ID, which can be set by setuid/setgid.
575 It can be set (unless you are root) only to either what it already is
576 (returned by geteuid/getegid, now in make_uid/make_gid),
577 the real ID (return by getuid/getgid, now in user_uid/user_gid),
578 or the saved set ID (what the effective ID was before this set-ID
579 executable (make) was exec'd). */
581 if (setuid (user_uid
) < 0)
582 pfatal_with_name ("user_access: setuid");
584 #else /* HAVE_SETREUID. */
586 /* In 4BSD, the setreuid/setregid calls set both the real and effective IDs.
587 They may be set to themselves or each other. So you have two alternatives
588 at any one time. If you use setuid/setgid, the effective will be set to
589 the real, leaving only one alternative. Using setreuid/setregid, however,
590 you can toggle between your two alternatives by swapping the values in a
591 single setreuid or setregid call. */
593 if (setreuid (make_uid
, user_uid
) < 0)
594 pfatal_with_name ("user_access: setreuid");
596 #endif /* Not HAVE_SETREUID. */
597 #endif /* HAVE_SETEUID. */
600 if (setegid (user_gid
) < 0)
601 pfatal_with_name ("user_access: setegid");
603 #ifndef HAVE_SETREGID
604 if (setgid (user_gid
) < 0)
605 pfatal_with_name ("user_access: setgid");
607 if (setregid (make_gid
, user_gid
) < 0)
608 pfatal_with_name ("user_access: setregid");
612 current_access
= user
;
616 #endif /* GETLOADAVG_PRIVILEGED */
619 /* Give the process appropriate permissions for access to
620 make data (i.e., the load average). */
624 #ifdef GETLOADAVG_PRIVILEGED
629 if (current_access
== make
)
632 /* See comments in user_access, above. */
635 if (seteuid (make_uid
) < 0)
636 pfatal_with_name ("make_access: seteuid");
638 #ifndef HAVE_SETREUID
639 if (setuid (make_uid
) < 0)
640 pfatal_with_name ("make_access: setuid");
642 if (setreuid (user_uid
, make_uid
) < 0)
643 pfatal_with_name ("make_access: setreuid");
648 if (setegid (make_gid
) < 0)
649 pfatal_with_name ("make_access: setegid");
651 #ifndef HAVE_SETREGID
652 if (setgid (make_gid
) < 0)
653 pfatal_with_name ("make_access: setgid");
655 if (setregid (user_gid
, make_gid
) < 0)
656 pfatal_with_name ("make_access: setregid");
660 current_access
= make
;
664 #endif /* GETLOADAVG_PRIVILEGED */
667 /* Give the process appropriate permissions for a child process.
668 This is like user_access, but you can't get back to make_access. */
672 #ifdef GETLOADAVG_PRIVILEGED
677 /* Set both the real and effective UID and GID to the user's.
678 They cannot be changed back to make's. */
680 #ifndef HAVE_SETREUID
681 if (setuid (user_uid
) < 0)
682 pfatal_with_name ("child_access: setuid");
684 if (setreuid (user_uid
, user_uid
) < 0)
685 pfatal_with_name ("child_access: setreuid");
688 #ifndef HAVE_SETREGID
689 if (setgid (user_gid
) < 0)
690 pfatal_with_name ("child_access: setgid");
692 if (setregid (user_gid
, user_gid
) < 0)
693 pfatal_with_name ("child_access: setregid");
696 log_access ("Child");
698 #endif /* GETLOADAVG_PRIVILEGED */
701 #ifdef NEED_GET_PATH_MAX
705 static unsigned int value
;
709 long int x
= pathconf ("/", _PC_PATH_MAX
);
720 /* On some systems, stat can return EINTR. */
723 safe_stat (name
, buf
)
732 ret
= stat (name
, buf
);
734 while (ret
< 0 && errno
== EINTR
);