test-mergesort.con commit Merge branch 'rs/commit-list-sort-in-batch' (ba8e632)
   1#include "cache.h"
   2#include "mergesort.h"
   3
   4struct line {
   5        char *text;
   6        struct line *next;
   7};
   8
   9static void *get_next(const void *a)
  10{
  11        return ((const struct line *)a)->next;
  12}
  13
  14static void set_next(void *a, void *b)
  15{
  16        ((struct line *)a)->next = b;
  17}
  18
  19static 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
  25int main(int argc, const char **argv)
  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
  45        lines = llist_mergesort(lines, get_next, set_next, compare_strings);
  46
  47        while (lines) {
  48                printf("%s", lines->text);
  49                lines = lines->next;
  50        }
  51        return 0;
  52}