bug-coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

minor cleanup to reuse stdin / stdout in du and uniq


From: Paul Eggert
Subject: minor cleanup to reuse stdin / stdout in du and uniq
Date: Sat, 02 Jul 2005 23:30:04 -0700
User-agent: Gnus/5.1007 (Gnus v5.10.7) Emacs/21.4 (gnu/linux)

While looking into the *_safer functions I noticed a couple of minor
opportunities for code cleanup/simplification.  I installed this:

2005-07-02  Paul Eggert  <address@hidden>

        * src/du.c (main): Reuse stdin rather than opening a new stream.
        This saves a file descriptor.
        * src/uniq.c: Don't include stdio-safer.h; no longer needed.
        (writeline): Remove stream arg; we now always output to stdout.
        All callers changed.
        (check_file): Reuse stdout rather than opening a new stream.
        This saves a file descriptor.

Index: src/du.c
===================================================================
RCS file: /fetish/cu/src/du.c,v
retrieving revision 1.214
diff -p -u -r1.214 du.c
--- src/du.c    30 Jun 2005 16:47:38 -0000      1.214
+++ src/du.c    3 Jul 2005 06:08:37 -0000
@@ -941,8 +941,6 @@ main (int argc, char **argv)
 
   if (files_from)
     {
-      FILE *istream;
-
       /* When using --files0-from=F, you may not specify any files
         on the command-line.  */
       if (optind < argc)
@@ -953,14 +951,13 @@ main (int argc, char **argv)
          usage (EXIT_FAILURE);
        }
 
-      istream = (STREQ (files_from, "-") ? stdin : fopen (files_from, "r"));
-      if (istream == NULL)
+      if (! (STREQ (files_from, "-") || freopen (files_from, "r", stdin)))
        error (EXIT_FAILURE, errno, _("cannot open %s for reading"),
               quote (files_from));
 
       readtokens0_init (&tok);
 
-      if (! readtokens0 (istream, &tok) || fclose (istream) != 0)
+      if (! readtokens0 (stdin, &tok) || fclose (stdin) != 0)
        error (EXIT_FAILURE, 0, _("cannot read file names from %s"),
               quote (files_from));
 
Index: src/uniq.c
===================================================================
RCS file: /fetish/cu/src/uniq.c,v
retrieving revision 1.124
diff -p -u -r1.124 uniq.c
--- src/uniq.c  14 May 2005 07:58:37 -0000      1.124
+++ src/uniq.c  3 Jul 2005 06:08:38 -0000
@@ -30,7 +30,6 @@
 #include "hard-locale.h"
 #include "posixver.h"
 #include "quote.h"
-#include "stdio-safer.h"
 #include "xmemcoll.h"
 #include "xstrtol.h"
 #include "memcasecmp.h"
@@ -235,14 +234,14 @@ different (char *old, char *new, size_t 
     return oldlen != newlen || memcmp (old, new, oldlen);
 }
 
-/* Output the line in linebuffer LINE to stream STREAM
+/* Output the line in linebuffer LINE to standard output
    provided that the switches say it should be output.
    MATCH is true if the line matches the previous line.
    If requested, print the number of times it occurred, as well;
    LINECOUNT + 1 is the number of times that the line occurred. */
 
 static void
-writeline (struct linebuffer const *line, FILE *stream,
+writeline (struct linebuffer const *line,
           bool match, uintmax_t linecount)
 {
   if (! (linecount == 0 ? output_unique
@@ -251,9 +250,9 @@ writeline (struct linebuffer const *line
     return;
 
   if (countmode == count_occurrences)
-    fprintf (stream, "%7" PRIuMAX " ", linecount + 1);
+    printf ("%7" PRIuMAX " ", linecount + 1);
 
-  fwrite (line->buffer, sizeof (char), line->length, stream);
+  fwrite (line->buffer, sizeof (char), line->length, stdout);
 }
 
 /* Process input file INFILE with output to OUTFILE.
@@ -262,22 +261,13 @@ writeline (struct linebuffer const *line
 static void
 check_file (const char *infile, const char *outfile)
 {
-  FILE *ostream;
   struct linebuffer lb1, lb2;
   struct linebuffer *thisline, *prevline;
-  bool is_stdin = STREQ (infile, "-");
-  bool is_stdout = STREQ (outfile, "-");
 
-  if (!is_stdin && ! freopen (infile, "r", stdin))
+  if (! (STREQ (infile, "-") || freopen (infile, "r", stdin)))
     error (EXIT_FAILURE, errno, "%s", infile);
-  if (is_stdout)
-    ostream = stdout;
-  else
-    {
-      ostream = fopen_safer (outfile, "w");
-      if (! ostream)
-       error (EXIT_FAILURE, errno, "%s", outfile);
-    }
+  if (! (STREQ (outfile, "-") || freopen (outfile, "w", stdout)))
+    error (EXIT_FAILURE, errno, "%s", outfile);
 
   thisline = &lb1;
   prevline = &lb2;
@@ -309,7 +299,7 @@ check_file (const char *infile, const ch
              || different (thisfield, prevfield, thislen, prevlen))
            {
              fwrite (thisline->buffer, sizeof (char),
-                     thisline->length, ostream);
+                     thisline->length, stdout);
 
              SWAP_LINES (prevline, thisline);
              prevfield = thisfield;
@@ -364,13 +354,13 @@ check_file (const char *infile, const ch
                  if ((delimit_groups == DM_PREPEND)
                      || (delimit_groups == DM_SEPARATE
                          && !first_delimiter))
-                   putc ('\n', ostream);
+                   putchar ('\n');
                }
            }
 
          if (!match || output_later_repeated)
            {
-             writeline (prevline, ostream, match, match_count);
+             writeline (prevline, match, match_count);
              SWAP_LINES (prevline, thisline);
              prevfield = thisfield;
              prevlen = thislen;
@@ -379,17 +369,14 @@ check_file (const char *infile, const ch
            }
        }
 
-      writeline (prevline, ostream, false, match_count);
+      writeline (prevline, false, match_count);
     }
 
  closefiles:
   if (ferror (stdin) || fclose (stdin) != 0)
     error (EXIT_FAILURE, 0, _("error reading %s"), infile);
 
-  /* Check for errors and close ostream only if it's not stdout --
-     stdout is handled via the atexit-invoked close_stdout function.  */
-  if (!is_stdout && (ferror (ostream) || fclose (ostream) != 0))
-    error (EXIT_FAILURE, 0, _("error writing %s"), outfile);
+  /* stdout is handled via the atexit-invoked close_stdout function.  */
 
   free (lb1.buffer);
   free (lb2.buffer);




reply via email to

[Prev in Thread] Current Thread [Next in Thread]