From c8bf38e5fb717d40635a2a89b22ed71b0de4266b Mon Sep 17 00:00:00 2001 From: Toni Uhlig Date: Tue, 1 Dec 2020 13:33:34 +0100 Subject: Squashed 'dependencies/uthash/' content from commit 8e67ced git-subtree-dir: dependencies/uthash git-subtree-split: 8e67ced1d1c5bd8141c542a22630e6de78aa6b90 --- tests/test26.c | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 tests/test26.c (limited to 'tests/test26.c') diff --git a/tests/test26.c b/tests/test26.c new file mode 100644 index 000000000..6e9d96214 --- /dev/null +++ b/tests/test26.c @@ -0,0 +1,61 @@ +#include +#include +#include +#include "utlist.h" + +#define BUFLEN 20 + +typedef struct el { + char bname[BUFLEN]; + struct el *next, *prev; +} el; + +static int namecmp(void *_a, void *_b) +{ + el *a = (el*)_a; + el *b = (el*)_b; + return strcmp(a->bname,b->bname); +} + +int main() +{ + el *name, *elt, *tmp, etmp; + el *head = NULL; /* important- initialize to NULL! */ + + char linebuf[BUFLEN]; + FILE *file; + + file = fopen( "test11.dat", "r" ); + if (file == NULL) { + perror("can't open: "); + exit(-1); + } + + while (fgets(linebuf,BUFLEN,file) != NULL) { + name = (el*)malloc(sizeof(el)); + if (name == NULL) { + exit(-1); + } + strcpy(name->bname, linebuf); + DL_APPEND(head, name); + } + DL_SORT(head, namecmp); + DL_FOREACH(head,elt) { + printf("%s", elt->bname); + } + + memcpy(etmp.bname, "WES\n", 5UL); + DL_SEARCH(head,elt,&etmp,namecmp); + if (elt != NULL) { + printf("found %s\n", elt->bname); + } + + /* now delete each element, use the safe iterator */ + DL_FOREACH_SAFE(head,elt,tmp) { + DL_DELETE(head,elt); + } + + fclose(file); + + return 0; +} -- cgit v1.2.3