René Scharfe | 0db71e0 | 2012-04-01 00:10:11 +0200 | [diff] [blame] | 1 | #include "cache.h" |
| 2 | #include "mergesort.h" |
| 3 | |
| 4 | struct line { |
| 5 | char *text; |
| 6 | struct line *next; |
| 7 | }; |
| 8 | |
| 9 | static void *get_next(const void *a) |
| 10 | { |
| 11 | return ((const struct line *)a)->next; |
| 12 | } |
| 13 | |
| 14 | static void set_next(void *a, void *b) |
| 15 | { |
| 16 | ((struct line *)a)->next = b; |
| 17 | } |
| 18 | |
| 19 | static int compare_strings(const void *a, const void *b) |
| 20 | { |
| 21 | const struct line *x = a, *y = b; |
| 22 | return strcmp(x->text, y->text); |
| 23 | } |
| 24 | |
Ramsay Jones | 84d32bf | 2013-04-27 20:19:47 +0100 | [diff] [blame] | 25 | int main(int argc, char **argv) |
René Scharfe | 0db71e0 | 2012-04-01 00:10:11 +0200 | [diff] [blame] | 26 | { |
| 27 | struct line *line, *p = NULL, *lines = NULL; |
| 28 | struct strbuf sb = STRBUF_INIT; |
| 29 | |
| 30 | for (;;) { |
| 31 | if (strbuf_getwholeline(&sb, stdin, '\n')) |
| 32 | break; |
| 33 | line = xmalloc(sizeof(struct line)); |
| 34 | line->text = strbuf_detach(&sb, NULL); |
| 35 | if (p) { |
| 36 | line->next = p->next; |
| 37 | p->next = line; |
| 38 | } else { |
| 39 | line->next = NULL; |
| 40 | lines = line; |
| 41 | } |
| 42 | p = line; |
| 43 | } |
| 44 | |
Junio C Hamano | 7365c95 | 2012-04-17 11:07:01 -0700 | [diff] [blame] | 45 | lines = llist_mergesort(lines, get_next, set_next, compare_strings); |
René Scharfe | 0db71e0 | 2012-04-01 00:10:11 +0200 | [diff] [blame] | 46 | |
| 47 | while (lines) { |
| 48 | printf("%s", lines->text); |
| 49 | lines = lines->next; |
| 50 | } |
| 51 | return 0; |
| 52 | } |