Interested in helping open source friends on HP-UX?
[git/mjg.git] / test-mergesort.c
blobea3b959e94ff6f53726d4fce955bca1181a3be07
1 #include "cache.h"
2 #include "mergesort.h"
4 struct line {
5 char *text;
6 struct line *next;
7 };
9 static void *get_next(const void *a)
11 return ((const struct line *)a)->next;
14 static void set_next(void *a, void *b)
16 ((struct line *)a)->next = b;
19 static int compare_strings(const void *a, const void *b)
21 const struct line *x = a, *y = b;
22 return strcmp(x->text, y->text);
25 int main(int argc, char **argv)
27 struct line *line, *p = NULL, *lines = NULL;
28 struct strbuf sb = STRBUF_INIT;
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;
42 p = line;
45 lines = llist_mergesort(lines, get_next, set_next, compare_strings);
47 while (lines) {
48 printf("%s", lines->text);
49 lines = lines->next;
51 return 0;