diff options
author | Eric Andersen <andersen@codepoet.org> | 2000-12-09 16:37:53 +0000 |
---|---|---|
committer | Eric Andersen <andersen@codepoet.org> | 2000-12-09 16:37:53 +0000 |
commit | 5b5db38a7df20c8196e6a737cb2c76b219e152cc (patch) | |
tree | dcf75d601711f5c3b7ccf036ba80b7fcc71fc338 | |
parent | 1bca5ed886322f49fddd4b05ca76fd3a1906d98b (diff) | |
download | busybox-w32-5b5db38a7df20c8196e6a737cb2c76b219e152cc.tar.gz busybox-w32-5b5db38a7df20c8196e6a737cb2c76b219e152cc.tar.bz2 busybox-w32-5b5db38a7df20c8196e6a737cb2c76b219e152cc.zip |
Patch from Matt Kraai to implement uniq -[cdu]
-rw-r--r-- | applets/usage.c | 4 | ||||
-rw-r--r-- | coreutils/uniq.c | 45 | ||||
-rw-r--r-- | docs/busybox.pod | 8 | ||||
-rw-r--r-- | docs/busybox.sgml | 12 | ||||
-rw-r--r-- | uniq.c | 45 | ||||
-rw-r--r-- | usage.c | 4 |
6 files changed, 103 insertions, 15 deletions
diff --git a/applets/usage.c b/applets/usage.c index 75c421a09..ae6cec8df 100644 --- a/applets/usage.c +++ b/applets/usage.c | |||
@@ -1365,6 +1365,10 @@ const char uniq_usage[] = | |||
1365 | #ifndef BB_FEATURE_TRIVIAL_HELP | 1365 | #ifndef BB_FEATURE_TRIVIAL_HELP |
1366 | "\nDiscard all but one of successive identical lines from INPUT\n" | 1366 | "\nDiscard all but one of successive identical lines from INPUT\n" |
1367 | "(or standard input), writing to OUTPUT (or standard output).\n" | 1367 | "(or standard input), writing to OUTPUT (or standard output).\n" |
1368 | "Options:\n" | ||
1369 | "\t-c\tprefix lines by the number of occurrences\n" | ||
1370 | "\t-d\tonly print duplicate lines\n" | ||
1371 | "\t-u\tonly print unique lines\n" | ||
1368 | #endif | 1372 | #endif |
1369 | ; | 1373 | ; |
1370 | #endif | 1374 | #endif |
diff --git a/coreutils/uniq.c b/coreutils/uniq.c index cfe6cca5e..c0229aecb 100644 --- a/coreutils/uniq.c +++ b/coreutils/uniq.c | |||
@@ -28,28 +28,59 @@ | |||
28 | #include <string.h> | 28 | #include <string.h> |
29 | #include <errno.h> | 29 | #include <errno.h> |
30 | 30 | ||
31 | static int print_count; | ||
32 | static int print_uniq = 1; | ||
33 | static int print_duplicates = 1; | ||
34 | |||
35 | static void print_line(char *line, int count, FILE *fp) | ||
36 | { | ||
37 | if ((print_duplicates && count > 1) || (print_uniq && count == 1)) { | ||
38 | if (print_count) | ||
39 | fprintf(fp, "%7d\t%s", count, line); | ||
40 | else | ||
41 | fputs(line, fp); | ||
42 | } | ||
43 | } | ||
44 | |||
31 | int uniq_main(int argc, char **argv) | 45 | int uniq_main(int argc, char **argv) |
32 | { | 46 | { |
33 | FILE *in = stdin, *out = stdout; | 47 | FILE *in = stdin, *out = stdout; |
34 | char *lastline = NULL, *input; | 48 | char *lastline = NULL, *input; |
49 | int opt, count = 0; | ||
35 | 50 | ||
36 | /* parse argv[] */ | 51 | /* parse argv[] */ |
37 | if ((argc > 1 && **(argv + 1) == '-') || argc > 3) | 52 | while ((opt = getopt(argc, argv, "cdu")) > 0) { |
38 | usage(uniq_usage); | 53 | switch (opt) { |
54 | case 'c': | ||
55 | print_count = 1; | ||
56 | break; | ||
57 | case 'd': | ||
58 | print_duplicates = 1; | ||
59 | print_uniq = 0; | ||
60 | break; | ||
61 | case 'u': | ||
62 | print_duplicates = 0; | ||
63 | print_uniq = 1; | ||
64 | break; | ||
65 | } | ||
66 | } | ||
39 | 67 | ||
40 | if (argv[1] != NULL) { | 68 | if (argv[optind] != NULL) { |
41 | in = xfopen(argv[1], "r"); | 69 | in = xfopen(argv[optind], "r"); |
42 | if (argv[2] != NULL) | 70 | if (argv[optind+1] != NULL) |
43 | out = xfopen(argv[2], "w"); | 71 | out = xfopen(argv[optind+1], "w"); |
44 | } | 72 | } |
45 | 73 | ||
46 | while ((input = get_line_from_file(in)) != NULL) { | 74 | while ((input = get_line_from_file(in)) != NULL) { |
47 | if (lastline == NULL || strcmp(input, lastline) != 0) { | 75 | if (lastline == NULL || strcmp(input, lastline) != 0) { |
48 | fputs(input, out); | 76 | print_line(lastline, count, out); |
49 | free(lastline); | 77 | free(lastline); |
50 | lastline = input; | 78 | lastline = input; |
79 | count = 0; | ||
51 | } | 80 | } |
81 | count++; | ||
52 | } | 82 | } |
83 | print_line(lastline, count, out); | ||
53 | free(lastline); | 84 | free(lastline); |
54 | 85 | ||
55 | return EXIT_SUCCESS; | 86 | return EXIT_SUCCESS; |
diff --git a/docs/busybox.pod b/docs/busybox.pod index 2ddacd152..5e4798439 100644 --- a/docs/busybox.pod +++ b/docs/busybox.pod | |||
@@ -1954,6 +1954,12 @@ Usage: uniq [OPTION]... [INPUT [OUTPUT]] | |||
1954 | 1954 | ||
1955 | Discard all but one of successive identical lines from INPUT | 1955 | Discard all but one of successive identical lines from INPUT |
1956 | (or standard input), writing to OUTPUT (or standard output). | 1956 | (or standard input), writing to OUTPUT (or standard output). |
1957 | |||
1958 | Options: | ||
1959 | |||
1960 | -c prefix lines by the number of occurrences | ||
1961 | -d only print duplicate lines | ||
1962 | -u only print unique lines | ||
1957 | 1963 | ||
1958 | Example: | 1964 | Example: |
1959 | 1965 | ||
@@ -2286,4 +2292,4 @@ Enrique Zanardi <ezanardi@ull.es> | |||
2286 | 2292 | ||
2287 | =cut | 2293 | =cut |
2288 | 2294 | ||
2289 | # $Id: busybox.pod,v 1.79 2000/12/08 20:38:00 andersen Exp $ | 2295 | # $Id: busybox.pod,v 1.80 2000/12/09 16:37:53 andersen Exp $ |
diff --git a/docs/busybox.sgml b/docs/busybox.sgml index 184814125..7d86e19e2 100644 --- a/docs/busybox.sgml +++ b/docs/busybox.sgml | |||
@@ -3428,6 +3428,18 @@ | |||
3428 | </para> | 3428 | </para> |
3429 | 3429 | ||
3430 | <para> | 3430 | <para> |
3431 | Options: | ||
3432 | </para> | ||
3433 | |||
3434 | <para> | ||
3435 | <screen> | ||
3436 | -c prefix lines by the number of occurrences | ||
3437 | -d only print duplicate lines | ||
3438 | -u only print unique lines | ||
3439 | </screen> | ||
3440 | </para> | ||
3441 | |||
3442 | <para> | ||
3431 | Example: | 3443 | Example: |
3432 | </para> | 3444 | </para> |
3433 | 3445 | ||
@@ -28,28 +28,59 @@ | |||
28 | #include <string.h> | 28 | #include <string.h> |
29 | #include <errno.h> | 29 | #include <errno.h> |
30 | 30 | ||
31 | static int print_count; | ||
32 | static int print_uniq = 1; | ||
33 | static int print_duplicates = 1; | ||
34 | |||
35 | static void print_line(char *line, int count, FILE *fp) | ||
36 | { | ||
37 | if ((print_duplicates && count > 1) || (print_uniq && count == 1)) { | ||
38 | if (print_count) | ||
39 | fprintf(fp, "%7d\t%s", count, line); | ||
40 | else | ||
41 | fputs(line, fp); | ||
42 | } | ||
43 | } | ||
44 | |||
31 | int uniq_main(int argc, char **argv) | 45 | int uniq_main(int argc, char **argv) |
32 | { | 46 | { |
33 | FILE *in = stdin, *out = stdout; | 47 | FILE *in = stdin, *out = stdout; |
34 | char *lastline = NULL, *input; | 48 | char *lastline = NULL, *input; |
49 | int opt, count = 0; | ||
35 | 50 | ||
36 | /* parse argv[] */ | 51 | /* parse argv[] */ |
37 | if ((argc > 1 && **(argv + 1) == '-') || argc > 3) | 52 | while ((opt = getopt(argc, argv, "cdu")) > 0) { |
38 | usage(uniq_usage); | 53 | switch (opt) { |
54 | case 'c': | ||
55 | print_count = 1; | ||
56 | break; | ||
57 | case 'd': | ||
58 | print_duplicates = 1; | ||
59 | print_uniq = 0; | ||
60 | break; | ||
61 | case 'u': | ||
62 | print_duplicates = 0; | ||
63 | print_uniq = 1; | ||
64 | break; | ||
65 | } | ||
66 | } | ||
39 | 67 | ||
40 | if (argv[1] != NULL) { | 68 | if (argv[optind] != NULL) { |
41 | in = xfopen(argv[1], "r"); | 69 | in = xfopen(argv[optind], "r"); |
42 | if (argv[2] != NULL) | 70 | if (argv[optind+1] != NULL) |
43 | out = xfopen(argv[2], "w"); | 71 | out = xfopen(argv[optind+1], "w"); |
44 | } | 72 | } |
45 | 73 | ||
46 | while ((input = get_line_from_file(in)) != NULL) { | 74 | while ((input = get_line_from_file(in)) != NULL) { |
47 | if (lastline == NULL || strcmp(input, lastline) != 0) { | 75 | if (lastline == NULL || strcmp(input, lastline) != 0) { |
48 | fputs(input, out); | 76 | print_line(lastline, count, out); |
49 | free(lastline); | 77 | free(lastline); |
50 | lastline = input; | 78 | lastline = input; |
79 | count = 0; | ||
51 | } | 80 | } |
81 | count++; | ||
52 | } | 82 | } |
83 | print_line(lastline, count, out); | ||
53 | free(lastline); | 84 | free(lastline); |
54 | 85 | ||
55 | return EXIT_SUCCESS; | 86 | return EXIT_SUCCESS; |
@@ -1365,6 +1365,10 @@ const char uniq_usage[] = | |||
1365 | #ifndef BB_FEATURE_TRIVIAL_HELP | 1365 | #ifndef BB_FEATURE_TRIVIAL_HELP |
1366 | "\nDiscard all but one of successive identical lines from INPUT\n" | 1366 | "\nDiscard all but one of successive identical lines from INPUT\n" |
1367 | "(or standard input), writing to OUTPUT (or standard output).\n" | 1367 | "(or standard input), writing to OUTPUT (or standard output).\n" |
1368 | "Options:\n" | ||
1369 | "\t-c\tprefix lines by the number of occurrences\n" | ||
1370 | "\t-d\tonly print duplicate lines\n" | ||
1371 | "\t-u\tonly print unique lines\n" | ||
1368 | #endif | 1372 | #endif |
1369 | ; | 1373 | ; |
1370 | #endif | 1374 | #endif |