.
[coreutils.git] / src / uniq.c
blob8f86e978fbf8dacb26f3c9a3ebb46555df348bbd
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>
28 #include "system.h"
29 #include "linebuffer.h"
30 #include "version.h"
31 #include "error.h"
33 /* Undefine, to avoid warning about redefinition on some systems. */
34 #undef min
35 #define min(x, y) ((x) < (y) ? (x) : (y))
37 static char *find_field ();
38 static int different ();
39 static void check_file ();
40 static void usage ();
41 static void writeline ();
43 /* The name this program was run with. */
44 char *program_name;
46 /* Number of fields to skip on each line when doing comparisons. */
47 static int skip_fields;
49 /* Number of chars to skip after skipping any fields. */
50 static int skip_chars;
52 /* Number of chars to compare; if 0, compare the whole lines. */
53 static int check_chars;
55 enum countmode
57 count_occurrences, /* -c Print count before output lines. */
58 count_none /* Default. Do not print counts. */
61 /* Whether and how to precede the output lines with a count of the number of
62 times they occurred in the input. */
63 static enum countmode countmode;
65 enum output_mode
67 output_repeated, /* -d Only lines that are repeated. */
68 output_unique, /* -u Only lines that are not repeated. */
69 output_all /* Default. Print first copy of each line. */
72 /* Which lines to output. */
73 static enum output_mode mode;
75 /* If non-zero, display usage information and exit. */
76 static int show_help;
78 /* If non-zero, print the version on standard output then exit. */
79 static int show_version;
81 static struct option const longopts[] =
83 {"count", no_argument, NULL, 'c'},
84 {"repeated", no_argument, NULL, 'd'},
85 {"unique", no_argument, NULL, 'u'},
86 {"skip-fields", required_argument, NULL, 'f'},
87 {"skip-chars", required_argument, NULL, 's'},
88 {"check-chars", required_argument, NULL, 'w'},
89 {"help", no_argument, &show_help, 1},
90 {"version", no_argument, &show_version, 1},
91 {NULL, 0, NULL, 0}
94 void
95 main (argc, argv)
96 int argc;
97 char *argv[];
99 int optc;
100 char *infile = "-", *outfile = "-";
102 program_name = argv[0];
103 skip_chars = 0;
104 skip_fields = 0;
105 check_chars = 0;
106 mode = output_all;
107 countmode = count_none;
109 while ((optc = getopt_long (argc, argv, "0123456789cdf:s:uw:", longopts,
110 (int *) 0)) != EOF)
112 switch (optc)
114 case 0:
115 break;
117 case '0':
118 case '1':
119 case '2':
120 case '3':
121 case '4':
122 case '5':
123 case '6':
124 case '7':
125 case '8':
126 case '9':
127 skip_fields = skip_fields * 10 + optc - '0';
128 break;
130 case 'c':
131 countmode = count_occurrences;
132 break;
134 case 'd':
135 mode = output_repeated;
136 break;
138 case 'f': /* Like '-#'. */
139 skip_fields = atoi (optarg);
140 break;
142 case 's': /* Like '+#'. */
143 skip_chars = atoi (optarg);
144 break;
146 case 'u':
147 mode = output_unique;
148 break;
150 case 'w':
151 check_chars = atoi (optarg);
152 break;
154 default:
155 usage (1);
159 if (show_version)
161 printf ("uniq - %s\n", version_string);
162 exit (0);
165 if (show_help)
166 usage (0);
168 if (optind >= 2 && strcmp (argv[optind - 1], "--") != 0)
170 /* Interpret non-option arguments with leading `+' only
171 if we haven't seen `--'. */
172 while (optind < argc && argv[optind][0] == '+')
173 skip_chars = atoi (argv[optind++]);
176 if (optind < argc)
177 infile = argv[optind++];
179 if (optind < argc)
180 outfile = argv[optind++];
182 if (optind < argc)
183 usage (1); /* Extra arguments. */
185 check_file (infile, outfile);
187 exit (0);
190 /* Process input file INFILE with output to OUTFILE.
191 If either is "-", use the standard I/O stream for it instead. */
193 static void
194 check_file (infile, outfile)
195 char *infile, *outfile;
197 FILE *istream;
198 FILE *ostream;
199 struct linebuffer lb1, lb2;
200 struct linebuffer *thisline, *prevline, *exch;
201 char *prevfield, *thisfield;
202 int prevlen, thislen;
203 int match_count = 0;
205 if (!strcmp (infile, "-"))
206 istream = stdin;
207 else
208 istream = fopen (infile, "r");
209 if (istream == NULL)
210 error (1, errno, "%s", infile);
212 if (!strcmp (outfile, "-"))
213 ostream = stdout;
214 else
215 ostream = fopen (outfile, "w");
216 if (ostream == NULL)
217 error (1, errno, "%s", outfile);
219 thisline = &lb1;
220 prevline = &lb2;
222 initbuffer (thisline);
223 initbuffer (prevline);
225 if (readline (prevline, istream) == 0)
226 goto closefiles;
227 prevfield = find_field (prevline);
228 prevlen = prevline->length - (prevfield - prevline->buffer);
230 while (!feof (istream))
232 if (readline (thisline, istream) == 0)
233 break;
234 thisfield = find_field (thisline);
235 thislen = thisline->length - (thisfield - thisline->buffer);
236 if (!different (thisfield, prevfield, thislen, prevlen))
237 match_count++;
238 else
240 writeline (prevline, ostream, match_count);
241 match_count = 0;
243 exch = prevline;
244 prevline = thisline;
245 thisline = exch;
246 prevfield = thisfield;
247 prevlen = thislen;
251 writeline (prevline, ostream, match_count);
253 closefiles:
254 if (ferror (istream) || fclose (istream) == EOF)
255 error (1, errno, "error reading %s", infile);
257 if (ferror (ostream) || fclose (ostream) == EOF)
258 error (1, errno, "error writing %s", outfile);
260 free (lb1.buffer);
261 free (lb2.buffer);
264 /* Given a linebuffer LINE,
265 return a pointer to the beginning of the line's field to be compared. */
267 static char *
268 find_field (line)
269 struct linebuffer *line;
271 register int count;
272 register char *lp = line->buffer;
273 register int size = line->length;
274 register int i = 0;
276 for (count = 0; count < skip_fields && i < size; count++)
278 while (i < size && ISBLANK (lp[i]))
279 i++;
280 while (i < size && !ISBLANK (lp[i]))
281 i++;
284 for (count = 0; count < skip_chars && i < size; count++)
285 i++;
287 return lp + i;
290 /* Return zero if two strings OLD and NEW match, nonzero if not.
291 OLD and NEW point not to the beginnings of the lines
292 but rather to the beginnings of the fields to compare.
293 OLDLEN and NEWLEN are their lengths. */
295 static int
296 different (old, new, oldlen, newlen)
297 char *old;
298 char *new;
299 int oldlen;
300 int newlen;
302 register int order;
304 if (check_chars)
306 if (oldlen > check_chars)
307 oldlen = check_chars;
308 if (newlen > check_chars)
309 newlen = check_chars;
311 order = memcmp (old, new, min (oldlen, newlen));
312 if (order == 0)
313 return oldlen - newlen;
314 return order;
317 /* Output the line in linebuffer LINE to stream STREAM
318 provided that the switches say it should be output.
319 If requested, print the number of times it occurred, as well;
320 LINECOUNT + 1 is the number of times that the line occurred. */
322 static void
323 writeline (line, stream, linecount)
324 struct linebuffer *line;
325 FILE *stream;
326 int linecount;
328 if ((mode == output_unique && linecount != 0)
329 || (mode == output_repeated && linecount == 0))
330 return;
332 if (countmode == count_occurrences)
333 fprintf (stream, "%7d\t", linecount + 1);
335 fwrite (line->buffer, sizeof (char), line->length, stream);
336 putc ('\n', stream);
339 static void
340 usage (status)
341 int status;
343 if (status != 0)
344 fprintf (stderr, "Try `%s --help' for more information.\n",
345 program_name);
346 else
348 printf ("\
349 Usage: %s [OPTION]... [INPUT [OUTPUT]]\n\
351 program_name);
352 printf ("\
353 Discard all but one of successive identical lines from INPUT (or\n\
354 standard input), writing to OUTPUT (or standard output).\n\
356 -c, --count prefix lines by the number of occurrences\n\
357 -d, --repeated only print duplicate lines\n\
358 -f, --skip-fields=N avoid comparing the N first fields\n\
359 -s, --skip-chars=N avoid comparing the N first characters\n\
360 -u, --unique only print unique lines\n\
361 -w, --check-chars=N compare no more than N characters in lines\n\
362 -N same as -f N\n\
363 +N same as -s N\n\
364 --help display this help and exit\n\
365 --version output version information and exit\n\
367 A field is a run of whitespace, than non-whitespace characters.\n\
368 Fields are skipped before chars. \n\
371 exit (status);