Applied patch from I.Q. to add sort -u as a feature.
authorMark Whitley <markw@lineo.com>
Tue, 17 Apr 2001 18:56:18 +0000 (18:56 -0000)
committerMark Whitley <markw@lineo.com>
Tue, 17 Apr 2001 18:56:18 +0000 (18:56 -0000)
Config.h
applets/usage.h
coreutils/sort.c
include/usage.h
sort.c
usage.h

index 569b4f6..bf477f6 100644 (file)
--- a/Config.h
+++ b/Config.h
 // Enable support for tar -z option (currently only works for inflating)
 #define BB_FEATURE_TAR_GZIP 
 //
-//// Enable reverse sort
+// Enable reverse sort
 #define BB_FEATURE_SORT_REVERSE
 //
+// Enable uniqe sort
+#define BB_FEATURE_SORT_UNIQUE
+//
 // Enable command line editing in the shell.  
 // Only relevant if BB_SH is enabled. On by default.
 #define BB_FEATURE_COMMAND_EDITING
index f375dfd..1e3023d 100644 (file)
        "[2 second delay results]\n"
 
 
+#ifdef BB_FEATURE_SORT_UNIQUE
+  #define USAGE_SORT_UNIQUE(a) a
+#else
+  #define USAGE_SORT_UNIQUE(a)
+#endif
 #ifdef BB_FEATURE_SORT_REVERSE
   #define USAGE_SORT_REVERSE(a) a
 #else
   #define USAGE_SORT_REVERSE(a)
 #endif
 #define sort_trivial_usage \
-       "[-n]" USAGE_SORT_REVERSE(" [-r]") " [FILE]..."
+       "[-n" USAGE_SORT_REVERSE("r") USAGE_SORT_UNIQUE("u") "] [FILE]..."
 #define sort_full_usage \
-       "Sorts lines of text in the specified files"
+       "Sorts lines of text in the specified files\n\n"\
+       "Options:\n" \
+       USAGE_SORT_UNIQUE("\t-u\tsuppress duplicate lines\n") \
+       USAGE_SORT_REVERSE("\t-r\tsort in reverse order\n") \
+       "\t-n\tsort numerics"
 #define sort_example_usage \
        "$ echo -e \"e\\nf\\nb\\nd\\nc\\na\" | sort\n" \
        "a\n" \
index 5ecca8b..4f4979c 100644 (file)
@@ -46,8 +46,11 @@ int sort_main(int argc, char **argv)
 #ifdef BB_FEATURE_SORT_REVERSE
        int reverse = FALSE;
 #endif
+#ifdef BB_FEATURE_SORT_UNIQUE
+       int unique = FALSE;
+#endif
 
-       while ((opt = getopt(argc, argv, "nr")) != -1) {
+       while ((opt = getopt(argc, argv, "nru")) != -1) {
                switch (opt) {
                        case 'n':
                                compare = compare_numeric;
@@ -57,6 +60,11 @@ int sort_main(int argc, char **argv)
                                reverse = TRUE;
                                break;
 #endif
+#ifdef BB_FEATURE_SORT_UNIQUE
+                       case 'u':
+                               unique = TRUE;
+                               break;
+#endif
                        default:
                                show_usage();
                }
@@ -81,12 +89,18 @@ int sort_main(int argc, char **argv)
 
        /* print it */
 #ifdef BB_FEATURE_SORT_REVERSE
-       if (reverse)
-               for (i = nlines - 1; 0 <= i; i--)
-                       puts(lines[i]);
-       else
+       if (reverse) {
+               for (i = --nlines; 0 <= i; i--)
+#ifdef BB_FEATURE_SORT_UNIQUE
+                       if((!unique) || (i == nlines) || (strcmp(lines[i + 1], lines[i])))
+#endif
+                               puts(lines[i]);
+       } else
+#endif
+               for (i = 0; i < nlines; i++)
+#ifdef BB_FEATURE_SORT_UNIQUE
+                       if((!unique) || (!i) || (strcmp(lines[i - 1], lines[i])))
 #endif
-       for (i = 0; i < nlines; i++)
-               puts(lines[i]);
+                               puts(lines[i]);
        return EXIT_SUCCESS;
 }
index f375dfd..1e3023d 100644 (file)
        "[2 second delay results]\n"
 
 
+#ifdef BB_FEATURE_SORT_UNIQUE
+  #define USAGE_SORT_UNIQUE(a) a
+#else
+  #define USAGE_SORT_UNIQUE(a)
+#endif
 #ifdef BB_FEATURE_SORT_REVERSE
   #define USAGE_SORT_REVERSE(a) a
 #else
   #define USAGE_SORT_REVERSE(a)
 #endif
 #define sort_trivial_usage \
-       "[-n]" USAGE_SORT_REVERSE(" [-r]") " [FILE]..."
+       "[-n" USAGE_SORT_REVERSE("r") USAGE_SORT_UNIQUE("u") "] [FILE]..."
 #define sort_full_usage \
-       "Sorts lines of text in the specified files"
+       "Sorts lines of text in the specified files\n\n"\
+       "Options:\n" \
+       USAGE_SORT_UNIQUE("\t-u\tsuppress duplicate lines\n") \
+       USAGE_SORT_REVERSE("\t-r\tsort in reverse order\n") \
+       "\t-n\tsort numerics"
 #define sort_example_usage \
        "$ echo -e \"e\\nf\\nb\\nd\\nc\\na\" | sort\n" \
        "a\n" \
diff --git a/sort.c b/sort.c
index 5ecca8b..4f4979c 100644 (file)
--- a/sort.c
+++ b/sort.c
@@ -46,8 +46,11 @@ int sort_main(int argc, char **argv)
 #ifdef BB_FEATURE_SORT_REVERSE
        int reverse = FALSE;
 #endif
+#ifdef BB_FEATURE_SORT_UNIQUE
+       int unique = FALSE;
+#endif
 
-       while ((opt = getopt(argc, argv, "nr")) != -1) {
+       while ((opt = getopt(argc, argv, "nru")) != -1) {
                switch (opt) {
                        case 'n':
                                compare = compare_numeric;
@@ -57,6 +60,11 @@ int sort_main(int argc, char **argv)
                                reverse = TRUE;
                                break;
 #endif
+#ifdef BB_FEATURE_SORT_UNIQUE
+                       case 'u':
+                               unique = TRUE;
+                               break;
+#endif
                        default:
                                show_usage();
                }
@@ -81,12 +89,18 @@ int sort_main(int argc, char **argv)
 
        /* print it */
 #ifdef BB_FEATURE_SORT_REVERSE
-       if (reverse)
-               for (i = nlines - 1; 0 <= i; i--)
-                       puts(lines[i]);
-       else
+       if (reverse) {
+               for (i = --nlines; 0 <= i; i--)
+#ifdef BB_FEATURE_SORT_UNIQUE
+                       if((!unique) || (i == nlines) || (strcmp(lines[i + 1], lines[i])))
+#endif
+                               puts(lines[i]);
+       } else
+#endif
+               for (i = 0; i < nlines; i++)
+#ifdef BB_FEATURE_SORT_UNIQUE
+                       if((!unique) || (!i) || (strcmp(lines[i - 1], lines[i])))
 #endif
-       for (i = 0; i < nlines; i++)
-               puts(lines[i]);
+                               puts(lines[i]);
        return EXIT_SUCCESS;
 }
diff --git a/usage.h b/usage.h
index f375dfd..1e3023d 100644 (file)
--- a/usage.h
+++ b/usage.h
        "[2 second delay results]\n"
 
 
+#ifdef BB_FEATURE_SORT_UNIQUE
+  #define USAGE_SORT_UNIQUE(a) a
+#else
+  #define USAGE_SORT_UNIQUE(a)
+#endif
 #ifdef BB_FEATURE_SORT_REVERSE
   #define USAGE_SORT_REVERSE(a) a
 #else
   #define USAGE_SORT_REVERSE(a)
 #endif
 #define sort_trivial_usage \
-       "[-n]" USAGE_SORT_REVERSE(" [-r]") " [FILE]..."
+       "[-n" USAGE_SORT_REVERSE("r") USAGE_SORT_UNIQUE("u") "] [FILE]..."
 #define sort_full_usage \
-       "Sorts lines of text in the specified files"
+       "Sorts lines of text in the specified files\n\n"\
+       "Options:\n" \
+       USAGE_SORT_UNIQUE("\t-u\tsuppress duplicate lines\n") \
+       USAGE_SORT_REVERSE("\t-r\tsort in reverse order\n") \
+       "\t-n\tsort numerics"
 #define sort_example_usage \
        "$ echo -e \"e\\nf\\nb\\nd\\nc\\na\" | sort\n" \
        "a\n" \