Don't include version.h.
[coreutils.git] / src / uniq.c
blobd6900cefff89dc0161295a42eddd2c4839b3dc74
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)
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
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /* Written by Richard Stallman and David MacKenzie. */
20 #include <config.h>
22 /* Get isblank from GNU libc. */
23 #define _GNU_SOURCE
25 #include <stdio.h>
26 #include <getopt.h>
27 #include <sys/types.h>
29 #if HAVE_LIMITS_H
30 # include <limits.h>
31 #endif
33 #ifndef UINT_MAX
34 # define UINT_MAX ((unsigned int) ~(unsigned int) 0)
35 #endif
37 #ifndef INT_MAX
38 # define INT_MAX ((int) (UINT_MAX >> 1))
39 #endif
41 #include "system.h"
42 #include "linebuffer.h"
43 #include "version.h"
44 #include "error.h"
45 #include "xstrtol.h"
47 /* Undefine, to avoid warning about redefinition on some systems. */
48 #undef min
49 #define min(x, y) ((x) < (y) ? (x) : (y))
51 /* The name this program was run with. */
52 char *program_name;
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;
63 enum countmode
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;
73 enum output_mode
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. */
84 static int show_help;
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},
99 {NULL, 0, NULL, 0}
102 static void
103 usage (int status)
105 if (status != 0)
106 fprintf (stderr, _("Try `%s --help' for more information.\n"),
107 program_name);
108 else
110 printf (_("\
111 Usage: %s [OPTION]... [INPUT [OUTPUT]]\n\
113 program_name);
114 printf (_("\
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\
124 -N same as -f N\n\
125 +N same as -s N\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\
131 "));
133 exit (status);
136 /* Given a linebuffer LINE,
137 return a pointer to the beginning of the line's field to be compared. */
139 static char *
140 find_field (const struct linebuffer *line)
142 register int count;
143 register char *lp = line->buffer;
144 register int size = line->length;
145 register int i = 0;
147 for (count = 0; count < skip_fields && i < size; count++)
149 while (i < size && ISBLANK (lp[i]))
150 i++;
151 while (i < size && !ISBLANK (lp[i]))
152 i++;
155 for (count = 0; count < skip_chars && i < size; count++)
156 i++;
158 return lp + i;
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. */
166 static int
167 different (const char *old, const char *new, int oldlen, int newlen)
169 register int order;
171 if (check_chars)
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));
179 if (order == 0)
180 return oldlen - newlen;
181 return order;
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. */
189 static void
190 writeline (const struct linebuffer *line, FILE *stream, int linecount)
192 if ((mode == output_unique && linecount != 0)
193 || (mode == output_repeated && linecount == 0))
194 return;
196 if (countmode == count_occurrences)
197 fprintf (stream, "%7d\t", linecount + 1);
199 fwrite (line->buffer, sizeof (char), line->length, stream);
200 putc ('\n', stream);
203 /* Process input file INFILE with output to OUTFILE.
204 If either is "-", use the standard I/O stream for it instead. */
206 static void
207 check_file (const char *infile, const char *outfile)
209 FILE *istream;
210 FILE *ostream;
211 struct linebuffer lb1, lb2;
212 struct linebuffer *thisline, *prevline, *exch;
213 char *prevfield, *thisfield;
214 int prevlen, thislen;
215 int match_count = 0;
217 if (!strcmp (infile, "-"))
218 istream = stdin;
219 else
220 istream = fopen (infile, "r");
221 if (istream == NULL)
222 error (1, errno, "%s", infile);
224 if (!strcmp (outfile, "-"))
225 ostream = stdout;
226 else
227 ostream = fopen (outfile, "w");
228 if (ostream == NULL)
229 error (1, errno, "%s", outfile);
231 thisline = &lb1;
232 prevline = &lb2;
234 initbuffer (thisline);
235 initbuffer (prevline);
237 if (readline (prevline, istream) == 0)
238 goto closefiles;
239 prevfield = find_field (prevline);
240 prevlen = prevline->length - (prevfield - prevline->buffer);
242 while (!feof (istream))
244 if (readline (thisline, istream) == 0)
245 break;
246 thisfield = find_field (thisline);
247 thislen = thisline->length - (thisfield - thisline->buffer);
248 if (!different (thisfield, prevfield, thislen, prevlen))
249 match_count++;
250 else
252 writeline (prevline, ostream, match_count);
253 match_count = 0;
255 exch = prevline;
256 prevline = thisline;
257 thisline = exch;
258 prevfield = thisfield;
259 prevlen = thislen;
263 writeline (prevline, ostream, match_count);
265 closefiles:
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);
272 free (lb1.buffer);
273 free (lb2.buffer);
276 void
277 main (int argc, char **argv)
279 int optc;
280 char *infile = "-", *outfile = "-";
282 program_name = argv[0];
283 setlocale (LC_ALL, "");
284 bindtextdomain (PACKAGE, LOCALEDIR);
285 textdomain (PACKAGE);
287 skip_chars = 0;
288 skip_fields = 0;
289 check_chars = 0;
290 mode = output_all;
291 countmode = count_none;
293 while ((optc = getopt_long (argc, argv, "0123456789cdf:s:uw:", longopts,
294 (int *) 0)) != EOF)
296 switch (optc)
298 case 0:
299 break;
301 case '0':
302 case '1':
303 case '2':
304 case '3':
305 case '4':
306 case '5':
307 case '6':
308 case '7':
309 case '8':
310 case '9':
311 skip_fields = skip_fields * 10 + optc - '0';
312 break;
314 case 'c':
315 countmode = count_occurrences;
316 break;
318 case 'd':
319 mode = output_repeated;
320 break;
322 case 'f': /* Like '-#'. */
324 long int tmp_long;
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'"),
328 optarg);
329 skip_fields = (int) tmp_long;
331 break;
333 case 's': /* Like '+#'. */
335 long int tmp_long;
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'"),
339 optarg);
340 skip_chars = (int) tmp_long;
342 break;
344 case 'u':
345 mode = output_unique;
346 break;
348 case 'w':
350 long int tmp_long;
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'"),
354 optarg);
355 check_chars = (int) tmp_long;
357 break;
359 default:
360 usage (1);
364 if (show_version)
366 printf ("uniq - %s\n", version_string);
367 exit (0);
370 if (show_help)
371 usage (0);
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++];
380 long int tmp_long;
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'"),
384 opt_str);
385 skip_chars = (int) tmp_long;
389 if (optind < argc)
390 infile = argv[optind++];
392 if (optind < argc)
393 outfile = argv[optind++];
395 if (optind < argc)
396 usage (1); /* Extra arguments. */
398 check_file (infile, outfile);
400 exit (0);