X-Git-Url: http://pileus.org/git/?a=blobdiff_plain;f=util.c;h=18f98dc4f17859e5c5724a8d25ed39271ba89e70;hb=f0adaa78963ffc8b870059d5787079fbbd7c52a5;hp=c5f3b9af021c3351488198be26a5ccbbee8bde20;hpb=03e529a71c9743ae971d224ba353b5c5f9385366;p=wmpus diff --git a/util.c b/util.c index c5f3b9a..18f98dc 100644 --- a/util.c +++ b/util.c @@ -11,14 +11,26 @@ 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) + while (last && last->next) last = last->next; list_t *node = new0(list_t); node->data = data; @@ -45,8 +57,19 @@ int list_length(list_t *node) return len; } -void list_move(list_t *node, int offset) +list_t *list_last(list_t *list) +{ + while (list && list->next) + list = list->next; + return list; +} + +list_t *list_find(list_t *list, void *data) { + for (list_t *cur = list; cur; cur = cur->next) + if (cur->data == data) + return cur; + return NULL; } /* Misc */