X-Git-Url: http://pileus.org/git/?a=blobdiff_plain;f=util.c;h=a91d1bdc329d151ee9dad33ec7c1df34fd15c3ee;hb=644f831b887f220651fc651852cb4b668eae705c;hp=98c40c68a13e89d317992678da38c8361a617ea9;hpb=ee556f1413d9da165fd6ea74f0bf4039ab28da0d;p=wmpus diff --git a/util.c b/util.c index 98c40c6..a91d1bd 100644 --- a/util.c +++ b/util.c @@ -1,5 +1,6 @@ #include #include +#include #include "util.h" @@ -10,10 +11,34 @@ list_t *list_insert(list_t *next, void *data) node->next = next; node->prev = next ? next->prev : NULL; if (node->next) node->next->prev = node; - if (node->prev) node->next->next = node; + if (node->prev) node->prev->next = node; return node; } +void list_insert_after(list_t *prev, void *data) +{ + // prev must be valid, + // as we cannot return the original list head + list_t *node = new0(list_t); + node->data = data; + node->prev = prev; + node->next = prev->next; + prev->next = node; + if (node->next) node->next->prev = node; +} + +list_t *list_append(list_t *head, void *data) +{ + list_t *last = head; + while (last->next) + last = last->next; + list_t *node = new0(list_t); + node->data = data; + node->prev = last; + if (last) last->next = node; + return last ? head : node; +} + list_t *list_remove(list_t *head, list_t *node) { list_t *next = node->next; @@ -32,6 +57,15 @@ int list_length(list_t *node) return len; } -void list_move(list_t *node, int offset) +/* Misc */ +int error(char *fmt, ...) { + va_list ap; + va_start(ap, fmt); + fprintf(stderr, "Error: "); + vfprintf(stderr, fmt, ap); + fprintf(stderr, "\n"); + va_end(ap); + exit(1); + return 0; }