Imported Upstream version 3.82
[platform/upstream/make.git] / misc.c
1 /* Miscellaneous generic support functions for GNU Make.
2 Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
3 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
4 2010 Free Software Foundation, Inc.
5 This file is part of GNU Make.
6
7 GNU Make is free software; you can redistribute it and/or modify it under the
8 terms of the GNU General Public License as published by the Free Software
9 Foundation; either version 3 of the License, or (at your option) any later
10 version.
11
12 GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License along with
17 this program.  If not, see <http://www.gnu.org/licenses/>.  */
18
19 #include "make.h"
20 #include "dep.h"
21 #include "debug.h"
22
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.
27
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.  */
30
31 #if USE_VARIADIC
32 # if HAVE_STDARG_H
33 #  include <stdarg.h>
34 #  define VA_START(args, lastarg) va_start(args, lastarg)
35 # else
36 #  include <varargs.h>
37 #  define VA_START(args, lastarg) va_start(args)
38 # endif
39 # if HAVE_VPRINTF
40 #  define VA_PRINTF(fp, lastarg, args) vfprintf((fp), (lastarg), (args))
41 # else
42 #  define VA_PRINTF(fp, lastarg, args) _doprnt((lastarg), (args), (fp))
43 # endif
44 # define VA_END(args) va_end(args)
45 #else
46 /* We can't use any variadic interface! */
47 # define va_alist a1, a2, a3, a4, a5, a6, a7, a8
48 # define va_dcl char *a1, *a2, *a3, *a4, *a5, *a6, *a7, *a8;
49 # define VA_START(args, lastarg)
50 # define VA_PRINTF(fp, lastarg, args) fprintf((fp), (lastarg), va_alist)
51 # define VA_END(args)
52 #endif
53
54
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.  */
58
59 int
60 alpha_compare (const void *v1, const void *v2)
61 {
62   const char *s1 = *((char **)v1);
63   const char *s2 = *((char **)v2);
64
65   if (*s1 != *s2)
66     return *s1 - *s2;
67   return strcmp (s1, s2);
68 }
69 \f
70 /* Discard each backslash-newline combination from LINE.
71    Backslash-backslash-newline combinations become backslash-newlines.
72    This is done by copying the text at LINE into itself.  */
73
74 void
75 collapse_continuations (char *line)
76 {
77   register char *in, *out, *p;
78   register int backslash;
79   register unsigned int bs_write;
80
81   in = strchr (line, '\n');
82   if (in == 0)
83     return;
84
85   out = in;
86   while (out > line && out[-1] == '\\')
87     --out;
88
89   while (*in != '\0')
90     {
91       /* BS_WRITE gets the number of quoted backslashes at
92          the end just before IN, and BACKSLASH gets nonzero
93          if the next character is quoted.  */
94       backslash = 0;
95       bs_write = 0;
96       for (p = in - 1; p >= line && *p == '\\'; --p)
97         {
98           if (backslash)
99             ++bs_write;
100           backslash = !backslash;
101
102           /* It should be impossible to go back this far without exiting,
103              but if we do, we can't get the right answer.  */
104           if (in == out - 1)
105             abort ();
106         }
107
108       /* Output the appropriate number of backslashes.  */
109       while (bs_write-- > 0)
110         *out++ = '\\';
111
112       /* Skip the newline.  */
113       ++in;
114
115       /* If the newline is escaped, discard following whitespace leaving just
116          one space.  POSIX requires that each backslash/newline/following
117          whitespace sequence be reduced to a single space.  */
118       if (backslash)
119         {
120           in = next_token (in);
121           /* Removing this loop will fix Savannah bug #16670: do we want to? */
122           while (out > line && isblank ((unsigned char)out[-1]))
123             --out;
124           *out++ = ' ';
125         }
126       else
127         /* If the newline isn't quoted, put it in the output.  */
128         *out++ = '\n';
129
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 == '\\')
134           {
135             p = in + 1;
136             while (*p == '\\')
137               ++p;
138             if (*p == '\n')
139               {
140                 in = p;
141                 break;
142               }
143             while (in < p)
144               *out++ = *in++;
145           }
146         else
147           *out++ = *in++;
148     }
149
150   *out = '\0';
151 }
152 \f
153 /* Print N spaces (used in debug for target-depth).  */
154
155 void
156 print_spaces (unsigned int n)
157 {
158   while (n-- > 0)
159     putchar (' ');
160 }
161
162 \f
163 /* Return a string whose contents concatenate the NUM strings provided
164    This string lives in static, re-used memory.  */
165
166 const char *
167 #if HAVE_ANSI_COMPILER && USE_VARIADIC && HAVE_STDARG_H
168 concat (unsigned int num, ...)
169 #else
170 concat (num, va_alist)
171      unsigned int num;
172      va_dcl
173 #endif
174 {
175   static unsigned int rlen = 0;
176   static char *result = NULL;
177   int ri = 0;
178
179 #if USE_VARIADIC
180   va_list args;
181 #endif
182
183   VA_START (args, num);
184
185   while (num-- > 0)
186     {
187       const char *s = va_arg (args, const char *);
188       unsigned int l = s ? strlen (s) : 0;
189
190       if (l == 0)
191         continue;
192
193       if (ri + l > rlen)
194         {
195           rlen = ((rlen ? rlen : 60) + l) * 2;
196           result = xrealloc (result, rlen);
197         }
198
199       memcpy (result + ri, s, l);
200       ri += l;
201     }
202
203   VA_END (args);
204
205   /* Get some more memory if we don't have enough space for the
206      terminating '\0'.   */
207   if (ri == rlen)
208     {
209       rlen = (rlen ? rlen : 60) * 2;
210       result = xrealloc (result, rlen);
211     }
212
213   result[ri] = '\0';
214
215   return result;
216 }
217 \f
218 /* Print a message on stdout.  */
219
220 void
221 #if HAVE_ANSI_COMPILER && USE_VARIADIC && HAVE_STDARG_H
222 message (int prefix, const char *fmt, ...)
223 #else
224 message (prefix, fmt, va_alist)
225      int prefix;
226      const char *fmt;
227      va_dcl
228 #endif
229 {
230 #if USE_VARIADIC
231   va_list args;
232 #endif
233
234   log_working_directory (1);
235
236   if (fmt != 0)
237     {
238       if (prefix)
239         {
240           if (makelevel == 0)
241             printf ("%s: ", program);
242           else
243             printf ("%s[%u]: ", program, makelevel);
244         }
245       VA_START (args, fmt);
246       VA_PRINTF (stdout, fmt, args);
247       VA_END (args);
248       putchar ('\n');
249     }
250
251   fflush (stdout);
252 }
253
254 /* Print an error message.  */
255
256 void
257 #if HAVE_ANSI_COMPILER && USE_VARIADIC && HAVE_STDARG_H
258 error (const struct floc *flocp, const char *fmt, ...)
259 #else
260 error (flocp, fmt, va_alist)
261      const struct floc *flocp;
262      const char *fmt;
263      va_dcl
264 #endif
265 {
266 #if USE_VARIADIC
267   va_list args;
268 #endif
269
270   log_working_directory (1);
271
272   if (flocp && flocp->filenm)
273     fprintf (stderr, "%s:%lu: ", flocp->filenm, flocp->lineno);
274   else if (makelevel == 0)
275     fprintf (stderr, "%s: ", program);
276   else
277     fprintf (stderr, "%s[%u]: ", program, makelevel);
278
279   VA_START(args, fmt);
280   VA_PRINTF (stderr, fmt, args);
281   VA_END (args);
282
283   putc ('\n', stderr);
284   fflush (stderr);
285 }
286
287 /* Print an error message and exit.  */
288
289 void
290 #if HAVE_ANSI_COMPILER && USE_VARIADIC && HAVE_STDARG_H
291 fatal (const struct floc *flocp, const char *fmt, ...)
292 #else
293 fatal (flocp, fmt, va_alist)
294      const struct floc *flocp;
295      const char *fmt;
296      va_dcl
297 #endif
298 {
299 #if USE_VARIADIC
300   va_list args;
301 #endif
302
303   log_working_directory (1);
304
305   if (flocp && flocp->filenm)
306     fprintf (stderr, "%s:%lu: *** ", flocp->filenm, flocp->lineno);
307   else if (makelevel == 0)
308     fprintf (stderr, "%s: *** ", program);
309   else
310     fprintf (stderr, "%s[%u]: *** ", program, makelevel);
311
312   VA_START(args, fmt);
313   VA_PRINTF (stderr, fmt, args);
314   VA_END (args);
315
316   fputs (_(".  Stop.\n"), stderr);
317
318   die (2);
319 }
320
321 #ifndef HAVE_STRERROR
322
323 #undef  strerror
324
325 char *
326 strerror (int errnum)
327 {
328   extern int errno, sys_nerr;
329 #ifndef __DECC
330   extern char *sys_errlist[];
331 #endif
332   static char buf[] = "Unknown error 12345678901234567890";
333
334   if (errno < sys_nerr)
335     return sys_errlist[errnum];
336
337   sprintf (buf, _("Unknown error %d"), errnum);
338   return buf;
339 }
340 #endif
341
342 /* Print an error message from errno.  */
343
344 void
345 perror_with_name (const char *str, const char *name)
346 {
347   error (NILF, _("%s%s: %s"), str, name, strerror (errno));
348 }
349
350 /* Print an error message from errno and exit.  */
351
352 void
353 pfatal_with_name (const char *name)
354 {
355   fatal (NILF, _("%s: %s"), name, strerror (errno));
356
357   /* NOTREACHED */
358 }
359 \f
360 /* Like malloc but get fatal error if memory is exhausted.  */
361 /* Don't bother if we're using dmalloc; it provides these for us.  */
362
363 #ifndef HAVE_DMALLOC_H
364
365 #undef xmalloc
366 #undef xcalloc
367 #undef xrealloc
368 #undef xstrdup
369
370 void *
371 xmalloc (unsigned int size)
372 {
373   /* Make sure we don't allocate 0, for pre-ISO implementations.  */
374   void *result = malloc (size ? size : 1);
375   if (result == 0)
376     fatal (NILF, _("virtual memory exhausted"));
377   return result;
378 }
379
380
381 void *
382 xcalloc (unsigned int size)
383 {
384   /* Make sure we don't allocate 0, for pre-ISO implementations.  */
385   void *result = calloc (size ? size : 1, 1);
386   if (result == 0)
387     fatal (NILF, _("virtual memory exhausted"));
388   return result;
389 }
390
391
392 void *
393 xrealloc (void *ptr, unsigned int size)
394 {
395   void *result;
396
397   /* Some older implementations of realloc() don't conform to ISO.  */
398   if (! size)
399     size = 1;
400   result = ptr ? realloc (ptr, size) : malloc (size);
401   if (result == 0)
402     fatal (NILF, _("virtual memory exhausted"));
403   return result;
404 }
405
406
407 char *
408 xstrdup (const char *ptr)
409 {
410   char *result;
411
412 #ifdef HAVE_STRDUP
413   result = strdup (ptr);
414 #else
415   result = malloc (strlen (ptr) + 1);
416 #endif
417
418   if (result == 0)
419     fatal (NILF, _("virtual memory exhausted"));
420
421 #ifdef HAVE_STRDUP
422   return result;
423 #else
424   return strcpy (result, ptr);
425 #endif
426 }
427
428 #endif  /* HAVE_DMALLOC_H */
429
430 char *
431 xstrndup (const char *str, unsigned int length)
432 {
433   char *result;
434
435 #ifdef HAVE_STRNDUP
436   result = strndup (str, length);
437   if (result == 0)
438     fatal (NILF, _("virtual memory exhausted"));
439 #else
440   result = xmalloc (length + 1);
441   if (length > 0)
442     strncpy (result, str, length);
443   result[length] = '\0';
444 #endif
445
446   return result;
447 }
448 \f
449
450 /* Limited INDEX:
451    Search through the string STRING, which ends at LIMIT, for the character C.
452    Returns a pointer to the first occurrence, or nil if none is found.
453    Like INDEX except that the string searched ends where specified
454    instead of at the first null.  */
455
456 char *
457 lindex (const char *s, const char *limit, int c)
458 {
459   while (s < limit)
460     if (*s++ == c)
461       return (char *)(s - 1);
462
463   return 0;
464 }
465 \f
466 /* Return the address of the first whitespace or null in the string S.  */
467
468 char *
469 end_of_token (const char *s)
470 {
471   while (*s != '\0' && !isblank ((unsigned char)*s))
472     ++s;
473   return (char *)s;
474 }
475
476 #ifdef WINDOWS32
477 /*
478  * Same as end_of_token, but take into account a stop character
479  */
480 char *
481 end_of_token_w32 (const char *s, char stopchar)
482 {
483   const char *p = s;
484   int backslash = 0;
485
486   while (*p != '\0' && *p != stopchar
487          && (backslash || !isblank ((unsigned char)*p)))
488     {
489       if (*p++ == '\\')
490         {
491           backslash = !backslash;
492           while (*p == '\\')
493             {
494               backslash = !backslash;
495               ++p;
496             }
497         }
498       else
499         backslash = 0;
500     }
501
502   return (char *)p;
503 }
504 #endif
505
506 /* Return the address of the first nonwhitespace or null in the string S.  */
507
508 char *
509 next_token (const char *s)
510 {
511   while (isblank ((unsigned char)*s))
512     ++s;
513   return (char *)s;
514 }
515
516 /* Find the next token in PTR; return the address of it, and store the length
517    of the token into *LENGTHPTR if LENGTHPTR is not nil.  Set *PTR to the end
518    of the token, so this function can be called repeatedly in a loop.  */
519
520 char *
521 find_next_token (const char **ptr, unsigned int *lengthptr)
522 {
523   const char *p = next_token (*ptr);
524
525   if (*p == '\0')
526     return 0;
527
528   *ptr = end_of_token (p);
529   if (lengthptr != 0)
530     *lengthptr = *ptr - p;
531
532   return (char *)p;
533 }
534 \f
535
536 /* Copy a chain of `struct dep'.  For 2nd expansion deps, dup the name.  */
537
538 struct dep *
539 copy_dep_chain (const struct dep *d)
540 {
541   struct dep *firstnew = 0;
542   struct dep *lastnew = 0;
543
544   while (d != 0)
545     {
546       struct dep *c = xmalloc (sizeof (struct dep));
547       memcpy (c, d, sizeof (struct dep));
548
549       if (c->need_2nd_expansion)
550         c->name = xstrdup (c->name);
551
552       c->next = 0;
553       if (firstnew == 0)
554         firstnew = lastnew = c;
555       else
556         lastnew = lastnew->next = c;
557
558       d = d->next;
559     }
560
561   return firstnew;
562 }
563
564 /* Free a chain of 'struct dep'.  */
565
566 void
567 free_dep_chain (struct dep *d)
568 {
569   while (d != 0)
570     {
571       struct dep *df = d;
572       d = d->next;
573       free_dep (df);
574     }
575 }
576
577 /* Free a chain of struct nameseq.
578    For struct dep chains use free_dep_chain.  */
579
580 void
581 free_ns_chain (struct nameseq *ns)
582 {
583   while (ns != 0)
584     {
585       struct nameseq *t = ns;
586       ns = ns->next;
587       free (t);
588     }
589 }
590 \f
591
592 #if !HAVE_STRCASECMP && !HAVE_STRICMP && !HAVE_STRCMPI
593
594 /* If we don't have strcasecmp() (from POSIX), or anything that can substitute
595    for it, define our own version.  */
596
597 int
598 strcasecmp (const char *s1, const char *s2)
599 {
600   while (1)
601     {
602       int c1 = (int) *(s1++);
603       int c2 = (int) *(s2++);
604
605       if (isalpha (c1))
606         c1 = tolower (c1);
607       if (isalpha (c2))
608         c2 = tolower (c2);
609
610       if (c1 != '\0' && c1 == c2)
611         continue;
612
613       return (c1 - c2);
614     }
615 }
616 #endif
617
618 #if !HAVE_STRNCASECMP && !HAVE_STRNICMP && !HAVE_STRNCMPI
619
620 /* If we don't have strncasecmp() (from POSIX), or anything that can
621    substitute for it, define our own version.  */
622
623 int
624 strncasecmp (const char *s1, const char *s2, int n)
625 {
626   while (n-- > 0)
627     {
628       int c1 = (int) *(s1++);
629       int c2 = (int) *(s2++);
630
631       if (isalpha (c1))
632         c1 = tolower (c1);
633       if (isalpha (c2))
634         c2 = tolower (c2);
635
636       if (c1 != '\0' && c1 == c2)
637         continue;
638
639       return (c1 - c2);
640     }
641
642   return 0;
643 }
644 #endif
645 \f
646 #ifdef  GETLOADAVG_PRIVILEGED
647
648 #ifdef POSIX
649
650 /* Hopefully if a system says it's POSIX.1 and has the setuid and setgid
651    functions, they work as POSIX.1 says.  Some systems (Alpha OSF/1 1.2,
652    for example) which claim to be POSIX.1 also have the BSD setreuid and
653    setregid functions, but they don't work as in BSD and only the POSIX.1
654    way works.  */
655
656 #undef HAVE_SETREUID
657 #undef HAVE_SETREGID
658
659 #else   /* Not POSIX.  */
660
661 /* Some POSIX.1 systems have the seteuid and setegid functions.  In a
662    POSIX-like system, they are the best thing to use.  However, some
663    non-POSIX systems have them too but they do not work in the POSIX style
664    and we must use setreuid and setregid instead.  */
665
666 #undef HAVE_SETEUID
667 #undef HAVE_SETEGID
668
669 #endif  /* POSIX.  */
670
671 #ifndef HAVE_UNISTD_H
672 extern int getuid (), getgid (), geteuid (), getegid ();
673 extern int setuid (), setgid ();
674 #ifdef HAVE_SETEUID
675 extern int seteuid ();
676 #else
677 #ifdef  HAVE_SETREUID
678 extern int setreuid ();
679 #endif  /* Have setreuid.  */
680 #endif  /* Have seteuid.  */
681 #ifdef HAVE_SETEGID
682 extern int setegid ();
683 #else
684 #ifdef  HAVE_SETREGID
685 extern int setregid ();
686 #endif  /* Have setregid.  */
687 #endif  /* Have setegid.  */
688 #endif  /* No <unistd.h>.  */
689
690 /* Keep track of the user and group IDs for user- and make- access.  */
691 static int user_uid = -1, user_gid = -1, make_uid = -1, make_gid = -1;
692 #define access_inited   (user_uid != -1)
693 static enum { make, user } current_access;
694
695
696 /* Under -d, write a message describing the current IDs.  */
697
698 static void
699 log_access (const char *flavor)
700 {
701   if (! ISDB (DB_JOBS))
702     return;
703
704   /* All the other debugging messages go to stdout,
705      but we write this one to stderr because it might be
706      run in a child fork whose stdout is piped.  */
707
708   fprintf (stderr, _("%s: user %lu (real %lu), group %lu (real %lu)\n"),
709            flavor, (unsigned long) geteuid (), (unsigned long) getuid (),
710            (unsigned long) getegid (), (unsigned long) getgid ());
711   fflush (stderr);
712 }
713
714
715 static void
716 init_access (void)
717 {
718 #ifndef VMS
719   user_uid = getuid ();
720   user_gid = getgid ();
721
722   make_uid = geteuid ();
723   make_gid = getegid ();
724
725   /* Do these ever fail?  */
726   if (user_uid == -1 || user_gid == -1 || make_uid == -1 || make_gid == -1)
727     pfatal_with_name ("get{e}[gu]id");
728
729   log_access (_("Initialized access"));
730
731   current_access = make;
732 #endif
733 }
734
735 #endif  /* GETLOADAVG_PRIVILEGED */
736
737 /* Give the process appropriate permissions for access to
738    user data (i.e., to stat files, or to spawn a child process).  */
739 void
740 user_access (void)
741 {
742 #ifdef  GETLOADAVG_PRIVILEGED
743
744   if (!access_inited)
745     init_access ();
746
747   if (current_access == user)
748     return;
749
750   /* We are in "make access" mode.  This means that the effective user and
751      group IDs are those of make (if it was installed setuid or setgid).
752      We now want to set the effective user and group IDs to the real IDs,
753      which are the IDs of the process that exec'd make.  */
754
755 #ifdef  HAVE_SETEUID
756
757   /* Modern systems have the seteuid/setegid calls which set only the
758      effective IDs, which is ideal.  */
759
760   if (seteuid (user_uid) < 0)
761     pfatal_with_name ("user_access: seteuid");
762
763 #else   /* Not HAVE_SETEUID.  */
764
765 #ifndef HAVE_SETREUID
766
767   /* System V has only the setuid/setgid calls to set user/group IDs.
768      There is an effective ID, which can be set by setuid/setgid.
769      It can be set (unless you are root) only to either what it already is
770      (returned by geteuid/getegid, now in make_uid/make_gid),
771      the real ID (return by getuid/getgid, now in user_uid/user_gid),
772      or the saved set ID (what the effective ID was before this set-ID
773      executable (make) was exec'd).  */
774
775   if (setuid (user_uid) < 0)
776     pfatal_with_name ("user_access: setuid");
777
778 #else   /* HAVE_SETREUID.  */
779
780   /* In 4BSD, the setreuid/setregid calls set both the real and effective IDs.
781      They may be set to themselves or each other.  So you have two alternatives
782      at any one time.  If you use setuid/setgid, the effective will be set to
783      the real, leaving only one alternative.  Using setreuid/setregid, however,
784      you can toggle between your two alternatives by swapping the values in a
785      single setreuid or setregid call.  */
786
787   if (setreuid (make_uid, user_uid) < 0)
788     pfatal_with_name ("user_access: setreuid");
789
790 #endif  /* Not HAVE_SETREUID.  */
791 #endif  /* HAVE_SETEUID.  */
792
793 #ifdef  HAVE_SETEGID
794   if (setegid (user_gid) < 0)
795     pfatal_with_name ("user_access: setegid");
796 #else
797 #ifndef HAVE_SETREGID
798   if (setgid (user_gid) < 0)
799     pfatal_with_name ("user_access: setgid");
800 #else
801   if (setregid (make_gid, user_gid) < 0)
802     pfatal_with_name ("user_access: setregid");
803 #endif
804 #endif
805
806   current_access = user;
807
808   log_access (_("User access"));
809
810 #endif  /* GETLOADAVG_PRIVILEGED */
811 }
812
813 /* Give the process appropriate permissions for access to
814    make data (i.e., the load average).  */
815 void
816 make_access (void)
817 {
818 #ifdef  GETLOADAVG_PRIVILEGED
819
820   if (!access_inited)
821     init_access ();
822
823   if (current_access == make)
824     return;
825
826   /* See comments in user_access, above.  */
827
828 #ifdef  HAVE_SETEUID
829   if (seteuid (make_uid) < 0)
830     pfatal_with_name ("make_access: seteuid");
831 #else
832 #ifndef HAVE_SETREUID
833   if (setuid (make_uid) < 0)
834     pfatal_with_name ("make_access: setuid");
835 #else
836   if (setreuid (user_uid, make_uid) < 0)
837     pfatal_with_name ("make_access: setreuid");
838 #endif
839 #endif
840
841 #ifdef  HAVE_SETEGID
842   if (setegid (make_gid) < 0)
843     pfatal_with_name ("make_access: setegid");
844 #else
845 #ifndef HAVE_SETREGID
846   if (setgid (make_gid) < 0)
847     pfatal_with_name ("make_access: setgid");
848 #else
849   if (setregid (user_gid, make_gid) < 0)
850     pfatal_with_name ("make_access: setregid");
851 #endif
852 #endif
853
854   current_access = make;
855
856   log_access (_("Make access"));
857
858 #endif  /* GETLOADAVG_PRIVILEGED */
859 }
860
861 /* Give the process appropriate permissions for a child process.
862    This is like user_access, but you can't get back to make_access.  */
863 void
864 child_access (void)
865 {
866 #ifdef  GETLOADAVG_PRIVILEGED
867
868   if (!access_inited)
869     abort ();
870
871   /* Set both the real and effective UID and GID to the user's.
872      They cannot be changed back to make's.  */
873
874 #ifndef HAVE_SETREUID
875   if (setuid (user_uid) < 0)
876     pfatal_with_name ("child_access: setuid");
877 #else
878   if (setreuid (user_uid, user_uid) < 0)
879     pfatal_with_name ("child_access: setreuid");
880 #endif
881
882 #ifndef HAVE_SETREGID
883   if (setgid (user_gid) < 0)
884     pfatal_with_name ("child_access: setgid");
885 #else
886   if (setregid (user_gid, user_gid) < 0)
887     pfatal_with_name ("child_access: setregid");
888 #endif
889
890   log_access (_("Child access"));
891
892 #endif  /* GETLOADAVG_PRIVILEGED */
893 }
894 \f
895 #ifdef NEED_GET_PATH_MAX
896 unsigned int
897 get_path_max (void)
898 {
899   static unsigned int value;
900
901   if (value == 0)
902     {
903       long int x = pathconf ("/", _PC_PATH_MAX);
904       if (x > 0)
905         value = x;
906       else
907         return MAXPATHLEN;
908     }
909
910   return value;
911 }
912 #endif
913 \f
914
915 /* This code is stolen from gnulib.
916    If/when we abandon the requirement to work with K&R compilers, we can
917    remove this (and perhaps other parts of GNU make!) and migrate to using
918    gnulib directly.
919
920    This is called only through atexit(), which means die() has already been
921    invoked.  So, call exit() here directly.  Apparently that works...?
922 */
923
924 /* Close standard output, exiting with status 'exit_failure' on failure.
925    If a program writes *anything* to stdout, that program should close
926    stdout and make sure that it succeeds before exiting.  Otherwise,
927    suppose that you go to the extreme of checking the return status
928    of every function that does an explicit write to stdout.  The last
929    printf can succeed in writing to the internal stream buffer, and yet
930    the fclose(stdout) could still fail (due e.g., to a disk full error)
931    when it tries to write out that buffered data.  Thus, you would be
932    left with an incomplete output file and the offending program would
933    exit successfully.  Even calling fflush is not always sufficient,
934    since some file systems (NFS and CODA) buffer written/flushed data
935    until an actual close call.
936
937    Besides, it's wasteful to check the return value from every call
938    that writes to stdout -- just let the internal stream state record
939    the failure.  That's what the ferror test is checking below.
940
941    It's important to detect such failures and exit nonzero because many
942    tools (most notably `make' and other build-management systems) depend
943    on being able to detect failure in other tools via their exit status.  */
944
945 void
946 close_stdout (void)
947 {
948   int prev_fail = ferror (stdout);
949   int fclose_fail = fclose (stdout);
950
951   if (prev_fail || fclose_fail)
952     {
953       if (fclose_fail)
954         error (NILF, _("write error: %s"), strerror (errno));
955       else
956         error (NILF, _("write error"));
957       exit (EXIT_FAILURE);
958     }
959 }