aboutsummaryrefslogtreecommitdiff
path: root/src/daemon
diff options
context:
space:
mode:
Diffstat (limited to 'src/daemon')
-rw-r--r--src/daemon/Makefile.am5
-rw-r--r--src/daemon/daemon.c6
-rw-r--r--src/daemon/tsearch.c123
-rw-r--r--src/daemon/tsearch.h39
4 files changed, 173 insertions, 0 deletions
diff --git a/src/daemon/Makefile.am b/src/daemon/Makefile.am
index d1ac855c..ca20a58f 100644
--- a/src/daemon/Makefile.am
+++ b/src/daemon/Makefile.am
@@ -29,6 +29,11 @@ if USE_COVERAGE
29 AM_CFLAGS = --coverage 29 AM_CFLAGS = --coverage
30endif 30endif
31 31
32if !HAVE_TSEARCH
33libmicrohttpd_la_SOURCES += \
34 tsearch.c tsearch.h
35endif
36
32if HAVE_POSTPROCESSOR 37if HAVE_POSTPROCESSOR
33libmicrohttpd_la_SOURCES += \ 38libmicrohttpd_la_SOURCES += \
34 postprocessor.c 39 postprocessor.c
diff --git a/src/daemon/daemon.c b/src/daemon/daemon.c
index 0668245f..52e47ca3 100644
--- a/src/daemon/daemon.c
+++ b/src/daemon/daemon.c
@@ -31,6 +31,12 @@
31#include "memorypool.h" 31#include "memorypool.h"
32#include <limits.h> 32#include <limits.h>
33 33
34#if HAVE_SEARCH_H
35#include <search.h>
36#else
37#include "tsearch.h"
38#endif
39
34#if HTTPS_SUPPORT 40#if HTTPS_SUPPORT
35#include "connection_https.h" 41#include "connection_https.h"
36#include <gnutls/gnutls.h> 42#include <gnutls/gnutls.h>
diff --git a/src/daemon/tsearch.c b/src/daemon/tsearch.c
new file mode 100644
index 00000000..2ab9a467
--- /dev/null
+++ b/src/daemon/tsearch.c
@@ -0,0 +1,123 @@
1/* $NetBSD: tsearch.c,v 1.3 1999/09/16 11:45:37 lukem Exp $ */
2
3/*
4 * Tree search generalized from Knuth (6.2.2) Algorithm T just like
5 * the AT&T man page says.
6 *
7 * The node_t structure is for internal use only, lint doesn't grok it.
8 *
9 * Written by reading the System V Interface Definition, not the code.
10 *
11 * Totally public domain.
12 */
13
14#include <sys/cdefs.h>
15#define _SEARCH_PRIVATE
16#include <tsearch.h>
17#include <stdlib.h>
18
19/* find or insert datum into search tree */
20void *
21tsearch(vkey, vrootp, compar)
22 const void *vkey; /* key to be located */
23 void **vrootp; /* address of tree root */
24 int (*compar)(const void *, const void *);
25{
26 node_t *q;
27 node_t **rootp = (node_t **)vrootp;
28
29 if (rootp == NULL)
30 return NULL;
31
32 while (*rootp != NULL) { /* Knuth's T1: */
33 int r;
34
35 if ((r = (*compar)(vkey, (*rootp)->key)) == 0) /* T2: */
36 return *rootp; /* we found it! */
37
38 rootp = (r < 0) ?
39 &(*rootp)->llink : /* T3: follow left branch */
40 &(*rootp)->rlink; /* T4: follow right branch */
41 }
42
43 q = malloc(sizeof(node_t)); /* T5: key not found */
44 if (q != 0) { /* make new node */
45 *rootp = q; /* link new node to old */
46 /* LINTED const castaway ok */
47 q->key = (void *)vkey; /* initialize new node */
48 q->llink = q->rlink = NULL;
49 }
50 return q;
51}
52
53/* find a node, or return 0 */
54void *
55tfind(vkey, vrootp, compar)
56 const void *vkey; /* key to be found */
57 void * const *vrootp; /* address of the tree root */
58 int (*compar)(const void *, const void *);
59{
60 node_t **rootp = (node_t **)vrootp;
61
62 if (rootp == NULL)
63 return NULL;
64
65 while (*rootp != NULL) { /* T1: */
66 int r;
67
68 if ((r = (*compar)(vkey, (*rootp)->key)) == 0) /* T2: */
69 return *rootp; /* key found */
70 rootp = (r < 0) ?
71 &(*rootp)->llink : /* T3: follow left branch */
72 &(*rootp)->rlink; /* T4: follow right branch */
73 }
74 return NULL;
75}
76
77/*
78 * delete node with given key
79 *
80 * vkey: key to be deleted
81 * vrootp: address of the root of the tree
82 * compar: function to carry out node comparisons
83 */
84void *
85tdelete(const void * __restrict vkey, void ** __restrict vrootp,
86 int (*compar)(const void *, const void *))
87{
88 node_t **rootp = (node_t **)vrootp;
89 node_t *p, *q, *r;
90 int cmp;
91
92 if (rootp == NULL || (p = *rootp) == NULL)
93 return NULL;
94
95 while ((cmp = (*compar)(vkey, (*rootp)->key)) != 0) {
96 p = *rootp;
97 rootp = (cmp < 0) ?
98 &(*rootp)->llink : /* follow llink branch */
99 &(*rootp)->rlink; /* follow rlink branch */
100 if (*rootp == NULL)
101 return NULL; /* key not found */
102 }
103 r = (*rootp)->rlink; /* D1: */
104 if ((q = (*rootp)->llink) == NULL) /* Left NULL? */
105 q = r;
106 else if (r != NULL) { /* Right link is NULL? */
107 if (r->llink == NULL) { /* D2: Find successor */
108 r->llink = q;
109 q = r;
110 } else { /* D3: Find NULL link */
111 for (q = r->llink; q->llink != NULL; q = r->llink)
112 r = q;
113 r->llink = q->rlink;
114 q->llink = (*rootp)->llink;
115 q->rlink = (*rootp)->rlink;
116 }
117 }
118 free(*rootp); /* D4: Free node */
119 *rootp = q; /* link parent to new node */
120 return p;
121}
122
123/* end of tsearch.c */
diff --git a/src/daemon/tsearch.h b/src/daemon/tsearch.h
new file mode 100644
index 00000000..824f9a2d
--- /dev/null
+++ b/src/daemon/tsearch.h
@@ -0,0 +1,39 @@
1/*-
2 * Written by J.T. Conklin <jtc@netbsd.org>
3 * Public domain.
4 *
5 * $NetBSD: search.h,v 1.12 1999/02/22 10:34:28 christos Exp $
6 * $FreeBSD: release/9.0.0/include/search.h 105250 2002-10-16 14:29:23Z robert $
7 */
8
9#ifndef _SEARCH_H_
10#define _SEARCH_H_
11
12#include <sys/cdefs.h>
13#include <sys/types.h>
14
15typedef enum {
16 preorder,
17 postorder,
18 endorder,
19 leaf
20} VISIT;
21
22#ifdef _SEARCH_PRIVATE
23typedef struct node {
24 char *key;
25 struct node *llink, *rlink;
26} node_t;
27#endif
28
29__BEGIN_DECLS
30void *tdelete(const void * __restrict, void ** __restrict,
31 int (*)(const void *, const void *));
32void *tfind(const void *, void * const *,
33 int (*)(const void *, const void *));
34void *tsearch(const void *, void **, int (*)(const void *, const void *));
35void twalk(const void *, void (*)(const void *, VISIT, int));
36void tdestroy(void *, void (*)(void *));
37__END_DECLS
38
39#endif /* !_SEARCH_H_ */