liblist-c

C type-agnostic linked-list library
git clone https://git.sinitax.com/sinitax/liblist-c
Log | Files | Refs | LICENSE | sfeed.txt

test.c (916B)


      1#include "list.h"
      2
      3#include <stdlib.h>
      4#include <stdio.h>
      5#include <string.h>
      6
      7struct arg {
      8	const char *str;
      9
     10	struct list_link link;
     11};
     12
     13bool
     14test_sort(const void *p1, const void *p2, void *u)
     15{
     16	const struct arg *a1 = p1, *a2 = p2;
     17
     18	return strcmp(a1->str, a2->str) <= 0;
     19}
     20
     21int
     22main(int argc, const char **argv)
     23{
     24	struct list list;
     25	struct arg *item;
     26	const char **arg;
     27
     28	list_init(&list);
     29
     30	if (argc < 3) {
     31		fprintf(stderr, "Usage: test REVERSE [STR]..\n");
     32		return 1;
     33	}
     34
     35	for (arg = argv + 2; *arg; arg++) {
     36		item = malloc(sizeof(struct arg));
     37		if (!item) return 0;
     38		item->str = *arg;
     39		item->link = LIST_LINK_INIT;
     40		list_insert_back(&list, &item->link);
     41	}
     42
     43	list_insertion_sort(&list, atoi(argv[1]), test_sort,
     44		LIST_OFFSET(struct arg, link), NULL);
     45
     46	for (LIST_ITER_ITEMS(&list, item, struct arg, link))
     47		printf("%s\n", item->str);
     48
     49	list_free_items(&list, free, LIST_OFFSET(struct arg, link));
     50}