(main): Add some braces.
authorJim Meyering <jim@meyering.net>
Sat, 13 Dec 1997 23:39:09 +0000 (23:39 +0000)
committerJim Meyering <jim@meyering.net>
Sat, 13 Dec 1997 23:39:09 +0000 (23:39 +0000)
Check return code from fclose of each input file.
Close stdout and check for errors.

src/fmt.c

index 29b426f..5323896 100644 (file)
--- a/src/fmt.c
+++ b/src/fmt.c
@@ -323,7 +323,6 @@ int
 main (register int argc, register char **argv)
 {
   int optchar;
-  FILE *infile;
 
   program_name = argv[0];
   setlocale (LC_ALL, "");
@@ -409,20 +408,30 @@ main (register int argc, register char **argv)
   if (optind == argc)
     fmt (stdin);
   else
-    for (; optind < argc; optind++)
-      if (strcmp (argv[optind], "-") == 0)
-       fmt (stdin);
-      else
+    {
+      for (; optind < argc; optind++)
        {
-         infile = fopen (argv[optind], "r");
-         if (infile != NULL)
+         char *file = argv[optind];
+         if (strcmp (file, "-") == 0)
+           fmt (stdin);
+         else
            {
-             fmt (infile);
-             fclose (infile);
+             FILE *in_stream;
+             in_stream = fopen (file, "r");
+             if (in_stream != NULL)
+               {
+                 fmt (in_stream);
+                 if (fclose (in_stream) == EOF)
+                   error (EXIT_FAILURE, errno, file);
+               }
+             else
+               error (0, errno, file);
            }
-         else
-           error (0, errno, argv[optind]);
        }
+    }
+
+  if (ferror (stdout) || fclose (stdout) == EOF)
+    error (EXIT_FAILURE, errno, _("write error"));
 
   exit (EXIT_SUCCESS);
 }