Teach libc5 what a sighandler_t is
[oweals/busybox.git] / sort.c
diff --git a/sort.c b/sort.c
index b84453d3a4f17b19670490edb676cbc6b5ecbbb6..4f4979cc57facff7f476548e43fba6f8b9d2b01c 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;
@@ -56,6 +59,11 @@ int sort_main(int argc, char **argv)
                        case 'r':
                                reverse = TRUE;
                                break;
+#endif
+#ifdef BB_FEATURE_SORT_UNIQUE
+                       case 'u':
+                               unique = TRUE;
+                               break;
 #endif
                        default:
                                show_usage();
@@ -71,7 +79,7 @@ int sort_main(int argc, char **argv)
 
                while ((line = get_line_from_file(fp)) != NULL) {
                        lines = xrealloc(lines, sizeof(char *) * (nlines + 1));
-                       line[strlen(line) - 1] = '\0';
+                       chomp(line);
                        lines[nlines++] = line;
                }
        }
@@ -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;
 }