Fix eval bugs 1516 and 1517.
[make/kirr.git] / misc.c
blob5305220f9c79e3e2ea485d9517303ae941f96555
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)
9 any later version.
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. */
21 #include "make.h"
22 #include "dep.h"
23 #include "debug.h"
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
35 # if __STDC__
36 # include <stdarg.h>
37 # define VA_START(args, lastarg) va_start(args, lastarg)
38 # else
39 # include <varargs.h>
40 # define VA_START(args, lastarg) va_start(args)
41 # endif
42 # if HAVE_VPRINTF
43 # define VA_PRINTF(fp, lastarg, args) vfprintf((fp), (lastarg), (args))
44 # else
45 # define VA_PRINTF(fp, lastarg, args) _doprnt((lastarg), (args), (fp))
46 # endif
47 # define VA_END(args) va_end(args)
48 #else
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)
54 # define VA_END(args)
55 #endif
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. */
62 int
63 alpha_compare (const void *v1, const void *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 (char *line)
80 register char *in, *out, *p;
81 register int backslash;
82 register unsigned int bs_write;
84 in = strchr (line, '\n');
85 if (in == 0)
86 return;
88 out = in;
89 while (out > line && out[-1] == '\\')
90 --out;
92 while (*in != '\0')
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. */
97 backslash = 0;
98 bs_write = 0;
99 for (p = in - 1; p >= line && *p == '\\'; --p)
101 if (backslash)
102 ++bs_write;
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. */
107 if (in == out - 1)
108 abort ();
111 /* Output the appropriate number of backslashes. */
112 while (bs_write-- > 0)
113 *out++ = '\\';
115 /* Skip the newline. */
116 ++in;
118 /* If the newline is quoted, discard following whitespace
119 and any preceding whitespace; leave just one space. */
120 if (backslash)
122 in = next_token (in);
123 while (out > line && isblank ((unsigned char)out[-1]))
124 --out;
125 *out++ = ' ';
127 else
128 /* If the newline isn't quoted, put it in the output. */
129 *out++ = '\n';
131 /* Now copy the following line to the output.
132 Stop when we find backslashes followed by a newline. */
133 while (*in != '\0')
134 if (*in == '\\')
136 p = in + 1;
137 while (*p == '\\')
138 ++p;
139 if (*p == '\n')
141 in = p;
142 break;
144 while (in < p)
145 *out++ = *in++;
147 else
148 *out++ = *in++;
151 *out = '\0';
155 /* Remove comments from LINE.
156 This is done by copying the text at LINE onto itself. */
158 void
159 remove_comments (char *line)
161 char *comment;
163 comment = find_char_unquote (line, '#', 0, 0);
165 if (comment != 0)
166 /* Cut off the line at the #. */
167 *comment = '\0';
170 /* Print N spaces (used in debug for target-depth). */
172 void
173 print_spaces (unsigned int n)
175 while (n-- > 0)
176 putchar (' ');
180 /* Return a newly-allocated string whose contents
181 concatenate those of s1, s2, s3. */
183 char *
184 concat (const char *s1, const char *s2, const char *s3)
186 unsigned int len1, len2, len3;
187 char *result;
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);
195 if (*s1 != '\0')
196 bcopy (s1, result, len1);
197 if (*s2 != '\0')
198 bcopy (s2, result + len1, len2);
199 if (*s3 != '\0')
200 bcopy (s3, result + len1 + len2, len3);
201 *(result + len1 + len2 + len3) = '\0';
203 return result;
206 /* Print a message on stdout. */
208 void
209 #if __STDC__ && HAVE_STDVARARGS
210 message (int prefix, const char *fmt, ...)
211 #else
212 message (prefix, fmt, va_alist)
213 int prefix;
214 const char *fmt;
215 va_dcl
216 #endif
218 #if HAVE_STDVARARGS
219 va_list args;
220 #endif
222 log_working_directory (1);
224 if (fmt != 0)
226 if (prefix)
228 if (makelevel == 0)
229 printf ("%s: ", program);
230 else
231 printf ("%s[%u]: ", program, makelevel);
233 VA_START (args, fmt);
234 VA_PRINTF (stdout, fmt, args);
235 VA_END (args);
236 putchar ('\n');
239 fflush (stdout);
242 /* Print an error message. */
244 void
245 #if __STDC__ && HAVE_STDVARARGS
246 error (const struct floc *flocp, const char *fmt, ...)
247 #else
248 error (flocp, fmt, va_alist)
249 const struct floc *flocp;
250 const char *fmt;
251 va_dcl
252 #endif
254 #if HAVE_STDVARARGS
255 va_list args;
256 #endif
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);
264 else
265 fprintf (stderr, "%s[%u]: ", program, makelevel);
267 VA_START(args, fmt);
268 VA_PRINTF (stderr, fmt, args);
269 VA_END (args);
271 putc ('\n', stderr);
272 fflush (stderr);
275 /* Print an error message and exit. */
277 void
278 #if __STDC__ && HAVE_STDVARARGS
279 fatal (const struct floc *flocp, const char *fmt, ...)
280 #else
281 fatal (flocp, fmt, va_alist)
282 const struct floc *flocp;
283 const char *fmt;
284 va_dcl
285 #endif
287 #if HAVE_STDVARARGS
288 va_list args;
289 #endif
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);
297 else
298 fprintf (stderr, "%s[%u]: *** ", program, makelevel);
300 VA_START(args, fmt);
301 VA_PRINTF (stderr, fmt, args);
302 VA_END (args);
304 fputs (_(". Stop.\n"), stderr);
306 die (2);
309 #ifndef HAVE_STRERROR
311 #undef strerror
313 char *
314 strerror (int errnum)
316 extern int errno, sys_nerr;
317 #ifndef __DECC
318 extern char *sys_errlist[];
319 #endif
320 static char buf[] = "Unknown error 12345678901234567890";
322 if (errno < sys_nerr)
323 return sys_errlist[errnum];
325 sprintf (buf, _("Unknown error %d"), errnum);
326 return buf;
328 #endif
330 /* Print an error message from errno. */
332 void
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. */
340 void
341 pfatal_with_name (const char *name)
343 fatal (NILF, _("%s: %s"), name, strerror (errno));
345 /* NOTREACHED */
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
353 #undef xmalloc
354 #undef xrealloc
355 #undef xstrdup
357 char *
358 xmalloc (unsigned int size)
360 char *result = (char *) malloc (size);
361 if (result == 0)
362 fatal (NILF, _("virtual memory exhausted"));
363 return result;
367 char *
368 xrealloc (char *ptr, unsigned int size)
370 char *result;
372 /* Some older implementations of realloc() don't conform to ANSI. */
373 result = ptr ? realloc (ptr, size) : malloc (size);
374 if (result == 0)
375 fatal (NILF, _("virtual memory exhausted"));
376 return result;
380 char *
381 xstrdup (const char *ptr)
383 char *result;
385 #ifdef HAVE_STRDUP
386 result = strdup (ptr);
387 #else
388 result = (char *) malloc (strlen (ptr) + 1);
389 #endif
391 if (result == 0)
392 fatal (NILF, _("virtual memory exhausted"));
394 #ifdef HAVE_STRDUP
395 return result;
396 #else
397 return strcpy(result, ptr);
398 #endif
401 #endif /* HAVE_DMALLOC_H */
403 char *
404 savestring (const char *str, unsigned int length)
406 register char *out = (char *) xmalloc (length + 1);
407 if (length > 0)
408 bcopy (str, out, length);
409 out[length] = '\0';
410 return out;
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. */
417 char *
418 sindex (const char *big, unsigned int blen,
419 const char *small, unsigned int slen)
421 if (!blen)
422 blen = strlen (big);
423 if (!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. */
431 --slen;
432 blen -= slen;
434 for (b = 0; b < blen; ++b, ++big)
435 if (*big == *small && strneq (big + 1, small + 1, slen))
436 return (char *)big;
439 return 0;
442 /* Limited INDEX:
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. */
448 char *
449 lindex (const char *s, const char *limit, int c)
451 while (s < limit)
452 if (*s++ == c)
453 return (char *)(s - 1);
455 return 0;
458 /* Return the address of the first whitespace or null in the string S. */
460 char *
461 end_of_token (char *s)
463 while (*s != '\0' && !isblank ((unsigned char)*s))
464 ++s;
465 return s;
468 #ifdef WINDOWS32
470 * Same as end_of_token, but take into account a stop character
472 char *
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)))
481 if (*p++ == '\\')
483 backslash = !backslash;
484 while (*p == '\\')
486 backslash = !backslash;
487 ++p;
490 else
491 backslash = 0;
494 return p;
496 #endif
498 /* Return the address of the first nonwhitespace or null in the string S. */
500 char *
501 next_token (const char *s)
503 while (isblank ((unsigned char)*s))
504 ++s;
505 return (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. */
511 char *
512 find_next_token (char **ptr, unsigned int *lengthptr)
514 char *p = next_token (*ptr);
515 char *end;
517 if (*p == '\0')
518 return 0;
520 *ptr = end = end_of_token (p);
521 if (lengthptr != 0)
522 *lengthptr = end - p;
523 return p;
526 /* Copy a chain of `struct dep', making a new chain
527 with the same contents as the old one. */
529 struct dep *
530 copy_dep_chain (struct dep *d)
532 register struct dep *c;
533 struct dep *firstnew = 0;
534 struct dep *lastnew = 0;
536 while (d != 0)
538 c = (struct dep *) xmalloc (sizeof (struct dep));
539 bcopy ((char *) d, (char *) c, sizeof (struct dep));
540 if (c->name != 0)
541 c->name = xstrdup (c->name);
542 c->next = 0;
543 if (firstnew == 0)
544 firstnew = lastnew = c;
545 else
546 lastnew = lastnew->next = c;
548 d = d->next;
551 return firstnew;
554 #ifdef iAPX286
555 /* The losing compiler on this machine can't handle this macro. */
557 char *
558 dep_name (struct dep *dep)
560 return dep->name == 0 ? dep->file->name : dep->name;
562 #endif
564 #ifdef GETLOADAVG_PRIVILEGED
566 #ifdef POSIX
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
572 way works. */
574 #undef HAVE_SETREUID
575 #undef HAVE_SETREGID
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. */
584 #undef HAVE_SETEUID
585 #undef HAVE_SETEGID
587 #endif /* POSIX. */
589 #ifndef HAVE_UNISTD_H
590 extern int getuid (), getgid (), geteuid (), getegid ();
591 extern int setuid (), setgid ();
592 #ifdef HAVE_SETEUID
593 extern int seteuid ();
594 #else
595 #ifdef HAVE_SETREUID
596 extern int setreuid ();
597 #endif /* Have setreuid. */
598 #endif /* Have seteuid. */
599 #ifdef HAVE_SETEGID
600 extern int setegid ();
601 #else
602 #ifdef HAVE_SETREGID
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. */
616 static void
617 log_access (char *flavor)
619 if (! ISDB (DB_JOBS))
620 return;
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 ());
629 fflush (stderr);
633 static void
634 init_access (void)
636 #ifndef VMS
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;
650 #endif
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). */
657 void
658 user_access (void)
660 #ifdef GETLOADAVG_PRIVILEGED
662 if (!access_inited)
663 init_access ();
665 if (current_access == user)
666 return;
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. */
673 #ifdef HAVE_SETEUID
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. */
711 #ifdef HAVE_SETEGID
712 if (setegid (user_gid) < 0)
713 pfatal_with_name ("user_access: setegid");
714 #else
715 #ifndef HAVE_SETREGID
716 if (setgid (user_gid) < 0)
717 pfatal_with_name ("user_access: setgid");
718 #else
719 if (setregid (make_gid, user_gid) < 0)
720 pfatal_with_name ("user_access: setregid");
721 #endif
722 #endif
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). */
733 void
734 make_access (void)
736 #ifdef GETLOADAVG_PRIVILEGED
738 if (!access_inited)
739 init_access ();
741 if (current_access == make)
742 return;
744 /* See comments in user_access, above. */
746 #ifdef HAVE_SETEUID
747 if (seteuid (make_uid) < 0)
748 pfatal_with_name ("make_access: seteuid");
749 #else
750 #ifndef HAVE_SETREUID
751 if (setuid (make_uid) < 0)
752 pfatal_with_name ("make_access: setuid");
753 #else
754 if (setreuid (user_uid, make_uid) < 0)
755 pfatal_with_name ("make_access: setreuid");
756 #endif
757 #endif
759 #ifdef HAVE_SETEGID
760 if (setegid (make_gid) < 0)
761 pfatal_with_name ("make_access: setegid");
762 #else
763 #ifndef HAVE_SETREGID
764 if (setgid (make_gid) < 0)
765 pfatal_with_name ("make_access: setgid");
766 #else
767 if (setregid (user_gid, make_gid) < 0)
768 pfatal_with_name ("make_access: setregid");
769 #endif
770 #endif
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. */
781 void
782 child_access (void)
784 #ifdef GETLOADAVG_PRIVILEGED
786 if (!access_inited)
787 abort ();
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");
795 #else
796 if (setreuid (user_uid, user_uid) < 0)
797 pfatal_with_name ("child_access: setreuid");
798 #endif
800 #ifndef HAVE_SETREGID
801 if (setgid (user_gid) < 0)
802 pfatal_with_name ("child_access: setgid");
803 #else
804 if (setregid (user_gid, user_gid) < 0)
805 pfatal_with_name ("child_access: setregid");
806 #endif
808 log_access (_("Child access"));
810 #endif /* GETLOADAVG_PRIVILEGED */
813 #ifdef NEED_GET_PATH_MAX
814 unsigned int
815 get_path_max (void)
817 static unsigned int value;
819 if (value == 0)
821 long int x = pathconf ("/", _PC_PATH_MAX);
822 if (x > 0)
823 value = x;
824 else
825 return MAXPATHLEN;
828 return value;
830 #endif
833 #ifdef HAVE_BROKEN_RESTART
835 #undef stat
836 #undef readdir
839 atomic_stat (const char *file, struct stat *buf)
841 int r;
843 while ((r = stat (file, buf)) < 0)
844 if (errno != EINTR)
845 break;
847 return r;
850 struct dirent *
851 atomic_readdir (DIR *dir)
853 struct dirent *r;
855 while ((r = readdir (dir)) == NULL)
856 if (errno != EINTR)
857 break;
859 return r;
862 #endif /* HAVE_BROKEN_RESTART */