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, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
24 /* Variadic functions. We go through contortions to allow proper function
25 prototypes for both ANSI and pre-ANSI C compilers, and also for those
26 which support stdarg.h vs. varargs.h, and finally those which have
27 vfprintf(), etc. and those who have _doprnt... or nothing.
29 This fancy stuff all came from GNU fileutils, except for the VA_PRINTF and
30 VA_END macros used here since we have multiple print functions. */
32 #if HAVE_VPRINTF || HAVE_DOPRNT
33 # define HAVE_STDVARARGS 1
36 # define VA_START(args, lastarg) va_start(args, lastarg)
39 # define VA_START(args, lastarg) va_start(args)
42 # define VA_PRINTF(fp, lastarg, args) vfprintf((fp), (lastarg), (args))
44 # define VA_PRINTF(fp, lastarg, args) _doprnt((lastarg), (args), (fp))
46 # define VA_END(args) va_end(args)
48 /* # undef HAVE_STDVARARGS */
49 # define va_alist a1, a2, a3, a4, a5, a6, a7, a8
50 # define va_dcl char *a1, *a2, *a3, *a4, *a5, *a6, *a7, *a8;
51 # define VA_START(args, lastarg)
52 # define VA_PRINTF(fp, lastarg, args) fprintf((fp), (lastarg), va_alist)
57 /* Compare strings *S1 and *S2.
58 Return negative if the first is less, positive if it is greater,
59 zero if they are equal. */
62 alpha_compare (v1
, v2
)
65 const char *s1
= *((char **)v1
);
66 const char *s2
= *((char **)v2
);
70 return strcmp (s1
, s2
);
73 /* Discard each backslash-newline combination from LINE.
74 Backslash-backslash-newline combinations become backslash-newlines.
75 This is done by copying the text at LINE into itself. */
78 collapse_continuations (line
)
81 register char *in
, *out
, *p
;
82 register int backslash
;
83 register unsigned int bs_write
;
85 in
= strchr (line
, '\n');
90 while (out
> line
&& out
[-1] == '\\')
95 /* BS_WRITE gets the number of quoted backslashes at
96 the end just before IN, and BACKSLASH gets nonzero
97 if the next character is quoted. */
100 for (p
= in
- 1; p
>= line
&& *p
== '\\'; --p
)
104 backslash
= !backslash
;
106 /* It should be impossible to go back this far without exiting,
107 but if we do, we can't get the right answer. */
112 /* Output the appropriate number of backslashes. */
113 while (bs_write
-- > 0)
116 /* Skip the newline. */
119 /* If the newline is quoted, discard following whitespace
120 and any preceding whitespace; leave just one space. */
123 in
= next_token (in
);
124 while (out
> line
&& isblank ((unsigned char)out
[-1]))
129 /* If the newline isn't quoted, put it in the output. */
132 /* Now copy the following line to the output.
133 Stop when we find backslashes followed by a newline. */
156 /* Remove comments from LINE.
157 This is done by copying the text at LINE onto itself. */
160 remove_comments (line
)
165 comment
= find_char_unquote (line
, "#", 0);
168 /* Cut off the line at the #. */
172 /* Print N spaces (used in debug for target-depth). */
176 register unsigned int n
;
183 /* Return a newly-allocated string whose contents
184 concatenate those of s1, s2, s3. */
188 register char *s1
, *s2
, *s3
;
190 register unsigned int len1
, len2
, len3
;
191 register char *result
;
193 len1
= *s1
!= '\0' ? strlen (s1
) : 0;
194 len2
= *s2
!= '\0' ? strlen (s2
) : 0;
195 len3
= *s3
!= '\0' ? strlen (s3
) : 0;
197 result
= (char *) xmalloc (len1
+ len2
+ len3
+ 1);
200 bcopy (s1
, result
, len1
);
202 bcopy (s2
, result
+ len1
, len2
);
204 bcopy (s3
, result
+ len1
+ len2
, len3
);
205 *(result
+ len1
+ len2
+ len3
) = '\0';
210 /* Print a message on stdout. */
213 #if __STDC__ && HAVE_STDVARARGS
214 message (int prefix
, const char *fmt
, ...)
216 message (prefix
, fmt
, va_alist
)
226 log_working_directory (1);
233 printf ("%s: ", program
);
235 printf ("%s[%u]: ", program
, makelevel
);
237 VA_START (args
, fmt
);
238 VA_PRINTF (stdout
, fmt
, args
);
246 /* Print an error message. */
249 #if __STDC__ && HAVE_STDVARARGS
250 error (const struct floc
*flocp
, const char *fmt
, ...)
252 error (flocp
, fmt
, va_alist
)
253 const struct floc
*flocp
;
262 log_working_directory (1);
264 if (flocp
&& flocp
->filenm
)
265 fprintf (stderr
, "%s:%lu: ", flocp
->filenm
, flocp
->lineno
);
266 else if (makelevel
== 0)
267 fprintf (stderr
, "%s: ", program
);
269 fprintf (stderr
, "%s[%u]: ", program
, makelevel
);
272 VA_PRINTF (stderr
, fmt
, args
);
279 /* Print an error message and exit. */
282 #if __STDC__ && HAVE_STDVARARGS
283 fatal (const struct floc
*flocp
, const char *fmt
, ...)
285 fatal (flocp
, fmt
, va_alist
)
286 const struct floc
*flocp
;
295 log_working_directory (1);
297 if (flocp
&& flocp
->filenm
)
298 fprintf (stderr
, "%s:%lu: *** ", flocp
->filenm
, flocp
->lineno
);
299 else if (makelevel
== 0)
300 fprintf (stderr
, "%s: *** ", program
);
302 fprintf (stderr
, "%s[%u]: *** ", program
, makelevel
);
305 VA_PRINTF (stderr
, fmt
, args
);
308 fputs (_(". Stop.\n"), stderr
);
313 #ifndef HAVE_STRERROR
321 extern int errno
, sys_nerr
;
323 extern char *sys_errlist
[];
325 static char buf
[] = "Unknown error 12345678901234567890";
327 if (errno
< sys_nerr
)
328 return sys_errlist
[errnum
];
330 sprintf (buf
, _("Unknown error %d"), errnum
);
335 /* Print an error message from errno. */
338 perror_with_name (str
, name
)
341 error (NILF
, "%s%s: %s", str
, name
, strerror (errno
));
344 /* Print an error message from errno and exit. */
347 pfatal_with_name (name
)
350 fatal (NILF
, "%s: %s", name
, strerror (errno
));
355 /* Like malloc but get fatal error if memory is exhausted. */
356 /* Don't bother if we're using dmalloc; it provides these for us. */
358 #ifndef HAVE_DMALLOC_H
368 char *result
= (char *) malloc (size
);
370 fatal (NILF
, _("virtual memory exhausted"));
382 /* Some older implementations of realloc() don't conform to ANSI. */
383 result
= ptr
? realloc (ptr
, size
) : malloc (size
);
385 fatal (NILF
, _("virtual memory exhausted"));
397 result
= strdup (ptr
);
399 result
= (char *) malloc (strlen (ptr
) + 1);
403 fatal (NILF
, _("virtual memory exhausted"));
408 return strcpy(result
, ptr
);
412 #endif /* HAVE_DMALLOC_H */
415 savestring (str
, length
)
419 register char *out
= (char *) xmalloc (length
+ 1);
421 bcopy (str
, out
, length
);
426 /* Search string BIG (length BLEN) for an occurrence of
427 string SMALL (length SLEN). Return a pointer to the
428 beginning of the first occurrence, or return nil if none found. */
431 sindex (big
, blen
, small
, slen
)
440 slen
= strlen (small
);
442 if (slen
&& blen
>= slen
)
444 register unsigned int b
;
446 /* Quit when there's not enough room left for the small string. */
450 for (b
= 0; b
< blen
; ++b
, ++big
)
451 if (*big
== *small
&& strneq (big
+ 1, small
+ 1, slen
))
459 Search through the string STRING, which ends at LIMIT, for the character C.
460 Returns a pointer to the first occurrence, or nil if none is found.
461 Like INDEX except that the string searched ends where specified
462 instead of at the first null. */
466 register const char *s
, *limit
;
471 return (char *)(s
- 1);
476 /* Return the address of the first whitespace or null in the string S. */
482 while (*s
!= '\0' && !isblank ((unsigned char)*s
))
489 * Same as end_of_token, but take into account a stop character
492 end_of_token_w32 (s
, stopchar
)
496 register char *p
= s
;
497 register int backslash
= 0;
499 while (*p
!= '\0' && *p
!= stopchar
500 && (backslash
|| !isblank ((unsigned char)*p
)))
504 backslash
= !backslash
;
507 backslash
= !backslash
;
519 /* Return the address of the first nonwhitespace or null in the string S. */
525 register char *p
= s
;
527 while (isblank ((unsigned char)*p
))
532 /* Find the next token in PTR; return the address of it, and store the
533 length of the token into *LENGTHPTR if LENGTHPTR is not nil. */
536 find_next_token (ptr
, lengthptr
)
538 unsigned int *lengthptr
;
540 char *p
= next_token (*ptr
);
546 *ptr
= end
= end_of_token (p
);
548 *lengthptr
= end
- p
;
552 /* Copy a chain of `struct dep', making a new chain
553 with the same contents as the old one. */
557 register struct dep
*d
;
559 register struct dep
*c
;
560 struct dep
*firstnew
= 0;
561 struct dep
*lastnew
= 0;
565 c
= (struct dep
*) xmalloc (sizeof (struct dep
));
566 bcopy ((char *) d
, (char *) c
, sizeof (struct dep
));
568 c
->name
= xstrdup (c
->name
);
571 firstnew
= lastnew
= c
;
573 lastnew
= lastnew
->next
= c
;
582 /* The losing compiler on this machine can't handle this macro. */
588 return dep
->name
== 0 ? dep
->file
->name
: dep
->name
;
592 #ifdef GETLOADAVG_PRIVILEGED
596 /* Hopefully if a system says it's POSIX.1 and has the setuid and setgid
597 functions, they work as POSIX.1 says. Some systems (Alpha OSF/1 1.2,
598 for example) which claim to be POSIX.1 also have the BSD setreuid and
599 setregid functions, but they don't work as in BSD and only the POSIX.1
605 #else /* Not POSIX. */
607 /* Some POSIX.1 systems have the seteuid and setegid functions. In a
608 POSIX-like system, they are the best thing to use. However, some
609 non-POSIX systems have them too but they do not work in the POSIX style
610 and we must use setreuid and setregid instead. */
617 #ifndef HAVE_UNISTD_H
618 extern int getuid (), getgid (), geteuid (), getegid ();
619 extern int setuid (), setgid ();
621 extern int seteuid ();
624 extern int setreuid ();
625 #endif /* Have setreuid. */
626 #endif /* Have seteuid. */
628 extern int setegid ();
631 extern int setregid ();
632 #endif /* Have setregid. */
633 #endif /* Have setegid. */
634 #endif /* No <unistd.h>. */
636 /* Keep track of the user and group IDs for user- and make- access. */
637 static int user_uid
= -1, user_gid
= -1, make_uid
= -1, make_gid
= -1;
638 #define access_inited (user_uid != -1)
639 static enum { make
, user
} current_access
;
642 /* Under -d, write a message describing the current IDs. */
648 if (! ISDB (DB_JOBS
))
651 /* All the other debugging messages go to stdout,
652 but we write this one to stderr because it might be
653 run in a child fork whose stdout is piped. */
655 fprintf (stderr
, _("%s: user %lu (real %lu), group %lu (real %lu)\n"),
656 flavor
, (unsigned long) geteuid (), (unsigned long) getuid (),
657 (unsigned long) getegid (), (unsigned long) getgid ());
666 user_uid
= getuid ();
667 user_gid
= getgid ();
669 make_uid
= geteuid ();
670 make_gid
= getegid ();
672 /* Do these ever fail? */
673 if (user_uid
== -1 || user_gid
== -1 || make_uid
== -1 || make_gid
== -1)
674 pfatal_with_name ("get{e}[gu]id");
676 log_access (_("Initialized access"));
678 current_access
= make
;
682 #endif /* GETLOADAVG_PRIVILEGED */
684 /* Give the process appropriate permissions for access to
685 user data (i.e., to stat files, or to spawn a child process). */
689 #ifdef GETLOADAVG_PRIVILEGED
694 if (current_access
== user
)
697 /* We are in "make access" mode. This means that the effective user and
698 group IDs are those of make (if it was installed setuid or setgid).
699 We now want to set the effective user and group IDs to the real IDs,
700 which are the IDs of the process that exec'd make. */
704 /* Modern systems have the seteuid/setegid calls which set only the
705 effective IDs, which is ideal. */
707 if (seteuid (user_uid
) < 0)
708 pfatal_with_name ("user_access: seteuid");
710 #else /* Not HAVE_SETEUID. */
712 #ifndef HAVE_SETREUID
714 /* System V has only the setuid/setgid calls to set user/group IDs.
715 There is an effective ID, which can be set by setuid/setgid.
716 It can be set (unless you are root) only to either what it already is
717 (returned by geteuid/getegid, now in make_uid/make_gid),
718 the real ID (return by getuid/getgid, now in user_uid/user_gid),
719 or the saved set ID (what the effective ID was before this set-ID
720 executable (make) was exec'd). */
722 if (setuid (user_uid
) < 0)
723 pfatal_with_name ("user_access: setuid");
725 #else /* HAVE_SETREUID. */
727 /* In 4BSD, the setreuid/setregid calls set both the real and effective IDs.
728 They may be set to themselves or each other. So you have two alternatives
729 at any one time. If you use setuid/setgid, the effective will be set to
730 the real, leaving only one alternative. Using setreuid/setregid, however,
731 you can toggle between your two alternatives by swapping the values in a
732 single setreuid or setregid call. */
734 if (setreuid (make_uid
, user_uid
) < 0)
735 pfatal_with_name ("user_access: setreuid");
737 #endif /* Not HAVE_SETREUID. */
738 #endif /* HAVE_SETEUID. */
741 if (setegid (user_gid
) < 0)
742 pfatal_with_name ("user_access: setegid");
744 #ifndef HAVE_SETREGID
745 if (setgid (user_gid
) < 0)
746 pfatal_with_name ("user_access: setgid");
748 if (setregid (make_gid
, user_gid
) < 0)
749 pfatal_with_name ("user_access: setregid");
753 current_access
= user
;
755 log_access (_("User access"));
757 #endif /* GETLOADAVG_PRIVILEGED */
760 /* Give the process appropriate permissions for access to
761 make data (i.e., the load average). */
765 #ifdef GETLOADAVG_PRIVILEGED
770 if (current_access
== make
)
773 /* See comments in user_access, above. */
776 if (seteuid (make_uid
) < 0)
777 pfatal_with_name ("make_access: seteuid");
779 #ifndef HAVE_SETREUID
780 if (setuid (make_uid
) < 0)
781 pfatal_with_name ("make_access: setuid");
783 if (setreuid (user_uid
, make_uid
) < 0)
784 pfatal_with_name ("make_access: setreuid");
789 if (setegid (make_gid
) < 0)
790 pfatal_with_name ("make_access: setegid");
792 #ifndef HAVE_SETREGID
793 if (setgid (make_gid
) < 0)
794 pfatal_with_name ("make_access: setgid");
796 if (setregid (user_gid
, make_gid
) < 0)
797 pfatal_with_name ("make_access: setregid");
801 current_access
= make
;
803 log_access (_("Make access"));
805 #endif /* GETLOADAVG_PRIVILEGED */
808 /* Give the process appropriate permissions for a child process.
809 This is like user_access, but you can't get back to make_access. */
813 #ifdef GETLOADAVG_PRIVILEGED
818 /* Set both the real and effective UID and GID to the user's.
819 They cannot be changed back to make's. */
821 #ifndef HAVE_SETREUID
822 if (setuid (user_uid
) < 0)
823 pfatal_with_name ("child_access: setuid");
825 if (setreuid (user_uid
, user_uid
) < 0)
826 pfatal_with_name ("child_access: setreuid");
829 #ifndef HAVE_SETREGID
830 if (setgid (user_gid
) < 0)
831 pfatal_with_name ("child_access: setgid");
833 if (setregid (user_gid
, user_gid
) < 0)
834 pfatal_with_name ("child_access: setregid");
837 log_access (_("Child access"));
839 #endif /* GETLOADAVG_PRIVILEGED */
842 #ifdef NEED_GET_PATH_MAX
846 static unsigned int value
;
850 long int x
= pathconf ("/", _PC_PATH_MAX
);