]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
(check_file): Generate each line of output earlier,
authorJim Meyering <jim@meyering.net>
Sun, 22 Aug 1999 11:17:42 +0000 (11:17 +0000)
committerJim Meyering <jim@meyering.net>
Sun, 22 Aug 1999 11:17:42 +0000 (11:17 +0000)
when possible.  It is possible when using none of these options:
--count, -repeated, --all-repeated, --unique.
Based on a patch from Ian Turner.

(check_file): Move declarations of local variables into the scopes where used.
(min): Remove macro definition.
(different): Use MIN, not min.
(SWAP_LINES): New macro.
(check_file): Use it here.

src/uniq.c

index b17df336da6c12751b69551118fc1289f4c746a2..a3215646a3fca9e3c918991e1d0ebf78a75f21c6 100644 (file)
@@ -217,9 +217,6 @@ check_file (const char *infile, const char *outfile)
   FILE *ostream;
   struct linebuffer lb1, lb2;
   struct linebuffer *thisline, *prevline;
-  char *prevfield;
-  size_t prevlen;
-  int match_count = 0;
 
   if (STREQ (infile, "-"))
     istream = stdin;
@@ -241,37 +238,69 @@ check_file (const char *infile, const char *outfile)
   initbuffer (thisline);
   initbuffer (prevline);
 
-  if (readline (prevline, istream) == 0)
-    goto closefiles;
-  prevfield = find_field (prevline);
-  prevlen = prevline->length - (prevfield - prevline->buffer);
-
-  while (!feof (istream))
+  if (mode == output_all && countmode == count_none)
     {
-      int match;
-      char *thisfield;
-      size_t thislen;
-      if (readline (thisline, istream) == 0)
-       break;
-      thisfield = find_field (thisline);
-      thislen = thisline->length - (thisfield - thisline->buffer);
-      match = !different (thisfield, prevfield, thislen, prevlen);
-
-      if (match)
-       ++match_count;
-
-      if (!match || mode == output_all_repeated)
+      char *prevfield IF_LINT (= NULL);
+      size_t prevlen IF_LINT (= 0);
+
+      while (!feof (istream))
        {
-         writeline (prevline, ostream, match_count);
-         SWAP_LINES (prevline, thisline);
-         prevfield = thisfield;
-         prevlen = thislen;
-         if (!match)
-           match_count = 0;
+         char *thisfield;
+         size_t thislen;
+         if (readline (thisline, istream) == 0)
+           break;
+         thisfield = find_field (thisline);
+         thislen = thisline->length - (thisfield - thisline->buffer);
+         if (prevline->length == 0
+             || different (thisfield, prevfield, thislen, prevlen))
+           {
+             fwrite (thisline->buffer, sizeof (char),
+                     thisline->length, ostream);
+
+             SWAP_LINES (prevline, thisline);
+             prevfield = thisfield;
+             prevlen = thislen;
+           }
        }
     }
+  else
+    {
+      char *prevfield;
+      size_t prevlen;
+      int match_count = 0;
+
+      if (readline (prevline, istream) == 0)
+       goto closefiles;
+      prevfield = find_field (prevline);
+      prevlen = prevline->length - (prevfield - prevline->buffer);
 
-  writeline (prevline, ostream, match_count);
+      while (!feof (istream))
+       {
+         int match;
+         char *thisfield;
+         size_t thislen;
+         if (readline (thisline, istream) == 0)
+           break;
+         thisfield = find_field (thisline);
+         thislen = thisline->length - (thisfield - thisline->buffer);
+         match = !different (thisfield, prevfield, thislen, prevlen);
+
+         if (match)
+           ++match_count;
+
+         if (!match || mode == output_all_repeated)
+           {
+             writeline (prevline, ostream, match_count);
+             SWAP_LINES (prevline, thisline);
+             prevfield = thisfield;
+             prevlen = thislen;
+             if (!match)
+               match_count = 0;
+           }
+       }
+
+      writeline (prevline, ostream, match_count);
+    }
 
  closefiles:
   if (ferror (istream) || fclose (istream) == EOF)