*** empty log message ***
[coreutils.git] / src / uniq.c
blob509c0315f6d37f7ac937851a6bd47f881bcee979
1 /* uniq -- remove duplicate lines from a sorted file
2 Copyright (C) 86, 91, 95, 96, 1997, 1998 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)
7 any later version.
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. */
20 #include <config.h>
22 #include <stdio.h>
23 #include <getopt.h>
24 #include <sys/types.h>
26 #include "system.h"
27 #include "linebuffer.h"
28 #include "error.h"
29 #include "xstrtol.h"
30 #include "memcasecmp.h"
32 /* Undefine, to avoid warning about redefinition on some systems. */
33 #undef min
34 #define min(x, y) ((x) < (y) ? (x) : (y))
36 /* The name this program was run with. */
37 char *program_name;
39 /* Number of fields to skip on each line when doing comparisons. */
40 static int skip_fields;
42 /* Number of chars to skip after skipping any fields. */
43 static int skip_chars;
45 /* Number of chars to compare; if 0, compare the whole lines. */
46 static int check_chars;
48 enum countmode
50 count_occurrences, /* -c Print count before output lines. */
51 count_none /* Default. Do not print counts. */
54 /* Whether and how to precede the output lines with a count of the number of
55 times they occurred in the input. */
56 static enum countmode countmode;
58 enum output_mode
60 output_repeated, /* -d Only lines that are repeated. */
61 output_unique, /* -u Only lines that are not repeated. */
62 output_all /* Default. Print first copy of each line. */
65 /* Which lines to output. */
66 static enum output_mode mode;
68 /* If nonzero, ignore case when comparing. */
69 static int ignore_case;
71 /* If nonzero, display usage information and exit. */
72 static int show_help;
74 /* If nonzero, print the version on standard output then exit. */
75 static int show_version;
77 static struct option const longopts[] =
79 {"count", no_argument, NULL, 'c'},
80 {"repeated", no_argument, NULL, 'd'},
81 {"ignore-case", no_argument, NULL, 'i'},
82 {"unique", no_argument, NULL, 'u'},
83 {"skip-fields", required_argument, NULL, 'f'},
84 {"skip-chars", required_argument, NULL, 's'},
85 {"check-chars", required_argument, NULL, 'w'},
86 {"help", no_argument, &show_help, 1},
87 {"version", no_argument, &show_version, 1},
88 {NULL, 0, NULL, 0}
91 static void
92 usage (int status)
94 if (status != 0)
95 fprintf (stderr, _("Try `%s --help' for more information.\n"),
96 program_name);
97 else
99 printf (_("\
100 Usage: %s [OPTION]... [INPUT [OUTPUT]]\n\
102 program_name);
103 printf (_("\
104 Discard all but one of successive identical lines from INPUT (or\n\
105 standard input), writing to OUTPUT (or standard output).\n\
107 -c, --count prefix lines by the number of occurrences\n\
108 -d, --repeated only print duplicate lines\n\
109 -f, --skip-fields=N avoid comparing the first N fields\n\
110 -i, --ignore-case ignore differences in case when comparing\n\
111 -s, --skip-chars=N avoid comparing the first N characters\n\
112 -u, --unique only print unique lines\n\
113 -w, --check-chars=N compare no more than N characters in lines\n\
114 -N same as -f N\n\
115 +N same as -s N\n\
116 --help display this help and exit\n\
117 --version output version information and exit\n\
119 A field is a run of whitespace, then non-whitespace characters.\n\
120 Fields are skipped before chars.\n\
121 "));
122 puts (_("\nReport bugs to <bug-textutils@gnu.org>."));
124 exit (status == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
127 /* Given a linebuffer LINE,
128 return a pointer to the beginning of the line's field to be compared. */
130 static char *
131 find_field (const struct linebuffer *line)
133 register int count;
134 register char *lp = line->buffer;
135 register int size = line->length;
136 register int i = 0;
138 for (count = 0; count < skip_fields && i < size; count++)
140 while (i < size && ISBLANK (lp[i]))
141 i++;
142 while (i < size && !ISBLANK (lp[i]))
143 i++;
146 for (count = 0; count < skip_chars && i < size; count++)
147 i++;
149 return lp + i;
152 /* Return zero if two strings OLD and NEW match, nonzero if not.
153 OLD and NEW point not to the beginnings of the lines
154 but rather to the beginnings of the fields to compare.
155 OLDLEN and NEWLEN are their lengths. */
157 static int
158 different (const char *old, const char *new, int oldlen, int newlen)
160 register int order;
162 if (check_chars)
164 if (oldlen > check_chars)
165 oldlen = check_chars;
166 if (newlen > check_chars)
167 newlen = check_chars;
170 /* Use an if-statement here rather than a function variable to
171 avoid portability hassles of getting a non-conflicting declaration
172 of memcmp. */
173 if (ignore_case)
174 order = memcasecmp (old, new, min (oldlen, newlen));
175 else
176 order = memcmp (old, new, min (oldlen, newlen));
178 if (order == 0)
179 return oldlen - newlen;
180 return order;
183 /* Output the line in linebuffer LINE to stream STREAM
184 provided that the switches say it should be output.
185 If requested, print the number of times it occurred, as well;
186 LINECOUNT + 1 is the number of times that the line occurred. */
188 static void
189 writeline (const struct linebuffer *line, FILE *stream, int linecount)
191 if ((mode == output_unique && linecount != 0)
192 || (mode == output_repeated && linecount == 0))
193 return;
195 if (countmode == count_occurrences)
196 fprintf (stream, "%7d\t", linecount + 1);
198 fwrite (line->buffer, sizeof (char), line->length, stream);
199 putc ('\n', stream);
202 /* Process input file INFILE with output to OUTFILE.
203 If either is "-", use the standard I/O stream for it instead. */
205 static void
206 check_file (const char *infile, const char *outfile)
208 FILE *istream;
209 FILE *ostream;
210 struct linebuffer lb1, lb2;
211 struct linebuffer *thisline, *prevline, *exch;
212 char *prevfield, *thisfield;
213 int prevlen, thislen;
214 int match_count = 0;
216 if (STREQ (infile, "-"))
217 istream = stdin;
218 else
219 istream = fopen (infile, "r");
220 if (istream == NULL)
221 error (EXIT_FAILURE, errno, "%s", infile);
223 if (STREQ (outfile, "-"))
224 ostream = stdout;
225 else
226 ostream = fopen (outfile, "w");
227 if (ostream == NULL)
228 error (EXIT_FAILURE, errno, "%s", outfile);
230 thisline = &lb1;
231 prevline = &lb2;
233 initbuffer (thisline);
234 initbuffer (prevline);
236 if (readline (prevline, istream) == 0)
237 goto closefiles;
238 prevfield = find_field (prevline);
239 prevlen = prevline->length - (prevfield - prevline->buffer);
241 while (!feof (istream))
243 if (readline (thisline, istream) == 0)
244 break;
245 thisfield = find_field (thisline);
246 thislen = thisline->length - (thisfield - thisline->buffer);
247 if (!different (thisfield, prevfield, thislen, prevlen))
248 match_count++;
249 else
251 writeline (prevline, ostream, match_count);
252 match_count = 0;
254 exch = prevline;
255 prevline = thisline;
256 thisline = exch;
257 prevfield = thisfield;
258 prevlen = thislen;
262 writeline (prevline, ostream, match_count);
264 closefiles:
265 if (ferror (istream) || fclose (istream) == EOF)
266 error (EXIT_FAILURE, errno, _("error reading %s"), infile);
268 if (ferror (ostream) || fclose (ostream) == EOF)
269 error (EXIT_FAILURE, errno, _("error writing %s"), outfile);
271 free (lb1.buffer);
272 free (lb2.buffer);
276 main (int argc, char **argv)
278 int optc;
279 char *infile = "-", *outfile = "-";
281 program_name = argv[0];
282 setlocale (LC_ALL, "");
283 bindtextdomain (PACKAGE, LOCALEDIR);
284 textdomain (PACKAGE);
286 skip_chars = 0;
287 skip_fields = 0;
288 check_chars = 0;
289 mode = output_all;
290 countmode = count_none;
292 while ((optc = getopt_long (argc, argv, "0123456789cdf:is:uw:", longopts,
293 NULL)) != -1)
295 switch (optc)
297 case 0:
298 break;
300 case '0':
301 case '1':
302 case '2':
303 case '3':
304 case '4':
305 case '5':
306 case '6':
307 case '7':
308 case '8':
309 case '9':
310 skip_fields = skip_fields * 10 + optc - '0';
311 break;
313 case 'c':
314 countmode = count_occurrences;
315 break;
317 case 'd':
318 mode = output_repeated;
319 break;
321 case 'f': /* Like '-#'. */
323 long int tmp_long;
324 if (xstrtol (optarg, NULL, 10, &tmp_long, "") != LONGINT_OK
325 || tmp_long <= 0 || tmp_long > INT_MAX)
326 error (EXIT_FAILURE, 0,
327 _("invalid number of fields to skip: `%s'"),
328 optarg);
329 skip_fields = (int) tmp_long;
331 break;
333 case 'i':
334 ignore_case = 1;
335 break;
337 case 's': /* Like '+#'. */
339 long int tmp_long;
340 if (xstrtol (optarg, NULL, 10, &tmp_long, "") != LONGINT_OK
341 || tmp_long <= 0 || tmp_long > INT_MAX)
342 error (EXIT_FAILURE, 0,
343 _("invalid number of bytes to skip: `%s'"),
344 optarg);
345 skip_chars = (int) tmp_long;
347 break;
349 case 'u':
350 mode = output_unique;
351 break;
353 case 'w':
355 long int tmp_long;
356 if (xstrtol (optarg, NULL, 10, &tmp_long, "") != LONGINT_OK
357 || tmp_long <= 0 || tmp_long > INT_MAX)
358 error (EXIT_FAILURE, 0,
359 _("invalid number of bytes to compare: `%s'"),
360 optarg);
361 check_chars = (int) tmp_long;
363 break;
365 default:
366 usage (1);
370 if (show_version)
372 printf ("uniq (%s) %s\n", GNU_PACKAGE, VERSION);
373 exit (EXIT_SUCCESS);
376 if (show_help)
377 usage (0);
379 if (optind >= 2 && !STREQ (argv[optind - 1], "--"))
381 /* Interpret non-option arguments with leading `+' only
382 if we haven't seen `--'. */
383 while (optind < argc && argv[optind][0] == '+')
385 char *opt_str = argv[optind++];
386 long int tmp_long;
387 if (xstrtol (opt_str, NULL, 10, &tmp_long, "") != LONGINT_OK
388 || tmp_long <= 0 || tmp_long > INT_MAX)
389 error (EXIT_FAILURE, 0,
390 _("invalid number of bytes to compare: `%s'"),
391 opt_str);
392 skip_chars = (int) tmp_long;
396 if (optind < argc)
397 infile = argv[optind++];
399 if (optind < argc)
400 outfile = argv[optind++];
402 if (optind < argc)
403 usage (1); /* Extra arguments. */
405 check_file (infile, outfile);
407 exit (EXIT_SUCCESS);