summaryrefslogtreecommitdiff
path: root/libc/misc/search
diff options
context:
space:
mode:
authorEric Andersen <andersen@codepoet.org>2003-09-06 00:32:05 +0000
committerEric Andersen <andersen@codepoet.org>2003-09-06 00:32:05 +0000
commita1380a837e87b38c06256fac373b035478a7b4a2 (patch)
treeb4680a6eaf6ed6123f024cbe80af51b8a63b0de6 /libc/misc/search
parent41130f0233dbc9da839810b103540cd792f0d912 (diff)
Fix the bugs I stupidly added
Diffstat (limited to 'libc/misc/search')
-rw-r--r--libc/misc/search/Makefile2
-rw-r--r--libc/misc/search/tsearch.c27
2 files changed, 28 insertions, 1 deletions
diff --git a/libc/misc/search/Makefile b/libc/misc/search/Makefile
index 77175aca1..fa28f21d6 100644
--- a/libc/misc/search/Makefile
+++ b/libc/misc/search/Makefile
@@ -25,7 +25,7 @@ TOPDIR=../../../
include $(TOPDIR)Rules.mak
MSRC1=tsearch.c
-MOBJ1=tsearch.o tfind.o tdelete.o twalk.o
+MOBJ1=tsearch.o tfind.o tdelete.o twalk.o tdestroy.o
MSRC2=lsearch.c
MOBJ2=lfind.o lsearch.o
diff --git a/libc/misc/search/tsearch.c b/libc/misc/search/tsearch.c
index b584e2f15..72abcee7b 100644
--- a/libc/misc/search/tsearch.c
+++ b/libc/misc/search/tsearch.c
@@ -28,6 +28,7 @@ Cambridge, MA 02139, USA. */
*/
/*LINTLIBRARY*/
+#define _GNU_SOURCE
#include <search.h>
#include <stdlib.h>
@@ -187,4 +188,30 @@ void twalk(__const void *vroot, __action_fn_t action)
}
#endif
+#ifdef L_tdestroy
+/* The standardized functions miss an important functionality: the
+ tree cannot be removed easily. We provide a function to do this. */
+static void
+internal_function
+tdestroy_recurse (node *root, __free_fn_t freefct)
+{
+ if (root->left != NULL)
+ tdestroy_recurse (root->left, freefct);
+ if (root->right != NULL)
+ tdestroy_recurse (root->right, freefct);
+ (*freefct) ((void *) root->key);
+ /* Free the node itself. */
+ free (root);
+}
+
+void tdestroy (void *vroot, __free_fn_t freefct)
+{
+ node *root = (node *) vroot;
+ if (root != NULL) {
+ tdestroy_recurse (root, freefct);
+ }
+}
+#endif
+
/* tsearch.c ends here */
+