Updated from libc
[make.git] / misc.c
bloba27c2402232bbe70d9d0dfdce7b024cb6b72c591
1 /* Miscellaneous generic support functions for GNU Make.
2 Copyright (C) 1988, 89, 90, 91, 92, 93, 94, 1995 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 /* Compare strings *S1 and *S2.
24 Return negative if the first is less, positive if it is greater,
25 zero if they are equal. */
27 int
28 alpha_compare (s1, s2)
29 char **s1, **s2;
31 if (**s1 != **s2)
32 return **s1 - **s2;
33 return strcmp (*s1, *s2);
36 /* Discard each backslash-newline combination from LINE.
37 Backslash-backslash-newline combinations become backslash-newlines.
38 This is done by copying the text at LINE into itself. */
40 void
41 collapse_continuations (line)
42 char *line;
44 register char *in, *out, *p;
45 register int backslash;
46 register unsigned int bs_write;
48 in = index (line, '\n');
49 if (in == 0)
50 return;
52 out = in;
53 if (out > line)
54 while (out[-1] == '\\')
55 --out;
57 while (*in != '\0')
59 /* BS_WRITE gets the number of quoted backslashes at
60 the end just before IN, and BACKSLASH gets nonzero
61 if the next character is quoted. */
62 backslash = 0;
63 bs_write = 0;
64 for (p = in - 1; p >= line && *p == '\\'; --p)
66 if (backslash)
67 ++bs_write;
68 backslash = !backslash;
70 /* It should be impossible to go back this far without exiting,
71 but if we do, we can't get the right answer. */
72 if (in == out - 1)
73 abort ();
76 /* Output the appropriate number of backslashes. */
77 while (bs_write-- > 0)
78 *out++ = '\\';
80 /* Skip the newline. */
81 ++in;
83 /* If the newline is quoted, discard following whitespace
84 and any preceding whitespace; leave just one space. */
85 if (backslash)
87 in = next_token (in);
88 while (out > line && isblank (out[-1]))
89 --out;
90 *out++ = ' ';
92 else
93 /* If the newline isn't quoted, put it in the output. */
94 *out++ = '\n';
96 /* Now copy the following line to the output.
97 Stop when we find backslashes followed by a newline. */
98 while (*in != '\0')
99 if (*in == '\\')
101 p = in + 1;
102 while (*p == '\\')
103 ++p;
104 if (*p == '\n')
106 in = p;
107 break;
109 while (in < p)
110 *out++ = *in++;
112 else
113 *out++ = *in++;
116 *out = '\0';
120 /* Remove comments from LINE.
121 This is done by copying the text at LINE onto itself. */
123 void
124 remove_comments (line)
125 char *line;
127 char *comment;
129 comment = find_char_unquote (line, "#", 0);
131 if (comment != 0)
132 /* Cut off the line at the #. */
133 *comment = '\0';
136 /* Print N spaces (used by DEBUGPR for target-depth). */
138 void
139 print_spaces (n)
140 register unsigned int n;
142 while (n-- > 0)
143 putchar (' ');
147 /* Return a newly-allocated string whose contents
148 concatenate those of s1, s2, s3. */
150 char *
151 concat (s1, s2, s3)
152 register char *s1, *s2, *s3;
154 register unsigned int len1, len2, len3;
155 register char *result;
157 len1 = *s1 != '\0' ? strlen (s1) : 0;
158 len2 = *s2 != '\0' ? strlen (s2) : 0;
159 len3 = *s3 != '\0' ? strlen (s3) : 0;
161 result = (char *) xmalloc (len1 + len2 + len3 + 1);
163 if (*s1 != '\0')
164 bcopy (s1, result, len1);
165 if (*s2 != '\0')
166 bcopy (s2, result + len1, len2);
167 if (*s3 != '\0')
168 bcopy (s3, result + len1 + len2, len3);
169 *(result + len1 + len2 + len3) = '\0';
171 return result;
174 /* Print a message on stdout. */
176 void
177 message (prefix, s1, s2, s3, s4, s5, s6)
178 int prefix;
179 char *s1, *s2, *s3, *s4, *s5, *s6;
181 log_working_directory (1);
183 if (s1 != 0)
185 if (prefix)
187 if (makelevel == 0)
188 printf ("%s: ", program);
189 else
190 printf ("%s[%u]: ", program, makelevel);
192 printf (s1, s2, s3, s4, s5, s6);
193 putchar ('\n');
196 fflush (stdout);
199 /* Print an error message and exit. */
201 /* VARARGS1 */
202 void
203 fatal (s1, s2, s3, s4, s5, s6)
204 char *s1, *s2, *s3, *s4, *s5, *s6;
206 log_working_directory (1);
208 if (makelevel == 0)
209 fprintf (stderr, "%s: *** ", program);
210 else
211 fprintf (stderr, "%s[%u]: *** ", program, makelevel);
212 fprintf (stderr, s1, s2, s3, s4, s5, s6);
213 fputs (". Stop.\n", stderr);
215 die (2);
218 /* Print error message. `s1' is printf control string, `s2' is arg for it. */
220 /* VARARGS1 */
222 void
223 error (s1, s2, s3, s4, s5, s6)
224 char *s1, *s2, *s3, *s4, *s5, *s6;
226 log_working_directory (1);
228 if (makelevel == 0)
229 fprintf (stderr, "%s: ", program);
230 else
231 fprintf (stderr, "%s[%u]: ", program, makelevel);
232 fprintf (stderr, s1, s2, s3, s4, s5, s6);
233 putc ('\n', stderr);
234 fflush (stderr);
237 void
238 makefile_error (file, lineno, s1, s2, s3, s4, s5, s6)
239 char *file;
240 unsigned int lineno;
241 char *s1, *s2, *s3, *s4, *s5, *s6;
243 log_working_directory (1);
245 fprintf (stderr, "%s:%u: ", file, lineno);
246 fprintf (stderr, s1, s2, s3, s4, s5, s6);
247 putc ('\n', stderr);
248 fflush (stderr);
251 void
252 makefile_fatal (file, lineno, s1, s2, s3, s4, s5, s6)
253 char *file;
254 unsigned int lineno;
255 char *s1, *s2, *s3, *s4, *s5, *s6;
257 log_working_directory (1);
259 fprintf (stderr, "%s:%u: *** ", file, lineno);
260 fprintf (stderr, s1, s2, s3, s4, s5, s6);
261 fputs (". Stop.\n", stderr);
263 die (2);
266 #ifndef HAVE_STRERROR
268 #undef strerror
270 char *
271 strerror (errnum)
272 int errnum;
274 extern int errno, sys_nerr;
275 extern char *sys_errlist[];
276 static char buf[] = "Unknown error 12345678901234567890";
278 if (errno < sys_nerr)
279 return sys_errlist[errnum];
281 sprintf (buf, "Unknown error %d", errnum);
282 return buf;
284 #endif
286 /* Print an error message from errno. */
288 void
289 perror_with_name (str, name)
290 char *str, *name;
292 error ("%s%s: %s", str, name, strerror (errno));
295 /* Print an error message from errno and exit. */
297 void
298 pfatal_with_name (name)
299 char *name;
301 fatal ("%s: %s", name, strerror (errno));
303 /* NOTREACHED */
306 /* Like malloc but get fatal error if memory is exhausted. */
308 #undef xmalloc
309 #undef xrealloc
311 char *
312 xmalloc (size)
313 unsigned int size;
315 char *result = (char *) malloc (size);
316 if (result == 0)
317 fatal ("virtual memory exhausted");
318 return result;
322 char *
323 xrealloc (ptr, size)
324 char *ptr;
325 unsigned int size;
327 char *result = (char *) realloc (ptr, size);
328 if (result == 0)
329 fatal ("virtual memory exhausted");
330 return result;
333 char *
334 savestring (str, length)
335 char *str;
336 unsigned int length;
338 register char *out = (char *) xmalloc (length + 1);
339 if (length > 0)
340 bcopy (str, out, length);
341 out[length] = '\0';
342 return out;
345 /* Search string BIG (length BLEN) for an occurrence of
346 string SMALL (length SLEN). Return a pointer to the
347 beginning of the first occurrence, or return nil if none found. */
349 char *
350 sindex (big, blen, small, slen)
351 char *big;
352 unsigned int blen;
353 char *small;
354 unsigned int slen;
356 register unsigned int b;
358 if (blen < 1)
359 blen = strlen (big);
360 if (slen < 1)
361 slen = strlen (small);
363 for (b = 0; b < blen; ++b)
364 if (big[b] == *small && !strncmp (&big[b + 1], small + 1, slen - 1))
365 return (&big[b]);
367 return 0;
370 /* Limited INDEX:
371 Search through the string STRING, which ends at LIMIT, for the character C.
372 Returns a pointer to the first occurrence, or nil if none is found.
373 Like INDEX except that the string searched ends where specified
374 instead of at the first null. */
376 char *
377 lindex (s, limit, c)
378 register char *s, *limit;
379 int c;
381 while (s < limit)
382 if (*s++ == c)
383 return s - 1;
385 return 0;
388 /* Return the address of the first whitespace or null in the string S. */
390 char *
391 end_of_token (s)
392 char *s;
394 while (*s != '\0' && !isblank (*s))
395 ++s;
396 return s;
399 /* Return the address of the first nonwhitespace or null in the string S. */
401 char *
402 next_token (s)
403 char *s;
405 register char *p = s;
407 while (isblank (*p))
408 ++p;
409 return p;
412 /* Find the next token in PTR; return the address of it, and store the
413 length of the token into *LENGTHPTR if LENGTHPTR is not nil. */
415 char *
416 find_next_token (ptr, lengthptr)
417 char **ptr;
418 unsigned int *lengthptr;
420 char *p = next_token (*ptr);
421 char *end;
423 if (*p == '\0')
424 return 0;
426 *ptr = end = end_of_token (p);
427 if (lengthptr != 0)
428 *lengthptr = end - p;
429 return p;
432 /* Copy a chain of `struct dep', making a new chain
433 with the same contents as the old one. */
435 struct dep *
436 copy_dep_chain (d)
437 register struct dep *d;
439 register struct dep *c;
440 struct dep *firstnew = 0;
441 struct dep *lastnew;
443 while (d != 0)
445 c = (struct dep *) xmalloc (sizeof (struct dep));
446 bcopy ((char *) d, (char *) c, sizeof (struct dep));
447 if (c->name != 0)
448 c->name = savestring (c->name, strlen (c->name));
449 c->next = 0;
450 if (firstnew == 0)
451 firstnew = lastnew = c;
452 else
453 lastnew = lastnew->next = c;
455 d = d->next;
458 return firstnew;
461 #ifdef iAPX286
462 /* The losing compiler on this machine can't handle this macro. */
464 char *
465 dep_name (dep)
466 struct dep *dep;
468 return dep->name == 0 ? dep->file->name : dep->name;
470 #endif
472 #ifdef GETLOADAVG_PRIVILEGED
474 #ifdef POSIX
476 /* Hopefully if a system says it's POSIX.1 and has the setuid and setgid
477 functions, they work as POSIX.1 says. Some systems (Alpha OSF/1 1.2,
478 for example) which claim to be POSIX.1 also have the BSD setreuid and
479 setregid functions, but they don't work as in BSD and only the POSIX.1
480 way works. */
482 #undef HAVE_SETREUID
483 #undef HAVE_SETREGID
485 #else /* Not POSIX. */
487 /* Some POSIX.1 systems have the seteuid and setegid functions. In a
488 POSIX-like system, they are the best thing to use. However, some
489 non-POSIX systems have them too but they do not work in the POSIX style
490 and we must use setreuid and setregid instead. */
492 #undef HAVE_SETEUID
493 #undef HAVE_SETEGID
495 #endif /* POSIX. */
497 #ifndef HAVE_UNISTD_H
498 extern int getuid (), getgid (), geteuid (), getegid ();
499 extern int setuid (), setgid ();
500 #ifdef HAVE_SETEUID
501 extern int seteuid ();
502 #else
503 #ifdef HAVE_SETREUID
504 extern int setreuid ();
505 #endif /* Have setreuid. */
506 #endif /* Have seteuid. */
507 #ifdef HAVE_SETEGID
508 extern int setegid ();
509 #else
510 #ifdef HAVE_SETREGID
511 extern int setregid ();
512 #endif /* Have setregid. */
513 #endif /* Have setegid. */
514 #endif /* No <unistd.h>. */
516 /* Keep track of the user and group IDs for user- and make- access. */
517 static int user_uid = -1, user_gid = -1, make_uid = -1, make_gid = -1;
518 #define access_inited (user_uid != -1)
519 static enum { make, user } current_access;
522 /* Under -d, write a message describing the current IDs. */
524 static void
525 log_access (flavor)
526 char *flavor;
528 if (! debug_flag)
529 return;
531 /* All the other debugging messages go to stdout,
532 but we write this one to stderr because it might be
533 run in a child fork whose stdout is piped. */
535 fprintf (stderr, "%s access: user %d (real %d), group %d (real %d)\n",
536 flavor, geteuid (), getuid (), getegid (), getgid ());
537 fflush (stderr);
541 static void
542 init_access ()
544 user_uid = getuid ();
545 user_gid = getgid ();
547 make_uid = geteuid ();
548 make_gid = getegid ();
550 /* Do these ever fail? */
551 if (user_uid == -1 || user_gid == -1 || make_uid == -1 || make_gid == -1)
552 pfatal_with_name ("get{e}[gu]id");
554 log_access ("Initialized");
556 current_access = make;
559 #endif /* GETLOADAVG_PRIVILEGED */
561 /* Give the process appropriate permissions for access to
562 user data (i.e., to stat files, or to spawn a child process). */
563 void
564 user_access ()
566 #ifdef GETLOADAVG_PRIVILEGED
568 if (!access_inited)
569 init_access ();
571 if (current_access == user)
572 return;
574 /* We are in "make access" mode. This means that the effective user and
575 group IDs are those of make (if it was installed setuid or setgid).
576 We now want to set the effective user and group IDs to the real IDs,
577 which are the IDs of the process that exec'd make. */
579 #ifdef HAVE_SETEUID
581 /* Modern systems have the seteuid/setegid calls which set only the
582 effective IDs, which is ideal. */
584 if (seteuid (user_uid) < 0)
585 pfatal_with_name ("user_access: seteuid");
587 #else /* Not HAVE_SETEUID. */
589 #ifndef HAVE_SETREUID
591 /* System V has only the setuid/setgid calls to set user/group IDs.
592 There is an effective ID, which can be set by setuid/setgid.
593 It can be set (unless you are root) only to either what it already is
594 (returned by geteuid/getegid, now in make_uid/make_gid),
595 the real ID (return by getuid/getgid, now in user_uid/user_gid),
596 or the saved set ID (what the effective ID was before this set-ID
597 executable (make) was exec'd). */
599 if (setuid (user_uid) < 0)
600 pfatal_with_name ("user_access: setuid");
602 #else /* HAVE_SETREUID. */
604 /* In 4BSD, the setreuid/setregid calls set both the real and effective IDs.
605 They may be set to themselves or each other. So you have two alternatives
606 at any one time. If you use setuid/setgid, the effective will be set to
607 the real, leaving only one alternative. Using setreuid/setregid, however,
608 you can toggle between your two alternatives by swapping the values in a
609 single setreuid or setregid call. */
611 if (setreuid (make_uid, user_uid) < 0)
612 pfatal_with_name ("user_access: setreuid");
614 #endif /* Not HAVE_SETREUID. */
615 #endif /* HAVE_SETEUID. */
617 #ifdef HAVE_SETEGID
618 if (setegid (user_gid) < 0)
619 pfatal_with_name ("user_access: setegid");
620 #else
621 #ifndef HAVE_SETREGID
622 if (setgid (user_gid) < 0)
623 pfatal_with_name ("user_access: setgid");
624 #else
625 if (setregid (make_gid, user_gid) < 0)
626 pfatal_with_name ("user_access: setregid");
627 #endif
628 #endif
630 current_access = user;
632 log_access ("User");
634 #endif /* GETLOADAVG_PRIVILEGED */
637 /* Give the process appropriate permissions for access to
638 make data (i.e., the load average). */
639 void
640 make_access ()
642 #ifdef GETLOADAVG_PRIVILEGED
644 if (!access_inited)
645 init_access ();
647 if (current_access == make)
648 return;
650 /* See comments in user_access, above. */
652 #ifdef HAVE_SETEUID
653 if (seteuid (make_uid) < 0)
654 pfatal_with_name ("make_access: seteuid");
655 #else
656 #ifndef HAVE_SETREUID
657 if (setuid (make_uid) < 0)
658 pfatal_with_name ("make_access: setuid");
659 #else
660 if (setreuid (user_uid, make_uid) < 0)
661 pfatal_with_name ("make_access: setreuid");
662 #endif
663 #endif
665 #ifdef HAVE_SETEGID
666 if (setegid (make_gid) < 0)
667 pfatal_with_name ("make_access: setegid");
668 #else
669 #ifndef HAVE_SETREGID
670 if (setgid (make_gid) < 0)
671 pfatal_with_name ("make_access: setgid");
672 #else
673 if (setregid (user_gid, make_gid) < 0)
674 pfatal_with_name ("make_access: setregid");
675 #endif
676 #endif
678 current_access = make;
680 log_access ("Make");
682 #endif /* GETLOADAVG_PRIVILEGED */
685 /* Give the process appropriate permissions for a child process.
686 This is like user_access, but you can't get back to make_access. */
687 void
688 child_access ()
690 #ifdef GETLOADAVG_PRIVILEGED
692 if (!access_inited)
693 abort ();
695 /* Set both the real and effective UID and GID to the user's.
696 They cannot be changed back to make's. */
698 #ifndef HAVE_SETREUID
699 if (setuid (user_uid) < 0)
700 pfatal_with_name ("child_access: setuid");
701 #else
702 if (setreuid (user_uid, user_uid) < 0)
703 pfatal_with_name ("child_access: setreuid");
704 #endif
706 #ifndef HAVE_SETREGID
707 if (setgid (user_gid) < 0)
708 pfatal_with_name ("child_access: setgid");
709 #else
710 if (setregid (user_gid, user_gid) < 0)
711 pfatal_with_name ("child_access: setregid");
712 #endif
714 log_access ("Child");
716 #endif /* GETLOADAVG_PRIVILEGED */
719 #ifdef NEED_GET_PATH_MAX
720 unsigned int
721 get_path_max ()
723 static unsigned int value;
725 if (value == 0)
727 long int x = pathconf ("/", _PC_PATH_MAX);
728 if (x > 0)
729 value = x;
730 else
731 return MAXPATHLEN;
734 return value;
736 #endif
738 /* On some systems, stat can return EINTR. */
741 safe_stat (name, buf)
742 char *name;
743 struct stat *buf;
745 int ret;
747 #ifdef EINTR
749 #endif
750 ret = stat (name, buf);
751 #ifdef EINTR
752 while (ret < 0 && errno == EINTR);
753 #endif
755 return ret;