built-in add -i: accept open-ended ranges again
[git/debian.git] / t / helper / test-mergesort.c
blobc5cffaa4b73ff52b2f166231ceb4a77b24ee8cef
1 #include "test-tool.h"
2 #include "cache.h"
3 #include "mergesort.h"
5 struct line {
6 char *text;
7 struct line *next;
8 };
10 static void *get_next(const void *a)
12 return ((const struct line *)a)->next;
15 static void set_next(void *a, void *b)
17 ((struct line *)a)->next = b;
20 static int compare_strings(const void *a, const void *b)
22 const struct line *x = a, *y = b;
23 return strcmp(x->text, y->text);
26 int cmd__mergesort(int argc, const char **argv)
28 struct line *line, *p = NULL, *lines = NULL;
29 struct strbuf sb = STRBUF_INIT;
31 for (;;) {
32 if (strbuf_getwholeline(&sb, stdin, '\n'))
33 break;
34 line = xmalloc(sizeof(struct line));
35 line->text = strbuf_detach(&sb, NULL);
36 if (p) {
37 line->next = p->next;
38 p->next = line;
39 } else {
40 line->next = NULL;
41 lines = line;
43 p = line;
46 lines = llist_mergesort(lines, get_next, set_next, compare_strings);
48 while (lines) {
49 printf("%s", lines->text);
50 lines = lines->next;
52 return 0;