No empty .Rs/.Re
[netbsd-mini2440.git] / gnu / dist / gmake / misc.c
blob7f0b1b30f44b9eeec8fb0f0dd5b52db0517415ea
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 (v1, v2)
64 const void *v1, *v2;
66 const char *s1 = *((char **)v1);
67 const char *s2 = *((char **)v2);
69 if (*s1 != *s2)
70 return *s1 - *s2;
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. */
78 void
79 collapse_continuations (line)
80 char *line;
82 register char *in, *out, *p;
83 register int backslash;
84 register unsigned int bs_write;
86 in = strchr (line, '\n');
87 if (in == 0)
88 return;
90 out = in;
91 while (out > line && out[-1] == '\\')
92 --out;
94 while (*in != '\0')
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. */
99 backslash = 0;
100 bs_write = 0;
101 for (p = in - 1; p >= line && *p == '\\'; --p)
103 if (backslash)
104 ++bs_write;
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. */
109 if (in == out - 1)
110 abort ();
113 /* Output the appropriate number of backslashes. */
114 while (bs_write-- > 0)
115 *out++ = '\\';
117 /* Skip the newline. */
118 ++in;
120 /* If the newline is quoted, discard following whitespace
121 and any preceding whitespace; leave just one space. */
122 if (backslash)
124 in = next_token (in);
125 while (out > line && isblank ((unsigned char)out[-1]))
126 --out;
127 *out++ = ' ';
129 else
130 /* If the newline isn't quoted, put it in the output. */
131 *out++ = '\n';
133 /* Now copy the following line to the output.
134 Stop when we find backslashes followed by a newline. */
135 while (*in != '\0')
136 if (*in == '\\')
138 p = in + 1;
139 while (*p == '\\')
140 ++p;
141 if (*p == '\n')
143 in = p;
144 break;
146 while (in < p)
147 *out++ = *in++;
149 else
150 *out++ = *in++;
153 *out = '\0';
157 /* Remove comments from LINE.
158 This is done by copying the text at LINE onto itself. */
160 void
161 remove_comments (line)
162 char *line;
164 char *comment;
166 comment = find_char_unquote (line, '#', 0, 0);
168 if (comment != 0)
169 /* Cut off the line at the #. */
170 *comment = '\0';
173 /* Print N spaces (used in debug for target-depth). */
175 void
176 print_spaces (n)
177 unsigned int n;
179 while (n-- > 0)
180 putchar (' ');
184 /* Return a newly-allocated string whose contents
185 concatenate those of s1, s2, s3. */
187 char *
188 concat (s1, s2, s3)
189 const char *s1, *s2, *s3;
191 unsigned int len1, len2, len3;
192 char *result;
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);
200 if (*s1 != '\0')
201 bcopy (s1, result, len1);
202 if (*s2 != '\0')
203 bcopy (s2, result + len1, len2);
204 if (*s3 != '\0')
205 bcopy (s3, result + len1 + len2, len3);
206 *(result + len1 + len2 + len3) = '\0';
208 return result;
211 /* Print a message on stdout. */
213 void
214 #if __STDC__ && HAVE_STDVARARGS
215 message (int prefix, const char *fmt, ...)
216 #else
217 message (prefix, fmt, va_alist)
218 int prefix;
219 const char *fmt;
220 va_dcl
221 #endif
223 #if HAVE_STDVARARGS
224 va_list args;
225 #endif
227 log_working_directory (1);
229 if (fmt != 0)
231 if (prefix)
233 if (makelevel == 0)
234 printf ("%s: ", program);
235 else
236 printf ("%s[%u]: ", program, makelevel);
238 VA_START (args, fmt);
239 VA_PRINTF (stdout, fmt, args);
240 VA_END (args);
241 putchar ('\n');
244 fflush (stdout);
247 /* Print an error message. */
249 void
250 #if __STDC__ && HAVE_STDVARARGS
251 error (const struct floc *flocp, const char *fmt, ...)
252 #else
253 error (flocp, fmt, va_alist)
254 const struct floc *flocp;
255 const char *fmt;
256 va_dcl
257 #endif
259 #if HAVE_STDVARARGS
260 va_list args;
261 #endif
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);
269 else
270 fprintf (stderr, "%s[%u]: ", program, makelevel);
272 VA_START(args, fmt);
273 VA_PRINTF (stderr, fmt, args);
274 VA_END (args);
276 putc ('\n', stderr);
277 fflush (stderr);
280 /* Print an error message and exit. */
282 void
283 #if __STDC__ && HAVE_STDVARARGS
284 fatal (const struct floc *flocp, const char *fmt, ...)
285 #else
286 fatal (flocp, fmt, va_alist)
287 const struct floc *flocp;
288 const char *fmt;
289 va_dcl
290 #endif
292 #if HAVE_STDVARARGS
293 va_list args;
294 #endif
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);
302 else
303 fprintf (stderr, "%s[%u]: *** ", program, makelevel);
305 VA_START(args, fmt);
306 VA_PRINTF (stderr, fmt, args);
307 VA_END (args);
309 fputs (_(". Stop.\n"), stderr);
311 die (2);
314 #ifndef HAVE_STRERROR
316 #undef strerror
318 char *
319 strerror (errnum)
320 int errnum;
322 extern int errno, sys_nerr;
323 #ifndef __DECC
324 extern char *sys_errlist[];
325 #endif
326 static char buf[] = "Unknown error 12345678901234567890";
328 if (errno < sys_nerr)
329 return sys_errlist[errnum];
331 sprintf (buf, _("Unknown error %d"), errnum);
332 return buf;
334 #endif
336 /* Print an error message from errno. */
338 void
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. */
347 void
348 pfatal_with_name (name)
349 const char *name;
351 fatal (NILF, _("%s: %s"), name, strerror (errno));
353 /* NOTREACHED */
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
361 #undef xmalloc
362 #undef xrealloc
363 #undef xstrdup
365 char *
366 xmalloc (size)
367 unsigned int size;
369 char *result = (char *) malloc (size);
370 if (result == 0)
371 fatal (NILF, _("virtual memory exhausted"));
372 return result;
376 char *
377 xrealloc (ptr, size)
378 char *ptr;
379 unsigned int size;
381 char *result;
383 /* Some older implementations of realloc() don't conform to ANSI. */
384 result = ptr ? realloc (ptr, size) : malloc (size);
385 if (result == 0)
386 fatal (NILF, _("virtual memory exhausted"));
387 return result;
391 char *
392 xstrdup (ptr)
393 const char *ptr;
395 char *result;
397 #ifdef HAVE_STRDUP
398 result = strdup (ptr);
399 #else
400 result = (char *) malloc (strlen (ptr) + 1);
401 #endif
403 if (result == 0)
404 fatal (NILF, _("virtual memory exhausted"));
406 #ifdef HAVE_STRDUP
407 return result;
408 #else
409 return strcpy(result, ptr);
410 #endif
413 #endif /* HAVE_DMALLOC_H */
415 char *
416 savestring (str, length)
417 const char *str;
418 unsigned int length;
420 register char *out = (char *) xmalloc (length + 1);
421 if (length > 0)
422 bcopy (str, out, length);
423 out[length] = '\0';
424 return out;
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. */
431 char *
432 sindex (big, blen, small, slen)
433 const char *big;
434 unsigned int blen;
435 const char *small;
436 unsigned int slen;
438 if (!blen)
439 blen = strlen (big);
440 if (!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. */
448 --slen;
449 blen -= slen;
451 for (b = 0; b < blen; ++b, ++big)
452 if (*big == *small && strneq (big + 1, small + 1, slen))
453 return (char *)big;
456 return 0;
459 /* Limited INDEX:
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. */
465 char *
466 lindex (s, limit, c)
467 register const char *s, *limit;
468 int c;
470 while (s < limit)
471 if (*s++ == c)
472 return (char *)(s - 1);
474 return 0;
477 /* Return the address of the first whitespace or null in the string S. */
479 char *
480 end_of_token (s)
481 char *s;
483 while (*s != '\0' && !isblank ((unsigned char)*s))
484 ++s;
485 return s;
488 #ifdef WINDOWS32
490 * Same as end_of_token, but take into account a stop character
492 char *
493 end_of_token_w32 (s, stopchar)
494 char *s;
495 char stopchar;
497 register char *p = s;
498 register int backslash = 0;
500 while (*p != '\0' && *p != stopchar
501 && (backslash || !isblank ((unsigned char)*p)))
503 if (*p++ == '\\')
505 backslash = !backslash;
506 while (*p == '\\')
508 backslash = !backslash;
509 ++p;
512 else
513 backslash = 0;
516 return p;
518 #endif
520 /* Return the address of the first nonwhitespace or null in the string S. */
522 char *
523 next_token (s)
524 const char *s;
526 while (isblank ((unsigned char)*s))
527 ++s;
528 return (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. */
534 char *
535 find_next_token (ptr, lengthptr)
536 char **ptr;
537 unsigned int *lengthptr;
539 char *p = next_token (*ptr);
540 char *end;
542 if (*p == '\0')
543 return 0;
545 *ptr = end = end_of_token (p);
546 if (lengthptr != 0)
547 *lengthptr = end - p;
548 return p;
551 /* Copy a chain of `struct dep', making a new chain
552 with the same contents as the old one. */
554 struct dep *
555 copy_dep_chain (d)
556 register struct dep *d;
558 register struct dep *c;
559 struct dep *firstnew = 0;
560 struct dep *lastnew = 0;
562 while (d != 0)
564 c = (struct dep *) xmalloc (sizeof (struct dep));
565 bcopy ((char *) d, (char *) c, sizeof (struct dep));
566 if (c->name != 0)
567 c->name = xstrdup (c->name);
568 c->next = 0;
569 if (firstnew == 0)
570 firstnew = lastnew = c;
571 else
572 lastnew = lastnew->next = c;
574 d = d->next;
577 return firstnew;
580 #ifdef iAPX286
581 /* The losing compiler on this machine can't handle this macro. */
583 char *
584 dep_name (dep)
585 struct dep *dep;
587 return dep->name == 0 ? dep->file->name : dep->name;
589 #endif
591 #ifdef GETLOADAVG_PRIVILEGED
593 #ifdef POSIX
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
599 way works. */
601 #undef HAVE_SETREUID
602 #undef HAVE_SETREGID
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. */
611 #undef HAVE_SETEUID
612 #undef HAVE_SETEGID
614 #endif /* POSIX. */
616 #ifndef HAVE_UNISTD_H
617 extern int getuid (), getgid (), geteuid (), getegid ();
618 extern int setuid (), setgid ();
619 #ifdef HAVE_SETEUID
620 extern int seteuid ();
621 #else
622 #ifdef HAVE_SETREUID
623 extern int setreuid ();
624 #endif /* Have setreuid. */
625 #endif /* Have seteuid. */
626 #ifdef HAVE_SETEGID
627 extern int setegid ();
628 #else
629 #ifdef HAVE_SETREGID
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. */
643 static void
644 log_access (flavor)
645 char *flavor;
647 if (! ISDB (DB_JOBS))
648 return;
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 ());
657 fflush (stderr);
661 static void
662 init_access ()
664 #ifndef VMS
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;
678 #endif
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). */
685 void
686 user_access ()
688 #ifdef GETLOADAVG_PRIVILEGED
690 if (!access_inited)
691 init_access ();
693 if (current_access == user)
694 return;
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. */
701 #ifdef HAVE_SETEUID
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. */
739 #ifdef HAVE_SETEGID
740 if (setegid (user_gid) < 0)
741 pfatal_with_name ("user_access: setegid");
742 #else
743 #ifndef HAVE_SETREGID
744 if (setgid (user_gid) < 0)
745 pfatal_with_name ("user_access: setgid");
746 #else
747 if (setregid (make_gid, user_gid) < 0)
748 pfatal_with_name ("user_access: setregid");
749 #endif
750 #endif
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). */
761 void
762 make_access ()
764 #ifdef GETLOADAVG_PRIVILEGED
766 if (!access_inited)
767 init_access ();
769 if (current_access == make)
770 return;
772 /* See comments in user_access, above. */
774 #ifdef HAVE_SETEUID
775 if (seteuid (make_uid) < 0)
776 pfatal_with_name ("make_access: seteuid");
777 #else
778 #ifndef HAVE_SETREUID
779 if (setuid (make_uid) < 0)
780 pfatal_with_name ("make_access: setuid");
781 #else
782 if (setreuid (user_uid, make_uid) < 0)
783 pfatal_with_name ("make_access: setreuid");
784 #endif
785 #endif
787 #ifdef HAVE_SETEGID
788 if (setegid (make_gid) < 0)
789 pfatal_with_name ("make_access: setegid");
790 #else
791 #ifndef HAVE_SETREGID
792 if (setgid (make_gid) < 0)
793 pfatal_with_name ("make_access: setgid");
794 #else
795 if (setregid (user_gid, make_gid) < 0)
796 pfatal_with_name ("make_access: setregid");
797 #endif
798 #endif
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. */
809 void
810 child_access ()
812 #ifdef GETLOADAVG_PRIVILEGED
814 if (!access_inited)
815 abort ();
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");
823 #else
824 if (setreuid (user_uid, user_uid) < 0)
825 pfatal_with_name ("child_access: setreuid");
826 #endif
828 #ifndef HAVE_SETREGID
829 if (setgid (user_gid) < 0)
830 pfatal_with_name ("child_access: setgid");
831 #else
832 if (setregid (user_gid, user_gid) < 0)
833 pfatal_with_name ("child_access: setregid");
834 #endif
836 log_access (_("Child access"));
838 #endif /* GETLOADAVG_PRIVILEGED */
841 #ifdef NEED_GET_PATH_MAX
842 unsigned int
843 get_path_max ()
845 static unsigned int value;
847 if (value == 0)
849 long int x = pathconf ("/", _PC_PATH_MAX);
850 if (x > 0)
851 value = x;
852 else
853 return MAXPATHLEN;
856 return value;
858 #endif
861 #ifdef HAVE_BROKEN_RESTART
863 #undef stat
864 #undef readdir
867 atomic_stat(file, buf)
868 const char *file;
869 struct stat *buf;
871 int r;
873 while ((r = stat (file, buf)) < 0)
874 if (errno != EINTR)
875 break;
877 return r;
880 struct dirent *
881 atomic_readdir(dir)
882 DIR *dir;
884 struct dirent *r;
886 while ((r = readdir (dir)) == NULL)
887 if (errno != EINTR)
888 break;
890 return r;
893 #endif /* HAVE_BROKEN_RESTART */