1 /* uniq -- remove duplicate lines from a sorted file
2 Copyright (C) 1986, 1991, 1995 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
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /* Written by Richard Stallman and David MacKenzie. */
22 /* Get isblank from GNU libc. */
27 #include <sys/types.h>
34 # define UINT_MAX ((unsigned int) ~(unsigned int) 0)
38 # define INT_MAX ((int) (UINT_MAX >> 1))
42 #include "linebuffer.h"
47 /* Undefine, to avoid warning about redefinition on some systems. */
49 #define min(x, y) ((x) < (y) ? (x) : (y))
51 /* The name this program was run with. */
54 /* Number of fields to skip on each line when doing comparisons. */
55 static int skip_fields
;
57 /* Number of chars to skip after skipping any fields. */
58 static int skip_chars
;
60 /* Number of chars to compare; if 0, compare the whole lines. */
61 static int check_chars
;
65 count_occurrences
, /* -c Print count before output lines. */
66 count_none
/* Default. Do not print counts. */
69 /* Whether and how to precede the output lines with a count of the number of
70 times they occurred in the input. */
71 static enum countmode countmode
;
75 output_repeated
, /* -d Only lines that are repeated. */
76 output_unique
, /* -u Only lines that are not repeated. */
77 output_all
/* Default. Print first copy of each line. */
80 /* Which lines to output. */
81 static enum output_mode mode
;
83 /* If nonzero, display usage information and exit. */
86 /* If nonzero, print the version on standard output then exit. */
87 static int show_version
;
89 static struct option
const longopts
[] =
91 {"count", no_argument
, NULL
, 'c'},
92 {"repeated", no_argument
, NULL
, 'd'},
93 {"unique", no_argument
, NULL
, 'u'},
94 {"skip-fields", required_argument
, NULL
, 'f'},
95 {"skip-chars", required_argument
, NULL
, 's'},
96 {"check-chars", required_argument
, NULL
, 'w'},
97 {"help", no_argument
, &show_help
, 1},
98 {"version", no_argument
, &show_version
, 1},
106 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
111 Usage: %s [OPTION]... [INPUT [OUTPUT]]\n\
115 Discard all but one of successive identical lines from INPUT (or\n\
116 standard input), writing to OUTPUT (or standard output).\n\
118 -c, --count prefix lines by the number of occurrences\n\
119 -d, --repeated only print duplicate lines\n\
120 -f, --skip-fields=N avoid comparing the first N fields\n\
121 -s, --skip-chars=N avoid comparing the first N characters\n\
122 -u, --unique only print unique lines\n\
123 -w, --check-chars=N compare no more than N characters in lines\n\
126 --help display this help and exit\n\
127 --version output version information and exit\n\
129 A field is a run of whitespace, than non-whitespace characters.\n\
130 Fields are skipped before chars. \n\
136 /* Given a linebuffer LINE,
137 return a pointer to the beginning of the line's field to be compared. */
140 find_field (const struct linebuffer
*line
)
143 register char *lp
= line
->buffer
;
144 register int size
= line
->length
;
147 for (count
= 0; count
< skip_fields
&& i
< size
; count
++)
149 while (i
< size
&& ISBLANK (lp
[i
]))
151 while (i
< size
&& !ISBLANK (lp
[i
]))
155 for (count
= 0; count
< skip_chars
&& i
< size
; count
++)
161 /* Return zero if two strings OLD and NEW match, nonzero if not.
162 OLD and NEW point not to the beginnings of the lines
163 but rather to the beginnings of the fields to compare.
164 OLDLEN and NEWLEN are their lengths. */
167 different (const char *old
, const char *new, int oldlen
, int newlen
)
173 if (oldlen
> check_chars
)
174 oldlen
= check_chars
;
175 if (newlen
> check_chars
)
176 newlen
= check_chars
;
178 order
= memcmp (old
, new, min (oldlen
, newlen
));
180 return oldlen
- newlen
;
184 /* Output the line in linebuffer LINE to stream STREAM
185 provided that the switches say it should be output.
186 If requested, print the number of times it occurred, as well;
187 LINECOUNT + 1 is the number of times that the line occurred. */
190 writeline (const struct linebuffer
*line
, FILE *stream
, int linecount
)
192 if ((mode
== output_unique
&& linecount
!= 0)
193 || (mode
== output_repeated
&& linecount
== 0))
196 if (countmode
== count_occurrences
)
197 fprintf (stream
, "%7d\t", linecount
+ 1);
199 fwrite (line
->buffer
, sizeof (char), line
->length
, stream
);
203 /* Process input file INFILE with output to OUTFILE.
204 If either is "-", use the standard I/O stream for it instead. */
207 check_file (const char *infile
, const char *outfile
)
211 struct linebuffer lb1
, lb2
;
212 struct linebuffer
*thisline
, *prevline
, *exch
;
213 char *prevfield
, *thisfield
;
214 int prevlen
, thislen
;
217 if (!strcmp (infile
, "-"))
220 istream
= fopen (infile
, "r");
222 error (1, errno
, "%s", infile
);
224 if (!strcmp (outfile
, "-"))
227 ostream
= fopen (outfile
, "w");
229 error (1, errno
, "%s", outfile
);
234 initbuffer (thisline
);
235 initbuffer (prevline
);
237 if (readline (prevline
, istream
) == 0)
239 prevfield
= find_field (prevline
);
240 prevlen
= prevline
->length
- (prevfield
- prevline
->buffer
);
242 while (!feof (istream
))
244 if (readline (thisline
, istream
) == 0)
246 thisfield
= find_field (thisline
);
247 thislen
= thisline
->length
- (thisfield
- thisline
->buffer
);
248 if (!different (thisfield
, prevfield
, thislen
, prevlen
))
252 writeline (prevline
, ostream
, match_count
);
258 prevfield
= thisfield
;
263 writeline (prevline
, ostream
, match_count
);
266 if (ferror (istream
) || fclose (istream
) == EOF
)
267 error (1, errno
, _("error reading %s"), infile
);
269 if (ferror (ostream
) || fclose (ostream
) == EOF
)
270 error (1, errno
, _("error writing %s"), outfile
);
277 main (int argc
, char **argv
)
280 char *infile
= "-", *outfile
= "-";
282 program_name
= argv
[0];
283 setlocale (LC_ALL
, "");
284 bindtextdomain (PACKAGE
, LOCALEDIR
);
285 textdomain (PACKAGE
);
291 countmode
= count_none
;
293 while ((optc
= getopt_long (argc
, argv
, "0123456789cdf:s:uw:", longopts
,
311 skip_fields
= skip_fields
* 10 + optc
- '0';
315 countmode
= count_occurrences
;
319 mode
= output_repeated
;
322 case 'f': /* Like '-#'. */
325 if (xstrtol (optarg
, NULL
, 10, &tmp_long
, NULL
) != LONGINT_OK
326 || tmp_long
<= 0 || tmp_long
> INT_MAX
)
327 error (1, 0, _("invalid number of fields to skip: `%s'"),
329 skip_fields
= (int) tmp_long
;
333 case 's': /* Like '+#'. */
336 if (xstrtol (optarg
, NULL
, 10, &tmp_long
, NULL
) != LONGINT_OK
337 || tmp_long
<= 0 || tmp_long
> INT_MAX
)
338 error (1, 0, _("invalid number of bytes to skip: `%s'"),
340 skip_chars
= (int) tmp_long
;
345 mode
= output_unique
;
351 if (xstrtol (optarg
, NULL
, 10, &tmp_long
, NULL
) != LONGINT_OK
352 || tmp_long
<= 0 || tmp_long
> INT_MAX
)
353 error (1, 0, _("invalid number of bytes to compare: `%s'"),
355 check_chars
= (int) tmp_long
;
366 printf ("uniq - %s\n", version_string
);
373 if (optind
>= 2 && strcmp (argv
[optind
- 1], "--") != 0)
375 /* Interpret non-option arguments with leading `+' only
376 if we haven't seen `--'. */
377 while (optind
< argc
&& argv
[optind
][0] == '+')
379 char *opt_str
= argv
[optind
++];
381 if (xstrtol (opt_str
, NULL
, 10, &tmp_long
, NULL
) != LONGINT_OK
382 || tmp_long
<= 0 || tmp_long
> INT_MAX
)
383 error (1, 0, _("invalid number of bytes to compare: `%s'"),
385 skip_chars
= (int) tmp_long
;
390 infile
= argv
[optind
++];
393 outfile
= argv
[optind
++];
396 usage (1); /* Extra arguments. */
398 check_file (infile
, outfile
);