Formerly configure.in.~28~
[make.git] / misc.c
blob885d0751df4a3db6a8644cd7f9df58b2ed167499
1 /* Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
2 This file is part of GNU Make.
4 GNU Make is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
9 GNU Make is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with GNU Make; see the file COPYING. If not, write to
16 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
18 #include "make.h"
19 #include "dep.h"
22 /* Compare strings *S1 and *S2.
23 Return negative if the first is less, positive if it is greater,
24 zero if they are equal. */
26 int
27 alpha_compare (s1, s2)
28 char **s1, **s2;
30 if (**s1 != **s2)
31 return **s1 - **s2;
32 return strcmp (*s1, *s2);
35 /* Discard each backslash-newline combination from LINE.
36 Backslash-backslash-newline combinations become backslash-newlines.
37 This is done by copying the text at LINE into itself. */
39 void
40 collapse_continuations (line)
41 char *line;
43 register char *in, *out, *p;
44 register int backslash;
45 register unsigned int bs_write;
47 in = index (line, '\n');
48 if (in == 0)
49 return;
51 out = in;
52 if (out > line)
53 while (out[-1] == '\\')
54 --out;
56 while (*in != '\0')
58 /* BS_WRITE gets the number of quoted backslashes at
59 the end just before IN, and BACKSLASH gets nonzero
60 if the next character is quoted. */
61 backslash = 0;
62 bs_write = 0;
63 for (p = in - 1; p >= line && *p == '\\'; --p)
65 if (backslash)
66 ++bs_write;
67 backslash = !backslash;
69 /* It should be impossible to go back this far without exiting,
70 but if we do, we can't get the right answer. */
71 if (in == out - 1)
72 abort ();
75 /* Output the appropriate number of backslashes. */
76 while (bs_write-- > 0)
77 *out++ = '\\';
79 /* Skip the newline. */
80 ++in;
82 /* If the newline is quoted, discard following whitespace
83 and any preceding whitespace; leave just one space. */
84 if (backslash)
86 in = next_token (in);
87 while (out > line && isblank (out[-1]))
88 --out;
89 *out++ = ' ';
91 else
92 /* If the newline isn't quoted, put it in the output. */
93 *out++ = '\n';
95 /* Now copy the following line to the output.
96 Stop when we find backslashes followed by a newline. */
97 while (*in != '\0')
98 if (*in == '\\')
100 p = in + 1;
101 while (*p == '\\')
102 ++p;
103 if (*p == '\n')
105 in = p;
106 break;
108 while (in < p)
109 *out++ = *in++;
111 else
112 *out++ = *in++;
115 *out = '\0';
119 /* Remove comments from LINE.
120 This is done by copying the text at LINE onto itself. */
122 void
123 remove_comments (line)
124 char *line;
126 register char *p, *p2;
127 register int backslash;
128 register unsigned int bs_write;
130 while (1)
132 p = index (line, '#');
133 if (p == 0)
134 break;
136 backslash = 0;
137 bs_write = 0;
138 for (p2 = p - 1; p2 >= line && *p2 == '\\'; --p2)
140 if (backslash)
141 ++bs_write;
142 backslash = !backslash;
145 if (!backslash)
147 /* Cut off the line at the #. */
148 *p = '\0';
149 break;
152 /* strcpy better copy left to right. */
153 line = p;
154 strcpy (p2 + 1 + bs_write, line);
158 /* Print N spaces (used by DEBUGPR for target-depth). */
160 void
161 print_spaces (n)
162 register unsigned int n;
164 while (n-- > 0)
165 putchar (' ');
169 /* Return a newly-allocated string whose contents
170 concatenate those of s1, s2, s3. */
172 char *
173 concat (s1, s2, s3)
174 register char *s1, *s2, *s3;
176 register unsigned int len1, len2, len3;
177 register char *result;
179 len1 = *s1 != '\0' ? strlen (s1) : 0;
180 len2 = *s2 != '\0' ? strlen (s2) : 0;
181 len3 = *s3 != '\0' ? strlen (s3) : 0;
183 result = (char *) xmalloc (len1 + len2 + len3 + 1);
185 if (*s1 != '\0')
186 bcopy (s1, result, len1);
187 if (*s2 != '\0')
188 bcopy (s2, result + len1, len2);
189 if (*s3 != '\0')
190 bcopy (s3, result + len1 + len2, len3);
191 *(result + len1 + len2 + len3) = '\0';
193 return result;
196 /* Print a message on stdout. */
198 void
199 message (s1, s2, s3, s4, s5, s6)
200 char *s1, *s2, *s3, *s4, *s5, *s6;
202 if (makelevel == 0)
203 printf ("%s: ", program);
204 else
205 printf ("%s[%u]: ", program, makelevel);
206 printf (s1, s2, s3, s4, s5, s6);
207 putchar ('\n');
208 fflush (stdout);
211 /* Print an error message and exit. */
213 /* VARARGS1 */
214 void
215 fatal (s1, s2, s3, s4, s5, s6)
216 char *s1, *s2, *s3, *s4, *s5, *s6;
218 if (makelevel == 0)
219 fprintf (stderr, "%s: *** ", program);
220 else
221 fprintf (stderr, "%s[%u]: *** ", program, makelevel);
222 fprintf (stderr, s1, s2, s3, s4, s5, s6);
223 fputs (". Stop.\n", stderr);
225 die (1);
228 /* Print error message. `s1' is printf control string, `s2' is arg for it. */
230 /* VARARGS1 */
232 void
233 error (s1, s2, s3, s4, s5, s6)
234 char *s1, *s2, *s3, *s4, *s5, *s6;
236 if (makelevel == 0)
237 fprintf (stderr, "%s: ", program);
238 else
239 fprintf (stderr, "%s[%u]: ", program, makelevel);
240 fprintf (stderr, s1, s2, s3, s4, s5, s6);
241 putc ('\n', stderr);
242 fflush (stderr);
245 void
246 makefile_error (file, lineno, s1, s2, s3, s4, s5, s6)
247 char *file;
248 unsigned int lineno;
249 char *s1, *s2, *s3, *s4, *s5, *s6;
251 fprintf (stderr, "%s:%u: ", file, lineno);
252 fprintf (stderr, s1, s2, s3, s4, s5, s6);
253 putc ('\n', stderr);
254 fflush (stderr);
257 void
258 makefile_fatal (file, lineno, s1, s2, s3, s4, s5, s6)
259 char *file;
260 unsigned int lineno;
261 char *s1, *s2, *s3, *s4, *s5, *s6;
263 fprintf (stderr, "%s:%u: *** ", file, lineno);
264 fprintf (stderr, s1, s2, s3, s4, s5, s6);
265 fputs (". Stop.\n", stderr);
267 die (1);
270 #ifndef HAVE_STRERROR
271 char *
272 strerror (errnum)
273 int errnum;
275 extern int errno, sys_nerr;
276 extern char *sys_errlist[];
277 static char buf[] = "Unknown error 12345678901234567890";
279 if (errno < sys_nerr)
280 return sys_errlist[errnum];
282 sprintf ("Unknown error %d", buf, errnum);
283 return buf;
285 #endif
287 /* Print an error message from errno. */
289 void
290 perror_with_name (str, name)
291 char *str, *name;
293 error ("%s%s: %s", str, name, strerror (errno));
296 /* Print an error message from errno and exit. */
298 void
299 pfatal_with_name (name)
300 char *name;
302 fatal ("%s: %s", name, strerror (errno));
304 /* NOTREACHED */
307 /* Like malloc but get fatal error if memory is exhausted. */
309 #undef xmalloc
310 #undef xrealloc
312 char *
313 xmalloc (size)
314 unsigned int size;
316 char *result = (char *) malloc (size);
317 if (result == 0)
318 fatal ("virtual memory exhausted");
319 return result;
323 char *
324 xrealloc (ptr, size)
325 char *ptr;
326 unsigned int size;
328 char *result = (char *) realloc (ptr, size);
329 if (result == 0)
330 fatal ("virtual memory exhausted");
331 return result;
334 char *
335 savestring (str, length)
336 char *str;
337 unsigned int length;
339 register char *out = (char *) xmalloc (length + 1);
340 if (length > 0)
341 bcopy (str, out, length);
342 out[length] = '\0';
343 return out;
346 /* Search string BIG (length BLEN) for an occurrence of
347 string SMALL (length SLEN). Return a pointer to the
348 beginning of the first occurrence, or return nil if none found. */
350 char *
351 sindex (big, blen, small, slen)
352 char *big;
353 unsigned int blen;
354 char *small;
355 unsigned int slen;
357 register unsigned int b;
359 if (blen < 1)
360 blen = strlen (big);
361 if (slen < 1)
362 slen = strlen (small);
364 for (b = 0; b < blen; ++b)
365 if (big[b] == *small && !strncmp (&big[b + 1], small + 1, slen - 1))
366 return (&big[b]);
368 return 0;
371 /* Limited INDEX:
372 Search through the string STRING, which ends at LIMIT, for the character C.
373 Returns a pointer to the first occurrence, or nil if none is found.
374 Like INDEX except that the string searched ends where specified
375 instead of at the first null. */
377 char *
378 lindex (s, limit, c)
379 register char *s, *limit;
380 int c;
382 while (s < limit)
383 if (*s++ == c)
384 return s - 1;
386 return 0;
389 /* Return the address of the first whitespace or null in the string S. */
391 char *
392 end_of_token (s)
393 char *s;
395 register char *p = s;
396 register int backslash = 0;
398 while (*p != '\0' && (backslash || !isblank (*p)))
400 if (*p++ == '\\')
402 backslash = !backslash;
403 while (*p == '\\')
405 backslash = !backslash;
406 ++p;
409 else
410 backslash = 0;
413 return p;
416 /* Return the address of the first nonwhitespace or null in the string S. */
418 char *
419 next_token (s)
420 char *s;
422 register char *p = s;
424 while (isblank (*p))
425 ++p;
426 return p;
429 /* Find the next token in PTR; return the address of it, and store the
430 length of the token into *LENGTHPTR if LENGTHPTR is not nil. */
432 char *
433 find_next_token (ptr, lengthptr)
434 char **ptr;
435 unsigned int *lengthptr;
437 char *p = next_token (*ptr);
438 char *end;
440 if (*p == '\0')
441 return 0;
443 *ptr = end = end_of_token (p);
444 if (lengthptr != 0)
445 *lengthptr = end - p;
446 return p;
449 /* Copy a chain of `struct dep', making a new chain
450 with the same contents as the old one. */
452 struct dep *
453 copy_dep_chain (d)
454 register struct dep *d;
456 register struct dep *c;
457 struct dep *firstnew = 0;
458 struct dep *lastnew;
460 while (d != 0)
462 c = (struct dep *) xmalloc (sizeof (struct dep));
463 bcopy ((char *) d, (char *) c, sizeof (struct dep));
464 if (c->name != 0)
465 c->name = savestring (c->name, strlen (c->name));
466 c->next = 0;
467 if (firstnew == 0)
468 firstnew = lastnew = c;
469 else
470 lastnew = lastnew->next = c;
472 d = d->next;
475 return firstnew;
478 #ifdef iAPX286
479 /* The losing compiler on this machine can't handle this macro. */
481 char *
482 dep_name (dep)
483 struct dep *dep;
485 return dep->name == 0 ? dep->file->name : dep->name;
487 #endif
489 #ifdef GETLOADAVG_PRIVILEGED
491 #ifndef HAVE_UNISTD_H
492 extern int getuid (), getgid (), geteuid (), getegid ();
493 extern int setuid (), setgid ();
494 #ifdef HAVE_SETREUID
495 extern int setreuid ();
496 #endif /* Have setreuid. */
497 #ifdef HAVE_SETREGID
498 extern int setregid ();
499 #endif /* Have setregid. */
500 #endif /* No <unistd.h>. */
502 /* Keep track of the user and group IDs for user- and make- access. */
503 static int user_uid = -1, user_gid = -1, make_uid = -1, make_gid = -1;
504 #define access_inited (user_uid != -1)
505 static enum { make, user } current_access;
508 /* Under -d, write a message describing the current IDs. */
510 static void
511 log_access (flavor)
512 char *flavor;
514 if (! debug_flag)
515 return;
517 /* All the other debugging messages go to stdout,
518 but we write this one to stderr because it might be
519 run in a child fork whose stdout is piped. */
521 fprintf (stderr, "%s access: user %d (real %d), group %d (real %d)\n",
522 flavor, geteuid (), getuid (), getegid (), getgid ());
523 fflush (stderr);
527 static void
528 init_access ()
530 user_uid = getuid ();
531 user_gid = getgid ();
533 make_uid = geteuid ();
534 make_gid = getegid ();
536 /* Do these ever fail? */
537 if (user_uid == -1 || user_gid == -1 || make_uid == -1 || make_gid == -1)
538 pfatal_with_name ("get{e}[gu]id");
540 log_access ("Initialized");
542 current_access = make;
545 #endif /* GETLOADAVG_PRIVILEGED */
547 /* Give the process appropriate permissions for access to
548 user data (i.e., to stat files, or to spawn a child process). */
549 void
550 user_access ()
552 #ifdef GETLOADAVG_PRIVILEGED
554 if (!access_inited)
555 init_access ();
557 if (current_access == user)
558 return;
560 /* We are in "make access" mode. This means that the effective user and
561 group IDs are those of make (if it was installed setuid or setgid).
562 We now want to set the effective user and group IDs to the real IDs,
563 which are the IDs of the process that exec'd make. */
565 #ifndef HAVE_SETREUID
567 /* System V has only the setuid/setgid calls to set user/group IDs.
568 There is an effective ID, which can be set by setuid/setgid.
569 It can be set (unless you are root) only to either what it already is
570 (returned by geteuid/getegid, now in make_uid/make_gid),
571 the real ID (return by getuid/getgid, now in user_uid/user_gid),
572 or the saved set ID (what the effective ID was before this set-ID
573 executable (make) was exec'd). */
575 if (setuid (user_uid) < 0)
576 pfatal_with_name ("user_access: setuid");
578 #else
580 /* In 4BSD, the setreuid/setregid calls set both the real and effective IDs.
581 They may be set to themselves or each other. So you have two alternatives
582 at any one time. If you use setuid/setgid, the effective will be set to
583 the real, leaving only one alternative. Using setreuid/setregid, however,
584 you can toggle between your two alternatives by swapping the values in a
585 single setreuid or setregid call. */
587 if (setreuid (make_uid, user_uid) < 0)
588 pfatal_with_name ("user_access: setreuid");
590 #endif
592 #ifndef HAVE_SETREGID
593 if (setgid (user_gid) < 0)
594 pfatal_with_name ("user_access: setgid");
595 #else
596 if (setregid (make_gid, user_gid) < 0)
597 pfatal_with_name ("user_access: setregid");
598 #endif
600 current_access = user;
602 log_access ("User");
604 #endif /* GETLOADAVG_PRIVILEGED */
607 /* Give the process appropriate permissions for access to
608 make data (i.e., the load average). */
609 void
610 make_access ()
612 #ifdef GETLOADAVG_PRIVILEGED
614 if (!access_inited)
615 init_access ();
617 if (current_access == make)
618 return;
620 /* See comments in user_access, above. */
622 #ifndef HAVE_SETREUID
623 if (setuid (make_uid) < 0)
624 pfatal_with_name ("make_access: setuid");
625 #else
626 if (setreuid (user_uid, make_uid) < 0)
627 pfatal_with_name ("make_access: setreuid");
628 #endif
630 #ifndef HAVE_SETREGID
631 if (setgid (make_gid) < 0)
632 pfatal_with_name ("make_access: setgid");
633 #else
634 if (setregid (user_gid, make_gid) < 0)
635 pfatal_with_name ("make_access: setregid");
636 #endif
638 current_access = make;
640 log_access ("Make");
642 #endif /* GETLOADAVG_PRIVILEGED */
645 /* Give the process appropriate permissions for a child process.
646 This is like user_access, but you can't get back to make_access. */
647 void
648 child_access ()
650 #ifdef GETLOADAVG_PRIVILEGED
652 if (!access_inited)
653 abort ();
655 /* Set both the real and effective UID and GID to the user's.
656 They cannot be changed back to make's. */
658 #ifndef HAVE_SETREUID
659 if (setuid (user_uid) < 0)
660 pfatal_with_name ("child_access: setuid");
661 #else
662 if (setreuid (user_uid, user_uid) < 0)
663 pfatal_with_name ("child_access: setreuid");
664 #endif
666 #ifndef HAVE_SETREGID
667 if (setgid (user_gid) < 0)
668 pfatal_with_name ("child_access: setgid");
669 #else
670 if (setregid (user_gid, user_gid) < 0)
671 pfatal_with_name ("child_access: setregid");
672 #endif
674 log_access ("Child");
676 #endif /* GETLOADAVG_PRIVILEGED */
679 #ifdef NEED_GET_PATH_MAX
680 unsigned int
681 get_path_max ()
683 static unsigned int value;
685 if (value == 0)
687 long int x = pathconf ("/", _PC_PATH_MAX);
688 if (x > 0)
689 value = x;
690 else
691 return MAXPATHLEN;
694 return value;
696 #endif