X-Git-Url: http://pileus.org/git/?p=wmpus;a=blobdiff_plain;f=util.c;h=cfe69a9b863e1d1a1b80825c506209b7dffc3f6c;hp=18f98dc4f17859e5c5724a8d25ed39271ba89e70;hb=a2379eee427e03e718b5307a6bd290453a058aca;hpb=f0adaa78963ffc8b870059d5787079fbbd7c52a5 diff --git a/util.c b/util.c index 18f98dc..cfe69a9 100644 --- a/util.c +++ b/util.c @@ -1,9 +1,25 @@ +/* + * Copyright (c) 2011, Andy Spencer + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + */ + #include #include #include #include "util.h" +/* Doubly linked lists */ list_t *list_insert(list_t *next, void *data) { list_t *node = new0(list_t); @@ -39,12 +55,14 @@ list_t *list_append(list_t *head, void *data) return last ? head : node; } -list_t *list_remove(list_t *head, list_t *node) +list_t *list_remove(list_t *head, list_t *node, int freedata) { list_t *next = node->next; list_t *prev = node->prev; if (next) next->prev = prev; if (prev) prev->next = next; + if (freedata) + free(node->data); free(node); return head == node ? next : head; } @@ -73,6 +91,13 @@ list_t *list_find(list_t *list, void *data) } /* Misc */ +int str2num(char *str, int def) +{ + char *end = NULL; + int num = strtol(str, &end, 10); + return end && *end == '\0' ? num : def; +} + int error(char *fmt, ...) { va_list ap;