Commit | Line | Data |
463ee0b2 |
1 | /* |
2 | * sdbm - ndbm work-alike hashed database library |
3 | * based on Per-Ake Larson's Dynamic Hashing algorithms. BIT 18 (1978). |
4 | * author: oz@nexus.yorku.ca |
5 | * status: public domain. |
6 | */ |
7 | #define DBLKSIZ 4096 |
8 | #define PBLKSIZ 1024 |
9 | #define PAIRMAX 1008 /* arbitrary on PBLKSIZ-N */ |
10 | #define SPLTMAX 10 /* maximum allowed splits */ |
11 | /* for a single insertion */ |
12 | #define DIRFEXT ".dir" |
13 | #define PAGFEXT ".pag" |
14 | |
15 | typedef struct { |
16 | int dirf; /* directory file descriptor */ |
17 | int pagf; /* page file descriptor */ |
18 | int flags; /* status/error flags, see below */ |
19 | long maxbno; /* size of dirfile in bits */ |
20 | long curbit; /* current bit number */ |
21 | long hmask; /* current hash mask */ |
22 | long blkptr; /* current block for nextkey */ |
23 | int keyptr; /* current key for nextkey */ |
24 | long blkno; /* current page to read/write */ |
25 | long pagbno; /* current page in pagbuf */ |
26 | char pagbuf[PBLKSIZ]; /* page file block buffer */ |
27 | long dirbno; /* current block in dirbuf */ |
28 | char dirbuf[DBLKSIZ]; /* directory file block buffer */ |
29 | } DBM; |
30 | |
31 | #define DBM_RDONLY 0x1 /* data base open read-only */ |
32 | #define DBM_IOERR 0x2 /* data base I/O error */ |
33 | |
34 | /* |
35 | * utility macros |
36 | */ |
37 | #define sdbm_rdonly(db) ((db)->flags & DBM_RDONLY) |
38 | #define sdbm_error(db) ((db)->flags & DBM_IOERR) |
39 | |
40 | #define sdbm_clearerr(db) ((db)->flags &= ~DBM_IOERR) /* ouch */ |
41 | |
42 | #define sdbm_dirfno(db) ((db)->dirf) |
43 | #define sdbm_pagfno(db) ((db)->pagf) |
44 | |
45 | typedef struct { |
46 | char *dptr; |
47 | int dsize; |
48 | } datum; |
49 | |
50 | extern datum nullitem; |
51 | |
4e35701f |
52 | #if defined(__STDC__) || defined(__cplusplus) |
463ee0b2 |
53 | #define proto(p) p |
54 | #else |
55 | #define proto(p) () |
56 | #endif |
57 | |
58 | /* |
59 | * flags to sdbm_store |
60 | */ |
61 | #define DBM_INSERT 0 |
62 | #define DBM_REPLACE 1 |
63 | |
64 | /* |
65 | * ndbm interface |
66 | */ |
67 | extern DBM *sdbm_open proto((char *, int, int)); |
68 | extern void sdbm_close proto((DBM *)); |
69 | extern datum sdbm_fetch proto((DBM *, datum)); |
70 | extern int sdbm_delete proto((DBM *, datum)); |
71 | extern int sdbm_store proto((DBM *, datum, datum, int)); |
72 | extern datum sdbm_firstkey proto((DBM *)); |
73 | extern datum sdbm_nextkey proto((DBM *)); |
74 | |
75 | /* |
76 | * other |
77 | */ |
78 | extern DBM *sdbm_prep proto((char *, char *, int, int)); |
79 | extern long sdbm_hash proto((char *, int)); |
80 | |
81 | #ifndef SDBM_ONLY |
ff68c719 |
82 | #define dbm_open sdbm_open |
83 | #define dbm_close sdbm_close |
84 | #define dbm_fetch sdbm_fetch |
85 | #define dbm_store sdbm_store |
86 | #define dbm_delete sdbm_delete |
87 | #define dbm_firstkey sdbm_firstkey |
88 | #define dbm_nextkey sdbm_nextkey |
89 | #define dbm_error sdbm_error |
90 | #define dbm_clearerr sdbm_clearerr |
463ee0b2 |
91 | #endif |
85e6fe83 |
92 | |
93 | /* Most of the following is stolen from perl.h. */ |
94 | #ifndef H_PERL /* Include guard */ |
95 | |
96 | /* |
97 | * The following contortions are brought to you on behalf of all the |
98 | * standards, semi-standards, de facto standards, not-so-de-facto standards |
99 | * of the world, as well as all the other botches anyone ever thought of. |
100 | * The basic theory is that if we work hard enough here, the rest of the |
101 | * code can be a lot prettier. Well, so much for theory. Sorry, Henry... |
102 | */ |
103 | |
104 | #include <errno.h> |
105 | #ifdef HAS_SOCKET |
106 | # ifdef I_NET_ERRNO |
107 | # include <net/errno.h> |
108 | # endif |
109 | #endif |
110 | |
85e6fe83 |
111 | #if defined(__STDC__) || defined(_AIX) || defined(__stdc__) || defined(__cplusplus) |
112 | # define STANDARD_C 1 |
113 | #endif |
114 | |
85e6fe83 |
115 | #include <stdio.h> |
116 | #include <ctype.h> |
117 | #include <setjmp.h> |
118 | |
119 | #ifdef I_UNISTD |
120 | #include <unistd.h> |
121 | #endif |
122 | |
137443ea |
123 | #if !defined(MSDOS) && !defined(WIN32) |
85e6fe83 |
124 | # ifdef PARAM_NEEDS_TYPES |
125 | # include <sys/types.h> |
126 | # endif |
127 | # include <sys/param.h> |
128 | #endif |
129 | |
130 | #ifndef _TYPES_ /* If types.h defines this it's easy. */ |
131 | # ifndef major /* Does everyone's types.h define this? */ |
132 | # include <sys/types.h> |
133 | # endif |
134 | #endif |
135 | |
85e6fe83 |
136 | #include <sys/stat.h> |
137 | |
138 | #ifndef SEEK_SET |
139 | # ifdef L_SET |
140 | # define SEEK_SET L_SET |
141 | # else |
142 | # define SEEK_SET 0 /* Wild guess. */ |
143 | # endif |
144 | #endif |
145 | |
146 | /* Use all the "standard" definitions? */ |
94b6baf5 |
147 | #if defined(STANDARD_C) && defined(I_STDLIB) |
85e6fe83 |
148 | # include <stdlib.h> |
85e6fe83 |
149 | #endif /* STANDARD_C */ |
150 | |
a0d0e21e |
151 | #define MEM_SIZE Size_t |
152 | |
55497cff |
153 | /* This comes after <stdlib.h> so we don't try to change the standard |
154 | * library prototypes; we'll use our own instead. */ |
155 | |
156 | #if defined(MYMALLOC) && (defined(HIDEMYMALLOC) || defined(EMBEDMYMALLOC)) |
157 | |
158 | # ifdef HIDEMYMALLOC |
159 | # define malloc Mymalloc |
160 | # define calloc Mycalloc |
161 | # define realloc Myremalloc |
162 | # define free Myfree |
163 | # endif |
164 | # ifdef EMBEDMYMALLOC |
165 | # define malloc Perl_malloc |
166 | # define calloc Perl_calloc |
167 | # define realloc Perl_realloc |
168 | # define free Perl_free |
169 | # endif |
170 | |
1e7d9bb3 |
171 | Malloc_t malloc proto((MEM_SIZE nbytes)); |
172 | Malloc_t calloc proto((MEM_SIZE elements, MEM_SIZE size)); |
173 | Malloc_t realloc proto((Malloc_t where, MEM_SIZE nbytes)); |
174 | Free_t free proto((Malloc_t where)); |
55497cff |
175 | |
176 | #endif /* MYMALLOC && (HIDEMYMALLOC || EMBEDMYMALLOC) */ |
177 | |
a0d0e21e |
178 | #ifdef I_STRING |
179 | #include <string.h> |
180 | #else |
181 | #include <strings.h> |
182 | #endif |
183 | |
184 | #ifdef I_MEMORY |
185 | #include <memory.h> |
f0f333f4 |
186 | #endif |
187 | |
188 | #ifdef __cplusplus |
189 | #define HAS_MEMCPY |
a0d0e21e |
190 | #endif |
191 | |
85e6fe83 |
192 | #ifdef HAS_MEMCPY |
193 | # if !defined(STANDARD_C) && !defined(I_STRING) && !defined(I_MEMORY) |
194 | # ifndef memcpy |
1e7d9bb3 |
195 | extern char * memcpy proto((char*, char*, int)); |
85e6fe83 |
196 | # endif |
197 | # endif |
198 | #else |
199 | # ifndef memcpy |
200 | # ifdef HAS_BCOPY |
201 | # define memcpy(d,s,l) bcopy(s,d,l) |
202 | # else |
203 | # define memcpy(d,s,l) my_bcopy(s,d,l) |
204 | # endif |
205 | # endif |
206 | #endif /* HAS_MEMCPY */ |
207 | |
208 | #ifdef HAS_MEMSET |
209 | # if !defined(STANDARD_C) && !defined(I_STRING) && !defined(I_MEMORY) |
210 | # ifndef memset |
1e7d9bb3 |
211 | extern char *memset proto((char*, int, int)); |
85e6fe83 |
212 | # endif |
213 | # endif |
214 | # define memzero(d,l) memset(d,0,l) |
215 | #else |
216 | # ifndef memzero |
217 | # ifdef HAS_BZERO |
218 | # define memzero(d,l) bzero(d,l) |
219 | # else |
220 | # define memzero(d,l) my_bzero(d,l) |
221 | # endif |
222 | # endif |
223 | #endif /* HAS_MEMSET */ |
224 | |
36477c24 |
225 | #if defined(mips) && defined(ultrix) && !defined(__STDC__) |
226 | # undef HAS_MEMCMP |
227 | #endif |
228 | |
229 | #if defined(HAS_MEMCMP) && defined(HAS_SANE_MEMCMP) |
85e6fe83 |
230 | # if !defined(STANDARD_C) && !defined(I_STRING) && !defined(I_MEMORY) |
231 | # ifndef memcmp |
1e7d9bb3 |
232 | extern int memcmp proto((char*, char*, int)); |
85e6fe83 |
233 | # endif |
234 | # endif |
36477c24 |
235 | # ifdef BUGGY_MSC |
236 | # pragma function(memcmp) |
237 | # endif |
85e6fe83 |
238 | #else |
239 | # ifndef memcmp |
36477c24 |
240 | # /* maybe we should have included the full embedding header... */ |
241 | # ifdef NO_EMBED |
242 | # define memcmp my_memcmp |
243 | # else |
244 | # define memcmp Perl_my_memcmp |
245 | # endif |
f0f333f4 |
246 | #ifndef __cplusplus |
1e7d9bb3 |
247 | extern int memcmp proto((char*, char*, int)); |
f0f333f4 |
248 | #endif |
85e6fe83 |
249 | # endif |
250 | #endif /* HAS_MEMCMP */ |
251 | |
85e6fe83 |
252 | #ifndef HAS_BCMP |
253 | # ifndef bcmp |
254 | # define bcmp(s1,s2,l) memcmp(s1,s2,l) |
255 | # endif |
36477c24 |
256 | #endif /* !HAS_BCMP */ |
257 | |
258 | #ifdef HAS_MEMCMP |
259 | # define memNE(s1,s2,l) (memcmp(s1,s2,l)) |
260 | # define memEQ(s1,s2,l) (!memcmp(s1,s2,l)) |
261 | #else |
262 | # define memNE(s1,s2,l) (bcmp(s1,s2,l)) |
263 | # define memEQ(s1,s2,l) (!bcmp(s1,s2,l)) |
264 | #endif |
85e6fe83 |
265 | |
266 | #ifdef I_NETINET_IN |
267 | # include <netinet/in.h> |
268 | #endif |
269 | |
270 | #endif /* Include guard */ |
4e35701f |
271 | |