Reorganise, make it just one function, remove -v option it didnt work properly anyway...
[oweals/busybox.git] / coreutils / sort.c
index ed687221846f29b59b6f8c5b62e9b041b8f3019a..fc12dfb01ee23db6c41d459778d9b156a56678c5 100644 (file)
  */
 
 #include <getopt.h>
+#include <string.h>
 #include <stdlib.h>
 #include "busybox.h"
 
-int compare_ascii(const void *x, const void *y)
+static int compare_ascii(const void *x, const void *y)
 {
        return strcmp(*(char **)x, *(char **)y);
 }
 
-int compare_numeric(const void *x, const void *y)
+static int compare_numeric(const void *x, const void *y)
 {
-       return atoi(*(char **)x) - atoi(*(char **)y);
+       int z = atoi(*(char **)x) - atoi(*(char **)y);
+       return z ? z : strcmp(*(char **)x, *(char **)y);
 }
 
 int sort_main(int argc, char **argv)
@@ -41,19 +43,27 @@ int sort_main(int argc, char **argv)
        char *line, **lines = NULL;
        int i, opt, nlines = 0;
        int (*compare)(const void *, const void *) = compare_ascii;
-#ifdef BB_FEATURE_SORT_REVERSE
+#ifdef CONFIG_FEATURE_SORT_REVERSE
        int reverse = FALSE;
 #endif
+#ifdef CONFIG_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;
                                break;
-#ifdef BB_FEATURE_SORT_REVERSE
+#ifdef CONFIG_FEATURE_SORT_REVERSE
                        case 'r':
                                reverse = TRUE;
                                break;
+#endif
+#ifdef CONFIG_FEATURE_SORT_UNIQUE
+                       case 'u':
+                               unique = TRUE;
+                               break;
 #endif
                        default:
                                show_usage();
@@ -69,6 +79,7 @@ int sort_main(int argc, char **argv)
 
                while ((line = get_line_from_file(fp)) != NULL) {
                        lines = xrealloc(lines, sizeof(char *) * (nlines + 1));
+                       chomp(line);
                        lines[nlines++] = line;
                }
        }
@@ -77,13 +88,19 @@ int sort_main(int argc, char **argv)
        qsort(lines, nlines, sizeof(char *), compare);
 
        /* print it */
-#ifdef BB_FEATURE_SORT_REVERSE
-       if (reverse)
-               for (i = nlines - 1; 0 <= i; i--)
-                       fputs(lines[i], stdout);
-       else
+#ifdef CONFIG_FEATURE_SORT_REVERSE
+       if (reverse) {
+               for (i = --nlines; 0 <= i; i--)
+#ifdef CONFIG_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 CONFIG_FEATURE_SORT_UNIQUE
+                       if((!unique) || (!i) || (strcmp(lines[i - 1], lines[i])))
 #endif
-       for (i = 0; i < nlines; i++)
-               fputs(lines[i], stdout);
+                               puts(lines[i]);
        return EXIT_SUCCESS;
 }