summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorNathan Angelacos <nangel@alpinelinux.org>2008-05-30 17:32:12 +0000
committerNathan Angelacos <nangel@alpinelinux.org>2008-05-30 17:32:12 +0000
commit7bafb94dca6393a48f774e987870f91f9fd82d73 (patch)
treefbd384123acbc5749983ed1454619e8586d5e385 /src
parentbb899c9dda1b0e7d317b89ee5bdca96b3d499c4d (diff)
downloadhaserl-7bafb94dca6393a48f774e987870f91f9fd82d73.tar.bz2
haserl-7bafb94dca6393a48f774e987870f91f9fd82d73.tar.xz
llist stuff
Diffstat (limited to 'src')
-rw-r--r--src/Makefile.am10
-rw-r--r--src/config.h.in2
-rw-r--r--src/llist.c168
-rw-r--r--src/xmalloc.h1
4 files changed, 72 insertions, 109 deletions
diff --git a/src/Makefile.am b/src/Makefile.am
index 5d38878..c12fc80 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -46,6 +46,7 @@ bin_PROGRAMS = haserl
haserl_SOURCES = common.c common.h sliding_buffer.c sliding_buffer.h \
h_error.c h_error.h h_script.c h_script.h rfc2388.c rfc2388.h \
+ llist.c \
$(BASHSOURCE) $(LUASOURCE) haserl.c haserl.h
install-strip:
@@ -55,12 +56,3 @@ install-strip:
echo "INSTALL_PROGRAM_ENV=STRIPPROG='$(STRIP)'"` install
-#--------- Unit Tests ----------------------
-#TESTS = utest_common
-
-#check_PROGRAMS = utest_common
-
-#utest_common: utest_common.c
-
-#--------- End Unit Tests -------------------
-
diff --git a/src/config.h.in b/src/config.h.in
index 215b591..d406303 100644
--- a/src/config.h.in
+++ b/src/config.h.in
@@ -99,5 +99,5 @@
/* Define to empty if `const' does not conform to ANSI C. */
#undef const
-/* Define to `unsigned int' if <sys/types.h> does not define. */
+/* Define to `unsigned' if <sys/types.h> does not define. */
#undef size_t
diff --git a/src/llist.c b/src/llist.c
index 48d01fa..00a9762 100644
--- a/src/llist.c
+++ b/src/llist.c
@@ -1,123 +1,93 @@
-/* vi: set sw=4 ts=4: */
/*
- * linked list helper functions.
- *
- * Copyright (C) 2003 Glenn McGrath
- * Copyright (C) 2005 Vladimir Oleynik
- * Copyright (C) 2005 Bernhard Fischer
- * Copyright (C) 2006 Rob Landley <rob@landley.net>
- *
- * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
- *
- * haserl uses busybox's libbb functions where appropriate because
- * a (very long-term) goal is to get it to be a bb applet.
+ * Linked List functions based on lib busybox
*/
-#include "llist.h"
-
-/* Add data to the start of the linked list. */
-void
-llist_add_to (llist_t ** old_head, void *data)
-{
- llist_t *new_head = xmalloc (sizeof (llist_t));
+#include <stdlib.h>
- new_head->data = data;
- new_head->link = *old_head;
- *old_head = new_head;
-}
+/* #include "xmalloc.h" */
+#include "llist.h"
-/* Add data to the end of the linked list. */
-void
-llist_add_to_end (llist_t ** list_head, void *data)
+/*
+typedef struct llist_t {
+ char *data;
+ struct llist_t *link;
+} llist_t;
+*/
+
+void
+llist_add_to(llist_t ** old_head, void *data)
{
- llist_t *new_item = xmalloc (sizeof (llist_t));
-
- new_item->data = data;
- new_item->link = NULL;
+ llist_t *new = xmalloc(sizeof(llist_t));
- if (!*list_head)
- *list_head = new_item;
- else
- {
- llist_t *tail = *list_head;
+ new->data = data;
+ new->link = *old_head;
- while (tail->link)
- tail = tail->link;
- tail->link = new_item;
- }
+ *old_head = new;
}
-/* Remove first element from the list and return it */
-void *
-llist_pop (llist_t ** head)
-{
- void *data, *next;
-
- if (!*head)
- return NULL;
- data = (*head)->data;
- next = (*head)->link;
- free (*head);
- *head = next;
-
- return data;
+void
+llist_add_to_end(llist_t ** list_head, void *data)
+{
+ llist_t *new = xmalloc(sizeof(llist_t));
+ llist_t *end = *list_head;
+
+ new->data = data;
+ new->link = NULL;
+
+ if (*list_head == NULL) {
+ *list_head = new;
+ } else {
+ while (end->link) {
+ end = end->link;
+ }
+ end->link = new;
+ }
}
-/* Unlink arbitrary given element from the list */
-void
-llist_unlink (llist_t ** head, llist_t * elm)
+void *
+llist_pop(llist_t ** list_head)
{
- llist_t *crt;
-
- if (!(elm && *head))
- return;
-
- if (elm == *head)
- {
- *head = (*head)->link;
- return;
- }
-
- for (crt = *head; crt; crt = crt->link)
- {
- if (crt->link == elm)
- {
- crt->link = elm->link;
- return;
+ llist_t *new_head;
+ void *data;
+
+ if (*list_head == NULL) {
+ return NULL;
}
- }
+ data = (*list_head)->data;
+ new_head = (*list_head)->link;
+ free(*list_head);
+ *list_head = new_head;
+ return data;
}
-/* Recursively free all elements in the linked list. If freeit != NULL
- * call it on each datum in the list */
-void
-llist_free (llist_t * elm, void (*freeit) (void *data))
+void
+llist_unlink(llist_t ** head, llist_t * elm)
{
- while (elm)
- {
- void *data = llist_pop (&elm);
+ llist_t *this = *head;
- if (freeit)
- freeit (data);
- }
+ if (elm == *head) {
+ *head = (*head)->link;
+ return;
+ }
+ while (this->link) {
+ if (this == elm) {
+ this->link = elm->link;
+ return;
+ }
+ this = this->link;
+ }
}
-#ifdef UNUSED
-/* Reverse list order. */
-llist_t *
-llist_rev (llist_t * list)
+void
+llist_free(llist_t * elm, void (*freeit) (void *data))
{
- llist_t *rev = NULL;
- while (list)
- {
- llist_t *next = list->link;
-
- list->link = rev;
- rev = list;
- list = next;
- }
- return rev;
+ while (elm) {
+ if (freeit) {
+ freeit(llist_pop(&elm));
+ } else {
+ llist_pop(&elm);
+ }
+ }
}
-#endif
diff --git a/src/xmalloc.h b/src/xmalloc.h
index dd26560..fb38f8c 100644
--- a/src/xmalloc.h
+++ b/src/xmalloc.h
@@ -1,6 +1,7 @@
#ifndef XMALLOC_H
#define XMALLOC_H 1
+
void *xmalloc (size_t size);
void *xrealloc (void *buf, size_t size);