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