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 (const void *v1
, const void *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 (char *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 ((unsigned char)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 (char *line
)
163 comment
= find_char_unquote (line
, '#', 0, 0);
166 /* Cut off the line at the #. */
170 /* Print N spaces (used in debug for target-depth). */
173 print_spaces (unsigned int n
)
180 /* Return a newly-allocated string whose contents
181 concatenate those of s1, s2, s3. */
184 concat (const char *s1
, const char *s2
, const char *s3
)
186 unsigned int len1
, len2
, len3
;
189 len1
= *s1
!= '\0' ? strlen (s1
) : 0;
190 len2
= *s2
!= '\0' ? strlen (s2
) : 0;
191 len3
= *s3
!= '\0' ? strlen (s3
) : 0;
193 result
= (char *) xmalloc (len1
+ len2
+ len3
+ 1);
196 bcopy (s1
, result
, len1
);
198 bcopy (s2
, result
+ len1
, len2
);
200 bcopy (s3
, result
+ len1
+ len2
, len3
);
201 *(result
+ len1
+ len2
+ len3
) = '\0';
206 /* Print a message on stdout. */
209 #if __STDC__ && HAVE_STDVARARGS
210 message (int prefix
, const char *fmt
, ...)
212 message (prefix
, fmt
, va_alist
)
222 log_working_directory (1);
229 printf ("%s: ", program
);
231 printf ("%s[%u]: ", program
, makelevel
);
233 VA_START (args
, fmt
);
234 VA_PRINTF (stdout
, fmt
, args
);
242 /* Print an error message. */
245 #if __STDC__ && HAVE_STDVARARGS
246 error (const struct floc
*flocp
, const char *fmt
, ...)
248 error (flocp
, fmt
, va_alist
)
249 const struct floc
*flocp
;
258 log_working_directory (1);
260 if (flocp
&& flocp
->filenm
)
261 fprintf (stderr
, "%s:%lu: ", flocp
->filenm
, flocp
->lineno
);
262 else if (makelevel
== 0)
263 fprintf (stderr
, "%s: ", program
);
265 fprintf (stderr
, "%s[%u]: ", program
, makelevel
);
268 VA_PRINTF (stderr
, fmt
, args
);
275 /* Print an error message and exit. */
278 #if __STDC__ && HAVE_STDVARARGS
279 fatal (const struct floc
*flocp
, const char *fmt
, ...)
281 fatal (flocp
, fmt
, va_alist
)
282 const struct floc
*flocp
;
291 log_working_directory (1);
293 if (flocp
&& flocp
->filenm
)
294 fprintf (stderr
, "%s:%lu: *** ", flocp
->filenm
, flocp
->lineno
);
295 else if (makelevel
== 0)
296 fprintf (stderr
, "%s: *** ", program
);
298 fprintf (stderr
, "%s[%u]: *** ", program
, makelevel
);
301 VA_PRINTF (stderr
, fmt
, args
);
304 fputs (_(". Stop.\n"), stderr
);
309 #ifndef HAVE_STRERROR
314 strerror (int errnum
)
316 extern int errno
, sys_nerr
;
318 extern char *sys_errlist
[];
320 static char buf
[] = "Unknown error 12345678901234567890";
322 if (errno
< sys_nerr
)
323 return sys_errlist
[errnum
];
325 sprintf (buf
, _("Unknown error %d"), errnum
);
330 /* Print an error message from errno. */
333 perror_with_name (const char *str
, const char *name
)
335 error (NILF
, _("%s%s: %s"), str
, name
, strerror (errno
));
338 /* Print an error message from errno and exit. */
341 pfatal_with_name (const char *name
)
343 fatal (NILF
, _("%s: %s"), name
, strerror (errno
));
348 /* Like malloc but get fatal error if memory is exhausted. */
349 /* Don't bother if we're using dmalloc; it provides these for us. */
351 #ifndef HAVE_DMALLOC_H
358 xmalloc (unsigned int size
)
360 char *result
= (char *) malloc (size
);
362 fatal (NILF
, _("virtual memory exhausted"));
368 xrealloc (char *ptr
, unsigned int size
)
372 /* Some older implementations of realloc() don't conform to ANSI. */
373 result
= ptr
? realloc (ptr
, size
) : malloc (size
);
375 fatal (NILF
, _("virtual memory exhausted"));
381 xstrdup (const char *ptr
)
386 result
= strdup (ptr
);
388 result
= (char *) malloc (strlen (ptr
) + 1);
392 fatal (NILF
, _("virtual memory exhausted"));
397 return strcpy(result
, ptr
);
401 #endif /* HAVE_DMALLOC_H */
404 savestring (const char *str
, unsigned int length
)
406 register char *out
= (char *) xmalloc (length
+ 1);
408 bcopy (str
, out
, length
);
413 /* Search string BIG (length BLEN) for an occurrence of
414 string SMALL (length SLEN). Return a pointer to the
415 beginning of the first occurrence, or return nil if none found. */
418 sindex (const char *big
, unsigned int blen
,
419 const char *small
, unsigned int slen
)
424 slen
= strlen (small
);
426 if (slen
&& blen
>= slen
)
428 register unsigned int b
;
430 /* Quit when there's not enough room left for the small string. */
434 for (b
= 0; b
< blen
; ++b
, ++big
)
435 if (*big
== *small
&& strneq (big
+ 1, small
+ 1, slen
))
443 Search through the string STRING, which ends at LIMIT, for the character C.
444 Returns a pointer to the first occurrence, or nil if none is found.
445 Like INDEX except that the string searched ends where specified
446 instead of at the first null. */
449 lindex (const char *s
, const char *limit
, int c
)
453 return (char *)(s
- 1);
458 /* Return the address of the first whitespace or null in the string S. */
461 end_of_token (char *s
)
463 while (*s
!= '\0' && !isblank ((unsigned char)*s
))
470 * Same as end_of_token, but take into account a stop character
473 end_of_token_w32 (char *s
, char stopchar
)
475 register char *p
= s
;
476 register int backslash
= 0;
478 while (*p
!= '\0' && *p
!= stopchar
479 && (backslash
|| !isblank ((unsigned char)*p
)))
483 backslash
= !backslash
;
486 backslash
= !backslash
;
498 /* Return the address of the first nonwhitespace or null in the string S. */
501 next_token (const char *s
)
503 while (isblank ((unsigned char)*s
))
508 /* Find the next token in PTR; return the address of it, and store the
509 length of the token into *LENGTHPTR if LENGTHPTR is not nil. */
512 find_next_token (char **ptr
, unsigned int *lengthptr
)
514 char *p
= next_token (*ptr
);
520 *ptr
= end
= end_of_token (p
);
522 *lengthptr
= end
- p
;
526 /* Copy a chain of `struct dep', making a new chain
527 with the same contents as the old one. */
530 copy_dep_chain (struct dep
*d
)
532 register struct dep
*c
;
533 struct dep
*firstnew
= 0;
534 struct dep
*lastnew
= 0;
538 c
= (struct dep
*) xmalloc (sizeof (struct dep
));
539 bcopy ((char *) d
, (char *) c
, sizeof (struct dep
));
541 c
->name
= xstrdup (c
->name
);
544 firstnew
= lastnew
= c
;
546 lastnew
= lastnew
->next
= c
;
555 /* The losing compiler on this machine can't handle this macro. */
558 dep_name (struct dep
*dep
)
560 return dep
->name
== 0 ? dep
->file
->name
: dep
->name
;
564 #ifdef GETLOADAVG_PRIVILEGED
568 /* Hopefully if a system says it's POSIX.1 and has the setuid and setgid
569 functions, they work as POSIX.1 says. Some systems (Alpha OSF/1 1.2,
570 for example) which claim to be POSIX.1 also have the BSD setreuid and
571 setregid functions, but they don't work as in BSD and only the POSIX.1
577 #else /* Not POSIX. */
579 /* Some POSIX.1 systems have the seteuid and setegid functions. In a
580 POSIX-like system, they are the best thing to use. However, some
581 non-POSIX systems have them too but they do not work in the POSIX style
582 and we must use setreuid and setregid instead. */
589 #ifndef HAVE_UNISTD_H
590 extern int getuid (), getgid (), geteuid (), getegid ();
591 extern int setuid (), setgid ();
593 extern int seteuid ();
596 extern int setreuid ();
597 #endif /* Have setreuid. */
598 #endif /* Have seteuid. */
600 extern int setegid ();
603 extern int setregid ();
604 #endif /* Have setregid. */
605 #endif /* Have setegid. */
606 #endif /* No <unistd.h>. */
608 /* Keep track of the user and group IDs for user- and make- access. */
609 static int user_uid
= -1, user_gid
= -1, make_uid
= -1, make_gid
= -1;
610 #define access_inited (user_uid != -1)
611 static enum { make
, user
} current_access
;
614 /* Under -d, write a message describing the current IDs. */
617 log_access (char *flavor
)
619 if (! ISDB (DB_JOBS
))
622 /* All the other debugging messages go to stdout,
623 but we write this one to stderr because it might be
624 run in a child fork whose stdout is piped. */
626 fprintf (stderr
, _("%s: user %lu (real %lu), group %lu (real %lu)\n"),
627 flavor
, (unsigned long) geteuid (), (unsigned long) getuid (),
628 (unsigned long) getegid (), (unsigned long) getgid ());
637 user_uid
= getuid ();
638 user_gid
= getgid ();
640 make_uid
= geteuid ();
641 make_gid
= getegid ();
643 /* Do these ever fail? */
644 if (user_uid
== -1 || user_gid
== -1 || make_uid
== -1 || make_gid
== -1)
645 pfatal_with_name ("get{e}[gu]id");
647 log_access (_("Initialized access"));
649 current_access
= make
;
653 #endif /* GETLOADAVG_PRIVILEGED */
655 /* Give the process appropriate permissions for access to
656 user data (i.e., to stat files, or to spawn a child process). */
660 #ifdef GETLOADAVG_PRIVILEGED
665 if (current_access
== user
)
668 /* We are in "make access" mode. This means that the effective user and
669 group IDs are those of make (if it was installed setuid or setgid).
670 We now want to set the effective user and group IDs to the real IDs,
671 which are the IDs of the process that exec'd make. */
675 /* Modern systems have the seteuid/setegid calls which set only the
676 effective IDs, which is ideal. */
678 if (seteuid (user_uid
) < 0)
679 pfatal_with_name ("user_access: seteuid");
681 #else /* Not HAVE_SETEUID. */
683 #ifndef HAVE_SETREUID
685 /* System V has only the setuid/setgid calls to set user/group IDs.
686 There is an effective ID, which can be set by setuid/setgid.
687 It can be set (unless you are root) only to either what it already is
688 (returned by geteuid/getegid, now in make_uid/make_gid),
689 the real ID (return by getuid/getgid, now in user_uid/user_gid),
690 or the saved set ID (what the effective ID was before this set-ID
691 executable (make) was exec'd). */
693 if (setuid (user_uid
) < 0)
694 pfatal_with_name ("user_access: setuid");
696 #else /* HAVE_SETREUID. */
698 /* In 4BSD, the setreuid/setregid calls set both the real and effective IDs.
699 They may be set to themselves or each other. So you have two alternatives
700 at any one time. If you use setuid/setgid, the effective will be set to
701 the real, leaving only one alternative. Using setreuid/setregid, however,
702 you can toggle between your two alternatives by swapping the values in a
703 single setreuid or setregid call. */
705 if (setreuid (make_uid
, user_uid
) < 0)
706 pfatal_with_name ("user_access: setreuid");
708 #endif /* Not HAVE_SETREUID. */
709 #endif /* HAVE_SETEUID. */
712 if (setegid (user_gid
) < 0)
713 pfatal_with_name ("user_access: setegid");
715 #ifndef HAVE_SETREGID
716 if (setgid (user_gid
) < 0)
717 pfatal_with_name ("user_access: setgid");
719 if (setregid (make_gid
, user_gid
) < 0)
720 pfatal_with_name ("user_access: setregid");
724 current_access
= user
;
726 log_access (_("User access"));
728 #endif /* GETLOADAVG_PRIVILEGED */
731 /* Give the process appropriate permissions for access to
732 make data (i.e., the load average). */
736 #ifdef GETLOADAVG_PRIVILEGED
741 if (current_access
== make
)
744 /* See comments in user_access, above. */
747 if (seteuid (make_uid
) < 0)
748 pfatal_with_name ("make_access: seteuid");
750 #ifndef HAVE_SETREUID
751 if (setuid (make_uid
) < 0)
752 pfatal_with_name ("make_access: setuid");
754 if (setreuid (user_uid
, make_uid
) < 0)
755 pfatal_with_name ("make_access: setreuid");
760 if (setegid (make_gid
) < 0)
761 pfatal_with_name ("make_access: setegid");
763 #ifndef HAVE_SETREGID
764 if (setgid (make_gid
) < 0)
765 pfatal_with_name ("make_access: setgid");
767 if (setregid (user_gid
, make_gid
) < 0)
768 pfatal_with_name ("make_access: setregid");
772 current_access
= make
;
774 log_access (_("Make access"));
776 #endif /* GETLOADAVG_PRIVILEGED */
779 /* Give the process appropriate permissions for a child process.
780 This is like user_access, but you can't get back to make_access. */
784 #ifdef GETLOADAVG_PRIVILEGED
789 /* Set both the real and effective UID and GID to the user's.
790 They cannot be changed back to make's. */
792 #ifndef HAVE_SETREUID
793 if (setuid (user_uid
) < 0)
794 pfatal_with_name ("child_access: setuid");
796 if (setreuid (user_uid
, user_uid
) < 0)
797 pfatal_with_name ("child_access: setreuid");
800 #ifndef HAVE_SETREGID
801 if (setgid (user_gid
) < 0)
802 pfatal_with_name ("child_access: setgid");
804 if (setregid (user_gid
, user_gid
) < 0)
805 pfatal_with_name ("child_access: setregid");
808 log_access (_("Child access"));
810 #endif /* GETLOADAVG_PRIVILEGED */
813 #ifdef NEED_GET_PATH_MAX
817 static unsigned int value
;
821 long int x
= pathconf ("/", _PC_PATH_MAX
);
833 #ifdef HAVE_BROKEN_RESTART
839 atomic_stat (const char *file
, struct stat
*buf
)
843 while ((r
= stat (file
, buf
)) < 0)
851 atomic_readdir (DIR *dir
)
855 while ((r
= readdir (dir
)) == NULL
)
862 #endif /* HAVE_BROKEN_RESTART */