aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2008-05-03 07:21:27 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2008-05-03 07:21:27 +0000
commit96b99b860cc15f13b85b1b2d5b5b20ab7183a652 (patch)
treef7f5236085dcc40f02bd29078900d971a787344e
parent687a26fe0dcf10f227cb0541882d1daa8a21991d (diff)
downloadbusybox-w32-96b99b860cc15f13b85b1b2d5b5b20ab7183a652.tar.gz
busybox-w32-96b99b860cc15f13b85b1b2d5b5b20ab7183a652.tar.bz2
busybox-w32-96b99b860cc15f13b85b1b2d5b5b20ab7183a652.zip
uniq: support -w. closes bug 3094.
function old new delta packed_usage 24136 24132 -4 uniq_main 399 384 -15
-rw-r--r--coreutils/uniq.c23
-rw-r--r--include/usage.h10
-rwxr-xr-xtestsuite/uniq.tests17
3 files changed, 33 insertions, 17 deletions
diff --git a/coreutils/uniq.c b/coreutils/uniq.c
index 32327c6ce..41f1fed7b 100644
--- a/coreutils/uniq.c
+++ b/coreutils/uniq.c
@@ -12,8 +12,6 @@
12 12
13#include "libbb.h" 13#include "libbb.h"
14 14
15static const char uniq_opts[] ALIGN1 = "cdu" "f:s:" "cdu\0\1\2\4";
16
17static FILE *xgetoptfile_uniq_s(char **argv, int read0write2) 15static FILE *xgetoptfile_uniq_s(char **argv, int read0write2)
18{ 16{
19 const char *n; 17 const char *n;
@@ -31,9 +29,11 @@ int uniq_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
31int uniq_main(int argc ATTRIBUTE_UNUSED, char **argv) 29int uniq_main(int argc ATTRIBUTE_UNUSED, char **argv)
32{ 30{
33 FILE *in, *out; 31 FILE *in, *out;
34 unsigned long dups, skip_fields, skip_chars, i;
35 const char *s0, *e0, *s1, *e1, *input_filename; 32 const char *s0, *e0, *s1, *e1, *input_filename;
33 unsigned long dups;
34 unsigned skip_fields, skip_chars, max_chars;
36 unsigned opt; 35 unsigned opt;
36 unsigned i;
37 37
38 enum { 38 enum {
39 OPT_c = 0x1, 39 OPT_c = 0x1,
@@ -41,15 +41,14 @@ int uniq_main(int argc ATTRIBUTE_UNUSED, char **argv)
41 OPT_u = 0x4, 41 OPT_u = 0x4,
42 OPT_f = 0x8, 42 OPT_f = 0x8,
43 OPT_s = 0x10, 43 OPT_s = 0x10,
44 OPT_w = 0x20,
44 }; 45 };
45 46
46 skip_fields = skip_chars = 0; 47 skip_fields = skip_chars = 0;
48 max_chars = -1;
47 49
48 opt = getopt32(argv, "cduf:s:", &s0, &s1); 50 opt_complementary = "f+:s+:w+";
49 if (opt & OPT_f) 51 opt = getopt32(argv, "cduf:s:w:", &skip_fields, &skip_chars, &max_chars);
50 skip_fields = xatoul(s0);
51 if (opt & OPT_s)
52 skip_chars = xatoul(s1);
53 argv += optind; 52 argv += optind;
54 53
55 input_filename = *argv; 54 input_filename = *argv;
@@ -63,7 +62,7 @@ int uniq_main(int argc ATTRIBUTE_UNUSED, char **argv)
63 bb_show_usage(); 62 bb_show_usage();
64 } 63 }
65 64
66 s1 = e1 = NULL; /* prime the pump */ 65 s1 = e1 = NULL; /* prime the pump */
67 66
68 do { 67 do {
69 s0 = s1; 68 s0 = s1;
@@ -81,16 +80,16 @@ int uniq_main(int argc ATTRIBUTE_UNUSED, char **argv)
81 ++e1; 80 ++e1;
82 } 81 }
83 82
84 if (!s0 || strcmp(e0, e1)) { 83 if (!s0 || strncmp(e0, e1, max_chars)) {
85 break; 84 break;
86 } 85 }
87 86
88 ++dups; /* Note: Testing for overflow seems excessive. */ 87 ++dups; /* note: testing for overflow seems excessive. */
89 } 88 }
90 89
91 if (s0) { 90 if (s0) {
92 if (!(opt & (OPT_d << !!dups))) { /* (if dups, opt & OPT_e) */ 91 if (!(opt & (OPT_d << !!dups))) { /* (if dups, opt & OPT_e) */
93 fprintf(out, "\0%d " + (opt & 1), dups + 1); 92 fprintf(out, "\0%ld " + (opt & 1), dups + 1); /* 1 == OPT_c */
94 fprintf(out, "%s\n", s0); 93 fprintf(out, "%s\n", s0);
95 } 94 }
96 free((void *)s0); 95 free((void *)s0);
diff --git a/include/usage.h b/include/usage.h
index cbc5cb0a2..e791ba6bf 100644
--- a/include/usage.h
+++ b/include/usage.h
@@ -4303,16 +4303,16 @@
4303 ) 4303 )
4304 4304
4305#define uniq_trivial_usage \ 4305#define uniq_trivial_usage \
4306 "[-fscdu]... [INPUT [OUTPUT]]" 4306 "[-fscduw]... [INPUT [OUTPUT]]"
4307#define uniq_full_usage "\n\n" \ 4307#define uniq_full_usage "\n\n" \
4308 "Discard all but one of successive identical lines from INPUT\n" \ 4308 "Discard duplicate lines\n" \
4309 "(or standard input), writing to OUTPUT (or standard output)\n" \
4310 "\nOptions:" \ 4309 "\nOptions:" \
4311 "\n -c Prefix lines by the number of occurrences" \ 4310 "\n -c Prefix lines by the number of occurrences" \
4312 "\n -d Only print duplicate lines" \ 4311 "\n -d Only print duplicate lines" \
4313 "\n -u Only print unique lines" \ 4312 "\n -u Only print unique lines" \
4314 "\n -f N Skip the first N fields" \ 4313 "\n -f N Skip first N fields" \
4315 "\n -s N Skip the first N chars (after any skipped fields)" \ 4314 "\n -s N Skip first N chars (after any skipped fields)" \
4315 "\n -w N Compare N characters in line" \
4316 4316
4317#define uniq_example_usage \ 4317#define uniq_example_usage \
4318 "$ echo -e \"a\\na\\nb\\nc\\nc\\na\" | sort | uniq\n" \ 4318 "$ echo -e \"a\\na\\nb\\nc\\nc\\na\" | sort | uniq\n" \
diff --git a/testsuite/uniq.tests b/testsuite/uniq.tests
index 49d4bed9c..8961d669c 100755
--- a/testsuite/uniq.tests
+++ b/testsuite/uniq.tests
@@ -41,6 +41,7 @@ testing "uniq input - (specify stdout)" "uniq input -" \
41#-c occurrences 41#-c occurrences
42#-d dups only 42#-d dups only
43#-u 43#-u
44#-w max chars
44 45
45# Test various command line options 46# Test various command line options
46 47
@@ -60,6 +61,22 @@ aa bb cc9
60bb cc dd8 61bb cc dd8
61aa bb cc9 62aa bb cc9
62" 63"
64testing "uniq -w (compare max characters)" "uniq -w 2" \
65"cc1
66" "" \
67"cc1
68cc2
69cc3
70"
71
72testing "uniq -s -w (skip fields and compare max chars)" \
73"uniq -s 2 -w 2" \
74"aaccaa
75" "" \
76"aaccaa
77aaccbb
78bbccaa
79"
63 80
64# -d is "Suppress the writing fo lines that are not repeated in the input." 81# -d is "Suppress the writing fo lines that are not repeated in the input."
65# -u is "Suppress the writing of lines that are repeated in the input." 82# -u is "Suppress the writing of lines that are repeated in the input."