comparison toys/posix/uniq.c @ 694:786841fdb1e0

Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style. The actual code should be the same afterward, this is just cosmetic refactoring.
author Rob Landley <rob@landley.net>
date Tue, 13 Nov 2012 17:14:08 -0600
parents 7e846e281e38
children 00c20f410c46
comparison
equal deleted inserted replaced
693:4a5a250e0633 694:786841fdb1e0
1 /* vi: set sw=4 ts=4: 1 /* uniq.c - report or filter out repeated lines in a file
2 *
3 * uniq.c - report or filter out repeated lines in a file
4 * 2 *
5 * Copyright 2012 Georgi Chorbadzhiyski <georgi@unixsol.org> 3 * Copyright 2012 Georgi Chorbadzhiyski <georgi@unixsol.org>
6 * 4 *
7 * See http://opengroup.org/onlinepubs/9699919799/utilities/uniq.html 5 * See http://opengroup.org/onlinepubs/9699919799/utilities/uniq.html
8 6
9 USE_UNIQ(NEWTOY(uniq, "f#s#w#zicdu", TOYFLAG_BIN)) 7 USE_UNIQ(NEWTOY(uniq, "f#s#w#zicdu", TOYFLAG_BIN))
10 8
11 config UNIQ 9 config UNIQ
12 bool "uniq" 10 bool "uniq"
13 default y 11 default y
14 help 12 help
15 usage: uniq [-cduiz] [-w maxchars] [-f fields] [-s char] [input_file [output_file]] 13 usage: uniq [-cduiz] [-w maxchars] [-f fields] [-s char] [input_file [output_file]]
16 14
17 Report or filter out repeated lines in a file 15 Report or filter out repeated lines in a file
18 16
19 -c show counts before each line 17 -c show counts before each line
20 -d show only lines that are repeated 18 -d show only lines that are repeated
21 -u show only lines that are unique 19 -u show only lines that are unique
22 -i ignore case when comparing lines 20 -i ignore case when comparing lines
23 -z lines end with \0 not \n 21 -z lines end with \0 not \n
24 -w compare maximum X chars per line 22 -w compare maximum X chars per line
25 -f ignore first X fields 23 -f ignore first X fields
26 -s ignore first X chars 24 -s ignore first X chars
27 */ 25 */
28 26
29 #define FOR_uniq 27 #define FOR_uniq
30 #include "toys.h" 28 #include "toys.h"
31 29
32 GLOBALS( 30 GLOBALS(
33 long maxchars; 31 long maxchars;
34 long nchars; 32 long nchars;
35 long nfields; 33 long nfields;
36 long repeats; 34 long repeats;
37 ) 35 )
38 36
39 static char *skip(char *str) 37 static char *skip(char *str)
40 { 38 {
41 long nchars = TT.nchars, nfields; 39 long nchars = TT.nchars, nfields;
42 40
43 // Skip fields first 41 // Skip fields first
44 for (nfields = TT.nfields; nfields; str++) { 42 for (nfields = TT.nfields; nfields; str++) {
45 while (*str && isspace(*str)) str++; 43 while (*str && isspace(*str)) str++;
46 while (*str && !isspace(*str)) str++; 44 while (*str && !isspace(*str)) str++;
47 nfields--; 45 nfields--;
48 } 46 }
49 // Skip chars 47 // Skip chars
50 while (*str && nchars--) str++; 48 while (*str && nchars--) str++;
51 49
52 return str; 50 return str;
53 } 51 }
54 52
55 static void print_line(FILE *f, char *line) 53 static void print_line(FILE *f, char *line)
56 { 54 {
57 if (toys.optflags & (TT.repeats ? FLAG_u : FLAG_d)) return; 55 if (toys.optflags & (TT.repeats ? FLAG_u : FLAG_d)) return;
58 if (toys.optflags & FLAG_c) fprintf(f, "%7lu ", TT.repeats + 1); 56 if (toys.optflags & FLAG_c) fprintf(f, "%7lu ", TT.repeats + 1);
59 fputs(line, f); 57 fputs(line, f);
60 if (toys.optflags & FLAG_z) fputc(0, f); 58 if (toys.optflags & FLAG_z) fputc(0, f);
61 } 59 }
62 60
63 void uniq_main(void) 61 void uniq_main(void)
64 { 62 {
65 FILE *infile = stdin, *outfile = stdout; 63 FILE *infile = stdin, *outfile = stdout;
66 char *thisline = NULL, *prevline = NULL, *tmpline, eol = '\n'; 64 char *thisline = NULL, *prevline = NULL, *tmpline, eol = '\n';
67 size_t thissize, prevsize = 0, tmpsize; 65 size_t thissize, prevsize = 0, tmpsize;
68 66
69 if (toys.optc >= 1) infile = xfopen(toys.optargs[0], "r"); 67 if (toys.optc >= 1) infile = xfopen(toys.optargs[0], "r");
70 if (toys.optc >= 2) outfile = xfopen(toys.optargs[1], "w"); 68 if (toys.optc >= 2) outfile = xfopen(toys.optargs[1], "w");
71 69
72 if (toys.optflags & FLAG_z) eol = 0; 70 if (toys.optflags & FLAG_z) eol = 0;
73 71
74 // If first line can't be read 72 // If first line can't be read
75 if (getdelim(&prevline, &prevsize, eol, infile) < 0) 73 if (getdelim(&prevline, &prevsize, eol, infile) < 0)
76 return; 74 return;
77 75
78 while (getdelim(&thisline, &thissize, eol, infile) > 0) { 76 while (getdelim(&thisline, &thissize, eol, infile) > 0) {
79 int diff; 77 int diff;
80 char *t1, *t2; 78 char *t1, *t2;
81 79
82 // If requested get the chosen fields + character offsets. 80 // If requested get the chosen fields + character offsets.
83 if (TT.nfields || TT.nchars) { 81 if (TT.nfields || TT.nchars) {
84 t1 = skip(thisline); 82 t1 = skip(thisline);
85 t2 = skip(prevline); 83 t2 = skip(prevline);
86 } else { 84 } else {
87 t1 = thisline; 85 t1 = thisline;
88 t2 = prevline; 86 t2 = prevline;
89 } 87 }
90 88
91 if (TT.maxchars == 0) { 89 if (TT.maxchars == 0) {
92 diff = !(toys.optflags & FLAG_i) 90 diff = !(toys.optflags & FLAG_i) ? strcmp(t1, t2) : strcasecmp(t1, t2);
93 ? strcmp(t1, t2) 91 } else {
94 : strcasecmp(t1, t2); 92 diff = !(toys.optflags & FLAG_i) ? strncmp(t1, t2, TT.maxchars)
95 } else { 93 : strncasecmp(t1, t2, TT.maxchars);
96 diff = !(toys.optflags & FLAG_i) 94 }
97 ? strncmp(t1, t2, TT.maxchars)
98 : strncasecmp(t1, t2, TT.maxchars);
99 }
100 95
101 if (diff == 0) { // same 96 if (diff == 0) { // same
102 TT.repeats++; 97 TT.repeats++;
103 } else { 98 } else {
104 print_line(outfile, prevline); 99 print_line(outfile, prevline);
105 100
106 TT.repeats = 0; 101 TT.repeats = 0;
107 102
108 tmpline = prevline; 103 tmpline = prevline;
109 prevline = thisline; 104 prevline = thisline;
110 thisline = tmpline; 105 thisline = tmpline;
111 106
112 tmpsize = prevsize; 107 tmpsize = prevsize;
113 prevsize = thissize; 108 prevsize = thissize;
114 thissize = tmpsize; 109 thissize = tmpsize;
115 } 110 }
116 } 111 }
117 112
118 print_line(outfile, prevline); 113 print_line(outfile, prevline);
119 114
120 if (CFG_TOYBOX_FREE) { 115 if (CFG_TOYBOX_FREE) {
121 free(prevline); 116 free(prevline);
122 free(thisline); 117 free(thisline);
123 } 118 }
124 } 119 }