* Fix PR/1791.
[make.git] / misc.c
blob276706ef24e260843dddd29e94dd891c0e0712d2
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)
8 any later version.
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. */
20 #include "make.h"
21 #include "dep.h"
22 #include "debug.h"
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
34 # if __STDC__
35 # include <stdarg.h>
36 # define VA_START(args, lastarg) va_start(args, lastarg)
37 # else
38 # include <varargs.h>
39 # define VA_START(args, lastarg) va_start(args)
40 # endif
41 # if HAVE_VPRINTF
42 # define VA_PRINTF(fp, lastarg, args) vfprintf((fp), (lastarg), (args))
43 # else
44 # define VA_PRINTF(fp, lastarg, args) _doprnt((lastarg), (args), (fp))
45 # endif
46 # define VA_END(args) va_end(args)
47 #else
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)
52 # define VA_PRINTF(fp, lastarg, args) fprintf((fp), (lastarg), va_alist)
53 # define VA_END(args)
54 #endif
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. */
61 int
62 alpha_compare (v1, v2)
63 const void *v1, *v2;
65 const char *s1 = *((char **)v1);
66 const char *s2 = *((char **)v2);
68 if (*s1 != *s2)
69 return *s1 - *s2;
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. */
77 void
78 collapse_continuations (line)
79 char *line;
81 register char *in, *out, *p;
82 register int backslash;
83 register unsigned int bs_write;
85 in = strchr (line, '\n');
86 if (in == 0)
87 return;
89 out = in;
90 while (out > line && out[-1] == '\\')
91 --out;
93 while (*in != '\0')
95 /* BS_WRITE gets the number of quoted backslashes at
96 the end just before IN, and BACKSLASH gets nonzero
97 if the next character is quoted. */
98 backslash = 0;
99 bs_write = 0;
100 for (p = in - 1; p >= line && *p == '\\'; --p)
102 if (backslash)
103 ++bs_write;
104 backslash = !backslash;
106 /* It should be impossible to go back this far without exiting,
107 but if we do, we can't get the right answer. */
108 if (in == out - 1)
109 abort ();
112 /* Output the appropriate number of backslashes. */
113 while (bs_write-- > 0)
114 *out++ = '\\';
116 /* Skip the newline. */
117 ++in;
119 /* If the newline is quoted, discard following whitespace
120 and any preceding whitespace; leave just one space. */
121 if (backslash)
123 in = next_token (in);
124 while (out > line && isblank ((unsigned char)out[-1]))
125 --out;
126 *out++ = ' ';
128 else
129 /* If the newline isn't quoted, put it in the output. */
130 *out++ = '\n';
132 /* Now copy the following line to the output.
133 Stop when we find backslashes followed by a newline. */
134 while (*in != '\0')
135 if (*in == '\\')
137 p = in + 1;
138 while (*p == '\\')
139 ++p;
140 if (*p == '\n')
142 in = p;
143 break;
145 while (in < p)
146 *out++ = *in++;
148 else
149 *out++ = *in++;
152 *out = '\0';
156 /* Remove comments from LINE.
157 This is done by copying the text at LINE onto itself. */
159 void
160 remove_comments (line)
161 char *line;
163 char *comment;
165 comment = find_char_unquote (line, "#", 0);
167 if (comment != 0)
168 /* Cut off the line at the #. */
169 *comment = '\0';
172 /* Print N spaces (used in debug for target-depth). */
174 void
175 print_spaces (n)
176 register unsigned int n;
178 while (n-- > 0)
179 putchar (' ');
183 /* Return a newly-allocated string whose contents
184 concatenate those of s1, s2, s3. */
186 char *
187 concat (s1, s2, s3)
188 register char *s1, *s2, *s3;
190 register unsigned int len1, len2, len3;
191 register char *result;
193 len1 = *s1 != '\0' ? strlen (s1) : 0;
194 len2 = *s2 != '\0' ? strlen (s2) : 0;
195 len3 = *s3 != '\0' ? strlen (s3) : 0;
197 result = (char *) xmalloc (len1 + len2 + len3 + 1);
199 if (*s1 != '\0')
200 bcopy (s1, result, len1);
201 if (*s2 != '\0')
202 bcopy (s2, result + len1, len2);
203 if (*s3 != '\0')
204 bcopy (s3, result + len1 + len2, len3);
205 *(result + len1 + len2 + len3) = '\0';
207 return result;
210 /* Print a message on stdout. */
212 void
213 #if __STDC__ && HAVE_STDVARARGS
214 message (int prefix, const char *fmt, ...)
215 #else
216 message (prefix, fmt, va_alist)
217 int prefix;
218 const char *fmt;
219 va_dcl
220 #endif
222 #if HAVE_STDVARARGS
223 va_list args;
224 #endif
226 log_working_directory (1);
228 if (fmt != 0)
230 if (prefix)
232 if (makelevel == 0)
233 printf ("%s: ", program);
234 else
235 printf ("%s[%u]: ", program, makelevel);
237 VA_START (args, fmt);
238 VA_PRINTF (stdout, fmt, args);
239 VA_END (args);
240 putchar ('\n');
243 fflush (stdout);
246 /* Print an error message. */
248 void
249 #if __STDC__ && HAVE_STDVARARGS
250 error (const struct floc *flocp, const char *fmt, ...)
251 #else
252 error (flocp, fmt, va_alist)
253 const struct floc *flocp;
254 const char *fmt;
255 va_dcl
256 #endif
258 #if HAVE_STDVARARGS
259 va_list args;
260 #endif
262 log_working_directory (1);
264 if (flocp && flocp->filenm)
265 fprintf (stderr, "%s:%lu: ", flocp->filenm, flocp->lineno);
266 else if (makelevel == 0)
267 fprintf (stderr, "%s: ", program);
268 else
269 fprintf (stderr, "%s[%u]: ", program, makelevel);
271 VA_START(args, fmt);
272 VA_PRINTF (stderr, fmt, args);
273 VA_END (args);
275 putc ('\n', stderr);
276 fflush (stderr);
279 /* Print an error message and exit. */
281 void
282 #if __STDC__ && HAVE_STDVARARGS
283 fatal (const struct floc *flocp, const char *fmt, ...)
284 #else
285 fatal (flocp, fmt, va_alist)
286 const struct floc *flocp;
287 const char *fmt;
288 va_dcl
289 #endif
291 #if HAVE_STDVARARGS
292 va_list args;
293 #endif
295 log_working_directory (1);
297 if (flocp && flocp->filenm)
298 fprintf (stderr, "%s:%lu: *** ", flocp->filenm, flocp->lineno);
299 else if (makelevel == 0)
300 fprintf (stderr, "%s: *** ", program);
301 else
302 fprintf (stderr, "%s[%u]: *** ", program, makelevel);
304 VA_START(args, fmt);
305 VA_PRINTF (stderr, fmt, args);
306 VA_END (args);
308 fputs (_(". Stop.\n"), stderr);
310 die (2);
313 #ifndef HAVE_STRERROR
315 #undef strerror
317 char *
318 strerror (errnum)
319 int errnum;
321 extern int errno, sys_nerr;
322 #ifndef __DECC
323 extern char *sys_errlist[];
324 #endif
325 static char buf[] = "Unknown error 12345678901234567890";
327 if (errno < sys_nerr)
328 return sys_errlist[errnum];
330 sprintf (buf, _("Unknown error %d"), errnum);
331 return buf;
333 #endif
335 /* Print an error message from errno. */
337 void
338 perror_with_name (str, name)
339 char *str, *name;
341 error (NILF, "%s%s: %s", str, name, strerror (errno));
344 /* Print an error message from errno and exit. */
346 void
347 pfatal_with_name (name)
348 char *name;
350 fatal (NILF, "%s: %s", name, strerror (errno));
352 /* NOTREACHED */
355 /* Like malloc but get fatal error if memory is exhausted. */
356 /* Don't bother if we're using dmalloc; it provides these for us. */
358 #ifndef HAVE_DMALLOC_H
360 #undef xmalloc
361 #undef xrealloc
362 #undef xstrdup
364 char *
365 xmalloc (size)
366 unsigned int size;
368 char *result = (char *) malloc (size);
369 if (result == 0)
370 fatal (NILF, _("virtual memory exhausted"));
371 return result;
375 char *
376 xrealloc (ptr, size)
377 char *ptr;
378 unsigned int size;
380 char *result;
382 /* Some older implementations of realloc() don't conform to ANSI. */
383 result = ptr ? realloc (ptr, size) : malloc (size);
384 if (result == 0)
385 fatal (NILF, _("virtual memory exhausted"));
386 return result;
390 char *
391 xstrdup (ptr)
392 const char *ptr;
394 char *result;
396 #ifdef HAVE_STRDUP
397 result = strdup (ptr);
398 #else
399 result = (char *) malloc (strlen (ptr) + 1);
400 #endif
402 if (result == 0)
403 fatal (NILF, _("virtual memory exhausted"));
405 #ifdef HAVE_STRDUP
406 return result;
407 #else
408 return strcpy(result, ptr);
409 #endif
412 #endif /* HAVE_DMALLOC_H */
414 char *
415 savestring (str, length)
416 const char *str;
417 unsigned int length;
419 register char *out = (char *) xmalloc (length + 1);
420 if (length > 0)
421 bcopy (str, out, length);
422 out[length] = '\0';
423 return out;
426 /* Search string BIG (length BLEN) for an occurrence of
427 string SMALL (length SLEN). Return a pointer to the
428 beginning of the first occurrence, or return nil if none found. */
430 char *
431 sindex (big, blen, small, slen)
432 const char *big;
433 unsigned int blen;
434 const char *small;
435 unsigned int slen;
437 if (!blen)
438 blen = strlen (big);
439 if (!slen)
440 slen = strlen (small);
442 if (slen && blen >= slen)
444 register unsigned int b;
446 /* Quit when there's not enough room left for the small string. */
447 --slen;
448 blen -= slen;
450 for (b = 0; b < blen; ++b, ++big)
451 if (*big == *small && strneq (big + 1, small + 1, slen))
452 return (char *)big;
455 return 0;
458 /* Limited INDEX:
459 Search through the string STRING, which ends at LIMIT, for the character C.
460 Returns a pointer to the first occurrence, or nil if none is found.
461 Like INDEX except that the string searched ends where specified
462 instead of at the first null. */
464 char *
465 lindex (s, limit, c)
466 register const char *s, *limit;
467 int c;
469 while (s < limit)
470 if (*s++ == c)
471 return (char *)(s - 1);
473 return 0;
476 /* Return the address of the first whitespace or null in the string S. */
478 char *
479 end_of_token (s)
480 char *s;
482 while (*s != '\0' && !isblank ((unsigned char)*s))
483 ++s;
484 return s;
487 #ifdef WINDOWS32
489 * Same as end_of_token, but take into account a stop character
491 char *
492 end_of_token_w32 (s, stopchar)
493 char *s;
494 char stopchar;
496 register char *p = s;
497 register int backslash = 0;
499 while (*p != '\0' && *p != stopchar
500 && (backslash || !isblank ((unsigned char)*p)))
502 if (*p++ == '\\')
504 backslash = !backslash;
505 while (*p == '\\')
507 backslash = !backslash;
508 ++p;
511 else
512 backslash = 0;
515 return p;
517 #endif
519 /* Return the address of the first nonwhitespace or null in the string S. */
521 char *
522 next_token (s)
523 char *s;
525 register char *p = s;
527 while (isblank ((unsigned char)*p))
528 ++p;
529 return p;
532 /* Find the next token in PTR; return the address of it, and store the
533 length of the token into *LENGTHPTR if LENGTHPTR is not nil. */
535 char *
536 find_next_token (ptr, lengthptr)
537 char **ptr;
538 unsigned int *lengthptr;
540 char *p = next_token (*ptr);
541 char *end;
543 if (*p == '\0')
544 return 0;
546 *ptr = end = end_of_token (p);
547 if (lengthptr != 0)
548 *lengthptr = end - p;
549 return p;
552 /* Copy a chain of `struct dep', making a new chain
553 with the same contents as the old one. */
555 struct dep *
556 copy_dep_chain (d)
557 register struct dep *d;
559 register struct dep *c;
560 struct dep *firstnew = 0;
561 struct dep *lastnew = 0;
563 while (d != 0)
565 c = (struct dep *) xmalloc (sizeof (struct dep));
566 bcopy ((char *) d, (char *) c, sizeof (struct dep));
567 if (c->name != 0)
568 c->name = xstrdup (c->name);
569 c->next = 0;
570 if (firstnew == 0)
571 firstnew = lastnew = c;
572 else
573 lastnew = lastnew->next = c;
575 d = d->next;
578 return firstnew;
581 #ifdef iAPX286
582 /* The losing compiler on this machine can't handle this macro. */
584 char *
585 dep_name (dep)
586 struct dep *dep;
588 return dep->name == 0 ? dep->file->name : dep->name;
590 #endif
592 #ifdef GETLOADAVG_PRIVILEGED
594 #ifdef POSIX
596 /* Hopefully if a system says it's POSIX.1 and has the setuid and setgid
597 functions, they work as POSIX.1 says. Some systems (Alpha OSF/1 1.2,
598 for example) which claim to be POSIX.1 also have the BSD setreuid and
599 setregid functions, but they don't work as in BSD and only the POSIX.1
600 way works. */
602 #undef HAVE_SETREUID
603 #undef HAVE_SETREGID
605 #else /* Not POSIX. */
607 /* Some POSIX.1 systems have the seteuid and setegid functions. In a
608 POSIX-like system, they are the best thing to use. However, some
609 non-POSIX systems have them too but they do not work in the POSIX style
610 and we must use setreuid and setregid instead. */
612 #undef HAVE_SETEUID
613 #undef HAVE_SETEGID
615 #endif /* POSIX. */
617 #ifndef HAVE_UNISTD_H
618 extern int getuid (), getgid (), geteuid (), getegid ();
619 extern int setuid (), setgid ();
620 #ifdef HAVE_SETEUID
621 extern int seteuid ();
622 #else
623 #ifdef HAVE_SETREUID
624 extern int setreuid ();
625 #endif /* Have setreuid. */
626 #endif /* Have seteuid. */
627 #ifdef HAVE_SETEGID
628 extern int setegid ();
629 #else
630 #ifdef HAVE_SETREGID
631 extern int setregid ();
632 #endif /* Have setregid. */
633 #endif /* Have setegid. */
634 #endif /* No <unistd.h>. */
636 /* Keep track of the user and group IDs for user- and make- access. */
637 static int user_uid = -1, user_gid = -1, make_uid = -1, make_gid = -1;
638 #define access_inited (user_uid != -1)
639 static enum { make, user } current_access;
642 /* Under -d, write a message describing the current IDs. */
644 static void
645 log_access (flavor)
646 char *flavor;
648 if (! ISDB (DB_JOBS))
649 return;
651 /* All the other debugging messages go to stdout,
652 but we write this one to stderr because it might be
653 run in a child fork whose stdout is piped. */
655 fprintf (stderr, _("%s access: user %lu (real %lu), group %lu (real %lu)\n"),
656 flavor, (unsigned long) geteuid (), (unsigned long) getuid (),
657 (unsigned long) getegid (), (unsigned long) getgid ());
658 fflush (stderr);
662 static void
663 init_access ()
665 #ifndef VMS
666 user_uid = getuid ();
667 user_gid = getgid ();
669 make_uid = geteuid ();
670 make_gid = getegid ();
672 /* Do these ever fail? */
673 if (user_uid == -1 || user_gid == -1 || make_uid == -1 || make_gid == -1)
674 pfatal_with_name ("get{e}[gu]id");
676 log_access (_("Initialized"));
678 current_access = make;
679 #endif
682 #endif /* GETLOADAVG_PRIVILEGED */
684 /* Give the process appropriate permissions for access to
685 user data (i.e., to stat files, or to spawn a child process). */
686 void
687 user_access ()
689 #ifdef GETLOADAVG_PRIVILEGED
691 if (!access_inited)
692 init_access ();
694 if (current_access == user)
695 return;
697 /* We are in "make access" mode. This means that the effective user and
698 group IDs are those of make (if it was installed setuid or setgid).
699 We now want to set the effective user and group IDs to the real IDs,
700 which are the IDs of the process that exec'd make. */
702 #ifdef HAVE_SETEUID
704 /* Modern systems have the seteuid/setegid calls which set only the
705 effective IDs, which is ideal. */
707 if (seteuid (user_uid) < 0)
708 pfatal_with_name ("user_access: seteuid");
710 #else /* Not HAVE_SETEUID. */
712 #ifndef HAVE_SETREUID
714 /* System V has only the setuid/setgid calls to set user/group IDs.
715 There is an effective ID, which can be set by setuid/setgid.
716 It can be set (unless you are root) only to either what it already is
717 (returned by geteuid/getegid, now in make_uid/make_gid),
718 the real ID (return by getuid/getgid, now in user_uid/user_gid),
719 or the saved set ID (what the effective ID was before this set-ID
720 executable (make) was exec'd). */
722 if (setuid (user_uid) < 0)
723 pfatal_with_name ("user_access: setuid");
725 #else /* HAVE_SETREUID. */
727 /* In 4BSD, the setreuid/setregid calls set both the real and effective IDs.
728 They may be set to themselves or each other. So you have two alternatives
729 at any one time. If you use setuid/setgid, the effective will be set to
730 the real, leaving only one alternative. Using setreuid/setregid, however,
731 you can toggle between your two alternatives by swapping the values in a
732 single setreuid or setregid call. */
734 if (setreuid (make_uid, user_uid) < 0)
735 pfatal_with_name ("user_access: setreuid");
737 #endif /* Not HAVE_SETREUID. */
738 #endif /* HAVE_SETEUID. */
740 #ifdef HAVE_SETEGID
741 if (setegid (user_gid) < 0)
742 pfatal_with_name ("user_access: setegid");
743 #else
744 #ifndef HAVE_SETREGID
745 if (setgid (user_gid) < 0)
746 pfatal_with_name ("user_access: setgid");
747 #else
748 if (setregid (make_gid, user_gid) < 0)
749 pfatal_with_name ("user_access: setregid");
750 #endif
751 #endif
753 current_access = user;
755 log_access ("User");
757 #endif /* GETLOADAVG_PRIVILEGED */
760 /* Give the process appropriate permissions for access to
761 make data (i.e., the load average). */
762 void
763 make_access ()
765 #ifdef GETLOADAVG_PRIVILEGED
767 if (!access_inited)
768 init_access ();
770 if (current_access == make)
771 return;
773 /* See comments in user_access, above. */
775 #ifdef HAVE_SETEUID
776 if (seteuid (make_uid) < 0)
777 pfatal_with_name ("make_access: seteuid");
778 #else
779 #ifndef HAVE_SETREUID
780 if (setuid (make_uid) < 0)
781 pfatal_with_name ("make_access: setuid");
782 #else
783 if (setreuid (user_uid, make_uid) < 0)
784 pfatal_with_name ("make_access: setreuid");
785 #endif
786 #endif
788 #ifdef HAVE_SETEGID
789 if (setegid (make_gid) < 0)
790 pfatal_with_name ("make_access: setegid");
791 #else
792 #ifndef HAVE_SETREGID
793 if (setgid (make_gid) < 0)
794 pfatal_with_name ("make_access: setgid");
795 #else
796 if (setregid (user_gid, make_gid) < 0)
797 pfatal_with_name ("make_access: setregid");
798 #endif
799 #endif
801 current_access = make;
803 log_access ("Make");
805 #endif /* GETLOADAVG_PRIVILEGED */
808 /* Give the process appropriate permissions for a child process.
809 This is like user_access, but you can't get back to make_access. */
810 void
811 child_access ()
813 #ifdef GETLOADAVG_PRIVILEGED
815 if (!access_inited)
816 abort ();
818 /* Set both the real and effective UID and GID to the user's.
819 They cannot be changed back to make's. */
821 #ifndef HAVE_SETREUID
822 if (setuid (user_uid) < 0)
823 pfatal_with_name ("child_access: setuid");
824 #else
825 if (setreuid (user_uid, user_uid) < 0)
826 pfatal_with_name ("child_access: setreuid");
827 #endif
829 #ifndef HAVE_SETREGID
830 if (setgid (user_gid) < 0)
831 pfatal_with_name ("child_access: setgid");
832 #else
833 if (setregid (user_gid, user_gid) < 0)
834 pfatal_with_name ("child_access: setregid");
835 #endif
837 log_access ("Child");
839 #endif /* GETLOADAVG_PRIVILEGED */
842 #ifdef NEED_GET_PATH_MAX
843 unsigned int
844 get_path_max ()
846 static unsigned int value;
848 if (value == 0)
850 long int x = pathconf ("/", _PC_PATH_MAX);
851 if (x > 0)
852 value = x;
853 else
854 return MAXPATHLEN;
857 return value;
859 #endif