Ensure variables defined in $(call ...) have global scope
[make.git] / misc.c
blob607e2a7e228ff348b89d2e3fcca8f96381751785
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.
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.
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.
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/>. */
19 #include "make.h"
20 #include "dep.h"
21 #include "debug.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 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
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 (const void *v1, const void *v2)
62 const char *s1 = *((char **)v1);
63 const char *s2 = *((char **)v2);
65 if (*s1 != *s2)
66 return *s1 - *s2;
67 return strcmp (s1, s2);
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. */
74 void
75 collapse_continuations (char *line)
77 register char *in, *out, *p;
78 register int backslash;
79 register unsigned int bs_write;
81 in = strchr (line, '\n');
82 if (in == 0)
83 return;
85 out = in;
86 while (out > line && out[-1] == '\\')
87 --out;
89 while (*in != '\0')
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)
98 if (backslash)
99 ++bs_write;
100 backslash = !backslash;
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 ();
108 /* Output the appropriate number of backslashes. */
109 while (bs_write-- > 0)
110 *out++ = '\\';
112 /* Skip the newline. */
113 ++in;
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)
120 in = next_token (in);
121 *out++ = ' ';
123 else
124 /* If the newline isn't quoted, put it in the output. */
125 *out++ = '\n';
127 /* Now copy the following line to the output.
128 Stop when we find backslashes followed by a newline. */
129 while (*in != '\0')
130 if (*in == '\\')
132 p = in + 1;
133 while (*p == '\\')
134 ++p;
135 if (*p == '\n')
137 in = p;
138 break;
140 while (in < p)
141 *out++ = *in++;
143 else
144 *out++ = *in++;
147 *out = '\0';
150 /* Print N spaces (used in debug for target-depth). */
152 void
153 print_spaces (unsigned int n)
155 while (n-- > 0)
156 putchar (' ');
160 /* Return a string whose contents concatenate the NUM strings provided
161 This string lives in static, re-used memory. */
163 const char *
164 #if HAVE_ANSI_COMPILER && USE_VARIADIC && HAVE_STDARG_H
165 concat (unsigned int num, ...)
166 #else
167 concat (num, va_alist)
168 unsigned int num;
169 va_dcl
170 #endif
172 static unsigned int rlen = 0;
173 static char *result = NULL;
174 int ri = 0;
176 #if USE_VARIADIC
177 va_list args;
178 #endif
180 VA_START (args, num);
182 while (num-- > 0)
184 const char *s = va_arg (args, const char *);
185 unsigned int l = s ? strlen (s) : 0;
187 if (l == 0)
188 continue;
190 if (ri + l > rlen)
192 rlen = ((rlen ? rlen : 60) + l) * 2;
193 result = xrealloc (result, rlen);
196 memcpy (result + ri, s, l);
197 ri += l;
200 VA_END (args);
202 /* Get some more memory if we don't have enough space for the
203 terminating '\0'. */
204 if (ri == rlen)
206 rlen = (rlen ? rlen : 60) * 2;
207 result = xrealloc (result, rlen);
210 result[ri] = '\0';
212 return result;
215 /* Print a message on stdout. */
217 void
218 #if HAVE_ANSI_COMPILER && USE_VARIADIC && HAVE_STDARG_H
219 message (int prefix, const char *fmt, ...)
220 #else
221 message (prefix, fmt, va_alist)
222 int prefix;
223 const char *fmt;
224 va_dcl
225 #endif
227 #if USE_VARIADIC
228 va_list args;
229 #endif
231 log_working_directory (1);
233 if (fmt != 0)
235 if (prefix)
237 if (makelevel == 0)
238 printf ("%s: ", program);
239 else
240 printf ("%s[%u]: ", program, makelevel);
242 VA_START (args, fmt);
243 VA_PRINTF (stdout, fmt, args);
244 VA_END (args);
245 putchar ('\n');
248 fflush (stdout);
251 /* Print an error message. */
253 void
254 #if HAVE_ANSI_COMPILER && USE_VARIADIC && HAVE_STDARG_H
255 error (const struct floc *flocp, const char *fmt, ...)
256 #else
257 error (flocp, fmt, va_alist)
258 const struct floc *flocp;
259 const char *fmt;
260 va_dcl
261 #endif
263 #if USE_VARIADIC
264 va_list args;
265 #endif
267 log_working_directory (1);
269 if (flocp && flocp->filenm)
270 fprintf (stderr, "%s:%lu: ", flocp->filenm, flocp->lineno);
271 else if (makelevel == 0)
272 fprintf (stderr, "%s: ", program);
273 else
274 fprintf (stderr, "%s[%u]: ", program, makelevel);
276 VA_START(args, fmt);
277 VA_PRINTF (stderr, fmt, args);
278 VA_END (args);
280 putc ('\n', stderr);
281 fflush (stderr);
284 /* Print an error message and exit. */
286 void
287 #if HAVE_ANSI_COMPILER && USE_VARIADIC && HAVE_STDARG_H
288 fatal (const struct floc *flocp, const char *fmt, ...)
289 #else
290 fatal (flocp, fmt, va_alist)
291 const struct floc *flocp;
292 const char *fmt;
293 va_dcl
294 #endif
296 #if USE_VARIADIC
297 va_list args;
298 #endif
300 log_working_directory (1);
302 if (flocp && flocp->filenm)
303 fprintf (stderr, "%s:%lu: *** ", flocp->filenm, flocp->lineno);
304 else if (makelevel == 0)
305 fprintf (stderr, "%s: *** ", program);
306 else
307 fprintf (stderr, "%s[%u]: *** ", program, makelevel);
309 VA_START(args, fmt);
310 VA_PRINTF (stderr, fmt, args);
311 VA_END (args);
313 fputs (_(". Stop.\n"), stderr);
315 die (2);
318 #ifndef HAVE_STRERROR
320 #undef strerror
322 char *
323 strerror (int errnum)
325 extern int errno, sys_nerr;
326 #ifndef __DECC
327 extern char *sys_errlist[];
328 #endif
329 static char buf[] = "Unknown error 12345678901234567890";
331 if (errno < sys_nerr)
332 return sys_errlist[errnum];
334 sprintf (buf, _("Unknown error %d"), errnum);
335 return buf;
337 #endif
339 /* Print an error message from errno. */
341 void
342 perror_with_name (const char *str, const char *name)
344 error (NILF, _("%s%s: %s"), str, name, strerror (errno));
347 /* Print an error message from errno and exit. */
349 void
350 pfatal_with_name (const char *name)
352 fatal (NILF, _("%s: %s"), name, strerror (errno));
354 /* NOTREACHED */
357 /* Like malloc but get fatal error if memory is exhausted. */
358 /* Don't bother if we're using dmalloc; it provides these for us. */
360 #ifndef HAVE_DMALLOC_H
362 #undef xmalloc
363 #undef xcalloc
364 #undef xrealloc
365 #undef xstrdup
367 void *
368 xmalloc (unsigned int size)
370 /* Make sure we don't allocate 0, for pre-ISO implementations. */
371 void *result = malloc (size ? size : 1);
372 if (result == 0)
373 fatal (NILF, _("virtual memory exhausted"));
374 return result;
378 void *
379 xcalloc (unsigned int size)
381 /* Make sure we don't allocate 0, for pre-ISO implementations. */
382 void *result = calloc (size ? size : 1, 1);
383 if (result == 0)
384 fatal (NILF, _("virtual memory exhausted"));
385 return result;
389 void *
390 xrealloc (void *ptr, unsigned int size)
392 void *result;
394 /* Some older implementations of realloc() don't conform to ISO. */
395 if (! size)
396 size = 1;
397 result = ptr ? realloc (ptr, size) : malloc (size);
398 if (result == 0)
399 fatal (NILF, _("virtual memory exhausted"));
400 return result;
404 char *
405 xstrdup (const char *ptr)
407 char *result;
409 #ifdef HAVE_STRDUP
410 result = strdup (ptr);
411 #else
412 result = malloc (strlen (ptr) + 1);
413 #endif
415 if (result == 0)
416 fatal (NILF, _("virtual memory exhausted"));
418 #ifdef HAVE_STRDUP
419 return result;
420 #else
421 return strcpy (result, ptr);
422 #endif
425 #endif /* HAVE_DMALLOC_H */
427 char *
428 xstrndup (const char *str, unsigned int length)
430 char *result;
432 #ifdef HAVE_STRNDUP
433 result = strndup (str, length);
434 if (result == 0)
435 fatal (NILF, _("virtual memory exhausted"));
436 #else
437 result = xmalloc (length + 1);
438 if (length > 0)
439 strncpy (result, str, length);
440 result[length] = '\0';
441 #endif
443 return result;
447 /* Limited INDEX:
448 Search through the string STRING, which ends at LIMIT, for the character C.
449 Returns a pointer to the first occurrence, or nil if none is found.
450 Like INDEX except that the string searched ends where specified
451 instead of at the first null. */
453 char *
454 lindex (const char *s, const char *limit, int c)
456 while (s < limit)
457 if (*s++ == c)
458 return (char *)(s - 1);
460 return 0;
463 /* Return the address of the first whitespace or null in the string S. */
465 char *
466 end_of_token (const char *s)
468 while (*s != '\0' && !isblank ((unsigned char)*s))
469 ++s;
470 return (char *)s;
473 #ifdef WINDOWS32
475 * Same as end_of_token, but take into account a stop character
477 char *
478 end_of_token_w32 (const char *s, char stopchar)
480 const char *p = s;
481 int backslash = 0;
483 while (*p != '\0' && *p != stopchar
484 && (backslash || !isblank ((unsigned char)*p)))
486 if (*p++ == '\\')
488 backslash = !backslash;
489 while (*p == '\\')
491 backslash = !backslash;
492 ++p;
495 else
496 backslash = 0;
499 return (char *)p;
501 #endif
503 /* Return the address of the first nonwhitespace or null in the string S. */
505 char *
506 next_token (const char *s)
508 while (isblank ((unsigned char)*s))
509 ++s;
510 return (char *)s;
513 /* Find the next token in PTR; return the address of it, and store the length
514 of the token into *LENGTHPTR if LENGTHPTR is not nil. Set *PTR to the end
515 of the token, so this function can be called repeatedly in a loop. */
517 char *
518 find_next_token (const char **ptr, unsigned int *lengthptr)
520 const char *p = next_token (*ptr);
522 if (*p == '\0')
523 return 0;
525 *ptr = end_of_token (p);
526 if (lengthptr != 0)
527 *lengthptr = *ptr - p;
529 return (char *)p;
533 /* Copy a chain of `struct dep'. For 2nd expansion deps, dup the name. */
535 struct dep *
536 copy_dep_chain (const struct dep *d)
538 struct dep *firstnew = 0;
539 struct dep *lastnew = 0;
541 while (d != 0)
543 struct dep *c = xmalloc (sizeof (struct dep));
544 memcpy (c, d, sizeof (struct dep));
546 if (c->need_2nd_expansion)
547 c->name = xstrdup (c->name);
549 c->next = 0;
550 if (firstnew == 0)
551 firstnew = lastnew = c;
552 else
553 lastnew = lastnew->next = c;
555 d = d->next;
558 return firstnew;
561 /* Free a chain of 'struct dep'. */
563 void
564 free_dep_chain (struct dep *d)
566 while (d != 0)
568 struct dep *df = d;
569 d = d->next;
570 free_dep (df);
574 /* Free a chain of struct nameseq.
575 For struct dep chains use free_dep_chain. */
577 void
578 free_ns_chain (struct nameseq *ns)
580 while (ns != 0)
582 struct nameseq *t = ns;
583 ns = ns->next;
584 free (t);
589 #if !HAVE_STRCASECMP && !HAVE_STRICMP && !HAVE_STRCMPI
591 /* If we don't have strcasecmp() (from POSIX), or anything that can substitute
592 for it, define our own version. */
595 strcasecmp (const char *s1, const char *s2)
597 while (1)
599 int c1 = (int) *(s1++);
600 int c2 = (int) *(s2++);
602 if (isalpha (c1))
603 c1 = tolower (c1);
604 if (isalpha (c2))
605 c2 = tolower (c2);
607 if (c1 != '\0' && c1 == c2)
608 continue;
610 return (c1 - c2);
613 #endif
615 #if !HAVE_STRNCASECMP && !HAVE_STRNICMP && !HAVE_STRNCMPI
617 /* If we don't have strncasecmp() (from POSIX), or anything that can
618 substitute for it, define our own version. */
621 strncasecmp (const char *s1, const char *s2, int n)
623 while (n-- > 0)
625 int c1 = (int) *(s1++);
626 int c2 = (int) *(s2++);
628 if (isalpha (c1))
629 c1 = tolower (c1);
630 if (isalpha (c2))
631 c2 = tolower (c2);
633 if (c1 != '\0' && c1 == c2)
634 continue;
636 return (c1 - c2);
639 return 0;
641 #endif
643 #ifdef GETLOADAVG_PRIVILEGED
645 #ifdef POSIX
647 /* Hopefully if a system says it's POSIX.1 and has the setuid and setgid
648 functions, they work as POSIX.1 says. Some systems (Alpha OSF/1 1.2,
649 for example) which claim to be POSIX.1 also have the BSD setreuid and
650 setregid functions, but they don't work as in BSD and only the POSIX.1
651 way works. */
653 #undef HAVE_SETREUID
654 #undef HAVE_SETREGID
656 #else /* Not POSIX. */
658 /* Some POSIX.1 systems have the seteuid and setegid functions. In a
659 POSIX-like system, they are the best thing to use. However, some
660 non-POSIX systems have them too but they do not work in the POSIX style
661 and we must use setreuid and setregid instead. */
663 #undef HAVE_SETEUID
664 #undef HAVE_SETEGID
666 #endif /* POSIX. */
668 #ifndef HAVE_UNISTD_H
669 extern int getuid (), getgid (), geteuid (), getegid ();
670 extern int setuid (), setgid ();
671 #ifdef HAVE_SETEUID
672 extern int seteuid ();
673 #else
674 #ifdef HAVE_SETREUID
675 extern int setreuid ();
676 #endif /* Have setreuid. */
677 #endif /* Have seteuid. */
678 #ifdef HAVE_SETEGID
679 extern int setegid ();
680 #else
681 #ifdef HAVE_SETREGID
682 extern int setregid ();
683 #endif /* Have setregid. */
684 #endif /* Have setegid. */
685 #endif /* No <unistd.h>. */
687 /* Keep track of the user and group IDs for user- and make- access. */
688 static int user_uid = -1, user_gid = -1, make_uid = -1, make_gid = -1;
689 #define access_inited (user_uid != -1)
690 static enum { make, user } current_access;
693 /* Under -d, write a message describing the current IDs. */
695 static void
696 log_access (const char *flavor)
698 if (! ISDB (DB_JOBS))
699 return;
701 /* All the other debugging messages go to stdout,
702 but we write this one to stderr because it might be
703 run in a child fork whose stdout is piped. */
705 fprintf (stderr, _("%s: user %lu (real %lu), group %lu (real %lu)\n"),
706 flavor, (unsigned long) geteuid (), (unsigned long) getuid (),
707 (unsigned long) getegid (), (unsigned long) getgid ());
708 fflush (stderr);
712 static void
713 init_access (void)
715 #ifndef VMS
716 user_uid = getuid ();
717 user_gid = getgid ();
719 make_uid = geteuid ();
720 make_gid = getegid ();
722 /* Do these ever fail? */
723 if (user_uid == -1 || user_gid == -1 || make_uid == -1 || make_gid == -1)
724 pfatal_with_name ("get{e}[gu]id");
726 log_access (_("Initialized access"));
728 current_access = make;
729 #endif
732 #endif /* GETLOADAVG_PRIVILEGED */
734 /* Give the process appropriate permissions for access to
735 user data (i.e., to stat files, or to spawn a child process). */
736 void
737 user_access (void)
739 #ifdef GETLOADAVG_PRIVILEGED
741 if (!access_inited)
742 init_access ();
744 if (current_access == user)
745 return;
747 /* We are in "make access" mode. This means that the effective user and
748 group IDs are those of make (if it was installed setuid or setgid).
749 We now want to set the effective user and group IDs to the real IDs,
750 which are the IDs of the process that exec'd make. */
752 #ifdef HAVE_SETEUID
754 /* Modern systems have the seteuid/setegid calls which set only the
755 effective IDs, which is ideal. */
757 if (seteuid (user_uid) < 0)
758 pfatal_with_name ("user_access: seteuid");
760 #else /* Not HAVE_SETEUID. */
762 #ifndef HAVE_SETREUID
764 /* System V has only the setuid/setgid calls to set user/group IDs.
765 There is an effective ID, which can be set by setuid/setgid.
766 It can be set (unless you are root) only to either what it already is
767 (returned by geteuid/getegid, now in make_uid/make_gid),
768 the real ID (return by getuid/getgid, now in user_uid/user_gid),
769 or the saved set ID (what the effective ID was before this set-ID
770 executable (make) was exec'd). */
772 if (setuid (user_uid) < 0)
773 pfatal_with_name ("user_access: setuid");
775 #else /* HAVE_SETREUID. */
777 /* In 4BSD, the setreuid/setregid calls set both the real and effective IDs.
778 They may be set to themselves or each other. So you have two alternatives
779 at any one time. If you use setuid/setgid, the effective will be set to
780 the real, leaving only one alternative. Using setreuid/setregid, however,
781 you can toggle between your two alternatives by swapping the values in a
782 single setreuid or setregid call. */
784 if (setreuid (make_uid, user_uid) < 0)
785 pfatal_with_name ("user_access: setreuid");
787 #endif /* Not HAVE_SETREUID. */
788 #endif /* HAVE_SETEUID. */
790 #ifdef HAVE_SETEGID
791 if (setegid (user_gid) < 0)
792 pfatal_with_name ("user_access: setegid");
793 #else
794 #ifndef HAVE_SETREGID
795 if (setgid (user_gid) < 0)
796 pfatal_with_name ("user_access: setgid");
797 #else
798 if (setregid (make_gid, user_gid) < 0)
799 pfatal_with_name ("user_access: setregid");
800 #endif
801 #endif
803 current_access = user;
805 log_access (_("User access"));
807 #endif /* GETLOADAVG_PRIVILEGED */
810 /* Give the process appropriate permissions for access to
811 make data (i.e., the load average). */
812 void
813 make_access (void)
815 #ifdef GETLOADAVG_PRIVILEGED
817 if (!access_inited)
818 init_access ();
820 if (current_access == make)
821 return;
823 /* See comments in user_access, above. */
825 #ifdef HAVE_SETEUID
826 if (seteuid (make_uid) < 0)
827 pfatal_with_name ("make_access: seteuid");
828 #else
829 #ifndef HAVE_SETREUID
830 if (setuid (make_uid) < 0)
831 pfatal_with_name ("make_access: setuid");
832 #else
833 if (setreuid (user_uid, make_uid) < 0)
834 pfatal_with_name ("make_access: setreuid");
835 #endif
836 #endif
838 #ifdef HAVE_SETEGID
839 if (setegid (make_gid) < 0)
840 pfatal_with_name ("make_access: setegid");
841 #else
842 #ifndef HAVE_SETREGID
843 if (setgid (make_gid) < 0)
844 pfatal_with_name ("make_access: setgid");
845 #else
846 if (setregid (user_gid, make_gid) < 0)
847 pfatal_with_name ("make_access: setregid");
848 #endif
849 #endif
851 current_access = make;
853 log_access (_("Make access"));
855 #endif /* GETLOADAVG_PRIVILEGED */
858 /* Give the process appropriate permissions for a child process.
859 This is like user_access, but you can't get back to make_access. */
860 void
861 child_access (void)
863 #ifdef GETLOADAVG_PRIVILEGED
865 if (!access_inited)
866 abort ();
868 /* Set both the real and effective UID and GID to the user's.
869 They cannot be changed back to make's. */
871 #ifndef HAVE_SETREUID
872 if (setuid (user_uid) < 0)
873 pfatal_with_name ("child_access: setuid");
874 #else
875 if (setreuid (user_uid, user_uid) < 0)
876 pfatal_with_name ("child_access: setreuid");
877 #endif
879 #ifndef HAVE_SETREGID
880 if (setgid (user_gid) < 0)
881 pfatal_with_name ("child_access: setgid");
882 #else
883 if (setregid (user_gid, user_gid) < 0)
884 pfatal_with_name ("child_access: setregid");
885 #endif
887 log_access (_("Child access"));
889 #endif /* GETLOADAVG_PRIVILEGED */
892 #ifdef NEED_GET_PATH_MAX
893 unsigned int
894 get_path_max (void)
896 static unsigned int value;
898 if (value == 0)
900 long int x = pathconf ("/", _PC_PATH_MAX);
901 if (x > 0)
902 value = x;
903 else
904 return MAXPATHLEN;
907 return value;
909 #endif
912 /* This code is stolen from gnulib.
913 If/when we abandon the requirement to work with K&R compilers, we can
914 remove this (and perhaps other parts of GNU make!) and migrate to using
915 gnulib directly.
917 This is called only through atexit(), which means die() has already been
918 invoked. So, call exit() here directly. Apparently that works...?
921 /* Close standard output, exiting with status 'exit_failure' on failure.
922 If a program writes *anything* to stdout, that program should close
923 stdout and make sure that it succeeds before exiting. Otherwise,
924 suppose that you go to the extreme of checking the return status
925 of every function that does an explicit write to stdout. The last
926 printf can succeed in writing to the internal stream buffer, and yet
927 the fclose(stdout) could still fail (due e.g., to a disk full error)
928 when it tries to write out that buffered data. Thus, you would be
929 left with an incomplete output file and the offending program would
930 exit successfully. Even calling fflush is not always sufficient,
931 since some file systems (NFS and CODA) buffer written/flushed data
932 until an actual close call.
934 Besides, it's wasteful to check the return value from every call
935 that writes to stdout -- just let the internal stream state record
936 the failure. That's what the ferror test is checking below.
938 It's important to detect such failures and exit nonzero because many
939 tools (most notably `make' and other build-management systems) depend
940 on being able to detect failure in other tools via their exit status. */
942 void
943 close_stdout (void)
945 int prev_fail = ferror (stdout);
946 int fclose_fail = fclose (stdout);
948 if (prev_fail || fclose_fail)
950 if (fclose_fail)
951 error (NILF, _("write error: %s"), strerror (errno));
952 else
953 error (NILF, _("write error"));
954 exit (EXIT_FAILURE);