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. */
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 /* We can't use any variadic interface! */
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 (const void *v1
, const void *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 (char *line
)
79 register char *in
, *out
, *p
;
80 register int backslash
;
81 register unsigned int bs_write
;
83 in
= strchr (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 ((unsigned char)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 (char *line
)
162 comment
= find_char_unquote (line
, '#', 0, 0);
165 /* Cut off the line at the #. */
169 /* Print N spaces (used in debug for target-depth). */
172 print_spaces (unsigned int n
)
179 /* Return a newly-allocated string whose contents
180 concatenate those of s1, s2, s3. */
183 concat (const char *s1
, const char *s2
, const char *s3
)
185 unsigned int len1
, len2
, len3
;
188 len1
= *s1
!= '\0' ? strlen (s1
) : 0;
189 len2
= *s2
!= '\0' ? strlen (s2
) : 0;
190 len3
= *s3
!= '\0' ? strlen (s3
) : 0;
192 result
= (char *) xmalloc (len1
+ len2
+ len3
+ 1);
195 bcopy (s1
, result
, len1
);
197 bcopy (s2
, result
+ len1
, len2
);
199 bcopy (s3
, result
+ len1
+ len2
, len3
);
200 *(result
+ len1
+ len2
+ len3
) = '\0';
205 /* Print a message on stdout. */
208 #if HAVE_ANSI_COMPILER && USE_VARIADIC && HAVE_STDARG_H
209 message (int prefix
, const char *fmt
, ...)
211 message (prefix
, fmt
, va_alist
)
221 log_working_directory (1);
228 printf ("%s: ", program
);
230 printf ("%s[%u]: ", program
, makelevel
);
232 VA_START (args
, fmt
);
233 VA_PRINTF (stdout
, fmt
, args
);
241 /* Print an error message. */
244 #if HAVE_ANSI_COMPILER && USE_VARIADIC && HAVE_STDARG_H
245 error (const struct floc
*flocp
, const char *fmt
, ...)
247 error (flocp
, fmt
, va_alist
)
248 const struct floc
*flocp
;
257 log_working_directory (1);
259 if (flocp
&& flocp
->filenm
)
260 fprintf (stderr
, "%s:%lu: ", flocp
->filenm
, flocp
->lineno
);
261 else if (makelevel
== 0)
262 fprintf (stderr
, "%s: ", program
);
264 fprintf (stderr
, "%s[%u]: ", program
, makelevel
);
267 VA_PRINTF (stderr
, fmt
, args
);
274 /* Print an error message and exit. */
277 #if HAVE_ANSI_COMPILER && USE_VARIADIC && HAVE_STDARG_H
278 fatal (const struct floc
*flocp
, const char *fmt
, ...)
280 fatal (flocp
, fmt
, va_alist
)
281 const struct floc
*flocp
;
290 log_working_directory (1);
292 if (flocp
&& flocp
->filenm
)
293 fprintf (stderr
, "%s:%lu: *** ", flocp
->filenm
, flocp
->lineno
);
294 else if (makelevel
== 0)
295 fprintf (stderr
, "%s: *** ", program
);
297 fprintf (stderr
, "%s[%u]: *** ", program
, makelevel
);
300 VA_PRINTF (stderr
, fmt
, args
);
303 fputs (_(". Stop.\n"), stderr
);
308 #ifndef HAVE_STRERROR
313 strerror (int errnum
)
315 extern int errno
, sys_nerr
;
317 extern char *sys_errlist
[];
319 static char buf
[] = "Unknown error 12345678901234567890";
321 if (errno
< sys_nerr
)
322 return sys_errlist
[errnum
];
324 sprintf (buf
, _("Unknown error %d"), errnum
);
329 /* Print an error message from errno. */
332 perror_with_name (const char *str
, const char *name
)
334 error (NILF
, _("%s%s: %s"), str
, name
, strerror (errno
));
337 /* Print an error message from errno and exit. */
340 pfatal_with_name (const char *name
)
342 fatal (NILF
, _("%s: %s"), name
, strerror (errno
));
347 /* Like malloc but get fatal error if memory is exhausted. */
348 /* Don't bother if we're using dmalloc; it provides these for us. */
350 #ifndef HAVE_DMALLOC_H
357 xmalloc (unsigned int size
)
359 /* Make sure we don't allocate 0, for pre-ANSI libraries. */
360 char *result
= (char *) malloc (size
? size
: 1);
362 fatal (NILF
, _("virtual memory exhausted"));
368 xrealloc (char *ptr
, unsigned int size
)
372 /* Some older implementations of realloc() don't conform to ANSI. */
375 result
= ptr
? realloc (ptr
, size
) : malloc (size
);
377 fatal (NILF
, _("virtual memory exhausted"));
383 xstrdup (const char *ptr
)
388 result
= strdup (ptr
);
390 result
= (char *) malloc (strlen (ptr
) + 1);
394 fatal (NILF
, _("virtual memory exhausted"));
399 return strcpy(result
, ptr
);
403 #endif /* HAVE_DMALLOC_H */
406 savestring (const char *str
, unsigned int length
)
408 register char *out
= (char *) xmalloc (length
+ 1);
410 bcopy (str
, out
, length
);
417 Search through the string STRING, which ends at LIMIT, for the character C.
418 Returns a pointer to the first occurrence, or nil if none is found.
419 Like INDEX except that the string searched ends where specified
420 instead of at the first null. */
423 lindex (const char *s
, const char *limit
, int c
)
427 return (char *)(s
- 1);
432 /* Return the address of the first whitespace or null in the string S. */
435 end_of_token (char *s
)
437 while (*s
!= '\0' && !isblank ((unsigned char)*s
))
444 * Same as end_of_token, but take into account a stop character
447 end_of_token_w32 (char *s
, char stopchar
)
449 register char *p
= s
;
450 register int backslash
= 0;
452 while (*p
!= '\0' && *p
!= stopchar
453 && (backslash
|| !isblank ((unsigned char)*p
)))
457 backslash
= !backslash
;
460 backslash
= !backslash
;
472 /* Return the address of the first nonwhitespace or null in the string S. */
475 next_token (const char *s
)
477 while (isblank ((unsigned char)*s
))
482 /* Find the next token in PTR; return the address of it, and store the
483 length of the token into *LENGTHPTR if LENGTHPTR is not nil. */
486 find_next_token (char **ptr
, unsigned int *lengthptr
)
488 char *p
= next_token (*ptr
);
494 *ptr
= end
= end_of_token (p
);
496 *lengthptr
= end
- p
;
500 /* Copy a chain of `struct dep', making a new chain
501 with the same contents as the old one. */
504 copy_dep_chain (struct dep
*d
)
506 register struct dep
*c
;
507 struct dep
*firstnew
= 0;
508 struct dep
*lastnew
= 0;
512 c
= (struct dep
*) xmalloc (sizeof (struct dep
));
513 bcopy ((char *) d
, (char *) c
, sizeof (struct dep
));
515 c
->name
= xstrdup (c
->name
);
518 firstnew
= lastnew
= c
;
520 lastnew
= lastnew
->next
= c
;
529 /* The losing compiler on this machine can't handle this macro. */
532 dep_name (struct dep
*dep
)
534 return dep
->name
== 0 ? dep
->file
->name
: dep
->name
;
538 #ifdef GETLOADAVG_PRIVILEGED
542 /* Hopefully if a system says it's POSIX.1 and has the setuid and setgid
543 functions, they work as POSIX.1 says. Some systems (Alpha OSF/1 1.2,
544 for example) which claim to be POSIX.1 also have the BSD setreuid and
545 setregid functions, but they don't work as in BSD and only the POSIX.1
551 #else /* Not POSIX. */
553 /* Some POSIX.1 systems have the seteuid and setegid functions. In a
554 POSIX-like system, they are the best thing to use. However, some
555 non-POSIX systems have them too but they do not work in the POSIX style
556 and we must use setreuid and setregid instead. */
563 #ifndef HAVE_UNISTD_H
564 extern int getuid (), getgid (), geteuid (), getegid ();
565 extern int setuid (), setgid ();
567 extern int seteuid ();
570 extern int setreuid ();
571 #endif /* Have setreuid. */
572 #endif /* Have seteuid. */
574 extern int setegid ();
577 extern int setregid ();
578 #endif /* Have setregid. */
579 #endif /* Have setegid. */
580 #endif /* No <unistd.h>. */
582 /* Keep track of the user and group IDs for user- and make- access. */
583 static int user_uid
= -1, user_gid
= -1, make_uid
= -1, make_gid
= -1;
584 #define access_inited (user_uid != -1)
585 static enum { make
, user
} current_access
;
588 /* Under -d, write a message describing the current IDs. */
591 log_access (char *flavor
)
593 if (! ISDB (DB_JOBS
))
596 /* All the other debugging messages go to stdout,
597 but we write this one to stderr because it might be
598 run in a child fork whose stdout is piped. */
600 fprintf (stderr
, _("%s: user %lu (real %lu), group %lu (real %lu)\n"),
601 flavor
, (unsigned long) geteuid (), (unsigned long) getuid (),
602 (unsigned long) getegid (), (unsigned long) getgid ());
611 user_uid
= getuid ();
612 user_gid
= getgid ();
614 make_uid
= geteuid ();
615 make_gid
= getegid ();
617 /* Do these ever fail? */
618 if (user_uid
== -1 || user_gid
== -1 || make_uid
== -1 || make_gid
== -1)
619 pfatal_with_name ("get{e}[gu]id");
621 log_access (_("Initialized access"));
623 current_access
= make
;
627 #endif /* GETLOADAVG_PRIVILEGED */
629 /* Give the process appropriate permissions for access to
630 user data (i.e., to stat files, or to spawn a child process). */
634 #ifdef GETLOADAVG_PRIVILEGED
639 if (current_access
== user
)
642 /* We are in "make access" mode. This means that the effective user and
643 group IDs are those of make (if it was installed setuid or setgid).
644 We now want to set the effective user and group IDs to the real IDs,
645 which are the IDs of the process that exec'd make. */
649 /* Modern systems have the seteuid/setegid calls which set only the
650 effective IDs, which is ideal. */
652 if (seteuid (user_uid
) < 0)
653 pfatal_with_name ("user_access: seteuid");
655 #else /* Not HAVE_SETEUID. */
657 #ifndef HAVE_SETREUID
659 /* System V has only the setuid/setgid calls to set user/group IDs.
660 There is an effective ID, which can be set by setuid/setgid.
661 It can be set (unless you are root) only to either what it already is
662 (returned by geteuid/getegid, now in make_uid/make_gid),
663 the real ID (return by getuid/getgid, now in user_uid/user_gid),
664 or the saved set ID (what the effective ID was before this set-ID
665 executable (make) was exec'd). */
667 if (setuid (user_uid
) < 0)
668 pfatal_with_name ("user_access: setuid");
670 #else /* HAVE_SETREUID. */
672 /* In 4BSD, the setreuid/setregid calls set both the real and effective IDs.
673 They may be set to themselves or each other. So you have two alternatives
674 at any one time. If you use setuid/setgid, the effective will be set to
675 the real, leaving only one alternative. Using setreuid/setregid, however,
676 you can toggle between your two alternatives by swapping the values in a
677 single setreuid or setregid call. */
679 if (setreuid (make_uid
, user_uid
) < 0)
680 pfatal_with_name ("user_access: setreuid");
682 #endif /* Not HAVE_SETREUID. */
683 #endif /* HAVE_SETEUID. */
686 if (setegid (user_gid
) < 0)
687 pfatal_with_name ("user_access: setegid");
689 #ifndef HAVE_SETREGID
690 if (setgid (user_gid
) < 0)
691 pfatal_with_name ("user_access: setgid");
693 if (setregid (make_gid
, user_gid
) < 0)
694 pfatal_with_name ("user_access: setregid");
698 current_access
= user
;
700 log_access (_("User access"));
702 #endif /* GETLOADAVG_PRIVILEGED */
705 /* Give the process appropriate permissions for access to
706 make data (i.e., the load average). */
710 #ifdef GETLOADAVG_PRIVILEGED
715 if (current_access
== make
)
718 /* See comments in user_access, above. */
721 if (seteuid (make_uid
) < 0)
722 pfatal_with_name ("make_access: seteuid");
724 #ifndef HAVE_SETREUID
725 if (setuid (make_uid
) < 0)
726 pfatal_with_name ("make_access: setuid");
728 if (setreuid (user_uid
, make_uid
) < 0)
729 pfatal_with_name ("make_access: setreuid");
734 if (setegid (make_gid
) < 0)
735 pfatal_with_name ("make_access: setegid");
737 #ifndef HAVE_SETREGID
738 if (setgid (make_gid
) < 0)
739 pfatal_with_name ("make_access: setgid");
741 if (setregid (user_gid
, make_gid
) < 0)
742 pfatal_with_name ("make_access: setregid");
746 current_access
= make
;
748 log_access (_("Make access"));
750 #endif /* GETLOADAVG_PRIVILEGED */
753 /* Give the process appropriate permissions for a child process.
754 This is like user_access, but you can't get back to make_access. */
758 #ifdef GETLOADAVG_PRIVILEGED
763 /* Set both the real and effective UID and GID to the user's.
764 They cannot be changed back to make's. */
766 #ifndef HAVE_SETREUID
767 if (setuid (user_uid
) < 0)
768 pfatal_with_name ("child_access: setuid");
770 if (setreuid (user_uid
, user_uid
) < 0)
771 pfatal_with_name ("child_access: setreuid");
774 #ifndef HAVE_SETREGID
775 if (setgid (user_gid
) < 0)
776 pfatal_with_name ("child_access: setgid");
778 if (setregid (user_gid
, user_gid
) < 0)
779 pfatal_with_name ("child_access: setregid");
782 log_access (_("Child access"));
784 #endif /* GETLOADAVG_PRIVILEGED */
787 #ifdef NEED_GET_PATH_MAX
791 static unsigned int value
;
795 long int x
= pathconf ("/", _PC_PATH_MAX
);