1 /* Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
2 This file is part of GNU Make.
4 GNU Make is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 GNU Make is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with GNU Make; see the file COPYING. If not, write to
16 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
22 /* Compare strings *S1 and *S2.
23 Return negative if the first is less, positive if it is greater,
24 zero if they are equal. */
27 alpha_compare (s1
, s2
)
32 return strcmp (*s1
, *s2
);
35 /* Discard each backslash-newline combination from LINE.
36 Backslash-backslash-newline combinations become backslash-newlines.
37 This is done by copying the text at LINE into itself. */
40 collapse_continuations (line
)
43 register char *in
, *out
, *p
;
44 register int backslash
;
45 register unsigned int bs_write
;
47 in
= index (line
, '\n');
53 while (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
)
126 register char *p
, *p2
;
127 register int backslash
;
128 register unsigned int bs_write
;
132 p
= index (line
, '#');
138 for (p2
= p
- 1; p2
>= line
&& *p2
== '\\'; --p2
)
142 backslash
= !backslash
;
147 /* Cut off the line at the #. */
152 /* strcpy better copy left to right. */
154 strcpy (p2
+ 1 + bs_write
, line
);
158 /* Print N spaces (used by DEBUGPR for target-depth). */
162 register unsigned int n
;
169 /* Return a newly-allocated string whose contents
170 concatenate those of s1, s2, s3. */
174 register char *s1
, *s2
, *s3
;
176 register unsigned int len1
, len2
, len3
;
177 register char *result
;
179 len1
= *s1
!= '\0' ? strlen (s1
) : 0;
180 len2
= *s2
!= '\0' ? strlen (s2
) : 0;
181 len3
= *s3
!= '\0' ? strlen (s3
) : 0;
183 result
= (char *) xmalloc (len1
+ len2
+ len3
+ 1);
186 bcopy (s1
, result
, len1
);
188 bcopy (s2
, result
+ len1
, len2
);
190 bcopy (s3
, result
+ len1
+ len2
, len3
);
191 *(result
+ len1
+ len2
+ len3
) = '\0';
196 /* Print a message on stdout. */
199 message (s1
, s2
, s3
, s4
, s5
, s6
)
200 char *s1
, *s2
, *s3
, *s4
, *s5
, *s6
;
203 printf ("%s: ", program
);
205 printf ("%s[%u]: ", program
, makelevel
);
206 printf (s1
, s2
, s3
, s4
, s5
, s6
);
211 /* Print an error message and exit. */
215 fatal (s1
, s2
, s3
, s4
, s5
, s6
)
216 char *s1
, *s2
, *s3
, *s4
, *s5
, *s6
;
219 fprintf (stderr
, "%s: *** ", program
);
221 fprintf (stderr
, "%s[%u]: *** ", program
, makelevel
);
222 fprintf (stderr
, s1
, s2
, s3
, s4
, s5
, s6
);
223 fputs (". Stop.\n", stderr
);
228 /* Print error message. `s1' is printf control string, `s2' is arg for it. */
233 error (s1
, s2
, s3
, s4
, s5
, s6
)
234 char *s1
, *s2
, *s3
, *s4
, *s5
, *s6
;
237 fprintf (stderr
, "%s: ", program
);
239 fprintf (stderr
, "%s[%u]: ", program
, makelevel
);
240 fprintf (stderr
, s1
, s2
, s3
, s4
, s5
, s6
);
246 makefile_error (file
, lineno
, s1
, s2
, s3
, s4
, s5
, s6
)
249 char *s1
, *s2
, *s3
, *s4
, *s5
, *s6
;
251 fprintf (stderr
, "%s:%u: ", file
, lineno
);
252 fprintf (stderr
, s1
, s2
, s3
, s4
, s5
, s6
);
258 makefile_fatal (file
, lineno
, s1
, s2
, s3
, s4
, s5
, s6
)
261 char *s1
, *s2
, *s3
, *s4
, *s5
, *s6
;
263 fprintf (stderr
, "%s:%u: *** ", file
, lineno
);
264 fprintf (stderr
, s1
, s2
, s3
, s4
, s5
, s6
);
265 fputs (". Stop.\n", stderr
);
270 /* Print an error message from errno. */
273 perror_with_name (str
, name
)
276 extern int errno
, sys_nerr
;
277 extern char *sys_errlist
[];
279 if (errno
< sys_nerr
)
280 error ("%s%s: %s", str
, name
, sys_errlist
[errno
]);
282 error ("%s%s: Unknown error %d", str
, name
, errno
);
285 /* Print an error message from errno and exit. */
288 pfatal_with_name (name
)
291 extern int errno
, sys_nerr
;
292 extern char *sys_errlist
[];
294 if (errno
< sys_nerr
)
295 fatal ("%s: %s", name
, sys_errlist
[errno
]);
297 fatal ("%s: Unknown error %d", name
, errno
);
302 /* Like malloc but get fatal error if memory is exhausted. */
311 char *result
= (char *) malloc (size
);
313 fatal ("virtual memory exhausted");
323 char *result
= (char *) realloc (ptr
, size
);
325 fatal ("virtual memory exhausted");
330 savestring (str
, length
)
334 register char *out
= (char *) xmalloc (length
+ 1);
336 bcopy (str
, out
, length
);
341 /* Search string BIG (length BLEN) for an occurrence of
342 string SMALL (length SLEN). Return a pointer to the
343 beginning of the first occurrence, or return nil if none found. */
346 sindex (big
, blen
, small
, slen
)
352 register unsigned int b
;
357 slen
= strlen (small
);
359 for (b
= 0; b
< blen
; ++b
)
360 if (big
[b
] == *small
&& !strncmp (&big
[b
+ 1], small
+ 1, slen
- 1))
367 Search through the string STRING, which ends at LIMIT, for the character C.
368 Returns a pointer to the first occurrence, or nil if none is found.
369 Like INDEX except that the string searched ends where specified
370 instead of at the first null. */
374 register char *s
, *limit
;
384 /* Return the address of the first whitespace or null in the string S. */
390 register char *p
= s
;
391 register int backslash
= 0;
393 while (*p
!= '\0' && (backslash
|| !isblank (*p
)))
397 backslash
= !backslash
;
400 backslash
= !backslash
;
411 /* Return the address of the first nonwhitespace or null in the string S. */
417 register char *p
= s
;
424 /* Find the next token in PTR; return the address of it, and store the
425 length of the token into *LENGTHPTR if LENGTHPTR is not nil. */
428 find_next_token (ptr
, lengthptr
)
430 unsigned int *lengthptr
;
432 char *p
= next_token (*ptr
);
438 *ptr
= end
= end_of_token (p
);
440 *lengthptr
= end
- p
;
444 /* Copy a chain of `struct dep', making a new chain
445 with the same contents as the old one. */
449 register struct dep
*d
;
451 register struct dep
*c
;
452 struct dep
*firstnew
= 0;
457 c
= (struct dep
*) xmalloc (sizeof (struct dep
));
458 bcopy ((char *) d
, (char *) c
, sizeof (struct dep
));
460 c
->name
= savestring (c
->name
, strlen (c
->name
));
463 firstnew
= lastnew
= c
;
465 lastnew
= lastnew
->next
= c
;
474 /* The losing compiler on this machine can't handle this macro. */
480 return dep
->name
== 0 ? dep
->file
->name
: dep
->name
;
484 #ifdef GETLOADAVG_PRIVILEGED
486 #ifndef HAVE_UNISTD_H
487 extern int getuid (), getgid (), geteuid (), getegid ();
488 extern int setuid (), setgid ();
490 extern int setreuid ();
491 #endif /* Have setreuid. */
493 extern int setregid ();
494 #endif /* Have setregid. */
495 #endif /* No <unistd.h>. */
497 /* Keep track of the user and group IDs for user- and make- access. */
498 static int user_uid
= -1, user_gid
= -1, make_uid
= -1, make_gid
= -1;
499 #define access_inited (user_uid != -1)
500 static enum { make
, user
} current_access
;
503 /* Under -d, write a message describing the current IDs. */
512 printf ("%s access: user %d (real %d), group %d (real %d)\n",
513 flavor
, geteuid (), getuid (), getegid (), getgid ());
521 user_uid
= getuid ();
522 user_gid
= getgid ();
524 make_uid
= geteuid ();
525 make_gid
= getegid ();
527 /* Do these ever fail? */
528 if (user_uid
== -1 || user_gid
== -1 || make_uid
== -1 || make_gid
== -1)
529 pfatal_with_name ("get{e}[gu]id");
531 log_access ("Initialized");
533 current_access
= make
;
536 #endif /* GETLOADAVG_PRIVILEGED */
538 /* Give the process appropriate permissions for access to
539 user data (i.e., to stat files, or to spawn a child process). */
543 #ifdef GETLOADAVG_PRIVILEGED
548 if (current_access
== user
)
551 /* We are in "make access" mode. This means that the effective user and
552 group IDs are those of make (if it was installed setuid or setgid).
553 We now want to set the effective user and group IDs to the real IDs,
554 which are the IDs of the process that exec'd make. */
556 #ifndef HAVE_SETREUID
558 /* System V has only the setuid/setgid calls to set user/group IDs.
559 There is an effective ID, which can be set by setuid/setgid.
560 It can be set (unless you are root) only to either what it already is
561 (returned by geteuid/getegid, now in make_uid/make_gid),
562 the real ID (return by getuid/getgid, now in user_uid/user_gid),
563 or the saved set ID (what the effective ID was before this set-ID
564 executable (make) was exec'd). */
566 if (setuid (user_uid
) < 0)
567 pfatal_with_name ("user_access: setuid");
571 /* In 4BSD, the setreuid/setregid calls set both the real and effective IDs.
572 They may be set to themselves or each other. So you have two alternatives
573 at any one time. If you use setuid/setgid, the effective will be set to
574 the real, leaving only one alternative. Using setreuid/setregid, however,
575 you can toggle between your two alternatives by swapping the values in a
576 single setreuid or setregid call. */
578 if (setreuid (make_uid
, user_uid
) < 0)
579 pfatal_with_name ("user_access: setreuid");
583 #ifndef HAVE_SETREGID
584 if (setgid (user_gid
) < 0)
585 pfatal_with_name ("user_access: setgid");
587 if (setregid (make_gid
, user_gid
) < 0)
588 pfatal_with_name ("user_access: setregid");
591 current_access
= user
;
595 #endif /* GETLOADAVG_PRIVILEGED */
598 /* Give the process appropriate permissions for access to
599 make data (i.e., the load average). */
603 #ifdef GETLOADAVG_PRIVILEGED
608 if (current_access
== make
)
611 /* See comments in user_access, above. */
613 #ifndef HAVE_SETREUID
614 if (setuid (make_uid
) < 0)
615 pfatal_with_name ("make_access: setuid");
617 if (setreuid (user_uid
, make_uid
) < 0)
618 pfatal_with_name ("make_access: setreuid");
621 #ifndef HAVE_SETREGID
622 if (setgid (make_gid
) < 0)
623 pfatal_with_name ("make_access: setgid");
625 if (setregid (user_gid
, make_gid
) < 0)
626 pfatal_with_name ("make_access: setregid");
629 current_access
= make
;
633 #endif /* GETLOADAVG_PRIVILEGED */
636 /* Give the process appropriate permissions for a child process.
637 This is like user_access, but you can't get back to make_access. */
641 #ifdef GETLOADAVG_PRIVILEGED
646 /* Set both the real and effective UID and GID to the user's.
647 They cannot be changed back to make's. */
649 #ifndef HAVE_SETREUID
650 if (setuid (user_uid
) < 0)
651 pfatal_with_name ("child_access: setuid");
653 if (setreuid (user_uid
, user_uid
) < 0)
654 pfatal_with_name ("child_access: setreuid");
657 #ifndef HAVE_SETREGID
658 if (setgid (user_gid
) < 0)
659 pfatal_with_name ("child_access: setgid");
661 if (setregid (user_gid
, user_gid
) < 0)
662 pfatal_with_name ("child_access: setregid");
665 log_access ("Child");
667 #endif /* GETLOADAVG_PRIVILEGED */
670 #ifdef NEED_GET_PATH_MAX
674 static unsigned int value
;
678 long int x
= pathconf ("/", _PC_PATH_MAX
);