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)
56 /* Compare strings *S1 and *S2.
57 Return negative if the first is less, positive if it is greater,
58 zero if they are equal. */
61 alpha_compare (v1
, v2
)
64 const char *s1
= *((char **)v1
);
65 const char *s2
= *((char **)v2
);
69 return strcmp (s1
, s2
);
72 /* Discard each backslash-newline combination from LINE.
73 Backslash-backslash-newline combinations become backslash-newlines.
74 This is done by copying the text at LINE into itself. */
77 collapse_continuations (line
)
80 register char *in
, *out
, *p
;
81 register int backslash
;
82 register unsigned int bs_write
;
84 in
= strchr (line
, '\n');
89 while (out
> line
&& out
[-1] == '\\')
94 /* BS_WRITE gets the number of quoted backslashes at
95 the end just before IN, and BACKSLASH gets nonzero
96 if the next character is quoted. */
99 for (p
= in
- 1; p
>= line
&& *p
== '\\'; --p
)
103 backslash
= !backslash
;
105 /* It should be impossible to go back this far without exiting,
106 but if we do, we can't get the right answer. */
111 /* Output the appropriate number of backslashes. */
112 while (bs_write
-- > 0)
115 /* Skip the newline. */
118 /* If the newline is quoted, discard following whitespace
119 and any preceding whitespace; leave just one space. */
122 in
= next_token (in
);
123 while (out
> line
&& isblank (out
[-1]))
128 /* If the newline isn't quoted, put it in the output. */
131 /* Now copy the following line to the output.
132 Stop when we find backslashes followed by a newline. */
155 /* Remove comments from LINE.
156 This is done by copying the text at LINE onto itself. */
159 remove_comments (line
)
164 comment
= find_char_unquote (line
, "#", 0);
167 /* Cut off the line at the #. */
171 /* Print N spaces (used in debug for target-depth). */
175 register unsigned int n
;
182 /* Return a newly-allocated string whose contents
183 concatenate those of s1, s2, s3. */
187 register char *s1
, *s2
, *s3
;
189 register unsigned int len1
, len2
, len3
;
190 register char *result
;
192 len1
= *s1
!= '\0' ? strlen (s1
) : 0;
193 len2
= *s2
!= '\0' ? strlen (s2
) : 0;
194 len3
= *s3
!= '\0' ? strlen (s3
) : 0;
196 result
= (char *) xmalloc (len1
+ len2
+ len3
+ 1);
199 bcopy (s1
, result
, len1
);
201 bcopy (s2
, result
+ len1
, len2
);
203 bcopy (s3
, result
+ len1
+ len2
, len3
);
204 *(result
+ len1
+ len2
+ len3
) = '\0';
209 /* Print a message on stdout. */
212 #if __STDC__ && HAVE_STDVARARGS
213 message (int prefix
, const char *fmt
, ...)
215 message (prefix
, fmt
, va_alist
)
225 log_working_directory (1);
232 printf ("%s: ", program
);
234 printf ("%s[%u]: ", program
, makelevel
);
236 VA_START (args
, fmt
);
237 VA_PRINTF (stdout
, fmt
, args
);
245 /* Print an error message. */
248 #if __STDC__ && HAVE_STDVARARGS
249 error (const struct floc
*flocp
, const char *fmt
, ...)
251 error (flocp
, fmt
, va_alist
)
252 const struct floc
*flocp
;
261 log_working_directory (1);
263 if (flocp
&& flocp
->filenm
)
264 fprintf (stderr
, "%s:%lu: ", flocp
->filenm
, flocp
->lineno
);
265 else if (makelevel
== 0)
266 fprintf (stderr
, "%s: ", program
);
268 fprintf (stderr
, "%s[%u]: ", program
, makelevel
);
271 VA_PRINTF (stderr
, fmt
, args
);
278 /* Print an error message and exit. */
281 #if __STDC__ && HAVE_STDVARARGS
282 fatal (const struct floc
*flocp
, const char *fmt
, ...)
284 fatal (flocp
, fmt
, va_alist
)
285 const struct floc
*flocp
;
294 log_working_directory (1);
296 if (flocp
&& flocp
->filenm
)
297 fprintf (stderr
, "%s:%lu: *** ", flocp
->filenm
, flocp
->lineno
);
298 else if (makelevel
== 0)
299 fprintf (stderr
, "%s: *** ", program
);
301 fprintf (stderr
, "%s[%u]: *** ", program
, makelevel
);
304 VA_PRINTF (stderr
, fmt
, args
);
307 fputs (_(". Stop.\n"), stderr
);
312 #ifndef HAVE_STRERROR
320 extern int errno
, sys_nerr
;
322 extern char *sys_errlist
[];
324 static char buf
[] = "Unknown error 12345678901234567890";
326 if (errno
< sys_nerr
)
327 return sys_errlist
[errnum
];
329 sprintf (buf
, _("Unknown error %d"), errnum
);
334 /* Print an error message from errno. */
337 perror_with_name (str
, name
)
340 error (NILF
, "%s%s: %s", str
, name
, strerror (errno
));
343 /* Print an error message from errno and exit. */
346 pfatal_with_name (name
)
349 fatal (NILF
, "%s: %s", name
, strerror (errno
));
354 /* Like malloc but get fatal error if memory is exhausted. */
355 /* Don't bother if we're using dmalloc; it provides these for us. */
357 #ifndef HAVE_DMALLOC_H
367 char *result
= (char *) malloc (size
);
369 fatal (NILF
, _("virtual memory exhausted"));
381 /* Some older implementations of realloc() don't conform to ANSI. */
382 result
= ptr
? realloc (ptr
, size
) : malloc (size
);
384 fatal (NILF
, _("virtual memory exhausted"));
396 result
= strdup (ptr
);
398 result
= (char *) malloc (strlen (ptr
) + 1);
402 fatal (NILF
, _("virtual memory exhausted"));
407 return strcpy(result
, ptr
);
411 #endif /* HAVE_DMALLOC_H */
414 savestring (str
, length
)
418 register char *out
= (char *) xmalloc (length
+ 1);
420 bcopy (str
, out
, length
);
425 /* Search string BIG (length BLEN) for an occurrence of
426 string SMALL (length SLEN). Return a pointer to the
427 beginning of the first occurrence, or return nil if none found. */
430 sindex (big
, blen
, small
, slen
)
439 slen
= strlen (small
);
441 if (slen
&& blen
>= slen
)
443 register unsigned int b
;
445 /* Quit when there's not enough room left for the small string. */
449 for (b
= 0; b
< blen
; ++b
, ++big
)
450 if (*big
== *small
&& strneq (big
+ 1, small
+ 1, slen
))
458 Search through the string STRING, which ends at LIMIT, for the character C.
459 Returns a pointer to the first occurrence, or nil if none is found.
460 Like INDEX except that the string searched ends where specified
461 instead of at the first null. */
465 register const char *s
, *limit
;
470 return (char *)(s
- 1);
475 /* Return the address of the first whitespace or null in the string S. */
481 while (*s
!= '\0' && !isblank (*s
))
488 * Same as end_of_token, but take into account a stop character
491 end_of_token_w32 (s
, stopchar
)
495 register char *p
= s
;
496 register int backslash
= 0;
498 while (*p
!= '\0' && *p
!= stopchar
&& (backslash
|| !isblank (*p
)))
502 backslash
= !backslash
;
505 backslash
= !backslash
;
517 /* Return the address of the first nonwhitespace or null in the string S. */
523 register char *p
= s
;
530 /* Find the next token in PTR; return the address of it, and store the
531 length of the token into *LENGTHPTR if LENGTHPTR is not nil. */
534 find_next_token (ptr
, lengthptr
)
536 unsigned int *lengthptr
;
538 char *p
= next_token (*ptr
);
544 *ptr
= end
= end_of_token (p
);
546 *lengthptr
= end
- p
;
550 /* Copy a chain of `struct dep', making a new chain
551 with the same contents as the old one. */
555 register struct dep
*d
;
557 register struct dep
*c
;
558 struct dep
*firstnew
= 0;
559 struct dep
*lastnew
= 0;
563 c
= (struct dep
*) xmalloc (sizeof (struct dep
));
564 bcopy ((char *) d
, (char *) c
, sizeof (struct dep
));
566 c
->name
= xstrdup (c
->name
);
569 firstnew
= lastnew
= c
;
571 lastnew
= lastnew
->next
= c
;
580 /* The losing compiler on this machine can't handle this macro. */
586 return dep
->name
== 0 ? dep
->file
->name
: dep
->name
;
590 #ifdef GETLOADAVG_PRIVILEGED
594 /* Hopefully if a system says it's POSIX.1 and has the setuid and setgid
595 functions, they work as POSIX.1 says. Some systems (Alpha OSF/1 1.2,
596 for example) which claim to be POSIX.1 also have the BSD setreuid and
597 setregid functions, but they don't work as in BSD and only the POSIX.1
603 #else /* Not POSIX. */
605 /* Some POSIX.1 systems have the seteuid and setegid functions. In a
606 POSIX-like system, they are the best thing to use. However, some
607 non-POSIX systems have them too but they do not work in the POSIX style
608 and we must use setreuid and setregid instead. */
615 #ifndef HAVE_UNISTD_H
616 extern int getuid (), getgid (), geteuid (), getegid ();
617 extern int setuid (), setgid ();
619 extern int seteuid ();
622 extern int setreuid ();
623 #endif /* Have setreuid. */
624 #endif /* Have seteuid. */
626 extern int setegid ();
629 extern int setregid ();
630 #endif /* Have setregid. */
631 #endif /* Have setegid. */
632 #endif /* No <unistd.h>. */
634 /* Keep track of the user and group IDs for user- and make- access. */
635 static int user_uid
= -1, user_gid
= -1, make_uid
= -1, make_gid
= -1;
636 #define access_inited (user_uid != -1)
637 static enum { make
, user
} current_access
;
640 /* Under -d, write a message describing the current IDs. */
646 if (! ISDB (DB_JOBS
))
649 /* All the other debugging messages go to stdout,
650 but we write this one to stderr because it might be
651 run in a child fork whose stdout is piped. */
653 fprintf (stderr
, _("%s access: user %lu (real %lu), group %lu (real %lu)\n"),
654 flavor
, (unsigned long) geteuid (), (unsigned long) getuid (),
655 (unsigned long) getegid (), (unsigned long) getgid ());
664 user_uid
= getuid ();
665 user_gid
= getgid ();
667 make_uid
= geteuid ();
668 make_gid
= getegid ();
670 /* Do these ever fail? */
671 if (user_uid
== -1 || user_gid
== -1 || make_uid
== -1 || make_gid
== -1)
672 pfatal_with_name ("get{e}[gu]id");
674 log_access (_("Initialized"));
676 current_access
= make
;
680 #endif /* GETLOADAVG_PRIVILEGED */
682 /* Give the process appropriate permissions for access to
683 user data (i.e., to stat files, or to spawn a child process). */
687 #ifdef GETLOADAVG_PRIVILEGED
692 if (current_access
== user
)
695 /* We are in "make access" mode. This means that the effective user and
696 group IDs are those of make (if it was installed setuid or setgid).
697 We now want to set the effective user and group IDs to the real IDs,
698 which are the IDs of the process that exec'd make. */
702 /* Modern systems have the seteuid/setegid calls which set only the
703 effective IDs, which is ideal. */
705 if (seteuid (user_uid
) < 0)
706 pfatal_with_name ("user_access: seteuid");
708 #else /* Not HAVE_SETEUID. */
710 #ifndef HAVE_SETREUID
712 /* System V has only the setuid/setgid calls to set user/group IDs.
713 There is an effective ID, which can be set by setuid/setgid.
714 It can be set (unless you are root) only to either what it already is
715 (returned by geteuid/getegid, now in make_uid/make_gid),
716 the real ID (return by getuid/getgid, now in user_uid/user_gid),
717 or the saved set ID (what the effective ID was before this set-ID
718 executable (make) was exec'd). */
720 if (setuid (user_uid
) < 0)
721 pfatal_with_name ("user_access: setuid");
723 #else /* HAVE_SETREUID. */
725 /* In 4BSD, the setreuid/setregid calls set both the real and effective IDs.
726 They may be set to themselves or each other. So you have two alternatives
727 at any one time. If you use setuid/setgid, the effective will be set to
728 the real, leaving only one alternative. Using setreuid/setregid, however,
729 you can toggle between your two alternatives by swapping the values in a
730 single setreuid or setregid call. */
732 if (setreuid (make_uid
, user_uid
) < 0)
733 pfatal_with_name ("user_access: setreuid");
735 #endif /* Not HAVE_SETREUID. */
736 #endif /* HAVE_SETEUID. */
739 if (setegid (user_gid
) < 0)
740 pfatal_with_name ("user_access: setegid");
742 #ifndef HAVE_SETREGID
743 if (setgid (user_gid
) < 0)
744 pfatal_with_name ("user_access: setgid");
746 if (setregid (make_gid
, user_gid
) < 0)
747 pfatal_with_name ("user_access: setregid");
751 current_access
= user
;
755 #endif /* GETLOADAVG_PRIVILEGED */
758 /* Give the process appropriate permissions for access to
759 make data (i.e., the load average). */
763 #ifdef GETLOADAVG_PRIVILEGED
768 if (current_access
== make
)
771 /* See comments in user_access, above. */
774 if (seteuid (make_uid
) < 0)
775 pfatal_with_name ("make_access: seteuid");
777 #ifndef HAVE_SETREUID
778 if (setuid (make_uid
) < 0)
779 pfatal_with_name ("make_access: setuid");
781 if (setreuid (user_uid
, make_uid
) < 0)
782 pfatal_with_name ("make_access: setreuid");
787 if (setegid (make_gid
) < 0)
788 pfatal_with_name ("make_access: setegid");
790 #ifndef HAVE_SETREGID
791 if (setgid (make_gid
) < 0)
792 pfatal_with_name ("make_access: setgid");
794 if (setregid (user_gid
, make_gid
) < 0)
795 pfatal_with_name ("make_access: setregid");
799 current_access
= make
;
803 #endif /* GETLOADAVG_PRIVILEGED */
806 /* Give the process appropriate permissions for a child process.
807 This is like user_access, but you can't get back to make_access. */
811 #ifdef GETLOADAVG_PRIVILEGED
816 /* Set both the real and effective UID and GID to the user's.
817 They cannot be changed back to make's. */
819 #ifndef HAVE_SETREUID
820 if (setuid (user_uid
) < 0)
821 pfatal_with_name ("child_access: setuid");
823 if (setreuid (user_uid
, user_uid
) < 0)
824 pfatal_with_name ("child_access: setreuid");
827 #ifndef HAVE_SETREGID
828 if (setgid (user_gid
) < 0)
829 pfatal_with_name ("child_access: setgid");
831 if (setregid (user_gid
, user_gid
) < 0)
832 pfatal_with_name ("child_access: setregid");
835 log_access ("Child");
837 #endif /* GETLOADAVG_PRIVILEGED */
840 #ifdef NEED_GET_PATH_MAX
844 static unsigned int value
;
848 long int x
= pathconf ("/", _PC_PATH_MAX
);