find: improve usage text (Natanael Copa <natanael.copa@gmail.com>)
[oweals/busybox.git] / coreutils / sort.c
index c7abc3355124db63c4cf1cad67e1be11426c145c..311d0cb9c1c5eeb704e1c55f9042c444fc8025ee 100644 (file)
@@ -75,14 +75,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 +95,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)
@@ -278,11 +271,13 @@ static unsigned str2u(char **str)
 }
 #endif
 
+int sort_main(int argc, char **argv);
 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;
 
@@ -290,8 +285,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(argc, 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) {
@@ -300,7 +296,8 @@ int sort_main(int argc, char **argv)
                key_separator = str_t[0];
        }
        /* parse sort key */
-       if (option_mask32 & FLAG_k) {
+       lst_k = llist_rev(lst_k);
+       while (lst_k) {
                enum {
                        FLAG_allowed_for_k =
                                FLAG_n | /* Numeric sort */
@@ -314,6 +311,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) */
@@ -343,6 +341,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 */