Fix for the problem that SDL applications exited
[AROS-Contrib.git] / gnu / make / misc.c
blob43d4cb356bad65aae80e385e5d7946e3b7d83c8a
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 USE_VARIADIC
34 # if HAVE_STDARG_H
35 # include <stdarg.h>
36 # define VA_START(args, lastarg) va_start(args, lastarg)
37 # else
38 # include <varargs.h>
39 # define VA_START(args, lastarg) va_start(args)
40 # endif
41 # if HAVE_VPRINTF
42 # define VA_PRINTF(fp, lastarg, args) vfprintf((fp), (lastarg), (args))
43 # else
44 # define VA_PRINTF(fp, lastarg, args) _doprnt((lastarg), (args), (fp))
45 # endif
46 # define VA_END(args) va_end(args)
47 #else
48 /* We can't use any variadic interface! */
49 # define va_alist a1, a2, a3, a4, a5, a6, a7, a8
50 # define va_dcl char *a1, *a2, *a3, *a4, *a5, *a6, *a7, *a8;
51 # define VA_START(args, lastarg)
52 # define VA_PRINTF(fp, lastarg, args) fprintf((fp), (lastarg), va_alist)
53 # define VA_END(args)
54 #endif
57 /* Compare strings *S1 and *S2.
58 Return negative if the first is less, positive if it is greater,
59 zero if they are equal. */
61 int
62 alpha_compare (const void *v1, const void *v2)
64 const char *s1 = *((char **)v1);
65 const char *s2 = *((char **)v2);
67 if (*s1 != *s2)
68 return *s1 - *s2;
69 return strcmp (s1, s2);
72 /* Discard each backslash-newline combination from LINE.
73 Backslash-backslash-newline combinations become backslash-newlines.
74 This is done by copying the text at LINE into itself. */
76 void
77 collapse_continuations (char *line)
79 char *in, *out, *p;
80 int backslash;
81 unsigned int bs_write;
83 in = strchr (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 ((unsigned char)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 (char *line)
160 char *comment;
162 comment = find_char_unquote (line, '#', 0, 0);
164 if (comment != 0)
165 /* Cut off the line at the #. */
166 *comment = '\0';
169 /* Print N spaces (used in debug for target-depth). */
171 void
172 print_spaces (unsigned int n)
174 while (n-- > 0)
175 putchar (' ');
179 /* Return a newly-allocated string whose contents
180 concatenate those of s1, s2, s3. */
182 char *
183 concat (const char *s1, const char *s2, const char *s3)
185 unsigned int len1, len2, len3;
186 char *result;
188 len1 = *s1 != '\0' ? strlen (s1) : 0;
189 len2 = *s2 != '\0' ? strlen (s2) : 0;
190 len3 = *s3 != '\0' ? strlen (s3) : 0;
192 result = (char *) xmalloc (len1 + len2 + len3 + 1);
194 if (*s1 != '\0')
195 bcopy (s1, result, len1);
196 if (*s2 != '\0')
197 bcopy (s2, result + len1, len2);
198 if (*s3 != '\0')
199 bcopy (s3, result + len1 + len2, len3);
200 *(result + len1 + len2 + len3) = '\0';
202 return result;
205 /* Print a message on stdout. */
207 void
208 #if HAVE_ANSI_COMPILER && USE_VARIADIC && HAVE_STDARG_H
209 message (int prefix, const char *fmt, ...)
210 #else
211 message (prefix, fmt, va_alist)
212 int prefix;
213 const char *fmt;
214 va_dcl
215 #endif
217 #if USE_VARIADIC
218 va_list args;
219 #endif
221 log_working_directory (1);
223 if (fmt != 0)
225 if (prefix)
227 if (makelevel == 0)
228 printf ("%s: ", program);
229 else
230 printf ("%s[%u]: ", program, makelevel);
232 VA_START (args, fmt);
233 VA_PRINTF (stdout, fmt, args);
234 VA_END (args);
235 putchar ('\n');
238 fflush (stdout);
241 /* Print an error message. */
243 void
244 #if HAVE_ANSI_COMPILER && USE_VARIADIC && HAVE_STDARG_H
245 error (const struct floc *flocp, const char *fmt, ...)
246 #else
247 error (flocp, fmt, va_alist)
248 const struct floc *flocp;
249 const char *fmt;
250 va_dcl
251 #endif
253 #if USE_VARIADIC
254 va_list args;
255 #endif
257 log_working_directory (1);
259 if (flocp && flocp->filenm)
260 fprintf (stderr, "%s:%lu: ", flocp->filenm, flocp->lineno);
261 else if (makelevel == 0)
262 fprintf (stderr, "%s: ", program);
263 else
264 fprintf (stderr, "%s[%u]: ", program, makelevel);
266 VA_START(args, fmt);
267 VA_PRINTF (stderr, fmt, args);
268 VA_END (args);
270 putc ('\n', stderr);
271 fflush (stderr);
274 /* Print an error message and exit. */
276 void
277 #if HAVE_ANSI_COMPILER && USE_VARIADIC && HAVE_STDARG_H
278 fatal (const struct floc *flocp, const char *fmt, ...)
279 #else
280 fatal (flocp, fmt, va_alist)
281 const struct floc *flocp;
282 const char *fmt;
283 va_dcl
284 #endif
286 #if USE_VARIADIC
287 va_list args;
288 #endif
290 log_working_directory (1);
292 if (flocp && flocp->filenm)
293 fprintf (stderr, "%s:%lu: *** ", flocp->filenm, flocp->lineno);
294 else if (makelevel == 0)
295 fprintf (stderr, "%s: *** ", program);
296 else
297 fprintf (stderr, "%s[%u]: *** ", program, makelevel);
299 VA_START(args, fmt);
300 VA_PRINTF (stderr, fmt, args);
301 VA_END (args);
303 fputs (_(". Stop.\n"), stderr);
305 die (2);
308 #ifndef HAVE_STRERROR
310 #undef strerror
312 char *
313 strerror (int errnum)
315 extern int errno, sys_nerr;
316 #ifndef __DECC
317 extern char *sys_errlist[];
318 #endif
319 static char buf[] = "Unknown error 12345678901234567890";
321 if (errno < sys_nerr)
322 return sys_errlist[errnum];
324 sprintf (buf, _("Unknown error %d"), errnum);
325 return buf;
327 #endif
329 /* Print an error message from errno. */
331 void
332 perror_with_name (const char *str, const char *name)
334 error (NILF, _("%s%s: %s"), str, name, strerror (errno));
337 /* Print an error message from errno and exit. */
339 void
340 pfatal_with_name (const char *name)
342 fatal (NILF, _("%s: %s"), name, strerror (errno));
344 /* NOTREACHED */
347 /* Like malloc but get fatal error if memory is exhausted. */
348 /* Don't bother if we're using dmalloc; it provides these for us. */
350 #ifndef HAVE_DMALLOC_H
352 #undef xmalloc
353 #undef xrealloc
354 #undef xstrdup
356 char *
357 xmalloc (unsigned int size)
359 /* Make sure we don't allocate 0, for pre-ANSI libraries. */
360 char *result = (char *) malloc (size ? size : 1);
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 if (! size)
374 size = 1;
375 result = ptr ? realloc (ptr, size) : malloc (size);
376 if (result == 0)
377 fatal (NILF, _("virtual memory exhausted"));
378 return result;
382 char *
383 xstrdup (const char *ptr)
385 char *result;
387 #ifdef HAVE_STRDUP
388 result = strdup (ptr);
389 #else
390 result = (char *) malloc (strlen (ptr) + 1);
391 #endif
393 if (result == 0)
394 fatal (NILF, _("virtual memory exhausted"));
396 #ifdef HAVE_STRDUP
397 return result;
398 #else
399 return strcpy(result, ptr);
400 #endif
403 #endif /* HAVE_DMALLOC_H */
405 char *
406 savestring (const char *str, unsigned int length)
408 register char *out = (char *) xmalloc (length + 1);
409 if (length > 0)
410 bcopy (str, out, length);
411 out[length] = '\0';
412 return out;
415 /* Search string BIG (length BLEN) for an occurrence of
416 string SMALL (length SLEN). Return a pointer to the
417 beginning of the first occurrence, or return nil if none found. */
419 char *
420 sindex (const char *big, unsigned int blen,
421 const char *small, unsigned int slen)
423 if (!blen)
424 blen = strlen (big);
425 if (!slen)
426 slen = strlen (small);
428 if (slen && blen >= slen)
430 register unsigned int b;
432 /* Quit when there's not enough room left for the small string. */
433 --slen;
434 blen -= slen;
436 for (b = 0; b < blen; ++b, ++big)
437 if (*big == *small && strneq (big + 1, small + 1, slen))
438 return (char *)big;
441 return 0;
444 /* Limited INDEX:
445 Search through the string STRING, which ends at LIMIT, for the character C.
446 Returns a pointer to the first occurrence, or nil if none is found.
447 Like INDEX except that the string searched ends where specified
448 instead of at the first null. */
450 char *
451 lindex (const char *s, const char *limit, int c)
453 while (s < limit)
454 if (*s++ == c)
455 return (char *)(s - 1);
457 return 0;
460 /* Return the address of the first whitespace or null in the string S. */
462 char *
463 end_of_token (char *s)
465 while (*s != '\0' && !isblank ((unsigned char)*s))
466 ++s;
467 return s;
470 #ifdef WINDOWS32
472 * Same as end_of_token, but take into account a stop character
474 char *
475 end_of_token_w32 (char *s, char stopchar)
477 register char *p = s;
478 register int backslash = 0;
480 while (*p != '\0' && *p != stopchar
481 && (backslash || !isblank ((unsigned char)*p)))
483 if (*p++ == '\\')
485 backslash = !backslash;
486 while (*p == '\\')
488 backslash = !backslash;
489 ++p;
492 else
493 backslash = 0;
496 return p;
498 #endif
500 /* Return the address of the first nonwhitespace or null in the string S. */
502 char *
503 next_token (const char *s)
505 while (isblank ((unsigned char)*s))
506 ++s;
507 return (char *)s;
510 /* Find the next token in PTR; return the address of it, and store the
511 length of the token into *LENGTHPTR if LENGTHPTR is not nil. */
513 char *
514 find_next_token (char **ptr, unsigned int *lengthptr)
516 char *p = next_token (*ptr);
517 char *end;
519 if (*p == '\0')
520 return 0;
522 *ptr = end = end_of_token (p);
523 if (lengthptr != 0)
524 *lengthptr = end - p;
525 return p;
528 /* Copy a chain of `struct dep', making a new chain
529 with the same contents as the old one. */
531 struct dep *
532 copy_dep_chain (struct dep *d)
534 register struct dep *c;
535 struct dep *firstnew = 0;
536 struct dep *lastnew = 0;
538 while (d != 0)
540 c = (struct dep *) xmalloc (sizeof (struct dep));
541 bcopy ((char *) d, (char *) c, sizeof (struct dep));
542 if (c->name != 0)
543 c->name = xstrdup (c->name);
544 c->next = 0;
545 if (firstnew == 0)
546 firstnew = lastnew = c;
547 else
548 lastnew = lastnew->next = c;
550 d = d->next;
553 return firstnew;
556 #ifdef iAPX286
557 /* The losing compiler on this machine can't handle this macro. */
559 char *
560 dep_name (struct dep *dep)
562 return dep->name == 0 ? dep->file->name : dep->name;
564 #endif
566 #ifdef GETLOADAVG_PRIVILEGED
568 #ifdef POSIX
570 /* Hopefully if a system says it's POSIX.1 and has the setuid and setgid
571 functions, they work as POSIX.1 says. Some systems (Alpha OSF/1 1.2,
572 for example) which claim to be POSIX.1 also have the BSD setreuid and
573 setregid functions, but they don't work as in BSD and only the POSIX.1
574 way works. */
576 #undef HAVE_SETREUID
577 #undef HAVE_SETREGID
579 #else /* Not POSIX. */
581 /* Some POSIX.1 systems have the seteuid and setegid functions. In a
582 POSIX-like system, they are the best thing to use. However, some
583 non-POSIX systems have them too but they do not work in the POSIX style
584 and we must use setreuid and setregid instead. */
586 #undef HAVE_SETEUID
587 #undef HAVE_SETEGID
589 #endif /* POSIX. */
591 #ifndef HAVE_UNISTD_H
592 extern int getuid (), getgid (), geteuid (), getegid ();
593 extern int setuid (), setgid ();
594 #ifdef HAVE_SETEUID
595 extern int seteuid ();
596 #else
597 #ifdef HAVE_SETREUID
598 extern int setreuid ();
599 #endif /* Have setreuid. */
600 #endif /* Have seteuid. */
601 #ifdef HAVE_SETEGID
602 extern int setegid ();
603 #else
604 #ifdef HAVE_SETREGID
605 extern int setregid ();
606 #endif /* Have setregid. */
607 #endif /* Have setegid. */
608 #endif /* No <unistd.h>. */
610 /* Keep track of the user and group IDs for user- and make- access. */
611 static int user_uid = -1, user_gid = -1, make_uid = -1, make_gid = -1;
612 #define access_inited (user_uid != -1)
613 static enum { make, user } current_access;
616 /* Under -d, write a message describing the current IDs. */
618 static void
619 log_access (char *flavor)
621 if (! ISDB (DB_JOBS))
622 return;
624 /* All the other debugging messages go to stdout,
625 but we write this one to stderr because it might be
626 run in a child fork whose stdout is piped. */
628 fprintf (stderr, _("%s: user %lu (real %lu), group %lu (real %lu)\n"),
629 flavor, (unsigned long) geteuid (), (unsigned long) getuid (),
630 (unsigned long) getegid (), (unsigned long) getgid ());
631 fflush (stderr);
635 static void
636 init_access (void)
638 #ifndef VMS
639 user_uid = getuid ();
640 user_gid = getgid ();
642 make_uid = geteuid ();
643 make_gid = getegid ();
645 /* Do these ever fail? */
646 if (user_uid == -1 || user_gid == -1 || make_uid == -1 || make_gid == -1)
647 pfatal_with_name ("get{e}[gu]id");
649 log_access (_("Initialized access"));
651 current_access = make;
652 #endif
655 #endif /* GETLOADAVG_PRIVILEGED */
657 /* Give the process appropriate permissions for access to
658 user data (i.e., to stat files, or to spawn a child process). */
659 void
660 user_access (void)
662 #ifdef GETLOADAVG_PRIVILEGED
664 if (!access_inited)
665 init_access ();
667 if (current_access == user)
668 return;
670 /* We are in "make access" mode. This means that the effective user and
671 group IDs are those of make (if it was installed setuid or setgid).
672 We now want to set the effective user and group IDs to the real IDs,
673 which are the IDs of the process that exec'd make. */
675 #ifdef HAVE_SETEUID
677 /* Modern systems have the seteuid/setegid calls which set only the
678 effective IDs, which is ideal. */
680 if (seteuid (user_uid) < 0)
681 pfatal_with_name ("user_access: seteuid");
683 #else /* Not HAVE_SETEUID. */
685 #ifndef HAVE_SETREUID
687 /* System V has only the setuid/setgid calls to set user/group IDs.
688 There is an effective ID, which can be set by setuid/setgid.
689 It can be set (unless you are root) only to either what it already is
690 (returned by geteuid/getegid, now in make_uid/make_gid),
691 the real ID (return by getuid/getgid, now in user_uid/user_gid),
692 or the saved set ID (what the effective ID was before this set-ID
693 executable (make) was exec'd). */
695 if (setuid (user_uid) < 0)
696 pfatal_with_name ("user_access: setuid");
698 #else /* HAVE_SETREUID. */
700 /* In 4BSD, the setreuid/setregid calls set both the real and effective IDs.
701 They may be set to themselves or each other. So you have two alternatives
702 at any one time. If you use setuid/setgid, the effective will be set to
703 the real, leaving only one alternative. Using setreuid/setregid, however,
704 you can toggle between your two alternatives by swapping the values in a
705 single setreuid or setregid call. */
707 if (setreuid (make_uid, user_uid) < 0)
708 pfatal_with_name ("user_access: setreuid");
710 #endif /* Not HAVE_SETREUID. */
711 #endif /* HAVE_SETEUID. */
713 #ifdef HAVE_SETEGID
714 if (setegid (user_gid) < 0)
715 pfatal_with_name ("user_access: setegid");
716 #else
717 #ifndef HAVE_SETREGID
718 if (setgid (user_gid) < 0)
719 pfatal_with_name ("user_access: setgid");
720 #else
721 if (setregid (make_gid, user_gid) < 0)
722 pfatal_with_name ("user_access: setregid");
723 #endif
724 #endif
726 current_access = user;
728 log_access (_("User access"));
730 #endif /* GETLOADAVG_PRIVILEGED */
733 /* Give the process appropriate permissions for access to
734 make data (i.e., the load average). */
735 void
736 make_access (void)
738 #ifdef GETLOADAVG_PRIVILEGED
740 if (!access_inited)
741 init_access ();
743 if (current_access == make)
744 return;
746 /* See comments in user_access, above. */
748 #ifdef HAVE_SETEUID
749 if (seteuid (make_uid) < 0)
750 pfatal_with_name ("make_access: seteuid");
751 #else
752 #ifndef HAVE_SETREUID
753 if (setuid (make_uid) < 0)
754 pfatal_with_name ("make_access: setuid");
755 #else
756 if (setreuid (user_uid, make_uid) < 0)
757 pfatal_with_name ("make_access: setreuid");
758 #endif
759 #endif
761 #ifdef HAVE_SETEGID
762 if (setegid (make_gid) < 0)
763 pfatal_with_name ("make_access: setegid");
764 #else
765 #ifndef HAVE_SETREGID
766 if (setgid (make_gid) < 0)
767 pfatal_with_name ("make_access: setgid");
768 #else
769 if (setregid (user_gid, make_gid) < 0)
770 pfatal_with_name ("make_access: setregid");
771 #endif
772 #endif
774 current_access = make;
776 log_access (_("Make access"));
778 #endif /* GETLOADAVG_PRIVILEGED */
781 /* Give the process appropriate permissions for a child process.
782 This is like user_access, but you can't get back to make_access. */
783 void
784 child_access (void)
786 #ifdef GETLOADAVG_PRIVILEGED
788 if (!access_inited)
789 abort ();
791 /* Set both the real and effective UID and GID to the user's.
792 They cannot be changed back to make's. */
794 #ifndef HAVE_SETREUID
795 if (setuid (user_uid) < 0)
796 pfatal_with_name ("child_access: setuid");
797 #else
798 if (setreuid (user_uid, user_uid) < 0)
799 pfatal_with_name ("child_access: setreuid");
800 #endif
802 #ifndef HAVE_SETREGID
803 if (setgid (user_gid) < 0)
804 pfatal_with_name ("child_access: setgid");
805 #else
806 if (setregid (user_gid, user_gid) < 0)
807 pfatal_with_name ("child_access: setregid");
808 #endif
810 log_access (_("Child access"));
812 #endif /* GETLOADAVG_PRIVILEGED */
815 #ifdef NEED_GET_PATH_MAX
816 unsigned int
817 get_path_max (void)
819 static unsigned int value;
821 if (value == 0)
823 long int x = pathconf ("/", _PC_PATH_MAX);
824 if (x > 0)
825 value = x;
826 else
827 return MAXPATHLEN;
830 return value;
832 #endif