sort: fix multiple -k (was ignoring all except last)

This commit is contained in:
Denis Vlasenko 2007-02-17 18:11:45 +00:00
parent ec27feb045
commit 54cf511ce1
8 changed files with 26 additions and 10 deletions

View File

@ -864,7 +864,7 @@ int tar_main(int argc, char **argv)
llist_add_to(&tar_handle->accept, argv[optind]); llist_add_to(&tar_handle->accept, argv[optind]);
optind++; optind++;
} }
tar_handle->accept = rev_llist(tar_handle->accept); tar_handle->accept = llist_rev(tar_handle->accept);
if (tar_handle->accept || tar_handle->reject) if (tar_handle->accept || tar_handle->reject)
tar_handle->filter = filter_accept_reject_list; tar_handle->filter = filter_accept_reject_list;

View File

@ -1312,7 +1312,7 @@ int od_main(int argc, char **argv)
if (opt & OPT_l) decode_format_string("d4"); if (opt & OPT_l) decode_format_string("d4");
if (opt & OPT_o) decode_format_string("o2"); if (opt & OPT_o) decode_format_string("o2");
//if (opt & OPT_t)... //if (opt & OPT_t)...
lst_t = rev_llist(lst_t); lst_t = llist_rev(lst_t);
while (lst_t) { while (lst_t) {
decode_format_string(lst_t->data); decode_format_string(lst_t->data);
lst_t = lst_t->link; lst_t = lst_t->link;

View File

@ -276,7 +276,8 @@ int sort_main(int argc, char **argv)
{ {
FILE *fp, *outfile = stdout; FILE *fp, *outfile = stdout;
char *line, **lines = NULL; 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 i, flag;
int linecount = 0; int linecount = 0;
@ -284,8 +285,9 @@ int sort_main(int argc, char **argv)
/* Parse command line options */ /* Parse command line options */
/* -o and -t can be given at most once */ /* -o and -t can be given at most once */
opt_complementary = "?:o--o:t--t"; opt_complementary = "?:o--o:t--t:" /* -t, -o: maximum one of each */
getopt32(argc, argv, OPT_STR, &str_ignored, &str_ignored, &str_o, &str_k, &str_t); "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 ENABLE_FEATURE_SORT_BIG
if (option_mask32 & FLAG_o) outfile = xfopen(str_o, "w"); if (option_mask32 & FLAG_o) outfile = xfopen(str_o, "w");
if (option_mask32 & FLAG_t) { if (option_mask32 & FLAG_t) {
@ -294,7 +296,8 @@ int sort_main(int argc, char **argv)
key_separator = str_t[0]; key_separator = str_t[0];
} }
/* parse sort key */ /* parse sort key */
if (option_mask32 & FLAG_k) { lst_k = llist_rev(lst_k);
while (lst_k) {
enum { enum {
FLAG_allowed_for_k = FLAG_allowed_for_k =
FLAG_n | /* Numeric sort */ FLAG_n | /* Numeric sort */
@ -308,6 +311,7 @@ int sort_main(int argc, char **argv)
0 0
}; };
struct sort_key *key = add_key(); struct sort_key *key = add_key();
char *str_k = lst_k->data;
const char *temp2; const char *temp2;
i = 0; /* i==0 before comma, 1 after (-k3,6) */ i = 0; /* i==0 before comma, 1 after (-k3,6) */
@ -337,6 +341,8 @@ int sort_main(int argc, char **argv)
str_k++; str_k++;
} }
} }
/* leaking lst_k... */
lst_k = lst_k->link;
} }
#endif #endif
/* global b strips leading and trailing spaces */ /* global b strips leading and trailing spaces */

View File

@ -473,7 +473,7 @@ extern void llist_add_to(llist_t **old_head, void *data);
extern void llist_add_to_end(llist_t **list_head, void *data); extern void llist_add_to_end(llist_t **list_head, void *data);
extern void *llist_pop(llist_t **elm); extern void *llist_pop(llist_t **elm);
extern void llist_free(llist_t *elm, void (*freeit)(void *data)); extern void llist_free(llist_t *elm, void (*freeit)(void *data));
extern llist_t* rev_llist(llist_t *list); extern llist_t* llist_rev(llist_t *list);
enum { enum {
LOGMODE_NONE = 0, LOGMODE_NONE = 0,

View File

@ -74,7 +74,7 @@ void llist_free(llist_t * elm, void (*freeit) (void *data))
/* Reverse list order. Useful since getopt32 saves option params /* Reverse list order. Useful since getopt32 saves option params
* in reverse order */ * in reverse order */
llist_t *rev_llist(llist_t * list) llist_t *llist_rev(llist_t * list)
{ {
llist_t *new = NULL; llist_t *new = NULL;

View File

@ -157,7 +157,7 @@ int wget_main(int argc, char **argv)
if (headers_llist) { if (headers_llist) {
int size = 1; int size = 1;
char *cp; char *cp;
llist_t *ll = headers_llist = rev_llist(headers_llist); llist_t *ll = headers_llist = llist_rev(headers_llist);
while (ll) { while (ll) {
size += strlen(ll->data) + 2; size += strlen(ll->data) + 2;
ll = ll->link; ll = ll->link;

View File

@ -253,7 +253,7 @@ int ps_main(int argc, char **argv)
opt_complementary = "o::"; opt_complementary = "o::";
getopt32(argc, argv, "o:aAdefl", &opt_o); getopt32(argc, argv, "o:aAdefl", &opt_o);
if (opt_o) { if (opt_o) {
opt_o = rev_llist(opt_o); opt_o = llist_rev(opt_o);
do { do {
parse_o(opt_o->data); parse_o(opt_o->data);
opt_o = opt_o->link; opt_o = opt_o->link;

View File

@ -66,6 +66,16 @@ testing "sort key range with multiple options" "sort -k2,3rn input" \
egg 1 2 papyrus egg 1 2 papyrus
" "$data" "" " "$data" ""
testing "sort key range with two -k options" "sort -k 2,2n -k 1,1r input" "\
d 2
b 2
c 3
" "\
c 3
b 2
d 2
" ""
testing "sort with non-default leading delim 1" "sort -n -k2 -t/ input" "\ testing "sort with non-default leading delim 1" "sort -n -k2 -t/ input" "\
/a/2 /a/2
/b/1 /b/1