maint: make-prime-list: do not ignore write failure
authorJim Meyering <meyering@redhat.com>
Sun, 23 Sep 2012 09:33:01 +0000 (11:33 +0200)
committerJim Meyering <meyering@redhat.com>
Thu, 4 Oct 2012 19:33:06 +0000 (21:33 +0200)
Even though this is just a helper program that is run solely to create
primes.h, it should not ignore a write failure.  Normally we would
simply call atexit (close_stdout), but we cannot do that from this
helper program, since it must be built before the generated header,
primes.h.  If we were to make the linking of make-prime-list depend
on libcoreutils.a, that would add all lib/*.o files to the list
of dependents of $(BUILT_HEADERS).  Then, since there is currently no
provision to ensure that a file like lib/stdio.h (another built header)
is built before the first lib/*.o file that also includes <stdio.h>,
some lib/*.o files would be built before lib/stdio.h and some after.
The former would provoke link failures due to undefined rpl_* functions.
* src/make-prime-list.c: Include <errno.h>.
(fclose): Undef, so that a definition to rpl_fclose does not
cause a link failure.
(main): Per the above, in this exceptional case, we check for fclose
and ferror failure manually, and don't worry about the ferror-only
failure case in which errno may not be relevant.

src/make-prime-list.c

index 724924b..b18b0e5 100644 (file)
@@ -23,6 +23,8 @@ this program.  If not, see http://www.gnu.org/licenses/.  */
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
+#include <errno.h>
+#undef fclose
 
 struct prime
 {
@@ -166,5 +168,11 @@ main (int argc, char **argv)
 
   output_primes (prime_list, nprimes);
 
+  if (ferror (stdout) + fclose (stdout))
+    {
+      fprintf (stderr, "write error: %s\n", strerror (errno));
+      return EXIT_FAILURE;
+    }
+
   return EXIT_SUCCESS;
 }