|
version 1.1.1.1, 2012/02/21 22:57:49
|
version 1.1.1.3, 2021/03/17 13:38:46
|
|
Line 1
|
Line 1
|
| /* uniq -- remove duplicate lines from a sorted file |
/* uniq -- remove duplicate lines from a sorted file |
| Copyright (C) 86, 91, 1995-1998, 1999 Free Software Foundation, Inc. | Copyright (C) 86, 91, 1995-1998, 1999, 2012 Free Software Foundation, Inc. |
| |
|
| This program is free software: you can redistribute it and/or modify |
This program is free software: you can redistribute it and/or modify |
| it under the terms of the GNU General Public License as published by |
it under the terms of the GNU General Public License as published by |
|
Line 12
|
Line 12
|
| GNU General Public License for more details. |
GNU General Public License for more details. |
| |
|
| You should have received a copy of the GNU General Public License |
You should have received a copy of the GNU General Public License |
| along with this program. If not, see <http://www.gnu.org/licenses/>. */ | along with this program. If not, see <https://www.gnu.org/licenses/>. */ |
| |
|
| /* Written by Richard Stallman and David MacKenzie. */ |
/* Written by Richard Stallman and David MacKenzie. */ |
| /* 2000-03-22 Trimmed down to the case of "uniq -u" by Bruno Haible. */ |
/* 2000-03-22 Trimmed down to the case of "uniq -u" by Bruno Haible. */ |
|
Line 58 xrealloc (void *p, size_t n)
|
Line 58 xrealloc (void *p, size_t n)
|
| return p; |
return p; |
| } |
} |
| |
|
| /* A `struct linebuffer' holds a line of text. */ | /* A 'struct linebuffer' holds a line of text. */ |
| |
|
| struct linebuffer |
struct linebuffer |
| { |
{ |
| size_t size; /* Allocated. */ | size_t size; /* Allocated. */ |
| size_t length; /* Used. */ | size_t length; /* Used. */ |
| char *buffer; |
char *buffer; |
| }; |
}; |
| |
|
|
Line 97 readline (struct linebuffer *linebuffer, FILE *stream)
|
Line 97 readline (struct linebuffer *linebuffer, FILE *stream)
|
| { |
{ |
| c = getc (stream); |
c = getc (stream); |
| if (c == EOF) |
if (c == EOF) |
| { | { |
| if (p == buffer) | if (p == buffer) |
| return 0; | return 0; |
| if (p[-1] == '\n') | if (p[-1] == '\n') |
| break; | break; |
| c = '\n'; | c = '\n'; |
| } | } |
| if (p == end) |
if (p == end) |
| { | { |
| linebuffer->size *= 2; | linebuffer->size *= 2; |
| buffer = (char *) xrealloc (buffer, linebuffer->size); | buffer = (char *) xrealloc (buffer, linebuffer->size); |
| p = p - linebuffer->buffer + buffer; | p = p - linebuffer->buffer + buffer; |
| linebuffer->buffer = buffer; | linebuffer->buffer = buffer; |
| end = buffer + linebuffer->size - 1; | end = buffer + linebuffer->size - 1; |
| } | } |
| *p++ = c; |
*p++ = c; |
| } |
} |
| while (c != '\n'); |
while (c != '\n'); |
|
Line 210 check_file (const char *infile, const char *outfile)
|
Line 210 check_file (const char *infile, const char *outfile)
|
| { |
{ |
| int match; |
int match; |
| if (readline (thisline, istream) == 0) |
if (readline (thisline, istream) == 0) |
| break; | break; |
| thisfield = thisline->buffer; |
thisfield = thisline->buffer; |
| thislen = thisline->length; |
thislen = thisline->length; |
| match = !different (thisfield, prevfield, thislen, prevlen); |
match = !different (thisfield, prevfield, thislen, prevlen); |
| |
|
| if (match) |
if (match) |
| ++match_count; | ++match_count; |
| |
|
| if (!match) |
if (!match) |
| { | { |
| writeline (prevline, ostream, match_count); | writeline (prevline, ostream, match_count); |
| exch = prevline; | exch = prevline; |
| prevline = thisline; | prevline = thisline; |
| thisline = exch; | thisline = exch; |
| prevfield = thisfield; | prevfield = thisfield; |
| prevlen = thislen; | prevlen = thislen; |
| if (!match) | if (!match) |
| match_count = 0; | match_count = 0; |
| } | } |
| } |
} |
| |
|
| writeline (prevline, ostream, match_count); |
writeline (prevline, ostream, match_count); |