1 /* uniq -- remove duplicate lines from a sorted file
2 Copyright (C) 86, 91, 1995-1998, 1999 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>
27 #include "linebuffer.h"
30 #include "memcasecmp.h"
32 /* The official name of this program (e.g., no `g' prefix). */
33 #define PROGRAM_NAME "uniq"
35 #define AUTHORS "Richard Stallman and David MacKenzie"
37 /* Undefine, to avoid warning about redefinition on some systems. */
39 #define min(x, y) ((x) < (y) ? (x) : (y))
41 /* The name this program was run with. */
44 /* Number of fields to skip on each line when doing comparisons. */
45 static int skip_fields
;
47 /* Number of chars to skip after skipping any fields. */
48 static int skip_chars
;
50 /* Number of chars to compare; if 0, compare the whole lines. */
51 static int check_chars
;
55 count_occurrences
, /* -c Print count before output lines. */
56 count_none
/* Default. Do not print counts. */
59 /* Whether and how to precede the output lines with a count of the number of
60 times they occurred in the input. */
61 static enum countmode countmode
;
65 output_repeated
, /* -d Only lines that are repeated. */
66 output_all_repeated
, /* -D All lines that are repeated. */
67 output_unique
, /* -u Only lines that are not repeated. */
68 output_all
/* Default. Print first copy of each line. */
71 /* Which lines to output. */
72 static enum output_mode mode
;
74 /* If nonzero, ignore case when comparing. */
75 static int ignore_case
;
77 static struct option
const longopts
[] =
79 {"count", no_argument
, NULL
, 'c'},
80 {"repeated", no_argument
, NULL
, 'd'},
81 {"all-repeated", no_argument
, NULL
, 'D'},
82 {"ignore-case", no_argument
, NULL
, 'i'},
83 {"unique", no_argument
, NULL
, 'u'},
84 {"skip-fields", required_argument
, NULL
, 'f'},
85 {"skip-chars", required_argument
, NULL
, 's'},
86 {"check-chars", required_argument
, NULL
, 'w'},
87 {GETOPT_HELP_OPTION_DECL
},
88 {GETOPT_VERSION_OPTION_DECL
},
96 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
101 Usage: %s [OPTION]... [INPUT [OUTPUT]]\n\
105 Discard all but one of successive identical lines from INPUT (or\n\
106 standard input), writing to OUTPUT (or standard output).\n\
108 -c, --count prefix lines by the number of occurrences\n\
109 -d, --repeated only print duplicate lines\n\
110 -D, --all-repeated print all duplicate lines\n\
111 -f, --skip-fields=N avoid comparing the first N fields\n\
112 -i, --ignore-case ignore differences in case when comparing\n\
113 -s, --skip-chars=N avoid comparing the first N characters\n\
114 -u, --unique only print unique lines\n\
115 -w, --check-chars=N compare no more than N characters in lines\n\
118 --help display this help and exit\n\
119 --version output version information and exit\n\
121 A field is a run of whitespace, then non-whitespace characters.\n\
122 Fields are skipped before chars.\n\
124 puts (_("\nReport bugs to <bug-textutils@gnu.org>."));
126 exit (status
== 0 ? EXIT_SUCCESS
: EXIT_FAILURE
);
129 /* Given a linebuffer LINE,
130 return a pointer to the beginning of the line's field to be compared. */
133 find_field (const struct linebuffer
*line
)
136 register char *lp
= line
->buffer
;
137 register size_t size
= line
->length
;
138 register size_t i
= 0;
140 for (count
= 0; count
< skip_fields
&& i
< size
; count
++)
142 while (i
< size
&& ISBLANK (lp
[i
]))
144 while (i
< size
&& !ISBLANK (lp
[i
]))
148 for (count
= 0; count
< skip_chars
&& i
< size
; count
++)
154 /* Return zero if two strings OLD and NEW match, nonzero if not.
155 OLD and NEW point not to the beginnings of the lines
156 but rather to the beginnings of the fields to compare.
157 OLDLEN and NEWLEN are their lengths. */
160 different (const char *old
, const char *new, size_t oldlen
, size_t newlen
)
166 if (oldlen
> check_chars
)
167 oldlen
= check_chars
;
168 if (newlen
> check_chars
)
169 newlen
= check_chars
;
172 /* Use an if-statement here rather than a function variable to
173 avoid portability hassles of getting a non-conflicting declaration
176 order
= memcasecmp (old
, new, min (oldlen
, newlen
));
178 order
= memcmp (old
, new, min (oldlen
, newlen
));
181 return oldlen
- newlen
;
185 /* Output the line in linebuffer LINE to stream STREAM
186 provided that the switches say it should be output.
187 If requested, print the number of times it occurred, as well;
188 LINECOUNT + 1 is the number of times that the line occurred. */
191 writeline (const struct linebuffer
*line
, FILE *stream
, int linecount
)
193 if ((mode
== output_unique
&& linecount
!= 0)
194 || (mode
== output_repeated
&& linecount
== 0)
195 || (mode
== output_all_repeated
&& linecount
== 0))
198 if (countmode
== count_occurrences
)
199 fprintf (stream
, "%7d\t", linecount
+ 1);
201 fwrite (line
->buffer
, sizeof (char), line
->length
, stream
);
204 /* Process input file INFILE with output to OUTFILE.
205 If either is "-", use the standard I/O stream for it instead. */
208 check_file (const char *infile
, const char *outfile
)
212 struct linebuffer lb1
, lb2
;
213 struct linebuffer
*thisline
, *prevline
, *exch
;
214 char *prevfield
, *thisfield
;
215 size_t prevlen
, thislen
;
218 if (STREQ (infile
, "-"))
221 istream
= fopen (infile
, "r");
223 error (EXIT_FAILURE
, errno
, "%s", infile
);
225 if (STREQ (outfile
, "-"))
228 ostream
= fopen (outfile
, "w");
230 error (EXIT_FAILURE
, errno
, "%s", outfile
);
235 initbuffer (thisline
);
236 initbuffer (prevline
);
238 if (readline (prevline
, istream
) == 0)
240 prevfield
= find_field (prevline
);
241 prevlen
= prevline
->length
- (prevfield
- prevline
->buffer
);
243 while (!feof (istream
))
246 if (readline (thisline
, istream
) == 0)
248 thisfield
= find_field (thisline
);
249 thislen
= thisline
->length
- (thisfield
- thisline
->buffer
);
250 match
= !different (thisfield
, prevfield
, thislen
, prevlen
);
255 if (!match
|| mode
== output_all_repeated
)
257 writeline (prevline
, ostream
, match_count
);
261 prevfield
= thisfield
;
268 writeline (prevline
, ostream
, match_count
);
271 if (ferror (istream
) || fclose (istream
) == EOF
)
272 error (EXIT_FAILURE
, errno
, _("error reading %s"), infile
);
274 if (ferror (ostream
) || fclose (ostream
) == EOF
)
275 error (EXIT_FAILURE
, errno
, _("error writing %s"), outfile
);
282 main (int argc
, char **argv
)
285 char *infile
= "-", *outfile
= "-";
287 program_name
= argv
[0];
288 setlocale (LC_ALL
, "");
289 bindtextdomain (PACKAGE
, LOCALEDIR
);
290 textdomain (PACKAGE
);
296 countmode
= count_none
;
298 while ((optc
= getopt_long (argc
, argv
, "0123456789cdDf:is:uw:", longopts
,
316 skip_fields
= skip_fields
* 10 + optc
- '0';
320 countmode
= count_occurrences
;
324 mode
= output_repeated
;
328 mode
= output_all_repeated
;
331 case 'f': /* Like '-#'. */
334 if (xstrtol (optarg
, NULL
, 10, &tmp_long
, "") != LONGINT_OK
335 || tmp_long
<= 0 || tmp_long
> INT_MAX
)
336 error (EXIT_FAILURE
, 0,
337 _("invalid number of fields to skip: `%s'"),
339 skip_fields
= (int) tmp_long
;
347 case 's': /* Like '+#'. */
350 if (xstrtol (optarg
, NULL
, 10, &tmp_long
, "") != LONGINT_OK
351 || tmp_long
<= 0 || tmp_long
> INT_MAX
)
352 error (EXIT_FAILURE
, 0,
353 _("invalid number of bytes to skip: `%s'"),
355 skip_chars
= (int) tmp_long
;
360 mode
= output_unique
;
366 if (xstrtol (optarg
, NULL
, 10, &tmp_long
, "") != LONGINT_OK
367 || tmp_long
<= 0 || tmp_long
> INT_MAX
)
368 error (EXIT_FAILURE
, 0,
369 _("invalid number of bytes to compare: `%s'"),
371 check_chars
= (int) tmp_long
;
375 case_GETOPT_HELP_CHAR
;
377 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, AUTHORS
);
384 if (optind
>= 2 && !STREQ (argv
[optind
- 1], "--"))
386 /* Interpret non-option arguments with leading `+' only
387 if we haven't seen `--'. */
388 while (optind
< argc
&& argv
[optind
][0] == '+')
390 char *opt_str
= argv
[optind
++];
392 if (xstrtol (opt_str
, NULL
, 10, &tmp_long
, "") != LONGINT_OK
393 || tmp_long
<= 0 || tmp_long
> INT_MAX
)
394 error (EXIT_FAILURE
, 0,
395 _("invalid number of bytes to compare: `%s'"),
397 skip_chars
= (int) tmp_long
;
402 infile
= argv
[optind
++];
405 outfile
= argv
[optind
++];
409 error (0, 0, _("too many arguments"));
413 if (countmode
== count_occurrences
&& mode
== output_all_repeated
)
416 _("printing all duplicated lines and repeat counts is meaningless"));
420 check_file (infile
, outfile
);