diff options
author | Jim Meyering <jim@meyering.net> | 1999-08-22 11:17:42 +0000 |
---|---|---|
committer | Jim Meyering <jim@meyering.net> | 1999-08-22 11:17:42 +0000 |
commit | 9503681d8665cc9ebc620cdb6c28f60b1b3d33b7 (patch) | |
tree | 58739796ad6da753d72523314ca47c24f292192d | |
parent | 7d8102bec24e6066bbcdf50cf788cd15f13811ea (diff) | |
download | coreutils-9503681d8665cc9ebc620cdb6c28f60b1b3d33b7.tar.xz |
(check_file): Generate each line of output earlier,
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.
-rw-r--r-- | src/uniq.c | 87 |
1 files changed, 58 insertions, 29 deletions
diff --git a/src/uniq.c b/src/uniq.c index b17df336d..a3215646a 100644 --- a/src/uniq.c +++ b/src/uniq.c @@ -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) |