From: Jim Meyering Date: Sun, 22 Aug 1999 11:17:42 +0000 (+0000) Subject: (check_file): Generate each line of output earlier, X-Git-Tag: FILEUTILS-4_0j-trial~297 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=9503681d8665cc9ebc620cdb6c28f60b1b3d33b7;p=thirdparty%2Fcoreutils.git (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. --- diff --git a/src/uniq.c b/src/uniq.c index b17df336da..a3215646a3 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)