sort: -z outputs NUL terminated lines. Closes bug 1591.
[oweals/busybox.git] / coreutils / sort.c
index ab59355a94279c3c072f68aa5c667e71e3276f05..d8df4c53260e63a8aa9de10c3a9a6f375e034562 100644 (file)
  * http://www.opengroup.org/onlinepubs/007904975/utilities/sort.html
  */
 
-#include "busybox.h"
+#include "libbb.h"
+
+/* This is a NOEXEC applet. Be very careful! */
+
 
 /*
        sort [-m][-o output][-bdfinru][-t char][-k keydef]... [file...]
@@ -20,7 +23,7 @@
 */
 
 /* These are sort types */
-static const char OPT_STR[] = "ngMucszbrdfimS:T:o:k:t:";
+static const char OPT_STR[] ALIGN1 = "ngMucszbrdfimS:T:o:k:t:";
 enum {
        FLAG_n  = 1,            /* Numeric sort */
        FLAG_g  = 2,            /* Sort using strtod() */
@@ -29,7 +32,7 @@ enum {
        FLAG_u  = 8,            /* Unique */
        FLAG_c  = 0x10,         /* Check: no output, exit(!ordered) */
        FLAG_s  = 0x20,         /* Stable sort, no ascii fallback at end */
-       FLAG_z  = 0x40,         /* Input is null terminated, not \n */
+       FLAG_z  = 0x40,         /* Input and output is NUL terminated, not \n */
 /* These can be applied to search keys, the previous four can't */
        FLAG_b  = 0x80,         /* Ignore leading blanks */
        FLAG_r  = 0x100,        /* Reverse */
@@ -75,14 +78,10 @@ static char *get_key(char *str, struct sort_key *key, int flags)
                        end = 0;
                        for (i = 1; i < key->range[2*j] + j; i++) {
                                if (key_separator) {
-                                       /* Skip first separator */
-                                       while (str[end] == key_separator)
-                                               end++;
-                                       /* Skip body of key */
+                                       /* Skip body of key and separator */
                                        while (str[end]) {
-                                               if (str[end] == key_separator)
+                                               if (str[end++] == key_separator)
                                                        break;
-                                               end++;
                                        }
                                } else {
                                        /* Skip leading blanks */
@@ -99,9 +98,6 @@ static char *get_key(char *str, struct sort_key *key, int flags)
                }
                if (!j) start = end;
        }
-       /* Key with explicit separator starts after separator */
-       if (key_separator && str[start] == key_separator)
-               start++;
        /* Strip leading whitespace if necessary */
 //XXX: skip_whitespace()
        if (flags & FLAG_b)
@@ -265,6 +261,7 @@ static int compare_keys(const void *xarg, const void *yarg)
        return retval;
 }
 
+#if ENABLE_FEATURE_SORT_BIG
 static unsigned str2u(char **str)
 {
        unsigned long lu;
@@ -275,12 +272,15 @@ static unsigned str2u(char **str)
                bb_error_msg_and_die("bad field specification");
        return lu;
 }
+#endif
 
+int sort_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
 int sort_main(int argc, char **argv)
 {
        FILE *fp, *outfile = stdout;
        char *line, **lines = NULL;
-       char *str_ignored, *str_o, *str_k, *str_t;
+       char *str_ignored, *str_o, *str_t;
+       llist_t *lst_k = NULL;
        int i, flag;
        int linecount = 0;
 
@@ -288,8 +288,9 @@ int sort_main(int argc, char **argv)
 
        /* Parse command line options */
        /* -o and -t can be given at most once */
-       opt_complementary = "?:o--o:t--t";
-       getopt32(argc, argv, OPT_STR, &str_ignored, &str_ignored, &str_o, &str_k, &str_t);
+       opt_complementary = "o--o:t--t:" /* -t, -o: maximum one of each */
+                       "k::"; /* -k takes list */
+       getopt32(argv, OPT_STR, &str_ignored, &str_ignored, &str_o, &lst_k, &str_t);
 #if ENABLE_FEATURE_SORT_BIG
        if (option_mask32 & FLAG_o) outfile = xfopen(str_o, "w");
        if (option_mask32 & FLAG_t) {
@@ -298,7 +299,7 @@ int sort_main(int argc, char **argv)
                key_separator = str_t[0];
        }
        /* parse sort key */
-       if (option_mask32 & FLAG_k) {
+       while (lst_k) {
                enum {
                        FLAG_allowed_for_k =
                                FLAG_n | /* Numeric sort */
@@ -312,6 +313,7 @@ int sort_main(int argc, char **argv)
                        0
                };
                struct sort_key *key = add_key();
+               char *str_k = lst_k->data;
                const char *temp2;
 
                i = 0; /* i==0 before comma, 1 after (-k3,6) */
@@ -341,6 +343,8 @@ int sort_main(int argc, char **argv)
                                str_k++;
                        }
                }
+               /* leaking lst_k... */
+               lst_k = lst_k->link;
        }
 #endif
        /* global b strips leading and trailing spaces */
@@ -370,9 +374,9 @@ int sort_main(int argc, char **argv)
                for (i = 1; i < linecount; i++)
                        if (compare_keys(&lines[i-1], &lines[i]) > j) {
                                fprintf(stderr, "Check line %d\n", i);
-                               return 1;
+                               return EXIT_FAILURE;
                        }
-               return 0;
+               return EXIT_SUCCESS;
        }
 #endif
        /* Perform the actual sort */
@@ -392,8 +396,9 @@ int sort_main(int argc, char **argv)
                if (linecount) linecount = flag+1;
        }
        /* Print it */
+       flag = (option_mask32 & FLAG_z) ? '\0' : '\n';
        for (i = 0; i < linecount; i++)
-               fprintf(outfile, "%s\n", lines[i]);
+               fprintf(outfile, "%s%c", lines[i], flag);
 
        fflush_stdout_and_exit(EXIT_SUCCESS);
 }