summaryrefslogtreecommitdiff
path: root/src/lib/libc/stdlib/tfind.c
diff options
context:
space:
mode:
authorderaadt <>1997-06-13 23:41:37 +0000
committerderaadt <>1997-06-13 23:41:37 +0000
commit737b5724f4dd92147b79a987347fe5278fbfa6ac (patch)
treebccfd6d24168ffe9910ba192aaf5e1928cc5ac7f /src/lib/libc/stdlib/tfind.c
parentdd2713d24981403d92672e7e2709bd887b9ad7c6 (diff)
downloadopenbsd-737b5724f4dd92147b79a987347fe5278fbfa6ac.tar.gz
openbsd-737b5724f4dd92147b79a987347fe5278fbfa6ac.tar.bz2
openbsd-737b5724f4dd92147b79a987347fe5278fbfa6ac.zip
PD tsearch as reqd by xpg; by esr
Diffstat (limited to 'src/lib/libc/stdlib/tfind.c')
-rw-r--r--src/lib/libc/stdlib/tfind.c41
1 files changed, 41 insertions, 0 deletions
diff --git a/src/lib/libc/stdlib/tfind.c b/src/lib/libc/stdlib/tfind.c
new file mode 100644
index 0000000000..8e77527bf5
--- /dev/null
+++ b/src/lib/libc/stdlib/tfind.c
@@ -0,0 +1,41 @@
1/*
2 * Tree search generalized from Knuth (6.2.2) Algorithm T just like
3 * the AT&T man page says.
4 *
5 * The node_t structure is for internal use only, lint doesn't grok it.
6 *
7 * Written by reading the System V Interface Definition, not the code.
8 *
9 * Totally public domain.
10 */
11/*LINTLIBRARY*/
12#include <search.h>
13
14typedef struct node_t
15{
16 char *key;
17 struct node_t *llink, *rlink;
18} node;
19
20/* find a node, or return 0 */
21void *
22tfind(vkey, vrootp, compar)
23 const void *vkey; /* key to be found */
24 void **vrootp; /* address of the tree root */
25 int (*compar) __P((const void *, const void *));
26{
27 char *key = (char *)vkey;
28 node **rootp = (node **)vrootp;
29
30 if (rootp == (struct node_t **)0)
31 return ((struct node_t *)0);
32 while (*rootp != (struct node_t *)0) { /* T1: */
33 int r;
34 if ((r = (*compar)(key, (*rootp)->key)) == 0) /* T2: */
35 return (*rootp); /* key found */
36 rootp = (r < 0) ?
37 &(*rootp)->llink : /* T3: follow left branch */
38 &(*rootp)->rlink; /* T4: follow right branch */
39 }
40 return (node *)0;
41}