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 /* Variadic functions. We go through contortions to allow proper function
24 prototypes for both ANSI and pre-ANSI C compilers, and also for those
25 which support stdarg.h vs. varargs.h, and finally those which have
26 vfprintf(), etc. and those who have _doprnt... or nothing.
28 This fancy stuff all came from GNU fileutils, except for the VA_PRINTF and
29 VA_END macros used here since we have multiple print functions. */
31 #if HAVE_VPRINTF || HAVE_DOPRNT
32 # define HAVE_STDVARARGS 1
35 # define VA_START(args, lastarg) va_start(args, lastarg)
38 # define VA_START(args, lastarg) va_start(args)
41 # define VA_PRINTF(fp, lastarg, args) vfprintf((fp), (lastarg), (args))
43 # define VA_PRINTF(fp, lastarg, args) _doprnt((lastarg), (args), (fp))
45 # define VA_END(args) va_end(args)
47 /* # undef HAVE_STDVARARGS */
48 # define va_alist a1, a2, a3, a4, a5, a6, a7, a8
49 # define va_dcl char *a1, *a2, *a3, *a4, *a5, *a6, *a7, *a8;
50 # define VA_START(args, lastarg)
55 /* Compare strings *S1 and *S2.
56 Return negative if the first is less, positive if it is greater,
57 zero if they are equal. */
60 alpha_compare (v1
, v2
)
63 const char *s1
= *((char **)v1
);
64 const char *s2
= *((char **)v2
);
68 return strcmp (s1
, s2
);
71 /* Discard each backslash-newline combination from LINE.
72 Backslash-backslash-newline combinations become backslash-newlines.
73 This is done by copying the text at LINE into itself. */
76 collapse_continuations (line
)
79 register char *in
, *out
, *p
;
80 register int backslash
;
81 register unsigned int bs_write
;
83 in
= index (line
, '\n');
88 while (out
> line
&& out
[-1] == '\\')
93 /* BS_WRITE gets the number of quoted backslashes at
94 the end just before IN, and BACKSLASH gets nonzero
95 if the next character is quoted. */
98 for (p
= in
- 1; p
>= line
&& *p
== '\\'; --p
)
102 backslash
= !backslash
;
104 /* It should be impossible to go back this far without exiting,
105 but if we do, we can't get the right answer. */
110 /* Output the appropriate number of backslashes. */
111 while (bs_write
-- > 0)
114 /* Skip the newline. */
117 /* If the newline is quoted, discard following whitespace
118 and any preceding whitespace; leave just one space. */
121 in
= next_token (in
);
122 while (out
> line
&& isblank (out
[-1]))
127 /* If the newline isn't quoted, put it in the output. */
130 /* Now copy the following line to the output.
131 Stop when we find backslashes followed by a newline. */
154 /* Remove comments from LINE.
155 This is done by copying the text at LINE onto itself. */
158 remove_comments (line
)
163 comment
= find_char_unquote (line
, "#", 0);
166 /* Cut off the line at the #. */
170 /* Print N spaces (used by DEBUGPR for target-depth). */
174 register unsigned int n
;
181 /* Return a newly-allocated string whose contents
182 concatenate those of s1, s2, s3. */
186 register char *s1
, *s2
, *s3
;
188 register unsigned int len1
, len2
, len3
;
189 register char *result
;
191 len1
= *s1
!= '\0' ? strlen (s1
) : 0;
192 len2
= *s2
!= '\0' ? strlen (s2
) : 0;
193 len3
= *s3
!= '\0' ? strlen (s3
) : 0;
195 result
= (char *) xmalloc (len1
+ len2
+ len3
+ 1);
198 bcopy (s1
, result
, len1
);
200 bcopy (s2
, result
+ len1
, len2
);
202 bcopy (s3
, result
+ len1
+ len2
, len3
);
203 *(result
+ len1
+ len2
+ len3
) = '\0';
208 /* Print a message on stdout. */
211 #if __STDC__ && HAVE_STDVARARGS
212 message (int prefix
, const char *fmt
, ...)
214 message (prefix
, fmt
, va_alist
)
224 log_working_directory (1);
231 printf ("%s: ", program
);
233 printf ("%s[%u]: ", program
, makelevel
);
235 VA_START (args
, fmt
);
236 VA_PRINTF (stdout
, fmt
, args
);
244 /* Print an error message. */
247 #if __STDC__ && HAVE_STDVARARGS
248 error (const struct floc
*flocp
, const char *fmt
, ...)
250 error (flocp
, fmt
, va_alist
)
251 const struct floc
*flocp
;
260 log_working_directory (1);
262 if (flocp
&& flocp
->filenm
)
263 fprintf (stderr
, "%s:%lu: ", flocp
->filenm
, flocp
->lineno
);
264 else if (makelevel
== 0)
265 fprintf (stderr
, "%s: ", program
);
267 fprintf (stderr
, "%s[%u]: ", program
, makelevel
);
270 VA_PRINTF (stderr
, fmt
, args
);
277 /* Print an error message and exit. */
280 #if __STDC__ && HAVE_STDVARARGS
281 fatal (const struct floc
*flocp
, const char *fmt
, ...)
283 fatal (flocp
, fmt
, va_alist
)
284 const struct floc
*flocp
;
293 log_working_directory (1);
295 if (flocp
&& flocp
->filenm
)
296 fprintf (stderr
, "%s:%lu: *** ", flocp
->filenm
, flocp
->lineno
);
297 else if (makelevel
== 0)
298 fprintf (stderr
, "%s: *** ", program
);
300 fprintf (stderr
, "%s[%u]: *** ", program
, makelevel
);
303 VA_PRINTF (stderr
, fmt
, args
);
306 fputs (". Stop.\n", stderr
);
311 #ifndef HAVE_STRERROR
319 extern int errno
, sys_nerr
;
321 extern char *sys_errlist
[];
323 static char buf
[] = "Unknown error 12345678901234567890";
325 if (errno
< sys_nerr
)
326 return sys_errlist
[errnum
];
328 sprintf (buf
, "Unknown error %d", errnum
);
333 /* Print an error message from errno. */
336 perror_with_name (str
, name
)
339 error (NILF
, "%s%s: %s", str
, name
, strerror (errno
));
342 /* Print an error message from errno and exit. */
345 pfatal_with_name (name
)
348 fatal (NILF
, "%s: %s", name
, strerror (errno
));
353 /* Like malloc but get fatal error if memory is exhausted. */
362 char *result
= (char *) malloc (size
);
364 fatal (NILF
, "virtual memory exhausted");
374 char *result
= (char *) realloc (ptr
, size
);
376 fatal (NILF
, "virtual memory exhausted");
381 savestring (str
, length
)
385 register char *out
= (char *) xmalloc (length
+ 1);
387 bcopy (str
, out
, length
);
392 /* Search string BIG (length BLEN) for an occurrence of
393 string SMALL (length SLEN). Return a pointer to the
394 beginning of the first occurrence, or return nil if none found. */
397 sindex (big
, blen
, small
, slen
)
403 register unsigned int b
;
408 slen
= strlen (small
);
410 for (b
= 0; b
< blen
; ++b
)
411 if (big
[b
] == *small
&& !strncmp (&big
[b
+ 1], small
+ 1, slen
- 1))
418 Search through the string STRING, which ends at LIMIT, for the character C.
419 Returns a pointer to the first occurrence, or nil if none is found.
420 Like INDEX except that the string searched ends where specified
421 instead of at the first null. */
425 register char *s
, *limit
;
435 /* Return the address of the first whitespace or null in the string S. */
441 while (*s
!= '\0' && !isblank (*s
))
448 * Same as end_of_token, but take into account a stop character
451 end_of_token_w32 (s
, stopchar
)
455 register char *p
= s
;
456 register int backslash
= 0;
458 while (*p
!= '\0' && *p
!= stopchar
&& (backslash
|| !isblank (*p
)))
462 backslash
= !backslash
;
465 backslash
= !backslash
;
477 /* Return the address of the first nonwhitespace or null in the string S. */
483 register char *p
= s
;
490 /* Find the next token in PTR; return the address of it, and store the
491 length of the token into *LENGTHPTR if LENGTHPTR is not nil. */
494 find_next_token (ptr
, lengthptr
)
496 unsigned int *lengthptr
;
498 char *p
= next_token (*ptr
);
504 *ptr
= end
= end_of_token (p
);
506 *lengthptr
= end
- p
;
510 /* Copy a chain of `struct dep', making a new chain
511 with the same contents as the old one. */
515 register struct dep
*d
;
517 register struct dep
*c
;
518 struct dep
*firstnew
= 0;
519 struct dep
*lastnew
= 0;
523 c
= (struct dep
*) xmalloc (sizeof (struct dep
));
524 bcopy ((char *) d
, (char *) c
, sizeof (struct dep
));
526 c
->name
= savestring (c
->name
, strlen (c
->name
));
529 firstnew
= lastnew
= c
;
531 lastnew
= lastnew
->next
= c
;
540 /* The losing compiler on this machine can't handle this macro. */
546 return dep
->name
== 0 ? dep
->file
->name
: dep
->name
;
550 #ifdef GETLOADAVG_PRIVILEGED
554 /* Hopefully if a system says it's POSIX.1 and has the setuid and setgid
555 functions, they work as POSIX.1 says. Some systems (Alpha OSF/1 1.2,
556 for example) which claim to be POSIX.1 also have the BSD setreuid and
557 setregid functions, but they don't work as in BSD and only the POSIX.1
563 #else /* Not POSIX. */
565 /* Some POSIX.1 systems have the seteuid and setegid functions. In a
566 POSIX-like system, they are the best thing to use. However, some
567 non-POSIX systems have them too but they do not work in the POSIX style
568 and we must use setreuid and setregid instead. */
575 #ifndef HAVE_UNISTD_H
576 extern int getuid (), getgid (), geteuid (), getegid ();
577 extern int setuid (), setgid ();
579 extern int seteuid ();
582 extern int setreuid ();
583 #endif /* Have setreuid. */
584 #endif /* Have seteuid. */
586 extern int setegid ();
589 extern int setregid ();
590 #endif /* Have setregid. */
591 #endif /* Have setegid. */
592 #endif /* No <unistd.h>. */
594 /* Keep track of the user and group IDs for user- and make- access. */
595 static int user_uid
= -1, user_gid
= -1, make_uid
= -1, make_gid
= -1;
596 #define access_inited (user_uid != -1)
597 static enum { make
, user
} current_access
;
600 /* Under -d, write a message describing the current IDs. */
609 /* All the other debugging messages go to stdout,
610 but we write this one to stderr because it might be
611 run in a child fork whose stdout is piped. */
613 fprintf (stderr
, "%s access: user %lu (real %lu), group %lu (real %lu)\n",
614 flavor
, (unsigned long) geteuid (), (unsigned long) getuid (),
615 (unsigned long) getegid (), (unsigned long) getgid ());
624 user_uid
= getuid ();
625 user_gid
= getgid ();
627 make_uid
= geteuid ();
628 make_gid
= getegid ();
630 /* Do these ever fail? */
631 if (user_uid
== -1 || user_gid
== -1 || make_uid
== -1 || make_gid
== -1)
632 pfatal_with_name ("get{e}[gu]id");
634 log_access ("Initialized");
636 current_access
= make
;
640 #endif /* GETLOADAVG_PRIVILEGED */
642 /* Give the process appropriate permissions for access to
643 user data (i.e., to stat files, or to spawn a child process). */
647 #ifdef GETLOADAVG_PRIVILEGED
652 if (current_access
== user
)
655 /* We are in "make access" mode. This means that the effective user and
656 group IDs are those of make (if it was installed setuid or setgid).
657 We now want to set the effective user and group IDs to the real IDs,
658 which are the IDs of the process that exec'd make. */
662 /* Modern systems have the seteuid/setegid calls which set only the
663 effective IDs, which is ideal. */
665 if (seteuid (user_uid
) < 0)
666 pfatal_with_name ("user_access: seteuid");
668 #else /* Not HAVE_SETEUID. */
670 #ifndef HAVE_SETREUID
672 /* System V has only the setuid/setgid calls to set user/group IDs.
673 There is an effective ID, which can be set by setuid/setgid.
674 It can be set (unless you are root) only to either what it already is
675 (returned by geteuid/getegid, now in make_uid/make_gid),
676 the real ID (return by getuid/getgid, now in user_uid/user_gid),
677 or the saved set ID (what the effective ID was before this set-ID
678 executable (make) was exec'd). */
680 if (setuid (user_uid
) < 0)
681 pfatal_with_name ("user_access: setuid");
683 #else /* HAVE_SETREUID. */
685 /* In 4BSD, the setreuid/setregid calls set both the real and effective IDs.
686 They may be set to themselves or each other. So you have two alternatives
687 at any one time. If you use setuid/setgid, the effective will be set to
688 the real, leaving only one alternative. Using setreuid/setregid, however,
689 you can toggle between your two alternatives by swapping the values in a
690 single setreuid or setregid call. */
692 if (setreuid (make_uid
, user_uid
) < 0)
693 pfatal_with_name ("user_access: setreuid");
695 #endif /* Not HAVE_SETREUID. */
696 #endif /* HAVE_SETEUID. */
699 if (setegid (user_gid
) < 0)
700 pfatal_with_name ("user_access: setegid");
702 #ifndef HAVE_SETREGID
703 if (setgid (user_gid
) < 0)
704 pfatal_with_name ("user_access: setgid");
706 if (setregid (make_gid
, user_gid
) < 0)
707 pfatal_with_name ("user_access: setregid");
711 current_access
= user
;
715 #endif /* GETLOADAVG_PRIVILEGED */
718 /* Give the process appropriate permissions for access to
719 make data (i.e., the load average). */
723 #ifdef GETLOADAVG_PRIVILEGED
728 if (current_access
== make
)
731 /* See comments in user_access, above. */
734 if (seteuid (make_uid
) < 0)
735 pfatal_with_name ("make_access: seteuid");
737 #ifndef HAVE_SETREUID
738 if (setuid (make_uid
) < 0)
739 pfatal_with_name ("make_access: setuid");
741 if (setreuid (user_uid
, make_uid
) < 0)
742 pfatal_with_name ("make_access: setreuid");
747 if (setegid (make_gid
) < 0)
748 pfatal_with_name ("make_access: setegid");
750 #ifndef HAVE_SETREGID
751 if (setgid (make_gid
) < 0)
752 pfatal_with_name ("make_access: setgid");
754 if (setregid (user_gid
, make_gid
) < 0)
755 pfatal_with_name ("make_access: setregid");
759 current_access
= make
;
763 #endif /* GETLOADAVG_PRIVILEGED */
766 /* Give the process appropriate permissions for a child process.
767 This is like user_access, but you can't get back to make_access. */
771 #ifdef GETLOADAVG_PRIVILEGED
776 /* Set both the real and effective UID and GID to the user's.
777 They cannot be changed back to make's. */
779 #ifndef HAVE_SETREUID
780 if (setuid (user_uid
) < 0)
781 pfatal_with_name ("child_access: setuid");
783 if (setreuid (user_uid
, user_uid
) < 0)
784 pfatal_with_name ("child_access: setreuid");
787 #ifndef HAVE_SETREGID
788 if (setgid (user_gid
) < 0)
789 pfatal_with_name ("child_access: setgid");
791 if (setregid (user_gid
, user_gid
) < 0)
792 pfatal_with_name ("child_access: setregid");
795 log_access ("Child");
797 #endif /* GETLOADAVG_PRIVILEGED */
800 #ifdef NEED_GET_PATH_MAX
804 static unsigned int value
;
808 long int x
= pathconf ("/", _PC_PATH_MAX
);