Mercurial > hg > toybox
view lib/llist.c @ 990:8caeba551a28
This is the preliminary version of lspci text output support (the location
of pci.ids is hard-coded to /usr/share/misc/pci.ids, as found on Debian/Ubuntu).
+88 lines in two files, including reformatting, comments, whitespace,
and build configuration as well as new code.
The changes:
-Add library code to look up descriptions.
-Add a counter for -n (for the sake of -nn)
-Add the file stream "db" to GLOBALS and open it conditionally
-Add name fields to bufs
-Look up text if enabled and -n is passed never or twice
-Print text if lookup succeeded
author | Isaac Dunham <idunham@lavabit.com> |
---|---|
date | Wed, 07 Aug 2013 11:51:26 -0500 |
parents | 50d759f8b371 |
children | ef72a16f4b3a |
line wrap: on
line source
/* llist.c - Linked list functions * * Linked list structures have a next pointer as their first element. */ #include "toys.h" // Call a function (such as free()) on each element of a linked list. void llist_traverse(void *list, void (*using)(void *data)) { void *old = list; while (list) { void *pop = llist_pop(&list); using(pop); // End doubly linked list too. if (old == list) break; } } // Return the first item from the list, advancing the list (which must be called // as &list) void *llist_pop(void *list) { // I'd use a void ** for the argument, and even accept the typecast in all // callers as documentation you need the &, except the stupid compiler // would then scream about type-punned pointers. Screw it. void **llist = (void **)list; void **next = (void **)*llist; *llist = *next; return (void *)next; } void dlist_add_nomalloc(struct double_list **list, struct double_list *new) { if (*list) { new->next = *list; new->prev = (*list)->prev; (*list)->prev->next = new; (*list)->prev = new; } else *list = new->next = new->prev = new; } // Add an entry to the end of a doubly linked list struct double_list *dlist_add(struct double_list **list, char *data) { struct double_list *new = xmalloc(sizeof(struct double_list)); new->data = data; dlist_add_nomalloc(list, new); return new; }