Modify backslash/newline handling for POSIX.
[make.git] / misc.c
blob0ea56bb344f55248cc6b48887c8c779980e47ce0
1 /* Miscellaneous generic support functions for GNU Make.
2 Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011,
4 2012 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 (backslash)
117 /* Backslash/newline handling:
118 In traditional GNU make all trailing whitespace, consecutive
119 backslash/newlines, and any leading whitespace on the next line
120 is reduced to a single space.
121 In POSIX, each backslash/newline and is replaced by a space. */
122 in = next_token (in);
123 if (! posix_pedantic)
124 while (out > line && isblank ((unsigned char)out[-1]))
125 --out;
126 *out++ = ' ';
128 else
129 /* If the newline isn't quoted, put it in the output. */
130 *out++ = '\n';
132 /* Now copy the following line to the output.
133 Stop when we find backslashes followed by a newline. */
134 while (*in != '\0')
135 if (*in == '\\')
137 p = in + 1;
138 while (*p == '\\')
139 ++p;
140 if (*p == '\n')
142 in = p;
143 break;
145 while (in < p)
146 *out++ = *in++;
148 else
149 *out++ = *in++;
152 *out = '\0';
155 /* Print N spaces (used in debug for target-depth). */
157 void
158 print_spaces (unsigned int n)
160 while (n-- > 0)
161 putchar (' ');
165 /* Return a string whose contents concatenate the NUM strings provided
166 This string lives in static, re-used memory. */
168 const char *
169 #if HAVE_ANSI_COMPILER && USE_VARIADIC && HAVE_STDARG_H
170 concat (unsigned int num, ...)
171 #else
172 concat (num, va_alist)
173 unsigned int num;
174 va_dcl
175 #endif
177 static unsigned int rlen = 0;
178 static char *result = NULL;
179 unsigned int ri = 0;
181 #if USE_VARIADIC
182 va_list args;
183 #endif
185 VA_START (args, num);
187 while (num-- > 0)
189 const char *s = va_arg (args, const char *);
190 unsigned int l = s ? strlen (s) : 0;
192 if (l == 0)
193 continue;
195 if (ri + l > rlen)
197 rlen = ((rlen ? rlen : 60) + l) * 2;
198 result = xrealloc (result, rlen);
201 memcpy (result + ri, s, l);
202 ri += l;
205 VA_END (args);
207 /* Get some more memory if we don't have enough space for the
208 terminating '\0'. */
209 if (ri == rlen)
211 rlen = (rlen ? rlen : 60) * 2;
212 result = xrealloc (result, rlen);
215 result[ri] = '\0';
217 return result;
220 /* Print a message on stdout. */
222 void
223 #if HAVE_ANSI_COMPILER && USE_VARIADIC && HAVE_STDARG_H
224 message (int prefix, const char *fmt, ...)
225 #else
226 message (prefix, fmt, va_alist)
227 int prefix;
228 const char *fmt;
229 va_dcl
230 #endif
232 #if USE_VARIADIC
233 va_list args;
234 #endif
236 log_working_directory (1);
238 if (fmt != 0)
240 if (prefix)
242 if (makelevel == 0)
243 printf ("%s: ", program);
244 else
245 printf ("%s[%u]: ", program, makelevel);
247 VA_START (args, fmt);
248 VA_PRINTF (stdout, fmt, args);
249 VA_END (args);
250 putchar ('\n');
253 fflush (stdout);
256 /* Print an error message. */
258 void
259 #if HAVE_ANSI_COMPILER && USE_VARIADIC && HAVE_STDARG_H
260 error (const struct floc *flocp, const char *fmt, ...)
261 #else
262 error (flocp, fmt, va_alist)
263 const struct floc *flocp;
264 const char *fmt;
265 va_dcl
266 #endif
268 #if USE_VARIADIC
269 va_list args;
270 #endif
272 log_working_directory (1);
274 if (flocp && flocp->filenm)
275 fprintf (stderr, "%s:%lu: ", flocp->filenm, flocp->lineno);
276 else if (makelevel == 0)
277 fprintf (stderr, "%s: ", program);
278 else
279 fprintf (stderr, "%s[%u]: ", program, makelevel);
281 VA_START(args, fmt);
282 VA_PRINTF (stderr, fmt, args);
283 VA_END (args);
285 putc ('\n', stderr);
286 fflush (stderr);
289 /* Print an error message and exit. */
291 void
292 #if HAVE_ANSI_COMPILER && USE_VARIADIC && HAVE_STDARG_H
293 fatal (const struct floc *flocp, const char *fmt, ...)
294 #else
295 fatal (flocp, fmt, va_alist)
296 const struct floc *flocp;
297 const char *fmt;
298 va_dcl
299 #endif
301 #if USE_VARIADIC
302 va_list args;
303 #endif
305 log_working_directory (1);
307 if (flocp && flocp->filenm)
308 fprintf (stderr, "%s:%lu: *** ", flocp->filenm, flocp->lineno);
309 else if (makelevel == 0)
310 fprintf (stderr, "%s: *** ", program);
311 else
312 fprintf (stderr, "%s[%u]: *** ", program, makelevel);
314 VA_START(args, fmt);
315 VA_PRINTF (stderr, fmt, args);
316 VA_END (args);
318 fputs (_(". Stop.\n"), stderr);
320 die (2);
323 #ifndef HAVE_STRERROR
325 #undef strerror
327 char *
328 strerror (int errnum)
330 extern int errno, sys_nerr;
331 #ifndef __DECC
332 extern char *sys_errlist[];
333 #endif
334 static char buf[] = "Unknown error 12345678901234567890";
336 if (errno < sys_nerr)
337 return sys_errlist[errnum];
339 sprintf (buf, _("Unknown error %d"), errnum);
340 return buf;
342 #endif
344 /* Print an error message from errno. */
346 void
347 perror_with_name (const char *str, const char *name)
349 error (NILF, _("%s%s: %s"), str, name, strerror (errno));
352 /* Print an error message from errno and exit. */
354 void
355 pfatal_with_name (const char *name)
357 fatal (NILF, _("%s: %s"), name, strerror (errno));
359 /* NOTREACHED */
362 /* Like malloc but get fatal error if memory is exhausted. */
363 /* Don't bother if we're using dmalloc; it provides these for us. */
365 #ifndef HAVE_DMALLOC_H
367 #undef xmalloc
368 #undef xcalloc
369 #undef xrealloc
370 #undef xstrdup
372 void *
373 xmalloc (unsigned int size)
375 /* Make sure we don't allocate 0, for pre-ISO implementations. */
376 void *result = malloc (size ? size : 1);
377 if (result == 0)
378 fatal (NILF, _("virtual memory exhausted"));
379 return result;
383 void *
384 xcalloc (unsigned int size)
386 /* Make sure we don't allocate 0, for pre-ISO implementations. */
387 void *result = calloc (size ? size : 1, 1);
388 if (result == 0)
389 fatal (NILF, _("virtual memory exhausted"));
390 return result;
394 void *
395 xrealloc (void *ptr, unsigned int size)
397 void *result;
399 /* Some older implementations of realloc() don't conform to ISO. */
400 if (! size)
401 size = 1;
402 result = ptr ? realloc (ptr, size) : malloc (size);
403 if (result == 0)
404 fatal (NILF, _("virtual memory exhausted"));
405 return result;
409 char *
410 xstrdup (const char *ptr)
412 char *result;
414 #ifdef HAVE_STRDUP
415 result = strdup (ptr);
416 #else
417 result = malloc (strlen (ptr) + 1);
418 #endif
420 if (result == 0)
421 fatal (NILF, _("virtual memory exhausted"));
423 #ifdef HAVE_STRDUP
424 return result;
425 #else
426 return strcpy (result, ptr);
427 #endif
430 #endif /* HAVE_DMALLOC_H */
432 char *
433 xstrndup (const char *str, unsigned int length)
435 char *result;
437 #ifdef HAVE_STRNDUP
438 result = strndup (str, length);
439 if (result == 0)
440 fatal (NILF, _("virtual memory exhausted"));
441 #else
442 result = xmalloc (length + 1);
443 if (length > 0)
444 strncpy (result, str, length);
445 result[length] = '\0';
446 #endif
448 return result;
452 /* Limited INDEX:
453 Search through the string STRING, which ends at LIMIT, for the character C.
454 Returns a pointer to the first occurrence, or nil if none is found.
455 Like INDEX except that the string searched ends where specified
456 instead of at the first null. */
458 char *
459 lindex (const char *s, const char *limit, int c)
461 while (s < limit)
462 if (*s++ == c)
463 return (char *)(s - 1);
465 return 0;
468 /* Return the address of the first whitespace or null in the string S. */
470 char *
471 end_of_token (const char *s)
473 while (*s != '\0' && !isblank ((unsigned char)*s))
474 ++s;
475 return (char *)s;
478 #ifdef WINDOWS32
480 * Same as end_of_token, but take into account a stop character
482 char *
483 end_of_token_w32 (const char *s, char stopchar)
485 const char *p = s;
486 int backslash = 0;
488 while (*p != '\0' && *p != stopchar
489 && (backslash || !isblank ((unsigned char)*p)))
491 if (*p++ == '\\')
493 backslash = !backslash;
494 while (*p == '\\')
496 backslash = !backslash;
497 ++p;
500 else
501 backslash = 0;
504 return (char *)p;
506 #endif
508 /* Return the address of the first nonwhitespace or null in the string S. */
510 char *
511 next_token (const char *s)
513 while (isblank ((unsigned char)*s))
514 ++s;
515 return (char *)s;
518 /* Find the next token in PTR; return the address of it, and store the length
519 of the token into *LENGTHPTR if LENGTHPTR is not nil. Set *PTR to the end
520 of the token, so this function can be called repeatedly in a loop. */
522 char *
523 find_next_token (const char **ptr, unsigned int *lengthptr)
525 const char *p = next_token (*ptr);
527 if (*p == '\0')
528 return 0;
530 *ptr = end_of_token (p);
531 if (lengthptr != 0)
532 *lengthptr = *ptr - p;
534 return (char *)p;
538 /* Copy a chain of `struct dep'. For 2nd expansion deps, dup the name. */
540 struct dep *
541 copy_dep_chain (const struct dep *d)
543 struct dep *firstnew = 0;
544 struct dep *lastnew = 0;
546 while (d != 0)
548 struct dep *c = xmalloc (sizeof (struct dep));
549 memcpy (c, d, sizeof (struct dep));
551 if (c->need_2nd_expansion)
552 c->name = xstrdup (c->name);
554 c->next = 0;
555 if (firstnew == 0)
556 firstnew = lastnew = c;
557 else
558 lastnew = lastnew->next = c;
560 d = d->next;
563 return firstnew;
566 /* Free a chain of 'struct dep'. */
568 void
569 free_dep_chain (struct dep *d)
571 while (d != 0)
573 struct dep *df = d;
574 d = d->next;
575 free_dep (df);
579 /* Free a chain of struct nameseq.
580 For struct dep chains use free_dep_chain. */
582 void
583 free_ns_chain (struct nameseq *ns)
585 while (ns != 0)
587 struct nameseq *t = ns;
588 ns = ns->next;
589 free (t);
594 #if !HAVE_STRCASECMP && !HAVE_STRICMP && !HAVE_STRCMPI
596 /* If we don't have strcasecmp() (from POSIX), or anything that can substitute
597 for it, define our own version. */
600 strcasecmp (const char *s1, const char *s2)
602 while (1)
604 int c1 = (int) *(s1++);
605 int c2 = (int) *(s2++);
607 if (isalpha (c1))
608 c1 = tolower (c1);
609 if (isalpha (c2))
610 c2 = tolower (c2);
612 if (c1 != '\0' && c1 == c2)
613 continue;
615 return (c1 - c2);
618 #endif
620 #if !HAVE_STRNCASECMP && !HAVE_STRNICMP && !HAVE_STRNCMPI
622 /* If we don't have strncasecmp() (from POSIX), or anything that can
623 substitute for it, define our own version. */
626 strncasecmp (const char *s1, const char *s2, int n)
628 while (n-- > 0)
630 int c1 = (int) *(s1++);
631 int c2 = (int) *(s2++);
633 if (isalpha (c1))
634 c1 = tolower (c1);
635 if (isalpha (c2))
636 c2 = tolower (c2);
638 if (c1 != '\0' && c1 == c2)
639 continue;
641 return (c1 - c2);
644 return 0;
646 #endif
648 #ifdef GETLOADAVG_PRIVILEGED
650 #ifdef POSIX
652 /* Hopefully if a system says it's POSIX.1 and has the setuid and setgid
653 functions, they work as POSIX.1 says. Some systems (Alpha OSF/1 1.2,
654 for example) which claim to be POSIX.1 also have the BSD setreuid and
655 setregid functions, but they don't work as in BSD and only the POSIX.1
656 way works. */
658 #undef HAVE_SETREUID
659 #undef HAVE_SETREGID
661 #else /* Not POSIX. */
663 /* Some POSIX.1 systems have the seteuid and setegid functions. In a
664 POSIX-like system, they are the best thing to use. However, some
665 non-POSIX systems have them too but they do not work in the POSIX style
666 and we must use setreuid and setregid instead. */
668 #undef HAVE_SETEUID
669 #undef HAVE_SETEGID
671 #endif /* POSIX. */
673 #ifndef HAVE_UNISTD_H
674 extern int getuid (), getgid (), geteuid (), getegid ();
675 extern int setuid (), setgid ();
676 #ifdef HAVE_SETEUID
677 extern int seteuid ();
678 #else
679 #ifdef HAVE_SETREUID
680 extern int setreuid ();
681 #endif /* Have setreuid. */
682 #endif /* Have seteuid. */
683 #ifdef HAVE_SETEGID
684 extern int setegid ();
685 #else
686 #ifdef HAVE_SETREGID
687 extern int setregid ();
688 #endif /* Have setregid. */
689 #endif /* Have setegid. */
690 #endif /* No <unistd.h>. */
692 /* Keep track of the user and group IDs for user- and make- access. */
693 static int user_uid = -1, user_gid = -1, make_uid = -1, make_gid = -1;
694 #define access_inited (user_uid != -1)
695 static enum { make, user } current_access;
698 /* Under -d, write a message describing the current IDs. */
700 static void
701 log_access (const char *flavor)
703 if (! ISDB (DB_JOBS))
704 return;
706 /* All the other debugging messages go to stdout,
707 but we write this one to stderr because it might be
708 run in a child fork whose stdout is piped. */
710 fprintf (stderr, _("%s: user %lu (real %lu), group %lu (real %lu)\n"),
711 flavor, (unsigned long) geteuid (), (unsigned long) getuid (),
712 (unsigned long) getegid (), (unsigned long) getgid ());
713 fflush (stderr);
717 static void
718 init_access (void)
720 #ifndef VMS
721 user_uid = getuid ();
722 user_gid = getgid ();
724 make_uid = geteuid ();
725 make_gid = getegid ();
727 /* Do these ever fail? */
728 if (user_uid == -1 || user_gid == -1 || make_uid == -1 || make_gid == -1)
729 pfatal_with_name ("get{e}[gu]id");
731 log_access (_("Initialized access"));
733 current_access = make;
734 #endif
737 #endif /* GETLOADAVG_PRIVILEGED */
739 /* Give the process appropriate permissions for access to
740 user data (i.e., to stat files, or to spawn a child process). */
741 void
742 user_access (void)
744 #ifdef GETLOADAVG_PRIVILEGED
746 if (!access_inited)
747 init_access ();
749 if (current_access == user)
750 return;
752 /* We are in "make access" mode. This means that the effective user and
753 group IDs are those of make (if it was installed setuid or setgid).
754 We now want to set the effective user and group IDs to the real IDs,
755 which are the IDs of the process that exec'd make. */
757 #ifdef HAVE_SETEUID
759 /* Modern systems have the seteuid/setegid calls which set only the
760 effective IDs, which is ideal. */
762 if (seteuid (user_uid) < 0)
763 pfatal_with_name ("user_access: seteuid");
765 #else /* Not HAVE_SETEUID. */
767 #ifndef HAVE_SETREUID
769 /* System V has only the setuid/setgid calls to set user/group IDs.
770 There is an effective ID, which can be set by setuid/setgid.
771 It can be set (unless you are root) only to either what it already is
772 (returned by geteuid/getegid, now in make_uid/make_gid),
773 the real ID (return by getuid/getgid, now in user_uid/user_gid),
774 or the saved set ID (what the effective ID was before this set-ID
775 executable (make) was exec'd). */
777 if (setuid (user_uid) < 0)
778 pfatal_with_name ("user_access: setuid");
780 #else /* HAVE_SETREUID. */
782 /* In 4BSD, the setreuid/setregid calls set both the real and effective IDs.
783 They may be set to themselves or each other. So you have two alternatives
784 at any one time. If you use setuid/setgid, the effective will be set to
785 the real, leaving only one alternative. Using setreuid/setregid, however,
786 you can toggle between your two alternatives by swapping the values in a
787 single setreuid or setregid call. */
789 if (setreuid (make_uid, user_uid) < 0)
790 pfatal_with_name ("user_access: setreuid");
792 #endif /* Not HAVE_SETREUID. */
793 #endif /* HAVE_SETEUID. */
795 #ifdef HAVE_SETEGID
796 if (setegid (user_gid) < 0)
797 pfatal_with_name ("user_access: setegid");
798 #else
799 #ifndef HAVE_SETREGID
800 if (setgid (user_gid) < 0)
801 pfatal_with_name ("user_access: setgid");
802 #else
803 if (setregid (make_gid, user_gid) < 0)
804 pfatal_with_name ("user_access: setregid");
805 #endif
806 #endif
808 current_access = user;
810 log_access (_("User access"));
812 #endif /* GETLOADAVG_PRIVILEGED */
815 /* Give the process appropriate permissions for access to
816 make data (i.e., the load average). */
817 void
818 make_access (void)
820 #ifdef GETLOADAVG_PRIVILEGED
822 if (!access_inited)
823 init_access ();
825 if (current_access == make)
826 return;
828 /* See comments in user_access, above. */
830 #ifdef HAVE_SETEUID
831 if (seteuid (make_uid) < 0)
832 pfatal_with_name ("make_access: seteuid");
833 #else
834 #ifndef HAVE_SETREUID
835 if (setuid (make_uid) < 0)
836 pfatal_with_name ("make_access: setuid");
837 #else
838 if (setreuid (user_uid, make_uid) < 0)
839 pfatal_with_name ("make_access: setreuid");
840 #endif
841 #endif
843 #ifdef HAVE_SETEGID
844 if (setegid (make_gid) < 0)
845 pfatal_with_name ("make_access: setegid");
846 #else
847 #ifndef HAVE_SETREGID
848 if (setgid (make_gid) < 0)
849 pfatal_with_name ("make_access: setgid");
850 #else
851 if (setregid (user_gid, make_gid) < 0)
852 pfatal_with_name ("make_access: setregid");
853 #endif
854 #endif
856 current_access = make;
858 log_access (_("Make access"));
860 #endif /* GETLOADAVG_PRIVILEGED */
863 /* Give the process appropriate permissions for a child process.
864 This is like user_access, but you can't get back to make_access. */
865 void
866 child_access (void)
868 #ifdef GETLOADAVG_PRIVILEGED
870 if (!access_inited)
871 abort ();
873 /* Set both the real and effective UID and GID to the user's.
874 They cannot be changed back to make's. */
876 #ifndef HAVE_SETREUID
877 if (setuid (user_uid) < 0)
878 pfatal_with_name ("child_access: setuid");
879 #else
880 if (setreuid (user_uid, user_uid) < 0)
881 pfatal_with_name ("child_access: setreuid");
882 #endif
884 #ifndef HAVE_SETREGID
885 if (setgid (user_gid) < 0)
886 pfatal_with_name ("child_access: setgid");
887 #else
888 if (setregid (user_gid, user_gid) < 0)
889 pfatal_with_name ("child_access: setregid");
890 #endif
892 log_access (_("Child access"));
894 #endif /* GETLOADAVG_PRIVILEGED */
897 #ifdef NEED_GET_PATH_MAX
898 unsigned int
899 get_path_max (void)
901 static unsigned int value;
903 if (value == 0)
905 long int x = pathconf ("/", _PC_PATH_MAX);
906 if (x > 0)
907 value = x;
908 else
909 return MAXPATHLEN;
912 return value;
914 #endif
917 /* This code is stolen from gnulib.
918 If/when we abandon the requirement to work with K&R compilers, we can
919 remove this (and perhaps other parts of GNU make!) and migrate to using
920 gnulib directly.
922 This is called only through atexit(), which means die() has already been
923 invoked. So, call exit() here directly. Apparently that works...?
926 /* Close standard output, exiting with status 'exit_failure' on failure.
927 If a program writes *anything* to stdout, that program should close
928 stdout and make sure that it succeeds before exiting. Otherwise,
929 suppose that you go to the extreme of checking the return status
930 of every function that does an explicit write to stdout. The last
931 printf can succeed in writing to the internal stream buffer, and yet
932 the fclose(stdout) could still fail (due e.g., to a disk full error)
933 when it tries to write out that buffered data. Thus, you would be
934 left with an incomplete output file and the offending program would
935 exit successfully. Even calling fflush is not always sufficient,
936 since some file systems (NFS and CODA) buffer written/flushed data
937 until an actual close call.
939 Besides, it's wasteful to check the return value from every call
940 that writes to stdout -- just let the internal stream state record
941 the failure. That's what the ferror test is checking below.
943 It's important to detect such failures and exit nonzero because many
944 tools (most notably `make' and other build-management systems) depend
945 on being able to detect failure in other tools via their exit status. */
947 void
948 close_stdout (void)
950 int prev_fail = ferror (stdout);
951 int fclose_fail = fclose (stdout);
953 if (prev_fail || fclose_fail)
955 if (fclose_fail)
956 error (NILF, _("write error: %s"), strerror (errno));
957 else
958 error (NILF, _("write error"));
959 exit (EXIT_FAILURE);