1 /* Miscellaneous generic support functions for GNU Make.
2 Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1997,
3 2002 Free Software Foundation, Inc.
4 This file is part of GNU Make.
6 GNU Make is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Make is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Make; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
25 /* Variadic functions. We go through contortions to allow proper function
26 prototypes for both ANSI and pre-ANSI C compilers, and also for those
27 which support stdarg.h vs. varargs.h, and finally those which have
28 vfprintf(), etc. and those who have _doprnt... or nothing.
30 This fancy stuff all came from GNU fileutils, except for the VA_PRINTF and
31 VA_END macros used here since we have multiple print functions. */
33 #if HAVE_VPRINTF || HAVE_DOPRNT
34 # define HAVE_STDVARARGS 1
37 # define VA_START(args, lastarg) va_start(args, lastarg)
40 # define VA_START(args, lastarg) va_start(args)
43 # define VA_PRINTF(fp, lastarg, args) vfprintf((fp), (lastarg), (args))
45 # define VA_PRINTF(fp, lastarg, args) _doprnt((lastarg), (args), (fp))
47 # define VA_END(args) va_end(args)
49 /* # undef HAVE_STDVARARGS */
50 # define va_alist a1, a2, a3, a4, a5, a6, a7, a8
51 # define va_dcl char *a1, *a2, *a3, *a4, *a5, *a6, *a7, *a8;
52 # define VA_START(args, lastarg)
53 # define VA_PRINTF(fp, lastarg, args) fprintf((fp), (lastarg), va_alist)
58 /* Compare strings *S1 and *S2.
59 Return negative if the first is less, positive if it is greater,
60 zero if they are equal. */
63 alpha_compare (v1
, v2
)
66 const char *s1
= *((char **)v1
);
67 const char *s2
= *((char **)v2
);
71 return strcmp (s1
, s2
);
74 /* Discard each backslash-newline combination from LINE.
75 Backslash-backslash-newline combinations become backslash-newlines.
76 This is done by copying the text at LINE into itself. */
79 collapse_continuations (line
)
82 register char *in
, *out
, *p
;
83 register int backslash
;
84 register unsigned int bs_write
;
86 in
= strchr (line
, '\n');
91 while (out
> line
&& out
[-1] == '\\')
96 /* BS_WRITE gets the number of quoted backslashes at
97 the end just before IN, and BACKSLASH gets nonzero
98 if the next character is quoted. */
101 for (p
= in
- 1; p
>= line
&& *p
== '\\'; --p
)
105 backslash
= !backslash
;
107 /* It should be impossible to go back this far without exiting,
108 but if we do, we can't get the right answer. */
113 /* Output the appropriate number of backslashes. */
114 while (bs_write
-- > 0)
117 /* Skip the newline. */
120 /* If the newline is quoted, discard following whitespace
121 and any preceding whitespace; leave just one space. */
124 in
= next_token (in
);
125 while (out
> line
&& isblank ((unsigned char)out
[-1]))
130 /* If the newline isn't quoted, put it in the output. */
133 /* Now copy the following line to the output.
134 Stop when we find backslashes followed by a newline. */
157 /* Remove comments from LINE.
158 This is done by copying the text at LINE onto itself. */
161 remove_comments (line
)
166 comment
= find_char_unquote (line
, '#', 0, 0);
169 /* Cut off the line at the #. */
173 /* Print N spaces (used in debug for target-depth). */
184 /* Return a newly-allocated string whose contents
185 concatenate those of s1, s2, s3. */
189 const char *s1
, *s2
, *s3
;
191 unsigned int len1
, len2
, len3
;
194 len1
= *s1
!= '\0' ? strlen (s1
) : 0;
195 len2
= *s2
!= '\0' ? strlen (s2
) : 0;
196 len3
= *s3
!= '\0' ? strlen (s3
) : 0;
198 result
= (char *) xmalloc (len1
+ len2
+ len3
+ 1);
201 bcopy (s1
, result
, len1
);
203 bcopy (s2
, result
+ len1
, len2
);
205 bcopy (s3
, result
+ len1
+ len2
, len3
);
206 *(result
+ len1
+ len2
+ len3
) = '\0';
211 /* Print a message on stdout. */
214 #if __STDC__ && HAVE_STDVARARGS
215 message (int prefix
, const char *fmt
, ...)
217 message (prefix
, fmt
, va_alist
)
227 log_working_directory (1);
234 printf ("%s: ", program
);
236 printf ("%s[%u]: ", program
, makelevel
);
238 VA_START (args
, fmt
);
239 VA_PRINTF (stdout
, fmt
, args
);
247 /* Print an error message. */
250 #if __STDC__ && HAVE_STDVARARGS
251 error (const struct floc
*flocp
, const char *fmt
, ...)
253 error (flocp
, fmt
, va_alist
)
254 const struct floc
*flocp
;
263 log_working_directory (1);
265 if (flocp
&& flocp
->filenm
)
266 fprintf (stderr
, "%s:%lu: ", flocp
->filenm
, flocp
->lineno
);
267 else if (makelevel
== 0)
268 fprintf (stderr
, "%s: ", program
);
270 fprintf (stderr
, "%s[%u]: ", program
, makelevel
);
273 VA_PRINTF (stderr
, fmt
, args
);
280 /* Print an error message and exit. */
283 #if __STDC__ && HAVE_STDVARARGS
284 fatal (const struct floc
*flocp
, const char *fmt
, ...)
286 fatal (flocp
, fmt
, va_alist
)
287 const struct floc
*flocp
;
296 log_working_directory (1);
298 if (flocp
&& flocp
->filenm
)
299 fprintf (stderr
, "%s:%lu: *** ", flocp
->filenm
, flocp
->lineno
);
300 else if (makelevel
== 0)
301 fprintf (stderr
, "%s: *** ", program
);
303 fprintf (stderr
, "%s[%u]: *** ", program
, makelevel
);
306 VA_PRINTF (stderr
, fmt
, args
);
309 fputs (_(". Stop.\n"), stderr
);
314 #ifndef HAVE_STRERROR
322 extern int errno
, sys_nerr
;
324 extern char *sys_errlist
[];
326 static char buf
[] = "Unknown error 12345678901234567890";
328 if (errno
< sys_nerr
)
329 return sys_errlist
[errnum
];
331 sprintf (buf
, _("Unknown error %d"), errnum
);
336 /* Print an error message from errno. */
339 perror_with_name (str
, name
)
340 const char *str
, *name
;
342 error (NILF
, _("%s%s: %s"), str
, name
, strerror (errno
));
345 /* Print an error message from errno and exit. */
348 pfatal_with_name (name
)
351 fatal (NILF
, _("%s: %s"), name
, strerror (errno
));
356 /* Like malloc but get fatal error if memory is exhausted. */
357 /* Don't bother if we're using dmalloc; it provides these for us. */
359 #ifndef HAVE_DMALLOC_H
369 char *result
= (char *) malloc (size
);
371 fatal (NILF
, _("virtual memory exhausted"));
383 /* Some older implementations of realloc() don't conform to ANSI. */
384 result
= ptr
? realloc (ptr
, size
) : malloc (size
);
386 fatal (NILF
, _("virtual memory exhausted"));
398 result
= strdup (ptr
);
400 result
= (char *) malloc (strlen (ptr
) + 1);
404 fatal (NILF
, _("virtual memory exhausted"));
409 return strcpy(result
, ptr
);
413 #endif /* HAVE_DMALLOC_H */
416 savestring (str
, length
)
420 register char *out
= (char *) xmalloc (length
+ 1);
422 bcopy (str
, out
, length
);
427 /* Search string BIG (length BLEN) for an occurrence of
428 string SMALL (length SLEN). Return a pointer to the
429 beginning of the first occurrence, or return nil if none found. */
432 sindex (big
, blen
, small
, slen
)
441 slen
= strlen (small
);
443 if (slen
&& blen
>= slen
)
445 register unsigned int b
;
447 /* Quit when there's not enough room left for the small string. */
451 for (b
= 0; b
< blen
; ++b
, ++big
)
452 if (*big
== *small
&& strneq (big
+ 1, small
+ 1, slen
))
460 Search through the string STRING, which ends at LIMIT, for the character C.
461 Returns a pointer to the first occurrence, or nil if none is found.
462 Like INDEX except that the string searched ends where specified
463 instead of at the first null. */
467 register const char *s
, *limit
;
472 return (char *)(s
- 1);
477 /* Return the address of the first whitespace or null in the string S. */
483 while (*s
!= '\0' && !isblank ((unsigned char)*s
))
490 * Same as end_of_token, but take into account a stop character
493 end_of_token_w32 (s
, stopchar
)
497 register char *p
= s
;
498 register int backslash
= 0;
500 while (*p
!= '\0' && *p
!= stopchar
501 && (backslash
|| !isblank ((unsigned char)*p
)))
505 backslash
= !backslash
;
508 backslash
= !backslash
;
520 /* Return the address of the first nonwhitespace or null in the string S. */
526 while (isblank ((unsigned char)*s
))
531 /* Find the next token in PTR; return the address of it, and store the
532 length of the token into *LENGTHPTR if LENGTHPTR is not nil. */
535 find_next_token (ptr
, lengthptr
)
537 unsigned int *lengthptr
;
539 char *p
= next_token (*ptr
);
545 *ptr
= end
= end_of_token (p
);
547 *lengthptr
= end
- p
;
551 /* Copy a chain of `struct dep', making a new chain
552 with the same contents as the old one. */
556 register struct dep
*d
;
558 register struct dep
*c
;
559 struct dep
*firstnew
= 0;
560 struct dep
*lastnew
= 0;
564 c
= (struct dep
*) xmalloc (sizeof (struct dep
));
565 bcopy ((char *) d
, (char *) c
, sizeof (struct dep
));
567 c
->name
= xstrdup (c
->name
);
570 firstnew
= lastnew
= c
;
572 lastnew
= lastnew
->next
= c
;
581 /* The losing compiler on this machine can't handle this macro. */
587 return dep
->name
== 0 ? dep
->file
->name
: dep
->name
;
591 #ifdef GETLOADAVG_PRIVILEGED
595 /* Hopefully if a system says it's POSIX.1 and has the setuid and setgid
596 functions, they work as POSIX.1 says. Some systems (Alpha OSF/1 1.2,
597 for example) which claim to be POSIX.1 also have the BSD setreuid and
598 setregid functions, but they don't work as in BSD and only the POSIX.1
604 #else /* Not POSIX. */
606 /* Some POSIX.1 systems have the seteuid and setegid functions. In a
607 POSIX-like system, they are the best thing to use. However, some
608 non-POSIX systems have them too but they do not work in the POSIX style
609 and we must use setreuid and setregid instead. */
616 #ifndef HAVE_UNISTD_H
617 extern int getuid (), getgid (), geteuid (), getegid ();
618 extern int setuid (), setgid ();
620 extern int seteuid ();
623 extern int setreuid ();
624 #endif /* Have setreuid. */
625 #endif /* Have seteuid. */
627 extern int setegid ();
630 extern int setregid ();
631 #endif /* Have setregid. */
632 #endif /* Have setegid. */
633 #endif /* No <unistd.h>. */
635 /* Keep track of the user and group IDs for user- and make- access. */
636 static int user_uid
= -1, user_gid
= -1, make_uid
= -1, make_gid
= -1;
637 #define access_inited (user_uid != -1)
638 static enum { make
, user
} current_access
;
641 /* Under -d, write a message describing the current IDs. */
647 if (! ISDB (DB_JOBS
))
650 /* All the other debugging messages go to stdout,
651 but we write this one to stderr because it might be
652 run in a child fork whose stdout is piped. */
654 fprintf (stderr
, _("%s: user %lu (real %lu), group %lu (real %lu)\n"),
655 flavor
, (unsigned long) geteuid (), (unsigned long) getuid (),
656 (unsigned long) getegid (), (unsigned long) getgid ());
665 user_uid
= getuid ();
666 user_gid
= getgid ();
668 make_uid
= geteuid ();
669 make_gid
= getegid ();
671 /* Do these ever fail? */
672 if (user_uid
== -1 || user_gid
== -1 || make_uid
== -1 || make_gid
== -1)
673 pfatal_with_name ("get{e}[gu]id");
675 log_access (_("Initialized access"));
677 current_access
= make
;
681 #endif /* GETLOADAVG_PRIVILEGED */
683 /* Give the process appropriate permissions for access to
684 user data (i.e., to stat files, or to spawn a child process). */
688 #ifdef GETLOADAVG_PRIVILEGED
693 if (current_access
== user
)
696 /* We are in "make access" mode. This means that the effective user and
697 group IDs are those of make (if it was installed setuid or setgid).
698 We now want to set the effective user and group IDs to the real IDs,
699 which are the IDs of the process that exec'd make. */
703 /* Modern systems have the seteuid/setegid calls which set only the
704 effective IDs, which is ideal. */
706 if (seteuid (user_uid
) < 0)
707 pfatal_with_name ("user_access: seteuid");
709 #else /* Not HAVE_SETEUID. */
711 #ifndef HAVE_SETREUID
713 /* System V has only the setuid/setgid calls to set user/group IDs.
714 There is an effective ID, which can be set by setuid/setgid.
715 It can be set (unless you are root) only to either what it already is
716 (returned by geteuid/getegid, now in make_uid/make_gid),
717 the real ID (return by getuid/getgid, now in user_uid/user_gid),
718 or the saved set ID (what the effective ID was before this set-ID
719 executable (make) was exec'd). */
721 if (setuid (user_uid
) < 0)
722 pfatal_with_name ("user_access: setuid");
724 #else /* HAVE_SETREUID. */
726 /* In 4BSD, the setreuid/setregid calls set both the real and effective IDs.
727 They may be set to themselves or each other. So you have two alternatives
728 at any one time. If you use setuid/setgid, the effective will be set to
729 the real, leaving only one alternative. Using setreuid/setregid, however,
730 you can toggle between your two alternatives by swapping the values in a
731 single setreuid or setregid call. */
733 if (setreuid (make_uid
, user_uid
) < 0)
734 pfatal_with_name ("user_access: setreuid");
736 #endif /* Not HAVE_SETREUID. */
737 #endif /* HAVE_SETEUID. */
740 if (setegid (user_gid
) < 0)
741 pfatal_with_name ("user_access: setegid");
743 #ifndef HAVE_SETREGID
744 if (setgid (user_gid
) < 0)
745 pfatal_with_name ("user_access: setgid");
747 if (setregid (make_gid
, user_gid
) < 0)
748 pfatal_with_name ("user_access: setregid");
752 current_access
= user
;
754 log_access (_("User access"));
756 #endif /* GETLOADAVG_PRIVILEGED */
759 /* Give the process appropriate permissions for access to
760 make data (i.e., the load average). */
764 #ifdef GETLOADAVG_PRIVILEGED
769 if (current_access
== make
)
772 /* See comments in user_access, above. */
775 if (seteuid (make_uid
) < 0)
776 pfatal_with_name ("make_access: seteuid");
778 #ifndef HAVE_SETREUID
779 if (setuid (make_uid
) < 0)
780 pfatal_with_name ("make_access: setuid");
782 if (setreuid (user_uid
, make_uid
) < 0)
783 pfatal_with_name ("make_access: setreuid");
788 if (setegid (make_gid
) < 0)
789 pfatal_with_name ("make_access: setegid");
791 #ifndef HAVE_SETREGID
792 if (setgid (make_gid
) < 0)
793 pfatal_with_name ("make_access: setgid");
795 if (setregid (user_gid
, make_gid
) < 0)
796 pfatal_with_name ("make_access: setregid");
800 current_access
= make
;
802 log_access (_("Make access"));
804 #endif /* GETLOADAVG_PRIVILEGED */
807 /* Give the process appropriate permissions for a child process.
808 This is like user_access, but you can't get back to make_access. */
812 #ifdef GETLOADAVG_PRIVILEGED
817 /* Set both the real and effective UID and GID to the user's.
818 They cannot be changed back to make's. */
820 #ifndef HAVE_SETREUID
821 if (setuid (user_uid
) < 0)
822 pfatal_with_name ("child_access: setuid");
824 if (setreuid (user_uid
, user_uid
) < 0)
825 pfatal_with_name ("child_access: setreuid");
828 #ifndef HAVE_SETREGID
829 if (setgid (user_gid
) < 0)
830 pfatal_with_name ("child_access: setgid");
832 if (setregid (user_gid
, user_gid
) < 0)
833 pfatal_with_name ("child_access: setregid");
836 log_access (_("Child access"));
838 #endif /* GETLOADAVG_PRIVILEGED */
841 #ifdef NEED_GET_PATH_MAX
845 static unsigned int value
;
849 long int x
= pathconf ("/", _PC_PATH_MAX
);
861 #ifdef HAVE_BROKEN_RESTART
867 atomic_stat(file
, buf
)
873 while ((r
= stat (file
, buf
)) < 0)
886 while ((r
= readdir (dir
)) == NULL
)
893 #endif /* HAVE_BROKEN_RESTART */