* New feature: .LIBPATTERNS controls the way -lfoo dependencies are expanded.
[make/kirr.git] / misc.c
blob0612862bf43e43d514c1abac705324cb0ab027b1
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, 675 Mass Ave, Cambridge, MA 02139, USA. */
19 #include "make.h"
20 #include "dep.h"
23 /* Variadic functions. We go through contortions to allow proper function
24 prototypes for both ANSI and pre-ANSI C compilers, and also for those
25 which support stdarg.h vs. varargs.h, and finally those which have
26 vfprintf(), etc. and those who have _doprnt... or nothing.
28 This fancy stuff all came from GNU fileutils, except for the VA_PRINTF and
29 VA_END macros used here since we have multiple print functions. */
31 #if HAVE_VPRINTF || HAVE_DOPRNT
32 # define HAVE_STDVARARGS 1
33 # if __STDC__
34 # include <stdarg.h>
35 # define VA_START(args, lastarg) va_start(args, lastarg)
36 # else
37 # include <varargs.h>
38 # define VA_START(args, lastarg) va_start(args)
39 # endif
40 # if HAVE_VPRINTF
41 # define VA_PRINTF(fp, lastarg, args) vfprintf((fp), (lastarg), (args))
42 # else
43 # define VA_PRINTF(fp, lastarg, args) _doprnt((lastarg), (args), (fp))
44 # endif
45 # define VA_END(args) va_end(args)
46 #else
47 /* # undef HAVE_STDVARARGS */
48 # define va_alist a1, a2, a3, a4, a5, a6, a7, a8
49 # define va_dcl char *a1, *a2, *a3, *a4, *a5, *a6, *a7, *a8;
50 # define VA_START(args, lastarg)
51 # define VA_END(args)
52 #endif
55 /* Compare strings *S1 and *S2.
56 Return negative if the first is less, positive if it is greater,
57 zero if they are equal. */
59 int
60 alpha_compare (v1, v2)
61 const void *v1, *v2;
63 const char *s1 = *((char **)v1);
64 const char *s2 = *((char **)v2);
66 if (*s1 != *s2)
67 return *s1 - *s2;
68 return strcmp (s1, s2);
71 /* Discard each backslash-newline combination from LINE.
72 Backslash-backslash-newline combinations become backslash-newlines.
73 This is done by copying the text at LINE into itself. */
75 void
76 collapse_continuations (line)
77 char *line;
79 register char *in, *out, *p;
80 register int backslash;
81 register unsigned int bs_write;
83 in = index (line, '\n');
84 if (in == 0)
85 return;
87 out = in;
88 while (out > line && out[-1] == '\\')
89 --out;
91 while (*in != '\0')
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. */
96 backslash = 0;
97 bs_write = 0;
98 for (p = in - 1; p >= line && *p == '\\'; --p)
100 if (backslash)
101 ++bs_write;
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. */
106 if (in == out - 1)
107 abort ();
110 /* Output the appropriate number of backslashes. */
111 while (bs_write-- > 0)
112 *out++ = '\\';
114 /* Skip the newline. */
115 ++in;
117 /* If the newline is quoted, discard following whitespace
118 and any preceding whitespace; leave just one space. */
119 if (backslash)
121 in = next_token (in);
122 while (out > line && isblank (out[-1]))
123 --out;
124 *out++ = ' ';
126 else
127 /* If the newline isn't quoted, put it in the output. */
128 *out++ = '\n';
130 /* Now copy the following line to the output.
131 Stop when we find backslashes followed by a newline. */
132 while (*in != '\0')
133 if (*in == '\\')
135 p = in + 1;
136 while (*p == '\\')
137 ++p;
138 if (*p == '\n')
140 in = p;
141 break;
143 while (in < p)
144 *out++ = *in++;
146 else
147 *out++ = *in++;
150 *out = '\0';
154 /* Remove comments from LINE.
155 This is done by copying the text at LINE onto itself. */
157 void
158 remove_comments (line)
159 char *line;
161 char *comment;
163 comment = find_char_unquote (line, "#", 0);
165 if (comment != 0)
166 /* Cut off the line at the #. */
167 *comment = '\0';
170 /* Print N spaces (used by DEBUGPR for target-depth). */
172 void
173 print_spaces (n)
174 register unsigned int n;
176 while (n-- > 0)
177 putchar (' ');
181 /* Return a newly-allocated string whose contents
182 concatenate those of s1, s2, s3. */
184 char *
185 concat (s1, s2, s3)
186 register char *s1, *s2, *s3;
188 register unsigned int len1, len2, len3;
189 register char *result;
191 len1 = *s1 != '\0' ? strlen (s1) : 0;
192 len2 = *s2 != '\0' ? strlen (s2) : 0;
193 len3 = *s3 != '\0' ? strlen (s3) : 0;
195 result = (char *) xmalloc (len1 + len2 + len3 + 1);
197 if (*s1 != '\0')
198 bcopy (s1, result, len1);
199 if (*s2 != '\0')
200 bcopy (s2, result + len1, len2);
201 if (*s3 != '\0')
202 bcopy (s3, result + len1 + len2, len3);
203 *(result + len1 + len2 + len3) = '\0';
205 return result;
208 /* Print a message on stdout. */
210 void
211 #if __STDC__ && HAVE_STDVARARGS
212 message (int prefix, const char *fmt, ...)
213 #else
214 message (prefix, fmt, va_alist)
215 int prefix;
216 const char *fmt;
217 va_dcl
218 #endif
220 #if HAVE_STDVARARGS
221 va_list args;
222 #endif
224 log_working_directory (1);
226 if (fmt != 0)
228 if (prefix)
230 if (makelevel == 0)
231 printf ("%s: ", program);
232 else
233 printf ("%s[%u]: ", program, makelevel);
235 VA_START (args, fmt);
236 VA_PRINTF (stdout, fmt, args);
237 VA_END (args);
238 putchar ('\n');
241 fflush (stdout);
244 /* Print an error message. */
246 void
247 #if __STDC__ && HAVE_STDVARARGS
248 error (const struct floc *flocp, const char *fmt, ...)
249 #else
250 error (flocp, fmt, va_alist)
251 const struct floc *flocp;
252 const char *fmt;
253 va_dcl
254 #endif
256 #if HAVE_STDVARARGS
257 va_list args;
258 #endif
260 log_working_directory (1);
262 if (flocp && flocp->filenm)
263 fprintf (stderr, "%s:%lu: ", flocp->filenm, flocp->lineno);
264 else if (makelevel == 0)
265 fprintf (stderr, "%s: ", program);
266 else
267 fprintf (stderr, "%s[%u]: ", program, makelevel);
269 VA_START(args, fmt);
270 VA_PRINTF (stderr, fmt, args);
271 VA_END (args);
273 putc ('\n', stderr);
274 fflush (stderr);
277 /* Print an error message and exit. */
279 void
280 #if __STDC__ && HAVE_STDVARARGS
281 fatal (const struct floc *flocp, const char *fmt, ...)
282 #else
283 fatal (flocp, fmt, va_alist)
284 const struct floc *flocp;
285 const char *fmt;
286 va_dcl
287 #endif
289 #if HAVE_STDVARARGS
290 va_list args;
291 #endif
293 log_working_directory (1);
295 if (flocp && flocp->filenm)
296 fprintf (stderr, "%s:%lu: *** ", flocp->filenm, flocp->lineno);
297 else if (makelevel == 0)
298 fprintf (stderr, "%s: *** ", program);
299 else
300 fprintf (stderr, "%s[%u]: *** ", program, makelevel);
302 VA_START(args, fmt);
303 VA_PRINTF (stderr, fmt, args);
304 VA_END (args);
306 fputs (". Stop.\n", stderr);
308 die (2);
311 #ifndef HAVE_STRERROR
313 #undef strerror
315 char *
316 strerror (errnum)
317 int errnum;
319 extern int errno, sys_nerr;
320 #ifndef __DECC
321 extern char *sys_errlist[];
322 #endif
323 static char buf[] = "Unknown error 12345678901234567890";
325 if (errno < sys_nerr)
326 return sys_errlist[errnum];
328 sprintf (buf, "Unknown error %d", errnum);
329 return buf;
331 #endif
333 /* Print an error message from errno. */
335 void
336 perror_with_name (str, name)
337 char *str, *name;
339 error (NILF, "%s%s: %s", str, name, strerror (errno));
342 /* Print an error message from errno and exit. */
344 void
345 pfatal_with_name (name)
346 char *name;
348 fatal (NILF, "%s: %s", name, strerror (errno));
350 /* NOTREACHED */
353 /* Like malloc but get fatal error if memory is exhausted. */
355 #undef xmalloc
356 #undef xrealloc
358 char *
359 xmalloc (size)
360 unsigned int size;
362 char *result = (char *) malloc (size);
363 if (result == 0)
364 fatal (NILF, "virtual memory exhausted");
365 return result;
369 char *
370 xrealloc (ptr, size)
371 char *ptr;
372 unsigned int size;
374 char *result = (char *) realloc (ptr, size);
375 if (result == 0)
376 fatal (NILF, "virtual memory exhausted");
377 return result;
380 char *
381 savestring (str, length)
382 char *str;
383 unsigned int length;
385 register char *out = (char *) xmalloc (length + 1);
386 if (length > 0)
387 bcopy (str, out, length);
388 out[length] = '\0';
389 return out;
392 /* Search string BIG (length BLEN) for an occurrence of
393 string SMALL (length SLEN). Return a pointer to the
394 beginning of the first occurrence, or return nil if none found. */
396 char *
397 sindex (big, blen, small, slen)
398 char *big;
399 unsigned int blen;
400 char *small;
401 unsigned int slen;
403 register unsigned int b;
405 if (blen < 1)
406 blen = strlen (big);
407 if (slen < 1)
408 slen = strlen (small);
410 for (b = 0; b < blen; ++b)
411 if (big[b] == *small && !strncmp (&big[b + 1], small + 1, slen - 1))
412 return (&big[b]);
414 return 0;
417 /* Limited INDEX:
418 Search through the string STRING, which ends at LIMIT, for the character C.
419 Returns a pointer to the first occurrence, or nil if none is found.
420 Like INDEX except that the string searched ends where specified
421 instead of at the first null. */
423 char *
424 lindex (s, limit, c)
425 register char *s, *limit;
426 int c;
428 while (s < limit)
429 if (*s++ == c)
430 return s - 1;
432 return 0;
435 /* Return the address of the first whitespace or null in the string S. */
437 char *
438 end_of_token (s)
439 char *s;
441 while (*s != '\0' && !isblank (*s))
442 ++s;
443 return s;
446 #ifdef WINDOWS32
448 * Same as end_of_token, but take into account a stop character
450 char *
451 end_of_token_w32 (s, stopchar)
452 char *s;
453 char stopchar;
455 register char *p = s;
456 register int backslash = 0;
458 while (*p != '\0' && *p != stopchar && (backslash || !isblank (*p)))
460 if (*p++ == '\\')
462 backslash = !backslash;
463 while (*p == '\\')
465 backslash = !backslash;
466 ++p;
469 else
470 backslash = 0;
473 return p;
475 #endif
477 /* Return the address of the first nonwhitespace or null in the string S. */
479 char *
480 next_token (s)
481 char *s;
483 register char *p = s;
485 while (isblank (*p))
486 ++p;
487 return p;
490 /* Find the next token in PTR; return the address of it, and store the
491 length of the token into *LENGTHPTR if LENGTHPTR is not nil. */
493 char *
494 find_next_token (ptr, lengthptr)
495 char **ptr;
496 unsigned int *lengthptr;
498 char *p = next_token (*ptr);
499 char *end;
501 if (*p == '\0')
502 return 0;
504 *ptr = end = end_of_token (p);
505 if (lengthptr != 0)
506 *lengthptr = end - p;
507 return p;
510 /* Copy a chain of `struct dep', making a new chain
511 with the same contents as the old one. */
513 struct dep *
514 copy_dep_chain (d)
515 register struct dep *d;
517 register struct dep *c;
518 struct dep *firstnew = 0;
519 struct dep *lastnew = 0;
521 while (d != 0)
523 c = (struct dep *) xmalloc (sizeof (struct dep));
524 bcopy ((char *) d, (char *) c, sizeof (struct dep));
525 if (c->name != 0)
526 c->name = savestring (c->name, strlen (c->name));
527 c->next = 0;
528 if (firstnew == 0)
529 firstnew = lastnew = c;
530 else
531 lastnew = lastnew->next = c;
533 d = d->next;
536 return firstnew;
539 #ifdef iAPX286
540 /* The losing compiler on this machine can't handle this macro. */
542 char *
543 dep_name (dep)
544 struct dep *dep;
546 return dep->name == 0 ? dep->file->name : dep->name;
548 #endif
550 #ifdef GETLOADAVG_PRIVILEGED
552 #ifdef POSIX
554 /* Hopefully if a system says it's POSIX.1 and has the setuid and setgid
555 functions, they work as POSIX.1 says. Some systems (Alpha OSF/1 1.2,
556 for example) which claim to be POSIX.1 also have the BSD setreuid and
557 setregid functions, but they don't work as in BSD and only the POSIX.1
558 way works. */
560 #undef HAVE_SETREUID
561 #undef HAVE_SETREGID
563 #else /* Not POSIX. */
565 /* Some POSIX.1 systems have the seteuid and setegid functions. In a
566 POSIX-like system, they are the best thing to use. However, some
567 non-POSIX systems have them too but they do not work in the POSIX style
568 and we must use setreuid and setregid instead. */
570 #undef HAVE_SETEUID
571 #undef HAVE_SETEGID
573 #endif /* POSIX. */
575 #ifndef HAVE_UNISTD_H
576 extern int getuid (), getgid (), geteuid (), getegid ();
577 extern int setuid (), setgid ();
578 #ifdef HAVE_SETEUID
579 extern int seteuid ();
580 #else
581 #ifdef HAVE_SETREUID
582 extern int setreuid ();
583 #endif /* Have setreuid. */
584 #endif /* Have seteuid. */
585 #ifdef HAVE_SETEGID
586 extern int setegid ();
587 #else
588 #ifdef HAVE_SETREGID
589 extern int setregid ();
590 #endif /* Have setregid. */
591 #endif /* Have setegid. */
592 #endif /* No <unistd.h>. */
594 /* Keep track of the user and group IDs for user- and make- access. */
595 static int user_uid = -1, user_gid = -1, make_uid = -1, make_gid = -1;
596 #define access_inited (user_uid != -1)
597 static enum { make, user } current_access;
600 /* Under -d, write a message describing the current IDs. */
602 static void
603 log_access (flavor)
604 char *flavor;
606 if (! debug_flag)
607 return;
609 /* All the other debugging messages go to stdout,
610 but we write this one to stderr because it might be
611 run in a child fork whose stdout is piped. */
613 fprintf (stderr, "%s access: user %lu (real %lu), group %lu (real %lu)\n",
614 flavor, (unsigned long) geteuid (), (unsigned long) getuid (),
615 (unsigned long) getegid (), (unsigned long) getgid ());
616 fflush (stderr);
620 static void
621 init_access ()
623 #ifndef VMS
624 user_uid = getuid ();
625 user_gid = getgid ();
627 make_uid = geteuid ();
628 make_gid = getegid ();
630 /* Do these ever fail? */
631 if (user_uid == -1 || user_gid == -1 || make_uid == -1 || make_gid == -1)
632 pfatal_with_name ("get{e}[gu]id");
634 log_access ("Initialized");
636 current_access = make;
637 #endif
640 #endif /* GETLOADAVG_PRIVILEGED */
642 /* Give the process appropriate permissions for access to
643 user data (i.e., to stat files, or to spawn a child process). */
644 void
645 user_access ()
647 #ifdef GETLOADAVG_PRIVILEGED
649 if (!access_inited)
650 init_access ();
652 if (current_access == user)
653 return;
655 /* We are in "make access" mode. This means that the effective user and
656 group IDs are those of make (if it was installed setuid or setgid).
657 We now want to set the effective user and group IDs to the real IDs,
658 which are the IDs of the process that exec'd make. */
660 #ifdef HAVE_SETEUID
662 /* Modern systems have the seteuid/setegid calls which set only the
663 effective IDs, which is ideal. */
665 if (seteuid (user_uid) < 0)
666 pfatal_with_name ("user_access: seteuid");
668 #else /* Not HAVE_SETEUID. */
670 #ifndef HAVE_SETREUID
672 /* System V has only the setuid/setgid calls to set user/group IDs.
673 There is an effective ID, which can be set by setuid/setgid.
674 It can be set (unless you are root) only to either what it already is
675 (returned by geteuid/getegid, now in make_uid/make_gid),
676 the real ID (return by getuid/getgid, now in user_uid/user_gid),
677 or the saved set ID (what the effective ID was before this set-ID
678 executable (make) was exec'd). */
680 if (setuid (user_uid) < 0)
681 pfatal_with_name ("user_access: setuid");
683 #else /* HAVE_SETREUID. */
685 /* In 4BSD, the setreuid/setregid calls set both the real and effective IDs.
686 They may be set to themselves or each other. So you have two alternatives
687 at any one time. If you use setuid/setgid, the effective will be set to
688 the real, leaving only one alternative. Using setreuid/setregid, however,
689 you can toggle between your two alternatives by swapping the values in a
690 single setreuid or setregid call. */
692 if (setreuid (make_uid, user_uid) < 0)
693 pfatal_with_name ("user_access: setreuid");
695 #endif /* Not HAVE_SETREUID. */
696 #endif /* HAVE_SETEUID. */
698 #ifdef HAVE_SETEGID
699 if (setegid (user_gid) < 0)
700 pfatal_with_name ("user_access: setegid");
701 #else
702 #ifndef HAVE_SETREGID
703 if (setgid (user_gid) < 0)
704 pfatal_with_name ("user_access: setgid");
705 #else
706 if (setregid (make_gid, user_gid) < 0)
707 pfatal_with_name ("user_access: setregid");
708 #endif
709 #endif
711 current_access = user;
713 log_access ("User");
715 #endif /* GETLOADAVG_PRIVILEGED */
718 /* Give the process appropriate permissions for access to
719 make data (i.e., the load average). */
720 void
721 make_access ()
723 #ifdef GETLOADAVG_PRIVILEGED
725 if (!access_inited)
726 init_access ();
728 if (current_access == make)
729 return;
731 /* See comments in user_access, above. */
733 #ifdef HAVE_SETEUID
734 if (seteuid (make_uid) < 0)
735 pfatal_with_name ("make_access: seteuid");
736 #else
737 #ifndef HAVE_SETREUID
738 if (setuid (make_uid) < 0)
739 pfatal_with_name ("make_access: setuid");
740 #else
741 if (setreuid (user_uid, make_uid) < 0)
742 pfatal_with_name ("make_access: setreuid");
743 #endif
744 #endif
746 #ifdef HAVE_SETEGID
747 if (setegid (make_gid) < 0)
748 pfatal_with_name ("make_access: setegid");
749 #else
750 #ifndef HAVE_SETREGID
751 if (setgid (make_gid) < 0)
752 pfatal_with_name ("make_access: setgid");
753 #else
754 if (setregid (user_gid, make_gid) < 0)
755 pfatal_with_name ("make_access: setregid");
756 #endif
757 #endif
759 current_access = make;
761 log_access ("Make");
763 #endif /* GETLOADAVG_PRIVILEGED */
766 /* Give the process appropriate permissions for a child process.
767 This is like user_access, but you can't get back to make_access. */
768 void
769 child_access ()
771 #ifdef GETLOADAVG_PRIVILEGED
773 if (!access_inited)
774 abort ();
776 /* Set both the real and effective UID and GID to the user's.
777 They cannot be changed back to make's. */
779 #ifndef HAVE_SETREUID
780 if (setuid (user_uid) < 0)
781 pfatal_with_name ("child_access: setuid");
782 #else
783 if (setreuid (user_uid, user_uid) < 0)
784 pfatal_with_name ("child_access: setreuid");
785 #endif
787 #ifndef HAVE_SETREGID
788 if (setgid (user_gid) < 0)
789 pfatal_with_name ("child_access: setgid");
790 #else
791 if (setregid (user_gid, user_gid) < 0)
792 pfatal_with_name ("child_access: setregid");
793 #endif
795 log_access ("Child");
797 #endif /* GETLOADAVG_PRIVILEGED */
800 #ifdef NEED_GET_PATH_MAX
801 unsigned int
802 get_path_max ()
804 static unsigned int value;
806 if (value == 0)
808 long int x = pathconf ("/", _PC_PATH_MAX);
809 if (x > 0)
810 value = x;
811 else
812 return MAXPATHLEN;
815 return value;
817 #endif