1 /* uniq -- remove duplicate lines from a sorted file
2 Copyright (C) 86, 91, 1995-2001, Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /* Written by Richard Stallman and David MacKenzie. */
24 #include <sys/types.h>
29 #include "linebuffer.h"
32 #include "memcasecmp.h"
34 /* The official name of this program (e.g., no `g' prefix). */
35 #define PROGRAM_NAME "uniq"
37 #define AUTHORS N_ ("Richard Stallman and David MacKenzie")
39 #define SWAP_LINES(A, B) \
42 struct linebuffer *_tmp; \
49 /* The name this program was run with. */
52 /* Number of fields to skip on each line when doing comparisons. */
53 static size_t skip_fields;
55 /* Number of chars to skip after skipping any fields. */
56 static size_t skip_chars;
58 /* Number of chars to compare. */
59 static size_t check_chars;
63 count_occurrences, /* -c Print count before output lines. */
64 count_none /* Default. Do not print counts. */
67 /* Whether and how to precede the output lines with a count of the number of
68 times they occurred in the input. */
69 static enum countmode countmode;
73 output_repeated, /* -d Only lines that are repeated. */
74 output_all_repeated, /* -D All lines that are repeated. */
75 output_unique, /* -u Only lines that are not repeated. */
76 output_all /* Default. Print first copy of each line. */
79 /* Which lines to output. */
80 static enum output_mode mode;
82 /* If nonzero, ignore case when comparing. */
83 static int ignore_case;
87 /* No delimiters output. --all-repeated[=none] */
90 /* Delimiter precedes all groups. --all-repeated=prepend */
93 /* Delimit all groups. --all-repeated=separate */
97 static char const *const delimit_method_string[] =
99 "none", "prepend", "separate", 0
102 static enum delimit_method const delimit_method_map[] =
104 DM_NONE, DM_PREPEND, DM_SEPARATE
107 /* Select whether/how to delimit groups of duplicate lines. */
108 static enum delimit_method delimit_groups;
110 static struct option const longopts[] =
112 {"count", no_argument, NULL, 'c'},
113 {"repeated", no_argument, NULL, 'd'},
114 {"all-repeated", optional_argument, NULL, 'D'},
115 {"ignore-case", no_argument, NULL, 'i'},
116 {"unique", no_argument, NULL, 'u'},
117 {"skip-fields", required_argument, NULL, 'f'},
118 {"skip-chars", required_argument, NULL, 's'},
119 {"check-chars", required_argument, NULL, 'w'},
120 {GETOPT_HELP_OPTION_DECL},
121 {GETOPT_VERSION_OPTION_DECL},
129 fprintf (stderr, _("Try `%s --help' for more information.\n"),
134 Usage: %s [OPTION]... [INPUT [OUTPUT]]\n\
138 Discard all but one of successive identical lines from INPUT (or\n\
139 standard input), writing to OUTPUT (or standard output).\n\
141 Mandatory arguments to long options are mandatory for short options too.\n\
142 -c, --count prefix lines by the number of occurrences\n\
143 -d, --repeated only print duplicate lines\n\
144 -D, --all-repeated[=delimit-method] print all duplicate lines\n\
145 delimit-method={none(default),prepend,separate)}\n\
146 Delimiting is done with blank lines.\n\
147 -f, --skip-fields=N avoid comparing the first N fields\n\
148 -i, --ignore-case ignore differences in case when comparing\n\
149 -s, --skip-chars=N avoid comparing the first N characters\n\
150 -u, --unique only print unique lines\n\
151 -w, --check-chars=N compare no more than N characters in lines\n\
153 +N same as -s N (obsolescent; will be withdrawn)\n\
154 --help display this help and exit\n\
155 --version output version information and exit\n\
157 A field is a run of whitespace, then non-whitespace characters.\n\
158 Fields are skipped before chars.\n\
160 puts (_("\nReport bugs to <bug-textutils@gnu.org>."));
162 exit (status == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
165 /* Convert OPT to size_t, reporting an error using MSGID if it does
169 size_opt (char const *opt, char const *msgid)
171 unsigned long int size;
172 if (xstrtoul (opt, NULL, 10, &size, "") != LONGINT_OK
174 error (EXIT_FAILURE, 0, "%s: %s", opt, _(msgid));
178 /* Given a linebuffer LINE,
179 return a pointer to the beginning of the line's field to be compared. */
182 find_field (const struct linebuffer *line)
184 register size_t count;
185 register char *lp = line->buffer;
186 register size_t size = line->length - 1;
187 register size_t i = 0;
189 for (count = 0; count < skip_fields && i < size; count++)
191 while (i < size && ISBLANK (lp[i]))
193 while (i < size && !ISBLANK (lp[i]))
197 for (count = 0; count < skip_chars && i < size; count++)
203 /* Return zero if two strings OLD and NEW match, nonzero if not.
204 OLD and NEW point not to the beginnings of the lines
205 but rather to the beginnings of the fields to compare.
206 OLDLEN and NEWLEN are their lengths. */
209 different (const char *old, const char *new, size_t oldlen, size_t newlen)
211 if (check_chars < oldlen)
212 oldlen = check_chars;
213 if (check_chars < newlen)
214 newlen = check_chars;
216 if (oldlen != newlen)
219 /* Use an if-statement here rather than a function variable to
220 avoid portability hassles of getting a non-conflicting declaration
223 return memcasecmp (old, new, oldlen);
225 return memcmp (old, new, oldlen);
228 /* Output the line in linebuffer LINE to stream STREAM
229 provided that the switches say it should be output.
230 If requested, print the number of times it occurred, as well;
231 LINECOUNT + 1 is the number of times that the line occurred. */
234 writeline (const struct linebuffer *line, FILE *stream, int linecount)
236 if ((mode == output_unique && linecount != 0)
237 || (mode == output_repeated && linecount == 0)
238 || (mode == output_all_repeated && linecount == 0))
241 if (countmode == count_occurrences)
242 fprintf (stream, "%7d\t", linecount + 1);
244 fwrite (line->buffer, sizeof (char), line->length, stream);
247 /* Process input file INFILE with output to OUTFILE.
248 If either is "-", use the standard I/O stream for it instead. */
251 check_file (const char *infile, const char *outfile)
255 struct linebuffer lb1, lb2;
256 struct linebuffer *thisline, *prevline;
258 if (STREQ (infile, "-"))
261 istream = fopen (infile, "r");
263 error (EXIT_FAILURE, errno, "%s", infile);
265 if (STREQ (outfile, "-"))
268 ostream = fopen (outfile, "w");
270 error (EXIT_FAILURE, errno, "%s", outfile);
275 initbuffer (thisline);
276 initbuffer (prevline);
278 /* The duplication in the following `if' and `else' blocks is an
279 optimization to distinguish the common case (in which none of
280 the following options has been specified: --count, -repeated,
281 --all-repeated, --unique) from the others. In the common case,
282 this optimization lets uniq output each different line right away,
283 without waiting to see if the next one is different. */
285 if (mode == output_all && countmode == count_none)
287 char *prevfield IF_LINT (= NULL);
288 size_t prevlen IF_LINT (= 0);
290 while (!feof (istream))
294 if (readline (thisline, istream) == 0)
296 thisfield = find_field (thisline);
297 thislen = thisline->length - (thisfield - thisline->buffer);
298 if (prevline->length == 0
299 || different (thisfield, prevfield, thislen, prevlen))
301 fwrite (thisline->buffer, sizeof (char),
302 thisline->length, ostream);
304 SWAP_LINES (prevline, thisline);
305 prevfield = thisfield;
315 int first_delimiter = 1;
317 if (readline (prevline, istream) == 0)
319 prevfield = find_field (prevline);
320 prevlen = prevline->length - (prevfield - prevline->buffer);
322 while (!feof (istream))
327 if (readline (thisline, istream) == 0)
329 thisfield = find_field (thisline);
330 thislen = thisline->length - (thisfield - thisline->buffer);
331 match = !different (thisfield, prevfield, thislen, prevlen);
336 if (mode == output_all_repeated && delimit_groups != DM_NONE)
340 if (match_count) /* a previous match */
341 first_delimiter = 0; /* Only used when DM_SEPARATE */
343 else if (match_count == 1)
345 if ((delimit_groups == DM_PREPEND)
346 || (delimit_groups == DM_SEPARATE
347 && !first_delimiter))
348 putc ('\n', ostream);
352 if (!match || mode == output_all_repeated)
354 writeline (prevline, ostream, match_count);
355 SWAP_LINES (prevline, thisline);
356 prevfield = thisfield;
363 writeline (prevline, ostream, match_count);
367 if (ferror (istream) || fclose (istream) == EOF)
368 error (EXIT_FAILURE, errno, _("error reading %s"), infile);
370 /* Close ostream only if it's not stdout -- the latter is closed
371 via the atexit-invoked close_stdout. */
372 if (ostream != stdout && (ferror (ostream) || fclose (ostream) == EOF))
373 error (EXIT_FAILURE, errno, _("error writing %s"), outfile);
380 main (int argc, char **argv)
383 int posixly_correct = (getenv ("POSIXLY_CORRECT") != NULL);
387 file[0] = file[1] = "-";
388 program_name = argv[0];
389 setlocale (LC_ALL, "");
390 bindtextdomain (PACKAGE, LOCALEDIR);
391 textdomain (PACKAGE);
393 atexit (close_stdout);
397 check_chars = SIZE_MAX;
399 countmode = count_none;
400 delimit_groups = DM_NONE;
404 /* Parse an operand with leading "+" as a file after "--" was
405 seen; or if pedantic and a file was seen. POSIX 1003.1-200x
406 d7 removes support for such operands, so when it becomes
407 official the code will need to be changed. */
410 || (posixly_correct && nfiles != 0)
411 || ((optc = getopt_long (argc, argv,
412 "-0123456789cdDf:is:uw:", longopts, NULL))
419 error (0, 0, _("extra operand `%s'"), argv[optind]);
422 file[nfiles++] = argv[optind++];
428 unsigned long int size;
430 && xstrtoul (optarg, NULL, 10, &size, "") == LONGINT_OK
433 else if (nfiles == 2)
435 error (0, 0, _("extra operand `%s'"), optarg);
439 file[nfiles++] = optarg;
454 size_t s = skip_fields;
455 skip_fields = s * 10 + optc - '0';
456 if (SIZE_MAX / 10 < s || skip_fields < s)
457 error (EXIT_FAILURE, 0, "%s",
458 _("invalid number of fields to skip"));
463 countmode = count_occurrences;
467 mode = output_repeated;
471 mode = output_all_repeated;
473 delimit_groups = DM_NONE;
475 delimit_groups = XARGMATCH ("--all-repeated", optarg,
476 delimit_method_string,
480 case 'f': /* Like '-#'. */
481 skip_fields = size_opt (optarg,
482 N_("invalid number of fields to skip"));
489 case 's': /* Like '+#'. */
490 skip_chars = size_opt (optarg,
491 N_("invalid number of bytes to skip"));
495 mode = output_unique;
499 check_chars = size_opt (optarg,
500 N_("invalid number of bytes to compare"));
503 case_GETOPT_HELP_CHAR;
505 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
512 if (countmode == count_occurrences && mode == output_all_repeated)
515 _("printing all duplicated lines and repeat counts is meaningless"));
519 check_file (file[0], file[1]);