home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume26 / db-1.6 / part03 < prev    next >
Encoding:
Text File  |  1993-07-05  |  90.8 KB  |  3,063 lines

  1. Newsgroups: comp.sources.unix
  2. From: bostic@cs.berkeley.edu (Keith Bostic)
  3. Subject: v26i282: db-1.6 - A New Hashing Package for UNIX(tm) (updates dbm/ndbm), Part03/09
  4. Sender: unix-sources-moderator@gw.home.vix.com
  5. Approved: vixie@gw.home.vix.com
  6.  
  7. Submitted-By: bostic@cs.berkeley.edu (Keith Bostic)
  8. Posting-Number: Volume 26, Issue 282
  9. Archive-Name: db-1.6/part03
  10.  
  11. #! /bin/sh
  12. # This is a shell archive.  Remove anything before this line, then unpack
  13. # it by saving it into a file and typing "sh file".  To overwrite existing
  14. # files, type "sh file -c".  You can also feed this as standard input via
  15. # unshar, or by typing "sh <file", e.g..  If this archive is complete, you
  16. # will see the following message at the end:
  17. #        "End of archive 3 (of 9)."
  18. # Contents:  PORT/include/compat.h PORT/include/db.h PORT/sys/compat.h
  19. #   PORT/sys/db.h btree/bt_get.c btree/bt_overflow.c btree/bt_utils.c
  20. #   man/btree.3 man/mpool.3 man/recno.3 recno/rec_get.c
  21. #   recno/rec_open.c recno/rec_put.c
  22. # Wrapped by vixie@gw.home.vix.com on Mon Jul  5 15:27:22 1993
  23. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  24. if test -f 'PORT/include/compat.h' -a "${1}" != "-c" ; then 
  25.   echo shar: Will not clobber existing file \"'PORT/include/compat.h'\"
  26. else
  27. echo shar: Extracting \"'PORT/include/compat.h'\" \(6332 characters\)
  28. sed "s/^X//" >'PORT/include/compat.h' <<'END_OF_FILE'
  29. X/*-
  30. X * Copyright (c) 1991, 1993
  31. X *    The Regents of the University of California.  All rights reserved.
  32. X *
  33. X * Redistribution and use in source and binary forms, with or without
  34. X * modification, are permitted provided that the following conditions
  35. X * are met:
  36. X * 1. Redistributions of source code must retain the above copyright
  37. X *    notice, this list of conditions and the following disclaimer.
  38. X * 2. Redistributions in binary form must reproduce the above copyright
  39. X *    notice, this list of conditions and the following disclaimer in the
  40. X *    documentation and/or other materials provided with the distribution.
  41. X * 3. All advertising materials mentioning features or use of this software
  42. X *    must display the following acknowledgement:
  43. X *    This product includes software developed by the University of
  44. X *    California, Berkeley and its contributors.
  45. X * 4. Neither the name of the University nor the names of its contributors
  46. X *    may be used to endorse or promote products derived from this software
  47. X *    without specific prior written permission.
  48. X *
  49. X * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  50. X * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  51. X * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  52. X * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  53. X * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  54. X * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  55. X * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  56. X * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  57. X * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  58. X * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  59. X * SUCH DAMAGE.
  60. X *
  61. X *    @(#)compat.h    8.1 (Berkeley) 6/2/93
  62. X */
  63. X
  64. X#ifndef    _COMPAT_H_
  65. X#define    _COMPAT_H_
  66. X
  67. X/*
  68. X * If your system doesn't typedef u_long, u_short, or u_char, change
  69. X * the 0 to a 1.
  70. X */
  71. X#if 0
  72. Xtypedef unsigned long    u_long;
  73. Xtypedef unsigned short    u_short;
  74. Xtypedef unsigned char    u_char;
  75. X#endif
  76. X
  77. X/* If your system doesn't typedef size_t, change the 0 to a 1. */
  78. X#if 0
  79. Xtypedef unsigned int    size_t;
  80. X#define    SIZE_T_MAX    UINT_MAX
  81. X#endif
  82. X
  83. X/*
  84. X * If your system doesn't specify a max size for a SIZE_T, check
  85. X * to make sure this is the right one.
  86. X */
  87. X#ifndef SIZE_T_MAX
  88. X#define    SIZE_T_MAX    UINT_MAX
  89. X#endif
  90. X
  91. X/*
  92. X * If your system doesn't have the POSIX type for a signal mask,
  93. X * change the 0 to a 1.
  94. X */
  95. X#if 0
  96. Xtypedef unsigned int    sigset_t;
  97. X#endif
  98. X
  99. X/*
  100. X * If your system's vsprintf returns a char *, not an int,
  101. X * change the 0 to a 1.
  102. X */
  103. X#if 0
  104. X#define    VSPRINTF_CHARSTAR
  105. X#endif
  106. X
  107. X/*
  108. X * If you don't have POSIX 1003.1 signals, the signal code surrounding the 
  109. X * temporary file creation is intended to block all of the possible signals
  110. X * long enough to create the file and unlink it.  All of this stuff is
  111. X * intended to use old-style BSD calls to fake POSIX 1003.1 calls.
  112. X */
  113. X#ifdef NO_POSIX_SIGNALS
  114. X#define sigemptyset(set)    (*(set) = 0)
  115. X#define sigfillset(set)        (*(set) = ~(sigset_t)0, 0)
  116. X#define sigaddset(set,signo)    (*(set) |= sigmask(signo), 0)
  117. X#define sigdelset(set,signo)    (*(set) &= ~sigmask(signo), 0)
  118. X#define sigismember(set,signo)    ((*(set) & sigmask(signo)) != 0)
  119. X
  120. X#define SIG_BLOCK    1
  121. X#define SIG_UNBLOCK    2
  122. X#define SIG_SETMASK    3
  123. X
  124. Xstatic int __sigtemp;        /* For the use of sigprocmask */
  125. X
  126. X#define sigprocmask(how,set,oset) \
  127. X    ((__sigtemp = (((how) == SIG_BLOCK) ? \
  128. X    sigblock(0) | *(set) : (((how) == SIG_UNBLOCK) ? \
  129. X    sigblock(0) & ~(*(set)) : ((how) == SIG_SETMASK ? \
  130. X    *(set) : sigblock(0))))), ((oset) ? \
  131. X    (*(oset) = sigsetmask(__sigtemp)) : sigsetmask(__sigtemp)), 0)
  132. X#endif
  133. X
  134. X/*
  135. X * If realloc(3) of a NULL pointer on your system isn't the same as
  136. X * a malloc(3) call, change the 0 to a 1, and add realloc.o to the
  137. X * MISC line in your Makefile.
  138. X */
  139. X#if 0
  140. X#define    realloc    __fix_realloc
  141. X#endif
  142. X
  143. X/*
  144. X * If your system doesn't have an include file with the appropriate
  145. X * byte order set, make sure you specify the correct one.
  146. X */
  147. X#ifndef BYTE_ORDER
  148. X#define LITTLE_ENDIAN    1234        /* LSB first: i386, vax */
  149. X#define BIG_ENDIAN    4321        /* MSB first: 68000, ibm, net */
  150. X#define BYTE_ORDER    BIG_ENDIAN    /* Set for your system. */
  151. X#endif
  152. X
  153. X#if defined(SYSV) || defined(SYSTEM5)
  154. X#define    index(a, b)        strchr(a, b)
  155. X#define    rindex(a, b)        strrchr(a, b)
  156. X#define    bzero(a, b)        memset(a, 0, b)
  157. X#define    bcmp(a, b, n)        memcmp(a, b, n)
  158. X#define    bcopy(a, b, n)        memmove(b, a, n)
  159. X#endif
  160. X
  161. X#if defined(BSD) || defined(BSD4_3)
  162. X#define    strchr(a, b)        index(a, b)
  163. X#define    strrchr(a, b)        rindex(a, b)
  164. X#define    memcmp(a, b, n)        bcmp(a, b, n)
  165. X#define    memmove(a, b, n)    bcopy(b, a, n)
  166. X#endif
  167. X
  168. X/*
  169. X * 32-bit machine.  The db routines are theoretically independent of
  170. X * the size of u_shorts and u_longs, but I don't know that anyone has
  171. X * ever actually tried it.  At a minimum, change the following #define's
  172. X * if you are trying to compile on a different type of system.
  173. X */
  174. X#ifndef USHRT_MAX
  175. X#define    USHRT_MAX        0xFFFF
  176. X#define    ULONG_MAX        0xFFFFFFFF
  177. X#endif
  178. X
  179. X/* POSIX 1003.1 access mode mask. */
  180. X#ifndef O_ACCMODE
  181. X#define    O_ACCMODE    (O_RDONLY|O_WRONLY|O_RDWR)
  182. X#endif
  183. X
  184. X/* POSIX 1003.2 RE limit. */
  185. X#ifndef    _POSIX2_RE_DUP_MAX
  186. X#define    _POSIX2_RE_DUP_MAX    255
  187. X#endif
  188. X
  189. X/*
  190. X * If you can't provide lock values in the open(2) call.  Note, this
  191. X * allows races to happen.
  192. X */
  193. X#ifndef O_EXLOCK
  194. X#define    O_EXLOCK    0
  195. X#endif
  196. X
  197. X#ifndef O_SHLOCK
  198. X#define    O_SHLOCK    0
  199. X#endif
  200. X
  201. X#ifndef EFTYPE
  202. X#define    EFTYPE        EINVAL        /* POSIX 1003.1 format errno. */
  203. X#endif
  204. X
  205. X#ifndef    WCOREDUMP            /* 4.4BSD extension */
  206. X#define    WCOREDUMP(a)    0
  207. X#endif
  208. X
  209. X#ifndef    STDERR_FILENO
  210. X#define    STDIN_FILENO    0        /* ANSI C #defines */
  211. X#define    STDOUT_FILENO    1
  212. X#define    STDERR_FILENO    2
  213. X#endif
  214. X
  215. X#ifndef SEEK_END
  216. X#define    SEEK_SET    0        /* POSIX 1003.1 seek values */
  217. X#define    SEEK_CUR    1
  218. X#define    SEEK_END    2
  219. X#endif
  220. X
  221. X#ifndef S_ISLNK                /* BSD POSIX 1003.1 extensions */
  222. X#define S_ISLNK(m)      ((m & 0170000) == 0120000)
  223. X#define S_ISSOCK(m)     ((m & 0170000) == 0140000)
  224. X#endif
  225. X
  226. X#ifndef _POSIX2_RE_DUP_MAX
  227. X#define    _POSIX2_RE_DUP_MAX    255
  228. X#endif
  229. X
  230. X#ifndef NULL                /* ANSI C #defines NULL everywhere. */
  231. X#define    NULL        0
  232. X#endif
  233. X
  234. X#ifndef    MAX
  235. X#define    MAX(_a,_b)    ((_a)<(_b)?(_b):(_a))
  236. X#endif
  237. X#ifndef    MIN
  238. X#define    MIN(_a,_b)    ((_a)<(_b)?(_a):(_b))
  239. X#endif
  240. X
  241. X#ifndef _BSD_VA_LIST_
  242. X#define    _BSD_VA_LIST_    char *
  243. X#endif
  244. X
  245. X#endif /* !_COMPAT_H_ */
  246. END_OF_FILE
  247. if test 6332 -ne `wc -c <'PORT/include/compat.h'`; then
  248.     echo shar: \"'PORT/include/compat.h'\" unpacked with wrong size!
  249. fi
  250. # end of 'PORT/include/compat.h'
  251. fi
  252. if test -f 'PORT/include/db.h' -a "${1}" != "-c" ; then 
  253.   echo shar: Will not clobber existing file \"'PORT/include/db.h'\"
  254. else
  255. echo shar: Extracting \"'PORT/include/db.h'\" \(6767 characters\)
  256. sed "s/^X//" >'PORT/include/db.h' <<'END_OF_FILE'
  257. X/*-
  258. X * Copyright (c) 1990, 1993
  259. X *    The Regents of the University of California.  All rights reserved.
  260. X *
  261. X * Redistribution and use in source and binary forms, with or without
  262. X * modification, are permitted provided that the following conditions
  263. X * are met:
  264. X * 1. Redistributions of source code must retain the above copyright
  265. X *    notice, this list of conditions and the following disclaimer.
  266. X * 2. Redistributions in binary form must reproduce the above copyright
  267. X *    notice, this list of conditions and the following disclaimer in the
  268. X *    documentation and/or other materials provided with the distribution.
  269. X * 3. All advertising materials mentioning features or use of this software
  270. X *    must display the following acknowledgement:
  271. X *    This product includes software developed by the University of
  272. X *    California, Berkeley and its contributors.
  273. X * 4. Neither the name of the University nor the names of its contributors
  274. X *    may be used to endorse or promote products derived from this software
  275. X *    without specific prior written permission.
  276. X *
  277. X * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  278. X * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  279. X * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  280. X * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  281. X * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  282. X * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  283. X * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  284. X * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  285. X * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  286. X * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  287. X * SUCH DAMAGE.
  288. X *
  289. X *    @(#)db.h    8.1 (Berkeley) 6/2/93
  290. X */
  291. X
  292. X#ifndef _DB_H_
  293. X#define    _DB_H_
  294. X
  295. X#include <sys/types.h>
  296. X#include <sys/cdefs.h>
  297. X
  298. X#include "compat.h"
  299. X
  300. X#define    RET_ERROR    -1        /* Return values. */
  301. X#define    RET_SUCCESS     0
  302. X#define    RET_SPECIAL     1
  303. X
  304. X#define    MAX_PAGE_NUMBER    ULONG_MAX    /* >= # of pages in a file */
  305. Xtypedef u_long    pgno_t;
  306. X#define    MAX_PAGE_OFFSET    USHRT_MAX    /* >= # of bytes in a page */
  307. Xtypedef u_short    indx_t;
  308. X#define    MAX_REC_NUMBER    ULONG_MAX    /* >= # of records in a tree */
  309. Xtypedef u_long    recno_t;
  310. X
  311. X/* Key/data structure -- a Data-Base Thang. */
  312. Xtypedef struct {
  313. X    void    *data;            /* data */
  314. X    size_t     size;            /* data length */
  315. X} DBT;
  316. X
  317. X/* Routine flags. */
  318. X#define    R_CURSOR    1        /* del, put, seq */
  319. X#define    __R_UNUSED    2        /* UNUSED */
  320. X#define    R_FIRST        3        /* seq */
  321. X#define    R_IAFTER    4        /* put (RECNO) */
  322. X#define    R_IBEFORE    5        /* put (RECNO) */
  323. X#define    R_LAST        6        /* seq (BTREE, RECNO) */
  324. X#define    R_NEXT        7        /* seq */
  325. X#define    R_NOOVERWRITE    8        /* put */
  326. X#define    R_PREV        9        /* seq (BTREE, RECNO) */
  327. X#define    R_SETCURSOR    10        /* put (RECNO) */
  328. X#define    R_RECNOSYNC    11        /* sync (RECNO) */
  329. X
  330. Xtypedef enum { DB_BTREE, DB_HASH, DB_RECNO } DBTYPE;
  331. X
  332. X#define    __USE_OPEN_FLAGS \
  333. X    (O_CREAT|O_EXCL|O_EXLOCK|O_RDONLY|O_RDWR|O_SHLOCK|O_TRUNC)
  334. X
  335. X/* Access method description structure. */
  336. Xtypedef struct __db {
  337. X    DBTYPE type;            /* underlying db type */
  338. X    int (*close)    __P((struct __db *));
  339. X    int (*del)    __P((const struct __db *, const DBT *, u_int));
  340. X    int (*fd)    __P((const struct __db *));
  341. X    int (*get)    __P((const struct __db *, const DBT *, DBT *, u_int));
  342. X    int (*put)    __P((const struct __db *, DBT *, const DBT *, u_int));
  343. X    int (*seq)    __P((const struct __db *, DBT *, DBT *, u_int));
  344. X    int (*sync)    __P((const struct __db *, u_int));
  345. X    void *internal;            /* access method private */
  346. X} DB;
  347. X
  348. X#define    BTREEMAGIC    0x053162
  349. X#define    BTREEVERSION    3
  350. X
  351. X/* Structure used to pass parameters to the btree routines. */
  352. Xtypedef struct {
  353. X#define    R_DUP        0x01    /* duplicate keys */
  354. X    u_long     flags;
  355. X    int     cachesize;    /* bytes to cache */
  356. X    int     maxkeypage;    /* maximum keys per page */
  357. X    int     minkeypage;    /* minimum keys per page */
  358. X    int     psize;        /* page size */
  359. X                /* comparison, prefix functions */
  360. X    int     (*compare)    __P((const DBT *, const DBT *));
  361. X    int     (*prefix)    __P((const DBT *, const DBT *));
  362. X    int     lorder;    /* byte order */
  363. X} BTREEINFO;
  364. X
  365. X#define    HASHMAGIC    0x061561
  366. X#define    HASHVERSION    2
  367. X
  368. X/* Structure used to pass parameters to the hashing routines. */
  369. Xtypedef struct {
  370. X    int     bsize;        /* bucket size */
  371. X    int     ffactor;    /* fill factor */
  372. X    int     nelem;        /* number of elements */
  373. X    int     cachesize;    /* bytes to cache */
  374. X                /* hash function */
  375. X    int     (*hash) __P((const void *, size_t));
  376. X    int     lorder;    /* byte order */
  377. X} HASHINFO;
  378. X
  379. X/* Structure used to pass parameters to the record routines. */
  380. Xtypedef struct {
  381. X#define    R_FIXEDLEN    0x01    /* fixed-length records */
  382. X#define    R_NOKEY        0x02    /* key not required */
  383. X#define    R_SNAPSHOT    0x04    /* snapshot the input */
  384. X    u_long     flags;
  385. X    int     cachesize;    /* bytes to cache */
  386. X    int     psize;        /* page size */
  387. X    int     lorder;    /* byte order */
  388. X    size_t     reclen;    /* record length (fixed-length records) */
  389. X    u_char     bval;        /* delimiting byte (variable-length records */
  390. X    char    *bfname;    /* btree file name */ 
  391. X} RECNOINFO;
  392. X
  393. X/*
  394. X * Little endian <==> big endian long swap macros.
  395. X *    BLSWAP        swap a memory location
  396. X *    BLPSWAP        swap a referenced memory location
  397. X *    BLSWAP_COPY    swap from one location to another
  398. X */
  399. X#define BLSWAP(a) { \
  400. X    u_long _tmp = a; \
  401. X    ((char *)&a)[0] = ((char *)&_tmp)[3]; \
  402. X    ((char *)&a)[1] = ((char *)&_tmp)[2]; \
  403. X    ((char *)&a)[2] = ((char *)&_tmp)[1]; \
  404. X    ((char *)&a)[3] = ((char *)&_tmp)[0]; \
  405. X}
  406. X#define    BLPSWAP(a) { \
  407. X    u_long _tmp = *(u_long *)a; \
  408. X    ((char *)a)[0] = ((char *)&_tmp)[3]; \
  409. X    ((char *)a)[1] = ((char *)&_tmp)[2]; \
  410. X    ((char *)a)[2] = ((char *)&_tmp)[1]; \
  411. X    ((char *)a)[3] = ((char *)&_tmp)[0]; \
  412. X}
  413. X#define    BLSWAP_COPY(a, b) { \
  414. X    ((char *)&(b))[0] = ((char *)&(a))[3]; \
  415. X    ((char *)&(b))[1] = ((char *)&(a))[2]; \
  416. X    ((char *)&(b))[2] = ((char *)&(a))[1]; \
  417. X    ((char *)&(b))[3] = ((char *)&(a))[0]; \
  418. X}
  419. X
  420. X/*
  421. X * Little endian <==> big endian short swap macros.
  422. X *    BSSWAP        swap a memory location
  423. X *    BSPSWAP        swap a referenced memory location
  424. X *    BSSWAP_COPY    swap from one location to another
  425. X */
  426. X#define BSSWAP(a) { \
  427. X    u_short _tmp = a; \
  428. X    ((char *)&a)[0] = ((char *)&_tmp)[1]; \
  429. X    ((char *)&a)[1] = ((char *)&_tmp)[0]; \
  430. X}
  431. X#define BSPSWAP(a) { \
  432. X    u_short _tmp = *(u_short *)a; \
  433. X    ((char *)a)[0] = ((char *)&_tmp)[1]; \
  434. X    ((char *)a)[1] = ((char *)&_tmp)[0]; \
  435. X}
  436. X#define BSSWAP_COPY(a, b) { \
  437. X    ((char *)&(b))[0] = ((char *)&(a))[1]; \
  438. X    ((char *)&(b))[1] = ((char *)&(a))[0]; \
  439. X}
  440. X
  441. X__BEGIN_DECLS
  442. XDB *dbopen __P((const char *, int, int, DBTYPE, const void *));
  443. X
  444. X#ifdef __DBINTERFACE_PRIVATE
  445. XDB    *__bt_open __P((const char *, int, int, const BTREEINFO *));
  446. XDB    *__hash_open __P((const char *, int, int, const HASHINFO *));
  447. XDB    *__rec_open __P((const char *, int, int, const RECNOINFO *));
  448. Xvoid     __dbpanic __P((DB *dbp));
  449. X#endif
  450. X__END_DECLS
  451. X#endif /* !_DB_H_ */
  452. END_OF_FILE
  453. if test 6767 -ne `wc -c <'PORT/include/db.h'`; then
  454.     echo shar: \"'PORT/include/db.h'\" unpacked with wrong size!
  455. fi
  456. # end of 'PORT/include/db.h'
  457. fi
  458. if test -f 'PORT/sys/compat.h' -a "${1}" != "-c" ; then 
  459.   echo shar: Will not clobber existing file \"'PORT/sys/compat.h'\"
  460. else
  461. echo shar: Extracting \"'PORT/sys/compat.h'\" \(6332 characters\)
  462. sed "s/^X//" >'PORT/sys/compat.h' <<'END_OF_FILE'
  463. X/*-
  464. X * Copyright (c) 1991, 1993
  465. X *    The Regents of the University of California.  All rights reserved.
  466. X *
  467. X * Redistribution and use in source and binary forms, with or without
  468. X * modification, are permitted provided that the following conditions
  469. X * are met:
  470. X * 1. Redistributions of source code must retain the above copyright
  471. X *    notice, this list of conditions and the following disclaimer.
  472. X * 2. Redistributions in binary form must reproduce the above copyright
  473. X *    notice, this list of conditions and the following disclaimer in the
  474. X *    documentation and/or other materials provided with the distribution.
  475. X * 3. All advertising materials mentioning features or use of this software
  476. X *    must display the following acknowledgement:
  477. X *    This product includes software developed by the University of
  478. X *    California, Berkeley and its contributors.
  479. X * 4. Neither the name of the University nor the names of its contributors
  480. X *    may be used to endorse or promote products derived from this software
  481. X *    without specific prior written permission.
  482. X *
  483. X * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  484. X * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  485. X * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  486. X * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  487. X * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  488. X * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  489. X * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  490. X * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  491. X * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  492. X * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  493. X * SUCH DAMAGE.
  494. X *
  495. X *    @(#)compat.h    8.1 (Berkeley) 6/2/93
  496. X */
  497. X
  498. X#ifndef    _COMPAT_H_
  499. X#define    _COMPAT_H_
  500. X
  501. X/*
  502. X * If your system doesn't typedef u_long, u_short, or u_char, change
  503. X * the 0 to a 1.
  504. X */
  505. X#if 0
  506. Xtypedef unsigned long    u_long;
  507. Xtypedef unsigned short    u_short;
  508. Xtypedef unsigned char    u_char;
  509. X#endif
  510. X
  511. X/* If your system doesn't typedef size_t, change the 0 to a 1. */
  512. X#if 0
  513. Xtypedef unsigned int    size_t;
  514. X#define    SIZE_T_MAX    UINT_MAX
  515. X#endif
  516. X
  517. X/*
  518. X * If your system doesn't specify a max size for a SIZE_T, check
  519. X * to make sure this is the right one.
  520. X */
  521. X#ifndef SIZE_T_MAX
  522. X#define    SIZE_T_MAX    UINT_MAX
  523. X#endif
  524. X
  525. X/*
  526. X * If your system doesn't have the POSIX type for a signal mask,
  527. X * change the 0 to a 1.
  528. X */
  529. X#if 0
  530. Xtypedef unsigned int    sigset_t;
  531. X#endif
  532. X
  533. X/*
  534. X * If your system's vsprintf returns a char *, not an int,
  535. X * change the 0 to a 1.
  536. X */
  537. X#if 0
  538. X#define    VSPRINTF_CHARSTAR
  539. X#endif
  540. X
  541. X/*
  542. X * If you don't have POSIX 1003.1 signals, the signal code surrounding the 
  543. X * temporary file creation is intended to block all of the possible signals
  544. X * long enough to create the file and unlink it.  All of this stuff is
  545. X * intended to use old-style BSD calls to fake POSIX 1003.1 calls.
  546. X */
  547. X#ifdef NO_POSIX_SIGNALS
  548. X#define sigemptyset(set)    (*(set) = 0)
  549. X#define sigfillset(set)        (*(set) = ~(sigset_t)0, 0)
  550. X#define sigaddset(set,signo)    (*(set) |= sigmask(signo), 0)
  551. X#define sigdelset(set,signo)    (*(set) &= ~sigmask(signo), 0)
  552. X#define sigismember(set,signo)    ((*(set) & sigmask(signo)) != 0)
  553. X
  554. X#define SIG_BLOCK    1
  555. X#define SIG_UNBLOCK    2
  556. X#define SIG_SETMASK    3
  557. X
  558. Xstatic int __sigtemp;        /* For the use of sigprocmask */
  559. X
  560. X#define sigprocmask(how,set,oset) \
  561. X    ((__sigtemp = (((how) == SIG_BLOCK) ? \
  562. X    sigblock(0) | *(set) : (((how) == SIG_UNBLOCK) ? \
  563. X    sigblock(0) & ~(*(set)) : ((how) == SIG_SETMASK ? \
  564. X    *(set) : sigblock(0))))), ((oset) ? \
  565. X    (*(oset) = sigsetmask(__sigtemp)) : sigsetmask(__sigtemp)), 0)
  566. X#endif
  567. X
  568. X/*
  569. X * If realloc(3) of a NULL pointer on your system isn't the same as
  570. X * a malloc(3) call, change the 0 to a 1, and add realloc.o to the
  571. X * MISC line in your Makefile.
  572. X */
  573. X#if 0
  574. X#define    realloc    __fix_realloc
  575. X#endif
  576. X
  577. X/*
  578. X * If your system doesn't have an include file with the appropriate
  579. X * byte order set, make sure you specify the correct one.
  580. X */
  581. X#ifndef BYTE_ORDER
  582. X#define LITTLE_ENDIAN    1234        /* LSB first: i386, vax */
  583. X#define BIG_ENDIAN    4321        /* MSB first: 68000, ibm, net */
  584. X#define BYTE_ORDER    BIG_ENDIAN    /* Set for your system. */
  585. X#endif
  586. X
  587. X#if defined(SYSV) || defined(SYSTEM5)
  588. X#define    index(a, b)        strchr(a, b)
  589. X#define    rindex(a, b)        strrchr(a, b)
  590. X#define    bzero(a, b)        memset(a, 0, b)
  591. X#define    bcmp(a, b, n)        memcmp(a, b, n)
  592. X#define    bcopy(a, b, n)        memmove(b, a, n)
  593. X#endif
  594. X
  595. X#if defined(BSD) || defined(BSD4_3)
  596. X#define    strchr(a, b)        index(a, b)
  597. X#define    strrchr(a, b)        rindex(a, b)
  598. X#define    memcmp(a, b, n)        bcmp(a, b, n)
  599. X#define    memmove(a, b, n)    bcopy(b, a, n)
  600. X#endif
  601. X
  602. X/*
  603. X * 32-bit machine.  The db routines are theoretically independent of
  604. X * the size of u_shorts and u_longs, but I don't know that anyone has
  605. X * ever actually tried it.  At a minimum, change the following #define's
  606. X * if you are trying to compile on a different type of system.
  607. X */
  608. X#ifndef USHRT_MAX
  609. X#define    USHRT_MAX        0xFFFF
  610. X#define    ULONG_MAX        0xFFFFFFFF
  611. X#endif
  612. X
  613. X/* POSIX 1003.1 access mode mask. */
  614. X#ifndef O_ACCMODE
  615. X#define    O_ACCMODE    (O_RDONLY|O_WRONLY|O_RDWR)
  616. X#endif
  617. X
  618. X/* POSIX 1003.2 RE limit. */
  619. X#ifndef    _POSIX2_RE_DUP_MAX
  620. X#define    _POSIX2_RE_DUP_MAX    255
  621. X#endif
  622. X
  623. X/*
  624. X * If you can't provide lock values in the open(2) call.  Note, this
  625. X * allows races to happen.
  626. X */
  627. X#ifndef O_EXLOCK
  628. X#define    O_EXLOCK    0
  629. X#endif
  630. X
  631. X#ifndef O_SHLOCK
  632. X#define    O_SHLOCK    0
  633. X#endif
  634. X
  635. X#ifndef EFTYPE
  636. X#define    EFTYPE        EINVAL        /* POSIX 1003.1 format errno. */
  637. X#endif
  638. X
  639. X#ifndef    WCOREDUMP            /* 4.4BSD extension */
  640. X#define    WCOREDUMP(a)    0
  641. X#endif
  642. X
  643. X#ifndef    STDERR_FILENO
  644. X#define    STDIN_FILENO    0        /* ANSI C #defines */
  645. X#define    STDOUT_FILENO    1
  646. X#define    STDERR_FILENO    2
  647. X#endif
  648. X
  649. X#ifndef SEEK_END
  650. X#define    SEEK_SET    0        /* POSIX 1003.1 seek values */
  651. X#define    SEEK_CUR    1
  652. X#define    SEEK_END    2
  653. X#endif
  654. X
  655. X#ifndef S_ISLNK                /* BSD POSIX 1003.1 extensions */
  656. X#define S_ISLNK(m)      ((m & 0170000) == 0120000)
  657. X#define S_ISSOCK(m)     ((m & 0170000) == 0140000)
  658. X#endif
  659. X
  660. X#ifndef _POSIX2_RE_DUP_MAX
  661. X#define    _POSIX2_RE_DUP_MAX    255
  662. X#endif
  663. X
  664. X#ifndef NULL                /* ANSI C #defines NULL everywhere. */
  665. X#define    NULL        0
  666. X#endif
  667. X
  668. X#ifndef    MAX
  669. X#define    MAX(_a,_b)    ((_a)<(_b)?(_b):(_a))
  670. X#endif
  671. X#ifndef    MIN
  672. X#define    MIN(_a,_b)    ((_a)<(_b)?(_a):(_b))
  673. X#endif
  674. X
  675. X#ifndef _BSD_VA_LIST_
  676. X#define    _BSD_VA_LIST_    char *
  677. X#endif
  678. X
  679. X#endif /* !_COMPAT_H_ */
  680. END_OF_FILE
  681. if test 6332 -ne `wc -c <'PORT/sys/compat.h'`; then
  682.     echo shar: \"'PORT/sys/compat.h'\" unpacked with wrong size!
  683. fi
  684. # end of 'PORT/sys/compat.h'
  685. fi
  686. if test -f 'PORT/sys/db.h' -a "${1}" != "-c" ; then 
  687.   echo shar: Will not clobber existing file \"'PORT/sys/db.h'\"
  688. else
  689. echo shar: Extracting \"'PORT/sys/db.h'\" \(6767 characters\)
  690. sed "s/^X//" >'PORT/sys/db.h' <<'END_OF_FILE'
  691. X/*-
  692. X * Copyright (c) 1990, 1993
  693. X *    The Regents of the University of California.  All rights reserved.
  694. X *
  695. X * Redistribution and use in source and binary forms, with or without
  696. X * modification, are permitted provided that the following conditions
  697. X * are met:
  698. X * 1. Redistributions of source code must retain the above copyright
  699. X *    notice, this list of conditions and the following disclaimer.
  700. X * 2. Redistributions in binary form must reproduce the above copyright
  701. X *    notice, this list of conditions and the following disclaimer in the
  702. X *    documentation and/or other materials provided with the distribution.
  703. X * 3. All advertising materials mentioning features or use of this software
  704. X *    must display the following acknowledgement:
  705. X *    This product includes software developed by the University of
  706. X *    California, Berkeley and its contributors.
  707. X * 4. Neither the name of the University nor the names of its contributors
  708. X *    may be used to endorse or promote products derived from this software
  709. X *    without specific prior written permission.
  710. X *
  711. X * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  712. X * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  713. X * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  714. X * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  715. X * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  716. X * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  717. X * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  718. X * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  719. X * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  720. X * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  721. X * SUCH DAMAGE.
  722. X *
  723. X *    @(#)db.h    8.1 (Berkeley) 6/2/93
  724. X */
  725. X
  726. X#ifndef _DB_H_
  727. X#define    _DB_H_
  728. X
  729. X#include <sys/types.h>
  730. X#include <sys/cdefs.h>
  731. X
  732. X#include "compat.h"
  733. X
  734. X#define    RET_ERROR    -1        /* Return values. */
  735. X#define    RET_SUCCESS     0
  736. X#define    RET_SPECIAL     1
  737. X
  738. X#define    MAX_PAGE_NUMBER    ULONG_MAX    /* >= # of pages in a file */
  739. Xtypedef u_long    pgno_t;
  740. X#define    MAX_PAGE_OFFSET    USHRT_MAX    /* >= # of bytes in a page */
  741. Xtypedef u_short    indx_t;
  742. X#define    MAX_REC_NUMBER    ULONG_MAX    /* >= # of records in a tree */
  743. Xtypedef u_long    recno_t;
  744. X
  745. X/* Key/data structure -- a Data-Base Thang. */
  746. Xtypedef struct {
  747. X    void    *data;            /* data */
  748. X    size_t     size;            /* data length */
  749. X} DBT;
  750. X
  751. X/* Routine flags. */
  752. X#define    R_CURSOR    1        /* del, put, seq */
  753. X#define    __R_UNUSED    2        /* UNUSED */
  754. X#define    R_FIRST        3        /* seq */
  755. X#define    R_IAFTER    4        /* put (RECNO) */
  756. X#define    R_IBEFORE    5        /* put (RECNO) */
  757. X#define    R_LAST        6        /* seq (BTREE, RECNO) */
  758. X#define    R_NEXT        7        /* seq */
  759. X#define    R_NOOVERWRITE    8        /* put */
  760. X#define    R_PREV        9        /* seq (BTREE, RECNO) */
  761. X#define    R_SETCURSOR    10        /* put (RECNO) */
  762. X#define    R_RECNOSYNC    11        /* sync (RECNO) */
  763. X
  764. Xtypedef enum { DB_BTREE, DB_HASH, DB_RECNO } DBTYPE;
  765. X
  766. X#define    __USE_OPEN_FLAGS \
  767. X    (O_CREAT|O_EXCL|O_EXLOCK|O_RDONLY|O_RDWR|O_SHLOCK|O_TRUNC)
  768. X
  769. X/* Access method description structure. */
  770. Xtypedef struct __db {
  771. X    DBTYPE type;            /* underlying db type */
  772. X    int (*close)    __P((struct __db *));
  773. X    int (*del)    __P((const struct __db *, const DBT *, u_int));
  774. X    int (*fd)    __P((const struct __db *));
  775. X    int (*get)    __P((const struct __db *, const DBT *, DBT *, u_int));
  776. X    int (*put)    __P((const struct __db *, DBT *, const DBT *, u_int));
  777. X    int (*seq)    __P((const struct __db *, DBT *, DBT *, u_int));
  778. X    int (*sync)    __P((const struct __db *, u_int));
  779. X    void *internal;            /* access method private */
  780. X} DB;
  781. X
  782. X#define    BTREEMAGIC    0x053162
  783. X#define    BTREEVERSION    3
  784. X
  785. X/* Structure used to pass parameters to the btree routines. */
  786. Xtypedef struct {
  787. X#define    R_DUP        0x01    /* duplicate keys */
  788. X    u_long     flags;
  789. X    int     cachesize;    /* bytes to cache */
  790. X    int     maxkeypage;    /* maximum keys per page */
  791. X    int     minkeypage;    /* minimum keys per page */
  792. X    int     psize;        /* page size */
  793. X                /* comparison, prefix functions */
  794. X    int     (*compare)    __P((const DBT *, const DBT *));
  795. X    int     (*prefix)    __P((const DBT *, const DBT *));
  796. X    int     lorder;    /* byte order */
  797. X} BTREEINFO;
  798. X
  799. X#define    HASHMAGIC    0x061561
  800. X#define    HASHVERSION    2
  801. X
  802. X/* Structure used to pass parameters to the hashing routines. */
  803. Xtypedef struct {
  804. X    int     bsize;        /* bucket size */
  805. X    int     ffactor;    /* fill factor */
  806. X    int     nelem;        /* number of elements */
  807. X    int     cachesize;    /* bytes to cache */
  808. X                /* hash function */
  809. X    int     (*hash) __P((const void *, size_t));
  810. X    int     lorder;    /* byte order */
  811. X} HASHINFO;
  812. X
  813. X/* Structure used to pass parameters to the record routines. */
  814. Xtypedef struct {
  815. X#define    R_FIXEDLEN    0x01    /* fixed-length records */
  816. X#define    R_NOKEY        0x02    /* key not required */
  817. X#define    R_SNAPSHOT    0x04    /* snapshot the input */
  818. X    u_long     flags;
  819. X    int     cachesize;    /* bytes to cache */
  820. X    int     psize;        /* page size */
  821. X    int     lorder;    /* byte order */
  822. X    size_t     reclen;    /* record length (fixed-length records) */
  823. X    u_char     bval;        /* delimiting byte (variable-length records */
  824. X    char    *bfname;    /* btree file name */ 
  825. X} RECNOINFO;
  826. X
  827. X/*
  828. X * Little endian <==> big endian long swap macros.
  829. X *    BLSWAP        swap a memory location
  830. X *    BLPSWAP        swap a referenced memory location
  831. X *    BLSWAP_COPY    swap from one location to another
  832. X */
  833. X#define BLSWAP(a) { \
  834. X    u_long _tmp = a; \
  835. X    ((char *)&a)[0] = ((char *)&_tmp)[3]; \
  836. X    ((char *)&a)[1] = ((char *)&_tmp)[2]; \
  837. X    ((char *)&a)[2] = ((char *)&_tmp)[1]; \
  838. X    ((char *)&a)[3] = ((char *)&_tmp)[0]; \
  839. X}
  840. X#define    BLPSWAP(a) { \
  841. X    u_long _tmp = *(u_long *)a; \
  842. X    ((char *)a)[0] = ((char *)&_tmp)[3]; \
  843. X    ((char *)a)[1] = ((char *)&_tmp)[2]; \
  844. X    ((char *)a)[2] = ((char *)&_tmp)[1]; \
  845. X    ((char *)a)[3] = ((char *)&_tmp)[0]; \
  846. X}
  847. X#define    BLSWAP_COPY(a, b) { \
  848. X    ((char *)&(b))[0] = ((char *)&(a))[3]; \
  849. X    ((char *)&(b))[1] = ((char *)&(a))[2]; \
  850. X    ((char *)&(b))[2] = ((char *)&(a))[1]; \
  851. X    ((char *)&(b))[3] = ((char *)&(a))[0]; \
  852. X}
  853. X
  854. X/*
  855. X * Little endian <==> big endian short swap macros.
  856. X *    BSSWAP        swap a memory location
  857. X *    BSPSWAP        swap a referenced memory location
  858. X *    BSSWAP_COPY    swap from one location to another
  859. X */
  860. X#define BSSWAP(a) { \
  861. X    u_short _tmp = a; \
  862. X    ((char *)&a)[0] = ((char *)&_tmp)[1]; \
  863. X    ((char *)&a)[1] = ((char *)&_tmp)[0]; \
  864. X}
  865. X#define BSPSWAP(a) { \
  866. X    u_short _tmp = *(u_short *)a; \
  867. X    ((char *)a)[0] = ((char *)&_tmp)[1]; \
  868. X    ((char *)a)[1] = ((char *)&_tmp)[0]; \
  869. X}
  870. X#define BSSWAP_COPY(a, b) { \
  871. X    ((char *)&(b))[0] = ((char *)&(a))[1]; \
  872. X    ((char *)&(b))[1] = ((char *)&(a))[0]; \
  873. X}
  874. X
  875. X__BEGIN_DECLS
  876. XDB *dbopen __P((const char *, int, int, DBTYPE, const void *));
  877. X
  878. X#ifdef __DBINTERFACE_PRIVATE
  879. XDB    *__bt_open __P((const char *, int, int, const BTREEINFO *));
  880. XDB    *__hash_open __P((const char *, int, int, const HASHINFO *));
  881. XDB    *__rec_open __P((const char *, int, int, const RECNOINFO *));
  882. Xvoid     __dbpanic __P((DB *dbp));
  883. X#endif
  884. X__END_DECLS
  885. X#endif /* !_DB_H_ */
  886. END_OF_FILE
  887. if test 6767 -ne `wc -c <'PORT/sys/db.h'`; then
  888.     echo shar: \"'PORT/sys/db.h'\" unpacked with wrong size!
  889. fi
  890. # end of 'PORT/sys/db.h'
  891. fi
  892. if test -f 'btree/bt_get.c' -a "${1}" != "-c" ; then 
  893.   echo shar: Will not clobber existing file \"'btree/bt_get.c'\"
  894. else
  895. echo shar: Extracting \"'btree/bt_get.c'\" \(6264 characters\)
  896. sed "s/^X//" >'btree/bt_get.c' <<'END_OF_FILE'
  897. X/*-
  898. X * Copyright (c) 1990, 1993
  899. X *    The Regents of the University of California.  All rights reserved.
  900. X *
  901. X * This code is derived from software contributed to Berkeley by
  902. X * Mike Olson.
  903. X *
  904. X * Redistribution and use in source and binary forms, with or without
  905. X * modification, are permitted provided that the following conditions
  906. X * are met:
  907. X * 1. Redistributions of source code must retain the above copyright
  908. X *    notice, this list of conditions and the following disclaimer.
  909. X * 2. Redistributions in binary form must reproduce the above copyright
  910. X *    notice, this list of conditions and the following disclaimer in the
  911. X *    documentation and/or other materials provided with the distribution.
  912. X * 3. All advertising materials mentioning features or use of this software
  913. X *    must display the following acknowledgement:
  914. X *    This product includes software developed by the University of
  915. X *    California, Berkeley and its contributors.
  916. X * 4. Neither the name of the University nor the names of its contributors
  917. X *    may be used to endorse or promote products derived from this software
  918. X *    without specific prior written permission.
  919. X *
  920. X * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  921. X * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  922. X * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  923. X * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  924. X * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  925. X * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  926. X * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  927. X * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  928. X * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  929. X * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  930. X * SUCH DAMAGE.
  931. X */
  932. X
  933. X#if defined(LIBC_SCCS) && !defined(lint)
  934. Xstatic char sccsid[] = "@(#)bt_get.c    8.1 (Berkeley) 6/4/93";
  935. X#endif /* LIBC_SCCS and not lint */
  936. X
  937. X#include <sys/types.h>
  938. X
  939. X#include <errno.h>
  940. X#include <stddef.h>
  941. X#include <stdio.h>
  942. X
  943. X#include <db.h>
  944. X#include "btree.h"
  945. X
  946. X/*
  947. X * __BT_GET -- Get a record from the btree.
  948. X *
  949. X * Parameters:
  950. X *    dbp:    pointer to access method
  951. X *    key:    key to find
  952. X *    data:    data to return
  953. X *    flag:    currently unused
  954. X *
  955. X * Returns:
  956. X *    RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key not found.
  957. X */
  958. Xint
  959. X__bt_get(dbp, key, data, flags)
  960. X    const DB *dbp;
  961. X    const DBT *key;
  962. X    DBT *data;
  963. X    u_int flags;
  964. X{
  965. X    BTREE *t;
  966. X    EPG *e;
  967. X    int exact, status;
  968. X
  969. X    if (flags) {
  970. X        errno = EINVAL;
  971. X        return (RET_ERROR);
  972. X    }
  973. X    t = dbp->internal;
  974. X    if ((e = __bt_search(t, key, &exact)) == NULL)
  975. X        return (RET_ERROR);
  976. X    if (!exact) {
  977. X        mpool_put(t->bt_mp, e->page, 0);
  978. X        return (RET_SPECIAL);
  979. X    }
  980. X
  981. X    /*
  982. X     * A special case is if we found the record but it's flagged for
  983. X     * deletion.  In this case, we want to find another record with the
  984. X     * same key, if it exists.  Rather than look around the tree we call
  985. X     * __bt_first and have it redo the search, as __bt_first will not
  986. X     * return keys marked for deletion.  Slow, but should never happen.
  987. X     */
  988. X    if (ISSET(t, B_DELCRSR) && e->page->pgno == t->bt_bcursor.pgno &&
  989. X        e->index == t->bt_bcursor.index) {
  990. X        mpool_put(t->bt_mp, e->page, 0);
  991. X        if ((e = __bt_first(t, key, &exact)) == NULL)
  992. X            return (RET_ERROR);
  993. X        if (!exact)
  994. X            return (RET_SPECIAL);
  995. X    }
  996. X
  997. X    status = __bt_ret(t, e, NULL, data);
  998. X    mpool_put(t->bt_mp, e->page, 0);
  999. X    return (status);
  1000. X}
  1001. X
  1002. X/*
  1003. X * __BT_FIRST -- Find the first entry.
  1004. X *
  1005. X * Parameters:
  1006. X *    t:    the tree
  1007. X *    key:    the key
  1008. X *
  1009. X * Returns:
  1010. X *    The first entry in the tree greater than or equal to key.
  1011. X */
  1012. XEPG *
  1013. X__bt_first(t, key, exactp)
  1014. X    BTREE *t;
  1015. X    const DBT *key;
  1016. X    int *exactp;
  1017. X{
  1018. X    register PAGE *h;
  1019. X    register EPG *e;
  1020. X    EPG save;
  1021. X    pgno_t cpgno, pg;
  1022. X    indx_t cindex;
  1023. X    int found;
  1024. X
  1025. X    /*
  1026. X     * Find any matching record; __bt_search pins the page.  Only exact
  1027. X     * matches are tricky, otherwise just return the location of the key
  1028. X     * if it were to be inserted into the tree.
  1029. X     */
  1030. X    if ((e = __bt_search(t, key, exactp)) == NULL)
  1031. X        return (NULL);
  1032. X    if (!*exactp)
  1033. X        return (e);
  1034. X
  1035. X    if (ISSET(t, B_DELCRSR)) {
  1036. X        cpgno = t->bt_bcursor.pgno;
  1037. X        cindex = t->bt_bcursor.index;
  1038. X    } else {
  1039. X        cpgno = P_INVALID;
  1040. X        cindex = 0;        /* GCC thinks it's uninitialized. */
  1041. X    }
  1042. X
  1043. X    /*
  1044. X     * Walk backwards, skipping empty pages, as long as the entry matches
  1045. X     * and there are keys left in the tree.  Save a copy of each match in
  1046. X     * case we go too far.  A special case is that we don't return a match
  1047. X     * on records that the cursor references that have already been flagged
  1048. X     * for deletion.
  1049. X     */
  1050. X    save = *e;
  1051. X    h = e->page;
  1052. X    found = 0;
  1053. X    do {
  1054. X        if (cpgno != h->pgno || cindex != e->index) {
  1055. X            if (save.page->pgno != e->page->pgno) {
  1056. X                mpool_put(t->bt_mp, save.page, 0);
  1057. X                save = *e;
  1058. X            } else
  1059. X                save.index = e->index;
  1060. X            found = 1;
  1061. X        }
  1062. X        /*
  1063. X         * Make a special effort not to unpin the page the last (or
  1064. X         * original) match was on, but also make sure it's unpinned
  1065. X         * if an error occurs.
  1066. X         */
  1067. X        while (e->index == 0) {
  1068. X            if (h->prevpg == P_INVALID)
  1069. X                goto done1;
  1070. X            if (h->pgno != save.page->pgno)
  1071. X                mpool_put(t->bt_mp, h, 0);
  1072. X            if ((h = mpool_get(t->bt_mp, h->prevpg, 0)) == NULL) {
  1073. X                if (h->pgno == save.page->pgno)
  1074. X                    mpool_put(t->bt_mp, save.page, 0);
  1075. X                return (NULL);
  1076. X            }
  1077. X            e->page = h;
  1078. X            e->index = NEXTINDEX(h);
  1079. X        }
  1080. X        --e->index;
  1081. X    } while (__bt_cmp(t, key, e) == 0);
  1082. X
  1083. X    /*
  1084. X     * Reach here with the last page that was looked at pinned, which may
  1085. X     * or may not be the same as the last (or original) match page.  If
  1086. X     * it's not useful, release it.
  1087. X     */
  1088. Xdone1:    if (h->pgno != save.page->pgno)
  1089. X        mpool_put(t->bt_mp, h, 0);
  1090. X
  1091. X    /*
  1092. X     * If still haven't found a record, the only possibility left is the
  1093. X     * next one.  Move forward one slot, skipping empty pages and check.
  1094. X     */
  1095. X    if (!found) {
  1096. X        h = save.page;
  1097. X        if (++save.index == NEXTINDEX(h)) {
  1098. X            do {
  1099. X                pg = h->nextpg;
  1100. X                mpool_put(t->bt_mp, h, 0);
  1101. X                if (pg == P_INVALID) {
  1102. X                    *exactp = 0;
  1103. X                    return (e);
  1104. X                }
  1105. X                if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL)
  1106. X                    return (NULL);
  1107. X            } while ((save.index = NEXTINDEX(h)) == 0);
  1108. X            save.page = h;
  1109. X        }
  1110. X        if (__bt_cmp(t, key, &save) != 0) {
  1111. X            *exactp = 0;
  1112. X            return (e);
  1113. X        }
  1114. X    }
  1115. X    *e = save;
  1116. X    *exactp = 1;
  1117. X    return (e);
  1118. X}
  1119. END_OF_FILE
  1120. if test 6264 -ne `wc -c <'btree/bt_get.c'`; then
  1121.     echo shar: \"'btree/bt_get.c'\" unpacked with wrong size!
  1122. fi
  1123. # end of 'btree/bt_get.c'
  1124. fi
  1125. if test -f 'btree/bt_overflow.c' -a "${1}" != "-c" ; then 
  1126.   echo shar: Will not clobber existing file \"'btree/bt_overflow.c'\"
  1127. else
  1128. echo shar: Extracting \"'btree/bt_overflow.c'\" \(5854 characters\)
  1129. sed "s/^X//" >'btree/bt_overflow.c' <<'END_OF_FILE'
  1130. X/*-
  1131. X * Copyright (c) 1990, 1993
  1132. X *    The Regents of the University of California.  All rights reserved.
  1133. X *
  1134. X * This code is derived from software contributed to Berkeley by
  1135. X * Mike Olson.
  1136. X *
  1137. X * Redistribution and use in source and binary forms, with or without
  1138. X * modification, are permitted provided that the following conditions
  1139. X * are met:
  1140. X * 1. Redistributions of source code must retain the above copyright
  1141. X *    notice, this list of conditions and the following disclaimer.
  1142. X * 2. Redistributions in binary form must reproduce the above copyright
  1143. X *    notice, this list of conditions and the following disclaimer in the
  1144. X *    documentation and/or other materials provided with the distribution.
  1145. X * 3. All advertising materials mentioning features or use of this software
  1146. X *    must display the following acknowledgement:
  1147. X *    This product includes software developed by the University of
  1148. X *    California, Berkeley and its contributors.
  1149. X * 4. Neither the name of the University nor the names of its contributors
  1150. X *    may be used to endorse or promote products derived from this software
  1151. X *    without specific prior written permission.
  1152. X *
  1153. X * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  1154. X * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  1155. X * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  1156. X * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  1157. X * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  1158. X * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  1159. X * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  1160. X * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  1161. X * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  1162. X * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  1163. X * SUCH DAMAGE.
  1164. X */
  1165. X
  1166. X#if defined(LIBC_SCCS) && !defined(lint)
  1167. Xstatic char sccsid[] = "@(#)bt_overflow.c    8.1 (Berkeley) 6/4/93";
  1168. X#endif /* LIBC_SCCS and not lint */
  1169. X
  1170. X#include <sys/param.h>
  1171. X
  1172. X#include <stdio.h>
  1173. X#include <stdlib.h>
  1174. X#include <string.h>
  1175. X
  1176. X#include <db.h>
  1177. X#include "btree.h"
  1178. X
  1179. X/*
  1180. X * Big key/data code.
  1181. X *
  1182. X * Big key and data entries are stored on linked lists of pages.  The initial
  1183. X * reference is byte string stored with the key or data and is the page number
  1184. X * and size.  The actual record is stored in a chain of pages linked by the
  1185. X * nextpg field of the PAGE header.
  1186. X *
  1187. X * The first page of the chain has a special property.  If the record is used
  1188. X * by an internal page, it cannot be deleted and the P_PRESERVE bit will be set
  1189. X * in the header.
  1190. X *
  1191. X * XXX
  1192. X * A single DBT is written to each chain, so a lot of space on the last page
  1193. X * is wasted.  This is a fairly major bug for some data sets.
  1194. X */
  1195. X
  1196. X/*
  1197. X * __OVFL_GET -- Get an overflow key/data item.
  1198. X *
  1199. X * Parameters:
  1200. X *    t:    tree
  1201. X *    p:    pointer to { pgno_t, size_t }
  1202. X *    buf:    storage address
  1203. X *    bufsz:    storage size
  1204. X *
  1205. X * Returns:
  1206. X *    RET_ERROR, RET_SUCCESS
  1207. X */
  1208. Xint
  1209. X__ovfl_get(t, p, ssz, buf, bufsz)
  1210. X    BTREE *t;
  1211. X    void *p;
  1212. X    size_t *ssz;
  1213. X    char **buf;
  1214. X    size_t *bufsz;
  1215. X{
  1216. X    PAGE *h;
  1217. X    pgno_t pg;
  1218. X    size_t nb, plen, sz;
  1219. X
  1220. X    memmove(&pg, p, sizeof(pgno_t));
  1221. X    memmove(&sz, (char *)p + sizeof(pgno_t), sizeof(size_t));
  1222. X    *ssz = sz;
  1223. X
  1224. X#ifdef DEBUG
  1225. X    if (pg == P_INVALID || sz == 0)
  1226. X        abort();
  1227. X#endif
  1228. X    /* Make the buffer bigger as necessary. */
  1229. X    if (*bufsz < sz) {
  1230. X        if ((*buf = realloc(*buf, sz)) == NULL)
  1231. X            return (RET_ERROR);
  1232. X        *bufsz = sz;
  1233. X    }
  1234. X
  1235. X    /*
  1236. X     * Step through the linked list of pages, copying the data on each one
  1237. X     * into the buffer.  Never copy more than the data's length.
  1238. X     */
  1239. X    plen = t->bt_psize - BTDATAOFF;
  1240. X    for (p = *buf;; p = (char *)p + nb, pg = h->nextpg) {
  1241. X        if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL)
  1242. X            return (RET_ERROR);
  1243. X
  1244. X        nb = MIN(sz, plen);
  1245. X        memmove(p, (char *)h + BTDATAOFF, nb);
  1246. X        mpool_put(t->bt_mp, h, 0);
  1247. X
  1248. X        if ((sz -= nb) == 0)
  1249. X            break;
  1250. X    }
  1251. X    return (RET_SUCCESS);
  1252. X}
  1253. X
  1254. X/*
  1255. X * __OVFL_PUT -- Store an overflow key/data item.
  1256. X *
  1257. X * Parameters:
  1258. X *    t:    tree
  1259. X *    data:    DBT to store
  1260. X *    pgno:    storage page number
  1261. X *
  1262. X * Returns:
  1263. X *    RET_ERROR, RET_SUCCESS
  1264. X */
  1265. Xint
  1266. X__ovfl_put(t, dbt, pg)
  1267. X    BTREE *t;
  1268. X    const DBT *dbt;
  1269. X    pgno_t *pg;
  1270. X{
  1271. X    PAGE *h, *last;
  1272. X    void *p;
  1273. X    pgno_t npg;
  1274. X    size_t nb, plen, sz;
  1275. X
  1276. X    /*
  1277. X     * Allocate pages and copy the key/data record into them.  Store the
  1278. X     * number of the first page in the chain.
  1279. X     */
  1280. X    plen = t->bt_psize - BTDATAOFF;
  1281. X    for (last = NULL, p = dbt->data, sz = dbt->size;;
  1282. X        p = (char *)p + plen, last = h) {
  1283. X        if ((h = __bt_new(t, &npg)) == NULL)
  1284. X            return (RET_ERROR);
  1285. X
  1286. X        h->pgno = npg;
  1287. X        h->nextpg = h->prevpg = P_INVALID;
  1288. X        h->flags = P_OVERFLOW;
  1289. X        h->lower = h->upper = 0;
  1290. X
  1291. X        nb = MIN(sz, plen);
  1292. X        memmove((char *)h + BTDATAOFF, p, nb);
  1293. X
  1294. X        if (last) {
  1295. X            last->nextpg = h->pgno;
  1296. X            mpool_put(t->bt_mp, last, MPOOL_DIRTY);
  1297. X        } else
  1298. X            *pg = h->pgno;
  1299. X
  1300. X        if ((sz -= nb) == 0) {
  1301. X            mpool_put(t->bt_mp, h, MPOOL_DIRTY);
  1302. X            break;
  1303. X        }
  1304. X    }
  1305. X    return (RET_SUCCESS);
  1306. X}
  1307. X
  1308. X/*
  1309. X * __OVFL_DELETE -- Delete an overflow chain.
  1310. X *
  1311. X * Parameters:
  1312. X *    t:    tree
  1313. X *    p:    pointer to { pgno_t, size_t }
  1314. X *
  1315. X * Returns:
  1316. X *    RET_ERROR, RET_SUCCESS
  1317. X */
  1318. Xint
  1319. X__ovfl_delete(t, p)
  1320. X    BTREE *t;
  1321. X    void *p;
  1322. X{
  1323. X    PAGE *h;
  1324. X    pgno_t pg;
  1325. X    size_t plen, sz;
  1326. X
  1327. X    memmove(&pg, p, sizeof(pgno_t));
  1328. X    memmove(&sz, (char *)p + sizeof(pgno_t), sizeof(size_t));
  1329. X
  1330. X#ifdef DEBUG
  1331. X    if (pg == P_INVALID || sz == 0)
  1332. X        abort();
  1333. X#endif
  1334. X    if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL)
  1335. X        return (RET_ERROR);
  1336. X
  1337. X    /* Don't delete chains used by internal pages. */
  1338. X    if (h->flags & P_PRESERVE) {
  1339. X        mpool_put(t->bt_mp, h, 0);
  1340. X        return (RET_SUCCESS);
  1341. X    }
  1342. X
  1343. X    /* Step through the chain, calling the free routine for each page. */
  1344. X    for (plen = t->bt_psize - BTDATAOFF;; sz -= plen) {
  1345. X        pg = h->nextpg;
  1346. X        __bt_free(t, h);
  1347. X        if (sz <= plen)
  1348. X            break;
  1349. X        if ((h = mpool_get(t->bt_mp, pg, 0)) == NULL)
  1350. X            return (RET_ERROR);
  1351. X    }
  1352. X    return (RET_SUCCESS);
  1353. X}
  1354. END_OF_FILE
  1355. if test 5854 -ne `wc -c <'btree/bt_overflow.c'`; then
  1356.     echo shar: \"'btree/bt_overflow.c'\" unpacked with wrong size!
  1357. fi
  1358. # end of 'btree/bt_overflow.c'
  1359. fi
  1360. if test -f 'btree/bt_utils.c' -a "${1}" != "-c" ; then 
  1361.   echo shar: Will not clobber existing file \"'btree/bt_utils.c'\"
  1362. else
  1363. echo shar: Extracting \"'btree/bt_utils.c'\" \(5835 characters\)
  1364. sed "s/^X//" >'btree/bt_utils.c' <<'END_OF_FILE'
  1365. X/*-
  1366. X * Copyright (c) 1990, 1993
  1367. X *    The Regents of the University of California.  All rights reserved.
  1368. X *
  1369. X * This code is derived from software contributed to Berkeley by
  1370. X * Mike Olson.
  1371. X *
  1372. X * Redistribution and use in source and binary forms, with or without
  1373. X * modification, are permitted provided that the following conditions
  1374. X * are met:
  1375. X * 1. Redistributions of source code must retain the above copyright
  1376. X *    notice, this list of conditions and the following disclaimer.
  1377. X * 2. Redistributions in binary form must reproduce the above copyright
  1378. X *    notice, this list of conditions and the following disclaimer in the
  1379. X *    documentation and/or other materials provided with the distribution.
  1380. X * 3. All advertising materials mentioning features or use of this software
  1381. X *    must display the following acknowledgement:
  1382. X *    This product includes software developed by the University of
  1383. X *    California, Berkeley and its contributors.
  1384. X * 4. Neither the name of the University nor the names of its contributors
  1385. X *    may be used to endorse or promote products derived from this software
  1386. X *    without specific prior written permission.
  1387. X *
  1388. X * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  1389. X * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  1390. X * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  1391. X * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  1392. X * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  1393. X * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  1394. X * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  1395. X * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  1396. X * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  1397. X * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  1398. X * SUCH DAMAGE.
  1399. X */
  1400. X
  1401. X#if defined(LIBC_SCCS) && !defined(lint)
  1402. Xstatic char sccsid[] = "@(#)bt_utils.c    8.1 (Berkeley) 6/4/93";
  1403. X#endif /* LIBC_SCCS and not lint */
  1404. X
  1405. X#include <sys/param.h>
  1406. X
  1407. X#include <stdio.h>
  1408. X#include <stdlib.h>
  1409. X#include <string.h>
  1410. X
  1411. X#include <db.h>
  1412. X#include "btree.h"
  1413. X
  1414. X/*
  1415. X * __BT_RET -- Build return key/data pair as a result of search or scan.
  1416. X *
  1417. X * Parameters:
  1418. X *    t:    tree
  1419. X *    d:    LEAF to be returned to the user.
  1420. X *    key:    user's key structure (NULL if not to be filled in)
  1421. X *    data:    user's data structure
  1422. X *
  1423. X * Returns:
  1424. X *    RET_SUCCESS, RET_ERROR.
  1425. X */
  1426. Xint
  1427. X__bt_ret(t, e, key, data)
  1428. X    BTREE *t;
  1429. X    EPG *e;
  1430. X    DBT *key, *data;
  1431. X{
  1432. X    register BLEAF *bl;
  1433. X    register void *p;
  1434. X
  1435. X    bl = GETBLEAF(e->page, e->index);
  1436. X
  1437. X    if (bl->flags & P_BIGDATA) {
  1438. X        if (__ovfl_get(t, bl->bytes + bl->ksize,
  1439. X            &data->size, &t->bt_dbuf, &t->bt_dbufsz))
  1440. X            return (RET_ERROR);
  1441. X    } else {
  1442. X        /* Use +1 in case the first record retrieved is 0 length. */
  1443. X        if (bl->dsize + 1 > t->bt_dbufsz) {
  1444. X            if ((p = realloc(t->bt_dbuf, bl->dsize + 1)) == NULL)
  1445. X                return (RET_ERROR);
  1446. X            t->bt_dbuf = p;
  1447. X            t->bt_dbufsz = bl->dsize + 1;
  1448. X        }
  1449. X        memmove(t->bt_dbuf, bl->bytes + bl->ksize, bl->dsize);
  1450. X        data->size = bl->dsize;
  1451. X    }
  1452. X    data->data = t->bt_dbuf;
  1453. X
  1454. X    if (key == NULL)
  1455. X        return (RET_SUCCESS);
  1456. X
  1457. X    if (bl->flags & P_BIGKEY) {
  1458. X        if (__ovfl_get(t, bl->bytes,
  1459. X            &key->size, &t->bt_kbuf, &t->bt_kbufsz))
  1460. X            return (RET_ERROR);
  1461. X    } else {
  1462. X        if (bl->ksize > t->bt_kbufsz) {
  1463. X            if ((p = realloc(t->bt_kbuf, bl->ksize)) == NULL)
  1464. X                return (RET_ERROR);
  1465. X            t->bt_kbuf = p;
  1466. X            t->bt_kbufsz = bl->ksize;
  1467. X        }
  1468. X        memmove(t->bt_kbuf, bl->bytes, bl->ksize);
  1469. X        key->size = bl->ksize;
  1470. X    }
  1471. X    key->data = t->bt_kbuf;
  1472. X    return (RET_SUCCESS);
  1473. X}
  1474. X
  1475. X/*
  1476. X * __BT_CMP -- Compare a key to a given record.
  1477. X *
  1478. X * Parameters:
  1479. X *    t:    tree
  1480. X *    k1:    DBT pointer of first arg to comparison
  1481. X *    e:    pointer to EPG for comparison
  1482. X *
  1483. X * Returns:
  1484. X *    < 0 if k1 is < record
  1485. X *    = 0 if k1 is = record
  1486. X *    > 0 if k1 is > record
  1487. X */
  1488. Xint
  1489. X__bt_cmp(t, k1, e)
  1490. X    BTREE *t;
  1491. X    const DBT *k1;
  1492. X    EPG *e;
  1493. X{
  1494. X    BINTERNAL *bi;
  1495. X    BLEAF *bl;
  1496. X    DBT k2;
  1497. X    PAGE *h;
  1498. X    void *bigkey;
  1499. X
  1500. X    /*
  1501. X     * The left-most key on internal pages, at any level of the tree, is
  1502. X     * guaranteed by the following code to be less than any user key.
  1503. X     * This saves us from having to update the leftmost key on an internal
  1504. X     * page when the user inserts a new key in the tree smaller than
  1505. X     * anything we've yet seen.
  1506. X     */
  1507. X    h = e->page;
  1508. X    if (e->index == 0 && h->prevpg == P_INVALID && !(h->flags & P_BLEAF))
  1509. X        return (1);
  1510. X
  1511. X    bigkey = NULL;
  1512. X    if (h->flags & P_BLEAF) {
  1513. X        bl = GETBLEAF(h, e->index);
  1514. X        if (bl->flags & P_BIGKEY)
  1515. X            bigkey = bl->bytes;
  1516. X        else {
  1517. X            k2.data = bl->bytes;
  1518. X            k2.size = bl->ksize;
  1519. X        }
  1520. X    } else {
  1521. X        bi = GETBINTERNAL(h, e->index);
  1522. X        if (bi->flags & P_BIGKEY)
  1523. X            bigkey = bi->bytes;
  1524. X        else {
  1525. X            k2.data = bi->bytes;
  1526. X            k2.size = bi->ksize;
  1527. X        }
  1528. X    }
  1529. X
  1530. X    if (bigkey) {
  1531. X        if (__ovfl_get(t, bigkey,
  1532. X            &k2.size, &t->bt_dbuf, &t->bt_dbufsz))
  1533. X            return (RET_ERROR);
  1534. X        k2.data = t->bt_dbuf;
  1535. X    }
  1536. X    return ((*t->bt_cmp)(k1, &k2));
  1537. X}
  1538. X
  1539. X/*
  1540. X * __BT_DEFCMP -- Default comparison routine.
  1541. X *
  1542. X * Parameters:
  1543. X *    a:    DBT #1
  1544. X *    b:     DBT #2
  1545. X *
  1546. X * Returns:
  1547. X *    < 0 if a is < b
  1548. X *    = 0 if a is = b
  1549. X *    > 0 if a is > b
  1550. X */
  1551. Xint
  1552. X__bt_defcmp(a, b)
  1553. X    const DBT *a, *b;
  1554. X{
  1555. X    register u_char *p1, *p2;
  1556. X    register int diff, len;
  1557. X
  1558. X    len = MIN(a->size, b->size);
  1559. X    for (p1 = a->data, p2 = b->data; len--; ++p1, ++p2)
  1560. X        if (diff = *p1 - *p2)
  1561. X            return (diff);
  1562. X    return (a->size - b->size);
  1563. X}
  1564. X
  1565. X/*
  1566. X * __BT_DEFPFX -- Default prefix routine.
  1567. X *
  1568. X * Parameters:
  1569. X *    a:    DBT #1
  1570. X *    b:     DBT #2
  1571. X *
  1572. X * Returns:
  1573. X *    Number of bytes needed to distinguish b from a.
  1574. X */
  1575. Xint
  1576. X__bt_defpfx(a, b)
  1577. X    const DBT *a, *b;
  1578. X{
  1579. X    register u_char *p1, *p2;
  1580. X    register int len;
  1581. X    int cnt;
  1582. X
  1583. X    cnt = 1;
  1584. X    len = MIN(a->size, b->size);
  1585. X    for (p1 = a->data, p2 = b->data; len--; ++p1, ++p2, ++cnt)
  1586. X        if (*p1 != *p2)
  1587. X            return (cnt);
  1588. X
  1589. X    /* a->size must be <= b->size, or they wouldn't be in this order. */
  1590. X    return (a->size < b->size ? a->size + 1 : a->size);
  1591. X}
  1592. END_OF_FILE
  1593. if test 5835 -ne `wc -c <'btree/bt_utils.c'`; then
  1594.     echo shar: \"'btree/bt_utils.c'\" unpacked with wrong size!
  1595. fi
  1596. # end of 'btree/bt_utils.c'
  1597. fi
  1598. if test -f 'man/btree.3' -a "${1}" != "-c" ; then 
  1599.   echo shar: Will not clobber existing file \"'man/btree.3'\"
  1600. else
  1601. echo shar: Extracting \"'man/btree.3'\" \(8129 characters\)
  1602. sed "s/^X//" >'man/btree.3' <<'END_OF_FILE'
  1603. X.\" Copyright (c) 1990, 1993
  1604. X.\"    The Regents of the University of California.  All rights reserved.
  1605. X.\"
  1606. X.\" Redistribution and use in source and binary forms, with or without
  1607. X.\" modification, are permitted provided that the following conditions
  1608. X.\" are met:
  1609. X.\" 1. Redistributions of source code must retain the above copyright
  1610. X.\"    notice, this list of conditions and the following disclaimer.
  1611. X.\" 2. Redistributions in binary form must reproduce the above copyright
  1612. X.\"    notice, this list of conditions and the following disclaimer in the
  1613. X.\"    documentation and/or other materials provided with the distribution.
  1614. X.\" 3. All advertising materials mentioning features or use of this software
  1615. X.\"    must display the following acknowledgement:
  1616. X.\"    This product includes software developed by the University of
  1617. X.\"    California, Berkeley and its contributors.
  1618. X.\" 4. Neither the name of the University nor the names of its contributors
  1619. X.\"    may be used to endorse or promote products derived from this software
  1620. X.\"    without specific prior written permission.
  1621. X.\"
  1622. X.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  1623. X.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  1624. X.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  1625. X.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  1626. X.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  1627. X.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  1628. X.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  1629. X.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  1630. X.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  1631. X.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  1632. X.\" SUCH DAMAGE.
  1633. X.\"
  1634. X.\"    @(#)btree.3    8.1 (Berkeley) 6/4/93
  1635. X.\"
  1636. X.TH BTREE 3
  1637. X.\".UC 7
  1638. X.SH NAME
  1639. Xbtree \- btree database access method
  1640. X.SH SYNOPSIS
  1641. X.nf
  1642. X.ft B
  1643. X#include <sys/types.h>
  1644. X#include <db.h>
  1645. X.ft R
  1646. X.fi
  1647. X.SH DESCRIPTION
  1648. XThe routine
  1649. X.IR dbopen
  1650. Xis the library interface to database files.
  1651. XOne of the supported file formats is btree files.
  1652. XThe general description of the database access methods is in
  1653. X.IR dbopen (3),
  1654. Xthis manual page describes only the btree specific information.
  1655. X.PP
  1656. XThe btree data structure is a sorted, balanced tree structure storing
  1657. Xassociated key/data pairs.
  1658. X.PP
  1659. XThe btree access method specific data structure provided to
  1660. X.I dbopen
  1661. Xis defined in the <db.h> include file as follows:
  1662. X.PP
  1663. Xtypedef struct {
  1664. X.RS
  1665. Xu_long flags;
  1666. X.br
  1667. Xu_int cachesize;
  1668. X.br
  1669. Xindex_t psize;
  1670. X.br
  1671. Xint lorder;
  1672. X.\" .br
  1673. X.\" int maxkeypage;
  1674. X.br
  1675. Xint minkeypage;
  1676. X.br
  1677. Xint (*compare)(const DBT *key1, const DBT *key2);
  1678. X.br
  1679. Xint (*prefix)(const DBT *key1, const DBT *key2);
  1680. X.RE
  1681. X} BTREEINFO;
  1682. X.PP
  1683. XThe elements of this structure are as follows:
  1684. X.TP
  1685. Xflags
  1686. XThe flag value is specified by
  1687. X.IR or 'ing
  1688. Xany of the following values:
  1689. X.RS
  1690. X.TP
  1691. XR_DUP
  1692. XPermit duplicate keys in the tree, i.e. permit insertion if the key to be
  1693. Xinserted already exists in the tree.
  1694. XThe default behavior, as described in
  1695. X.IR dbopen (3),
  1696. Xis to overwrite a matching key when inserting a new key or to fail if
  1697. Xthe R_NOOVERWRITE flag is specified.
  1698. XThe R_DUP flag is overridden by the R_NOOVERWRITE flag, and if the
  1699. XR_NOOVERWRITE flag is specified, attempts to insert duplicate keys into
  1700. Xthe tree will fail.
  1701. X.IP
  1702. XIf the database contains duplicate keys, the order of retrieval of
  1703. Xkey/data pairs is undefined if the
  1704. X.I get
  1705. Xroutine is used, however,
  1706. X.I seq
  1707. Xroutine calls with the R_CURSOR flag set will always return the logical
  1708. X``first'' of any group of duplicate keys.
  1709. X.RE
  1710. X.TP
  1711. Xcachesize
  1712. XA suggested maximum size (in bytes) of the memory cache.
  1713. XThis value is
  1714. X.B only
  1715. Xadvisory, and the access method will allocate more memory rather than fail.
  1716. XSince every search examines the root page of the tree, caching the most
  1717. Xrecently used pages substantially improves access time.
  1718. XIn addition, physical writes are delayed as long as possible, so a moderate
  1719. Xcache can reduce the number of I/O operations significantly.
  1720. XObviously, using a cache increases (but only increases) the likelihood of
  1721. Xcorruption or lost data if the system crashes while a tree is being modified.
  1722. XIf
  1723. X.I cachesize
  1724. Xis 0 (no size is specified) a default cache is used.
  1725. X.TP
  1726. Xpsize
  1727. XPage size is the size (in bytes) of the pages used for nodes in the tree.
  1728. XThe minimum page size is 512 bytes and the maximum page size is 64K.
  1729. XIf
  1730. X.I psize
  1731. Xis 0 (no page size is specified) a page size is chosen based on the
  1732. Xunderlying file system I/O block size.
  1733. X.TP
  1734. Xlorder
  1735. XThe byte order for integers in the stored database metadata.
  1736. XThe number should represent the order as an integer; for example, 
  1737. Xbig endian order would be the number 4,321.
  1738. XIf
  1739. X.I lorder
  1740. Xis 0 (no order is specified) the current host order is used.
  1741. X.\" .TP
  1742. X.\" maxkeypage
  1743. X.\" The maximum number of keys which will be stored on any single page.
  1744. X.\" Because of the way the btree data structure works,
  1745. X.\" .I maxkeypage
  1746. X.\" must always be greater than or equal to 2.
  1747. X.\" If
  1748. X.\" .I maxkeypage
  1749. X.\" is 0 (no maximum number of keys is specified) the page fill factor is
  1750. X.\" made as large as possible (which is almost invariably what is wanted).
  1751. X.TP
  1752. Xminkeypage
  1753. XThe minimum number of keys which will be stored on any single page.
  1754. XThis value is used to determine which keys will be stored on overflow
  1755. Xpages, i.e. if a key or data item is longer than the pagesize divided
  1756. Xby the minkeypage value, it will be stored on overflow pages instead
  1757. Xof in the page itself.
  1758. XIf
  1759. X.I minkeypage
  1760. Xis 0 (no minimum number of keys is specified) a value of 2 is used.
  1761. X.TP
  1762. Xcompare
  1763. XCompare is the key comparison function.
  1764. XIt must return an integer less than, equal to, or greater than zero if the
  1765. Xfirst key argument is considered to be respectively less than, equal to,
  1766. Xor greater than the second key argument.
  1767. XThe same comparison function must be used on a given tree every time it
  1768. Xis opened.
  1769. XIf
  1770. X.I compare
  1771. Xis NULL (no comparison function is specified), the keys are compared
  1772. Xlexically, with shorter keys considered less than longer keys.
  1773. X.TP
  1774. Xprefix
  1775. XPrefix is the prefix comparison function.
  1776. XIf specified, this routine must return the number of bytes of the second key
  1777. Xargument which are necessary to determine that it is greater than the first
  1778. Xkey argument.
  1779. XIf the keys are equal, the key length should be returned.
  1780. XNote, the usefulness of this routine is very data dependent, but, in some
  1781. Xdata sets can produce significantly reduced tree sizes and search times.
  1782. XIf
  1783. X.I prefix
  1784. Xis NULL (no prefix function is specified),
  1785. X.B and
  1786. Xno comparison function is specified, a default lexical comparison routine
  1787. Xis used.
  1788. XIf
  1789. X.I prefix
  1790. Xis NULL and a comparison routine is specified, no prefix comparison is
  1791. Xdone.
  1792. X.PP
  1793. XIf the file already exists (and the O_TRUNC flag is not specified), the
  1794. Xvalues specified for the parameters flags, lorder and psize are ignored
  1795. Xin favor of the values used when the tree was created.
  1796. X.PP
  1797. XForward sequential scans of a tree are from the least key to the greatest.
  1798. X.PP
  1799. XSpace freed up by deleting key/data pairs from the tree is never reclaimed,
  1800. Xalthough it is normally made available for reuse.
  1801. XThis means that the btree storage structure is grow-only.
  1802. XThe only solutions are to avoid excessive deletions, or to create a fresh
  1803. Xtree periodically from a scan of an existing one.
  1804. X.PP
  1805. XSearches, insertions, and deletions in a btree will all complete in
  1806. XO lg base N where base is the average fill factor.
  1807. XOften, inserting ordered data into btrees results in a low fill factor.
  1808. XThis implementation has been modified to make ordered insertion the best
  1809. Xcase, resulting in a much better than normal page fill factor.
  1810. X.SH "SEE ALSO"
  1811. X.IR dbopen (3),
  1812. X.IR hash (3),
  1813. X.IR mpool (3),
  1814. X.IR recno (3)
  1815. X.sp
  1816. X.IR "The Ubiquitous B-tree" ,
  1817. XDouglas Comer, ACM Comput. Surv. 11, 2 (June 1979), 121-138.
  1818. X.sp
  1819. X.IR "Prefix B-trees" ,
  1820. XBayer and Unterauer, ACM Transactions on Database Systems, Vol. 2, 1
  1821. X(March 1977), 11-26.
  1822. X.sp
  1823. X.IR "The Art of Computer Programming Vol. 3: Sorting and Searching" , 
  1824. XD.E. Knuth, 1968, pp 471-480.
  1825. X.SH BUGS
  1826. XOnly big and little endian byte order is supported.
  1827. END_OF_FILE
  1828. if test 8129 -ne `wc -c <'man/btree.3'`; then
  1829.     echo shar: \"'man/btree.3'\" unpacked with wrong size!
  1830. fi
  1831. # end of 'man/btree.3'
  1832. fi
  1833. if test -f 'man/mpool.3' -a "${1}" != "-c" ; then 
  1834.   echo shar: Will not clobber existing file \"'man/mpool.3'\"
  1835. else
  1836. echo shar: Extracting \"'man/mpool.3'\" \(6026 characters\)
  1837. sed "s/^X//" >'man/mpool.3' <<'END_OF_FILE'
  1838. X.\" Copyright (c) 1990, 1993
  1839. X.\"    The Regents of the University of California.  All rights reserved.
  1840. X.\"
  1841. X.\" Redistribution and use in source and binary forms, with or without
  1842. X.\" modification, are permitted provided that the following conditions
  1843. X.\" are met:
  1844. X.\" 1. Redistributions of source code must retain the above copyright
  1845. X.\"    notice, this list of conditions and the following disclaimer.
  1846. X.\" 2. Redistributions in binary form must reproduce the above copyright
  1847. X.\"    notice, this list of conditions and the following disclaimer in the
  1848. X.\"    documentation and/or other materials provided with the distribution.
  1849. X.\" 3. All advertising materials mentioning features or use of this software
  1850. X.\"    must display the following acknowledgement:
  1851. X.\"    This product includes software developed by the University of
  1852. X.\"    California, Berkeley and its contributors.
  1853. X.\" 4. Neither the name of the University nor the names of its contributors
  1854. X.\"    may be used to endorse or promote products derived from this software
  1855. X.\"    without specific prior written permission.
  1856. X.\"
  1857. X.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  1858. X.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  1859. X.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  1860. X.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  1861. X.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  1862. X.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  1863. X.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  1864. X.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  1865. X.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  1866. X.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  1867. X.\" SUCH DAMAGE.
  1868. X.\"
  1869. X.\"    @(#)mpool.3    8.1 (Berkeley) 6/4/93
  1870. X.\"
  1871. X.TH MPOOL 3 "June 4, 1993"
  1872. X.UC 7
  1873. X.SH NAME
  1874. Xmpool \- shared memory buffer pool
  1875. X.SH SYNOPSIS
  1876. X.nf
  1877. X.ft B
  1878. X#include <db.h>
  1879. X#include <mpool.h>
  1880. X
  1881. XMPOOL *
  1882. Xmpool_open (DBT *key, int fd, pgno_t pagesize, pgno_t maxcache);
  1883. X
  1884. Xvoid
  1885. Xmpool_filter (MPOOL *mp, void (*pgin)(void *, pgno_t, void *),
  1886. X.ti +5
  1887. Xvoid (*pgout)(void *, pgno_t, void *), void *pgcookie);
  1888. X
  1889. Xvoid *
  1890. Xmpool_new (MPOOL *mp, pgno_t *pgnoaddr);
  1891. X
  1892. Xvoid *
  1893. Xmpool_get (MPOOL *mp, pgno_t pgno, u_int flags);
  1894. X
  1895. Xint
  1896. Xmpool_put (MPOOL *mp, void *pgaddr, u_int flags);
  1897. X
  1898. Xint
  1899. Xmpool_sync (MPOOL *mp);
  1900. X
  1901. Xint
  1902. Xmpool_close (MPOOL *mp);
  1903. X.ft R
  1904. X.fi
  1905. X.SH DESCRIPTION
  1906. X.IR Mpool
  1907. Xis the library interface intended to provide page oriented buffer management
  1908. Xof files.
  1909. XThe buffers may be shared between processes.
  1910. X.PP
  1911. XThe function
  1912. X.I mpool_open
  1913. Xinitializes a memory pool.
  1914. XThe
  1915. X.I key
  1916. Xargument is the byte string used to negotiate between multiple
  1917. Xprocesses wishing to share buffers.
  1918. XIf the file buffers are mapped in shared memory, all processes using
  1919. Xthe same key will share the buffers.
  1920. XIf
  1921. X.I key
  1922. Xis NULL, the buffers are mapped into private memory.
  1923. XThe
  1924. X.I fd
  1925. Xargument is a file descriptor for the underlying file, which must be seekable.
  1926. XIf
  1927. X.I key
  1928. Xis non-NULL and matches a file already being mapped, the
  1929. X.I fd
  1930. Xargument is ignored.
  1931. X.PP
  1932. XThe
  1933. X.I pagesize
  1934. Xargument is the size, in bytes, of the pages into which the file is broken up.
  1935. XThe
  1936. X.I maxcache
  1937. Xargument is the maximum number of pages from the underlying file to cache
  1938. Xat any one time.
  1939. XThis value is not relative to the number of processes which share a file's
  1940. Xbuffers, but will be the largest value specified by any of the processes
  1941. Xsharing the file.
  1942. X.PP
  1943. XThe
  1944. X.I mpool_filter
  1945. Xfunction is intended to make transparent input and output processing of the
  1946. Xpages possible.
  1947. XIf the
  1948. X.I pgin
  1949. Xfunction is specified, it is called each time a buffer is read into the memory
  1950. Xpool from the backing file.
  1951. XIf the
  1952. X.I pgout
  1953. Xfunction is specified, it is called each time a buffer is written into the
  1954. Xbacking file.
  1955. XBoth functions are are called with the
  1956. X.I pgcookie
  1957. Xpointer, the page number and a pointer to the page to being read or written.
  1958. X.PP
  1959. XThe function
  1960. X.I mpool_new
  1961. Xtakes an MPOOL pointer and an address as arguments.
  1962. XIf a new page can be allocated, a pointer to the page is returned and
  1963. Xthe page number is stored into the
  1964. X.I pgnoaddr
  1965. Xaddress.
  1966. XOtherwise, NULL is returned and errno is set.
  1967. X.PP
  1968. XThe function
  1969. X.I mpool_get
  1970. Xtakes a MPOOL pointer and a page number as arguments.
  1971. XIf the page exists, a pointer to the page is returned.
  1972. XOtherwise, NULL is returned and errno is set.
  1973. XThe flags parameter is not currently used.
  1974. X.PP
  1975. XThe function
  1976. X.I mpool_put
  1977. Xunpins the page referenced by
  1978. X.IR pgaddr .
  1979. X.I Pgaddr
  1980. Xmust be an address previously returned by
  1981. X.I mpool_get
  1982. Xor
  1983. X.IR mpool_new .
  1984. XThe flag value is specified by
  1985. X.IR or 'ing
  1986. Xany of the following values:
  1987. X.TP
  1988. XMPOOL_DIRTY
  1989. XThe page has been modified and needs to be written to the backing file.
  1990. X.PP
  1991. X.I Mpool_put
  1992. Xreturns 0 on success and -1 if an error occurs.
  1993. X.PP
  1994. XThe function
  1995. X.I mpool_sync
  1996. Xwrites all modified pages associated with the MPOOL pointer to the
  1997. Xbacking file.
  1998. X.I Mpool_sync
  1999. Xreturns 0 on success and -1 if an error occurs.
  2000. X.PP
  2001. XThe
  2002. X.I mpool_close
  2003. Xfunction free's up any allocated memory associated with the memory pool
  2004. Xcookie.
  2005. XModified pages are
  2006. X.B not
  2007. Xwritten to the backing file.
  2008. X.I Mpool_close
  2009. Xreturns 0 on success and -1 if an error occurs.
  2010. X.SH ERRORS
  2011. XThe
  2012. X.I mpool_open
  2013. Xfunction may fail and set
  2014. X.I errno
  2015. Xfor any of the errors specified for the library routine
  2016. X.IR malloc (3).
  2017. X.PP
  2018. XThe
  2019. X.I mpool_get
  2020. Xfunction may fail and set
  2021. X.I errno
  2022. Xfor the following:
  2023. X.TP 15
  2024. X[EINVAL]
  2025. XThe requested record doesn't exist.
  2026. X.PP
  2027. XThe
  2028. X.I mpool_new
  2029. Xand
  2030. X.I mpool_get
  2031. Xfunctions may fail and set
  2032. X.I errno
  2033. Xfor any of the errors specified for the library routines
  2034. X.IR read (2) ,
  2035. X.IR write (2) ,
  2036. Xand
  2037. X.IR malloc (3).
  2038. X.PP
  2039. XThe
  2040. X.I mpool_sync
  2041. Xfunction may fail and set
  2042. X.I errno
  2043. Xfor any of the errors specified for the library routine
  2044. X.IR write (2).
  2045. X.PP
  2046. XThe
  2047. X.I mpool_close
  2048. Xfunction may fail and set
  2049. X.I errno
  2050. Xfor any of the errors specified for the library routine
  2051. X.IR free (3).
  2052. X.SH "SEE ALSO"
  2053. X.IR dbopen (3),
  2054. X.IR btree (3),
  2055. X.IR hash (3),
  2056. X.IR recno (3)
  2057. END_OF_FILE
  2058. if test 6026 -ne `wc -c <'man/mpool.3'`; then
  2059.     echo shar: \"'man/mpool.3'\" unpacked with wrong size!
  2060. fi
  2061. # end of 'man/mpool.3'
  2062. fi
  2063. if test -f 'man/recno.3' -a "${1}" != "-c" ; then 
  2064.   echo shar: Will not clobber existing file \"'man/recno.3'\"
  2065. else
  2066. echo shar: Extracting \"'man/recno.3'\" \(6295 characters\)
  2067. sed "s/^X//" >'man/recno.3' <<'END_OF_FILE'
  2068. X.\" Copyright (c) 1990, 1993
  2069. X.\"    The Regents of the University of California.  All rights reserved.
  2070. X.\"
  2071. X.\" Redistribution and use in source and binary forms, with or without
  2072. X.\" modification, are permitted provided that the following conditions
  2073. X.\" are met:
  2074. X.\" 1. Redistributions of source code must retain the above copyright
  2075. X.\"    notice, this list of conditions and the following disclaimer.
  2076. X.\" 2. Redistributions in binary form must reproduce the above copyright
  2077. X.\"    notice, this list of conditions and the following disclaimer in the
  2078. X.\"    documentation and/or other materials provided with the distribution.
  2079. X.\" 3. All advertising materials mentioning features or use of this software
  2080. X.\"    must display the following acknowledgement:
  2081. X.\"    This product includes software developed by the University of
  2082. X.\"    California, Berkeley and its contributors.
  2083. X.\" 4. Neither the name of the University nor the names of its contributors
  2084. X.\"    may be used to endorse or promote products derived from this software
  2085. X.\"    without specific prior written permission.
  2086. X.\"
  2087. X.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  2088. X.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  2089. X.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  2090. X.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  2091. X.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  2092. X.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  2093. X.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  2094. X.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  2095. X.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  2096. X.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  2097. X.\" SUCH DAMAGE.
  2098. X.\"
  2099. X.\"    @(#)recno.3    8.1 (Berkeley) 6/4/93
  2100. X.\"
  2101. X.TH RECNO 3 "June 4, 1993"
  2102. X.UC 7
  2103. X.SH NAME
  2104. Xrecno \- record number database access method
  2105. X.SH SYNOPSIS
  2106. X.nf
  2107. X.ft B
  2108. X#include <sys/types.h>
  2109. X#include <db.h>
  2110. X.ft R
  2111. X.fi
  2112. X.SH DESCRIPTION
  2113. XThe routine
  2114. X.IR dbopen
  2115. Xis the library interface to database files.
  2116. XOne of the supported file formats is record number files.
  2117. XThe general description of the database access methods is in
  2118. X.IR dbopen (3),
  2119. Xthis manual page describes only the recno specific information.
  2120. X.PP
  2121. XThe record number data structure is either variable or fixed-length
  2122. Xrecords stored in a flat-file format, accessed by the logical record
  2123. Xnumber.
  2124. XThe existence of record number five implies the existence of records
  2125. Xone through four, and the deletion of record number one causes
  2126. Xrecord number five to be renumbered to record number four, as well
  2127. Xas the cursor, if positioned after record number one, to shift down
  2128. Xone record.
  2129. X.PP
  2130. XThe recno access method specific data structure provided to
  2131. X.I dbopen
  2132. Xis defined in the <db.h> include file as follows:
  2133. X.PP
  2134. Xtypedef struct {
  2135. X.RS
  2136. Xu_char bval;
  2137. X.br
  2138. Xu_int cachesize;
  2139. X.br
  2140. Xindex_t psize;
  2141. X.br
  2142. Xu_long flags;
  2143. X.br
  2144. Xint lorder;
  2145. X.br
  2146. Xsize_t reclen;
  2147. X.br
  2148. Xchar *bfname;
  2149. X.RE
  2150. X} RECNOINFO;
  2151. X.PP
  2152. XThe elements of this structure are defined as follows:
  2153. X.TP
  2154. Xbval
  2155. XThe delimiting byte to be used to mark the end of a record for
  2156. Xvariable-length records, and the pad character for fixed-length
  2157. Xrecords.
  2158. XIf no value is specified, newlines (``\en'') are used to mark the end
  2159. Xof variable-length records and fixed-length records are padded with
  2160. Xspaces.
  2161. X.TP
  2162. Xcachesize
  2163. XA suggested maximum size, in bytes, of the memory cache.
  2164. XThis value is
  2165. X.B only
  2166. Xadvisory, and the access method will allocate more memory rather than fail.
  2167. XIf
  2168. X.I cachesize
  2169. Xis  0 (no size is specified) a default cache is used.
  2170. X.TP
  2171. Xpsize
  2172. XThe recno access method stores the in-memory copies of its records
  2173. Xin a btree.
  2174. XThis value is the size (in bytes) of the pages used for nodes in that tree.
  2175. XIf
  2176. X.I psize
  2177. Xis 0 (no page size is specified) a page size is chosen based on the
  2178. Xunderlying file system I/O block size.
  2179. XSee
  2180. X.IR btree (3)
  2181. Xfor more information.
  2182. X.TP
  2183. Xbfname
  2184. XThe recno access method stores the in-memory copies of its records
  2185. Xin a btree.
  2186. XIf bfname is non-NULL, it specifies the name of the btree file,
  2187. Xas if specified as the file name for a dbopen of a btree file.
  2188. X.TP
  2189. Xflags
  2190. XThe flag value is specified by
  2191. X.IR or 'ing
  2192. Xany of the following values:
  2193. X.RS
  2194. X.TP
  2195. XR_FIXEDLEN
  2196. XThe records are fixed-length, not byte delimited.
  2197. XThe structure element
  2198. X.I reclen
  2199. Xspecifies the length of the record, and the structure element
  2200. X.I bval
  2201. Xis used as the pad character.
  2202. X.TP
  2203. XR_NOKEY
  2204. XIn the interface specified by
  2205. X.IR dbopen ,
  2206. Xthe sequential record retrieval fills in both the caller's key and
  2207. Xdata structures.
  2208. XIf the R_NOKEY flag is specified, the
  2209. X.I cursor
  2210. Xroutines are not required to fill in the key structure.
  2211. XThis permits applications to retrieve records at the end of files without
  2212. Xreading all of the intervening records.
  2213. X.TP
  2214. XR_SNAPSHOT
  2215. XThis flag requires that a snapshot of the file be taken when
  2216. X.I dbopen
  2217. Xis called, instead of permitting any unmodified records to be read from
  2218. Xthe original file.
  2219. X.RE
  2220. X.TP
  2221. Xlorder
  2222. XThe byte order for integers in the stored database metadata.
  2223. XThe number should represent the order as an integer; for example,
  2224. Xbig endian order would be the number 4,321.
  2225. XIf
  2226. X.I lorder
  2227. Xis 0 (no order is specified) the current host order is used.
  2228. X.TP
  2229. Xreclen
  2230. XThe length of a fixed-length record.
  2231. X.PP
  2232. XThe data part of the key/data pair used by the recno access method
  2233. Xis the same as other access methods.
  2234. XThe key is different.
  2235. XThe
  2236. X.I data
  2237. Xfield of the key should be a pointer to a memory location of type
  2238. X.IR recno_t ,
  2239. Xas defined in the <db.h> include file.
  2240. XThis type is normally the largest unsigned integral type available to
  2241. Xthe implementation.
  2242. XThe
  2243. X.I size
  2244. Xfield of the key should be the size of that type.
  2245. X.PP
  2246. XIn the interface specified by
  2247. X.IR dbopen ,
  2248. Xusing the
  2249. X.I put
  2250. Xinterface to create a new record will cause the creation of multiple,
  2251. Xempty records if the record number is more than one greater than the
  2252. Xlargest record currently in the database.
  2253. X.SH "SEE ALSO"
  2254. X.IR dbopen (3),
  2255. X.IR hash (3),
  2256. X.IR mpool (3),
  2257. X.IR recno (3)
  2258. X.sp
  2259. X.IR "Document Processing in a Relational Database System" ,
  2260. XMichael Stonebraker, Heidi Stettner, Joseph Kalash, Antonin Guttman,
  2261. XNadene Lynn, Memorandum No. UCB/ERL M82/32, May 1982.
  2262. X.SH BUGS
  2263. XOnly big and little endian byte order is supported.
  2264. END_OF_FILE
  2265. if test 6295 -ne `wc -c <'man/recno.3'`; then
  2266.     echo shar: \"'man/recno.3'\" unpacked with wrong size!
  2267. fi
  2268. # end of 'man/recno.3'
  2269. fi
  2270. if test -f 'recno/rec_get.c' -a "${1}" != "-c" ; then 
  2271.   echo shar: Will not clobber existing file \"'recno/rec_get.c'\"
  2272. else
  2273. echo shar: Extracting \"'recno/rec_get.c'\" \(6484 characters\)
  2274. sed "s/^X//" >'recno/rec_get.c' <<'END_OF_FILE'
  2275. X/*-
  2276. X * Copyright (c) 1990, 1993
  2277. X *    The Regents of the University of California.  All rights reserved.
  2278. X *
  2279. X * Redistribution and use in source and binary forms, with or without
  2280. X * modification, are permitted provided that the following conditions
  2281. X * are met:
  2282. X * 1. Redistributions of source code must retain the above copyright
  2283. X *    notice, this list of conditions and the following disclaimer.
  2284. X * 2. Redistributions in binary form must reproduce the above copyright
  2285. X *    notice, this list of conditions and the following disclaimer in the
  2286. X *    documentation and/or other materials provided with the distribution.
  2287. X * 3. All advertising materials mentioning features or use of this software
  2288. X *    must display the following acknowledgement:
  2289. X *    This product includes software developed by the University of
  2290. X *    California, Berkeley and its contributors.
  2291. X * 4. Neither the name of the University nor the names of its contributors
  2292. X *    may be used to endorse or promote products derived from this software
  2293. X *    without specific prior written permission.
  2294. X *
  2295. X * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  2296. X * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  2297. X * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  2298. X * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  2299. X * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  2300. X * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  2301. X * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  2302. X * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  2303. X * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  2304. X * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  2305. X * SUCH DAMAGE.
  2306. X */
  2307. X
  2308. X#if defined(LIBC_SCCS) && !defined(lint)
  2309. Xstatic char sccsid[] = "@(#)rec_get.c    8.1 (Berkeley) 6/4/93";
  2310. X#endif /* LIBC_SCCS and not lint */
  2311. X
  2312. X#include <sys/types.h>
  2313. X
  2314. X#include <errno.h>
  2315. X#include <stddef.h>
  2316. X#include <stdio.h>
  2317. X#include <stdlib.h>
  2318. X#include <string.h>
  2319. X#include <unistd.h>
  2320. X
  2321. X#include <db.h>
  2322. X#include "recno.h"
  2323. X
  2324. X/*
  2325. X * __REC_GET -- Get a record from the btree.
  2326. X *
  2327. X * Parameters:
  2328. X *    dbp:    pointer to access method
  2329. X *    key:    key to find
  2330. X *    data:    data to return
  2331. X *    flag:    currently unused
  2332. X *
  2333. X * Returns:
  2334. X *    RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key not found.
  2335. X */
  2336. Xint
  2337. X__rec_get(dbp, key, data, flags)
  2338. X    const DB *dbp;
  2339. X    const DBT *key;
  2340. X    DBT *data;
  2341. X    u_int flags;
  2342. X{
  2343. X    BTREE *t;
  2344. X    EPG *e;
  2345. X    recno_t nrec;
  2346. X    int status;
  2347. X
  2348. X    if (flags || (nrec = *(recno_t *)key->data) == 0) {
  2349. X        errno = EINVAL;
  2350. X        return (RET_ERROR);
  2351. X    }
  2352. X
  2353. X    /*
  2354. X     * If we haven't seen this record yet, try to find it in the
  2355. X     * original file.
  2356. X     */
  2357. X    t = dbp->internal;
  2358. X    if (nrec > t->bt_nrecs) {
  2359. X        if (ISSET(t, R_EOF | R_INMEM))
  2360. X            return (RET_SPECIAL);
  2361. X        if ((status = t->bt_irec(t, nrec)) != RET_SUCCESS)
  2362. X            return (status);
  2363. X    }
  2364. X
  2365. X    --nrec;
  2366. X    if ((e = __rec_search(t, nrec, SEARCH)) == NULL)
  2367. X        return (RET_ERROR);
  2368. X
  2369. X    status = __rec_ret(t, e, 0, NULL, data);
  2370. X    mpool_put(t->bt_mp, e->page, 0);
  2371. X    return (status);
  2372. X}
  2373. X
  2374. X/*
  2375. X * __REC_FPIPE -- Get fixed length records from a pipe.
  2376. X *
  2377. X * Parameters:
  2378. X *    t:    tree
  2379. X *    cnt:    records to read
  2380. X *
  2381. X * Returns:
  2382. X *    RET_ERROR, RET_SUCCESS
  2383. X */
  2384. Xint
  2385. X__rec_fpipe(t, top)
  2386. X    BTREE *t;
  2387. X    recno_t top;
  2388. X{
  2389. X    DBT data;
  2390. X    recno_t nrec;
  2391. X    size_t len;
  2392. X    int ch;
  2393. X    char *p;
  2394. X
  2395. X    data.data = t->bt_dbuf;
  2396. X    data.size = t->bt_reclen;
  2397. X
  2398. X    if (t->bt_dbufsz < t->bt_reclen) {
  2399. X        if ((t->bt_dbuf = realloc(t->bt_dbuf, t->bt_reclen)) == NULL)
  2400. X            return (RET_ERROR);
  2401. X        t->bt_dbufsz = t->bt_reclen;
  2402. X    }
  2403. X    for (nrec = t->bt_nrecs; nrec < top; ++nrec) {
  2404. X        len = t->bt_reclen;
  2405. X        for (p = t->bt_dbuf;; *p++ = ch)
  2406. X            if ((ch = getc(t->bt_rfp)) == EOF || !len--) {
  2407. X                if (__rec_iput(t, nrec, &data, 0)
  2408. X                    != RET_SUCCESS)
  2409. X                    return (RET_ERROR);
  2410. X                break;
  2411. X            }
  2412. X        if (ch == EOF)
  2413. X            break;
  2414. X    }
  2415. X    if (nrec < top) {
  2416. X        SET(t, R_EOF);
  2417. X        return (RET_SPECIAL);
  2418. X    }
  2419. X    return (RET_SUCCESS);
  2420. X}
  2421. X
  2422. X/*
  2423. X * __REC_VPIPE -- Get variable length records from a pipe.
  2424. X *
  2425. X * Parameters:
  2426. X *    t:    tree
  2427. X *    cnt:    records to read
  2428. X *
  2429. X * Returns:
  2430. X *    RET_ERROR, RET_SUCCESS
  2431. X */
  2432. Xint
  2433. X__rec_vpipe(t, top)
  2434. X    BTREE *t;
  2435. X    recno_t top;
  2436. X{
  2437. X    DBT data;
  2438. X    recno_t nrec;
  2439. X    indx_t len;
  2440. X    size_t sz;
  2441. X    int bval, ch;
  2442. X    char *p;
  2443. X
  2444. X    bval = t->bt_bval;
  2445. X    for (nrec = t->bt_nrecs; nrec < top; ++nrec) {
  2446. X        for (p = t->bt_dbuf, sz = t->bt_dbufsz;; *p++ = ch, --sz) {
  2447. X            if ((ch = getc(t->bt_rfp)) == EOF || ch == bval) {
  2448. X                data.data = t->bt_dbuf;
  2449. X                data.size = p - t->bt_dbuf;
  2450. X                if (ch == EOF && data.size == 0)
  2451. X                    break;
  2452. X                if (__rec_iput(t, nrec, &data, 0)
  2453. X                    != RET_SUCCESS)
  2454. X                    return (RET_ERROR);
  2455. X                break;
  2456. X            }
  2457. X            if (sz == 0) {
  2458. X                len = p - t->bt_dbuf;
  2459. X                t->bt_dbufsz += (sz = 256);
  2460. X                if ((t->bt_dbuf =
  2461. X                    realloc(t->bt_dbuf, t->bt_dbufsz)) == NULL)
  2462. X                    return (RET_ERROR);
  2463. X                p = t->bt_dbuf + len;
  2464. X            }
  2465. X        }
  2466. X        if (ch == EOF)
  2467. X            break;
  2468. X    }
  2469. X    if (nrec < top) {
  2470. X        SET(t, R_EOF);
  2471. X        return (RET_SPECIAL);
  2472. X    }
  2473. X    return (RET_SUCCESS);
  2474. X}
  2475. X
  2476. X/*
  2477. X * __REC_FMAP -- Get fixed length records from a file.
  2478. X *
  2479. X * Parameters:
  2480. X *    t:    tree
  2481. X *    cnt:    records to read
  2482. X *
  2483. X * Returns:
  2484. X *    RET_ERROR, RET_SUCCESS
  2485. X */
  2486. Xint
  2487. X__rec_fmap(t, top)
  2488. X    BTREE *t;
  2489. X    recno_t top;
  2490. X{
  2491. X    DBT data;
  2492. X    recno_t nrec;
  2493. X    caddr_t sp, ep;
  2494. X    size_t len;
  2495. X    char *p;
  2496. X
  2497. X    sp = t->bt_cmap;
  2498. X    ep = t->bt_emap;
  2499. X    data.data = t->bt_dbuf;
  2500. X    data.size = t->bt_reclen;
  2501. X
  2502. X    if (t->bt_dbufsz < t->bt_reclen) {
  2503. X        if ((t->bt_dbuf = realloc(t->bt_dbuf, t->bt_reclen)) == NULL)
  2504. X            return (RET_ERROR);
  2505. X        t->bt_dbufsz = t->bt_reclen;
  2506. X    }
  2507. X    for (nrec = t->bt_nrecs; nrec < top; ++nrec) {
  2508. X        if (sp >= ep) {
  2509. X            SET(t, R_EOF);
  2510. X            return (RET_SPECIAL);
  2511. X        }
  2512. X        len = t->bt_reclen;
  2513. X        for (p = t->bt_dbuf; sp < ep && len--; *p++ = *sp++);
  2514. X        memset(p, t->bt_bval, len);
  2515. X        if (__rec_iput(t, nrec, &data, 0) != RET_SUCCESS)
  2516. X            return (RET_ERROR);
  2517. X    }
  2518. X    t->bt_cmap = sp;
  2519. X    return (RET_SUCCESS);
  2520. X}
  2521. X
  2522. X/*
  2523. X * __REC_VMAP -- Get variable length records from a file.
  2524. X *
  2525. X * Parameters:
  2526. X *    t:    tree
  2527. X *    cnt:    records to read
  2528. X *
  2529. X * Returns:
  2530. X *    RET_ERROR, RET_SUCCESS
  2531. X */
  2532. Xint
  2533. X__rec_vmap(t, top)
  2534. X    BTREE *t;
  2535. X    recno_t top;
  2536. X{
  2537. X    DBT data;
  2538. X    caddr_t sp, ep;
  2539. X    recno_t nrec;
  2540. X    int bval;
  2541. X
  2542. X    sp = t->bt_cmap;
  2543. X    ep = t->bt_emap;
  2544. X    bval = t->bt_bval;
  2545. X
  2546. X    for (nrec = t->bt_nrecs; nrec < top; ++nrec) {
  2547. X        if (sp >= ep) {
  2548. X            SET(t, R_EOF);
  2549. X            return (RET_SPECIAL);
  2550. X        }
  2551. X        for (data.data = sp; sp < ep && *sp != bval; ++sp);
  2552. X        data.size = sp - (caddr_t)data.data;
  2553. X        if (__rec_iput(t, nrec, &data, 0) != RET_SUCCESS)
  2554. X            return (RET_ERROR);
  2555. X        ++sp;
  2556. X    }
  2557. X    t->bt_cmap = sp;
  2558. X    return (RET_SUCCESS);
  2559. X}
  2560. END_OF_FILE
  2561. if test 6484 -ne `wc -c <'recno/rec_get.c'`; then
  2562.     echo shar: \"'recno/rec_get.c'\" unpacked with wrong size!
  2563. fi
  2564. # end of 'recno/rec_get.c'
  2565. fi
  2566. if test -f 'recno/rec_open.c' -a "${1}" != "-c" ; then 
  2567.   echo shar: Will not clobber existing file \"'recno/rec_open.c'\"
  2568. else
  2569. echo shar: Extracting \"'recno/rec_open.c'\" \(6244 characters\)
  2570. sed "s/^X//" >'recno/rec_open.c' <<'END_OF_FILE'
  2571. X/*-
  2572. X * Copyright (c) 1990, 1993
  2573. X *    The Regents of the University of California.  All rights reserved.
  2574. X *
  2575. X * This code is derived from software contributed to Berkeley by
  2576. X * Mike Olson.
  2577. X *
  2578. X * Redistribution and use in source and binary forms, with or without
  2579. X * modification, are permitted provided that the following conditions
  2580. X * are met:
  2581. X * 1. Redistributions of source code must retain the above copyright
  2582. X *    notice, this list of conditions and the following disclaimer.
  2583. X * 2. Redistributions in binary form must reproduce the above copyright
  2584. X *    notice, this list of conditions and the following disclaimer in the
  2585. X *    documentation and/or other materials provided with the distribution.
  2586. X * 3. All advertising materials mentioning features or use of this software
  2587. X *    must display the following acknowledgement:
  2588. X *    This product includes software developed by the University of
  2589. X *    California, Berkeley and its contributors.
  2590. X * 4. Neither the name of the University nor the names of its contributors
  2591. X *    may be used to endorse or promote products derived from this software
  2592. X *    without specific prior written permission.
  2593. X *
  2594. X * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  2595. X * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  2596. X * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  2597. X * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  2598. X * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  2599. X * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  2600. X * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  2601. X * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  2602. X * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  2603. X * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  2604. X * SUCH DAMAGE.
  2605. X */
  2606. X
  2607. X#if defined(LIBC_SCCS) && !defined(lint)
  2608. Xstatic char sccsid[] = "@(#)rec_open.c    8.1 (Berkeley) 6/4/93";
  2609. X#endif /* LIBC_SCCS and not lint */
  2610. X
  2611. X#include <sys/types.h>
  2612. X#include <sys/mman.h>
  2613. X#include <sys/stat.h>
  2614. X
  2615. X#include <errno.h>
  2616. X#include <fcntl.h>
  2617. X#include <limits.h>
  2618. X#include <stddef.h>
  2619. X#include <stdio.h>
  2620. X#include <unistd.h>
  2621. X
  2622. X#include <db.h>
  2623. X#include "recno.h"
  2624. X
  2625. XDB *
  2626. X__rec_open(fname, flags, mode, openinfo)
  2627. X    const char *fname;
  2628. X    int flags, mode;
  2629. X    const RECNOINFO *openinfo;
  2630. X{
  2631. X    BTREE *t;
  2632. X    BTREEINFO btopeninfo;
  2633. X    DB *dbp;
  2634. X    PAGE *h;
  2635. X    struct stat sb;
  2636. X    int rfd, sverrno;
  2637. X
  2638. X    /* Open the user's file -- if this fails, we're done. */
  2639. X    if (fname != NULL && (rfd = open(fname, flags, mode)) < 0)
  2640. X        return (NULL);
  2641. X
  2642. X    /* Create a btree in memory (backed by disk). */
  2643. X    dbp = NULL;
  2644. X    if (openinfo) {
  2645. X        if (openinfo->flags & ~(R_FIXEDLEN | R_NOKEY | R_SNAPSHOT))
  2646. X            goto einval;
  2647. X        btopeninfo.flags = 0;
  2648. X        btopeninfo.cachesize = openinfo->cachesize;
  2649. X        btopeninfo.maxkeypage = 0;
  2650. X        btopeninfo.minkeypage = 0;
  2651. X        btopeninfo.psize = openinfo->psize;
  2652. X        btopeninfo.compare = NULL;
  2653. X        btopeninfo.prefix = NULL;
  2654. X        btopeninfo.lorder = openinfo->lorder;
  2655. X        dbp = __bt_open(openinfo->bfname,
  2656. X            O_RDWR, S_IRUSR | S_IWUSR, &btopeninfo);
  2657. X    } else
  2658. X        dbp = __bt_open(NULL, O_RDWR, S_IRUSR | S_IWUSR, NULL);
  2659. X    if (dbp == NULL)
  2660. X        goto err;
  2661. X
  2662. X    /*
  2663. X     * Some fields in the tree structure are recno specific.  Fill them
  2664. X     * in and make the btree structure look like a recno structure.  We
  2665. X     * don't change the bt_ovflsize value, it's close enough and slightly
  2666. X     * bigger.
  2667. X     */
  2668. X    t = dbp->internal;
  2669. X    if (openinfo) {
  2670. X        if (openinfo->flags & R_FIXEDLEN) {
  2671. X            SET(t, R_FIXLEN);
  2672. X            t->bt_reclen = openinfo->reclen;
  2673. X            if (t->bt_reclen == 0)
  2674. X                goto einval;
  2675. X        }
  2676. X        t->bt_bval = openinfo->bval;
  2677. X    } else
  2678. X        t->bt_bval = '\n';
  2679. X
  2680. X    SET(t, R_RECNO);
  2681. X    if (fname == NULL)
  2682. X        SET(t, R_EOF | R_INMEM);
  2683. X    else
  2684. X        t->bt_rfd = rfd;
  2685. X    t->bt_rcursor = 0;
  2686. X
  2687. X    /*
  2688. X     * In 4.4BSD stat(2) returns true for ISSOCK on pipes.  Until
  2689. X     * then, this is fairly close.  Pipes are read-only.
  2690. X     */
  2691. X    if (fname != NULL) {
  2692. X        if (lseek(rfd, (off_t)0, SEEK_CUR) == -1 && errno == ESPIPE) {
  2693. X            switch (flags & O_ACCMODE) {
  2694. X            case O_RDONLY:
  2695. X                SET(t, R_RDONLY);
  2696. X                break;
  2697. X            default:
  2698. X                goto einval;
  2699. X            }
  2700. Xslow:            if ((t->bt_rfp = fdopen(rfd, "r")) == NULL)
  2701. X                goto err;
  2702. X            SET(t, R_CLOSEFP);
  2703. X            t->bt_irec =
  2704. X                ISSET(t, R_FIXLEN) ? __rec_fpipe : __rec_vpipe;
  2705. X        } else {
  2706. X            switch (flags & O_ACCMODE) {
  2707. X            case O_RDONLY:
  2708. X                SET(t, R_RDONLY);
  2709. X                break;
  2710. X            case O_RDWR:
  2711. X                break;
  2712. X            default:
  2713. X                goto einval;
  2714. X            }
  2715. X
  2716. X            if (fstat(rfd, &sb))
  2717. X                goto err;
  2718. X            /*
  2719. X             * Kluge -- we'd like to test to see if the file is too
  2720. X             * big to mmap.  Since, we don't know what size or type
  2721. X             * off_t's or size_t's are, what the largest unsigned
  2722. X             * integral type is, or what random insanity the local
  2723. X             * C compiler will perpetrate, doing the comparison in
  2724. X             * a portable way is flatly impossible.  Hope that mmap
  2725. X             * fails if the file is too large.
  2726. X             */
  2727. X            if (sb.st_size == 0)
  2728. X                SET(t, R_EOF);
  2729. X            else {
  2730. X                t->bt_msize = sb.st_size;
  2731. X                if ((t->bt_smap =
  2732. X                    mmap(NULL, t->bt_msize, PROT_READ, 0, rfd,
  2733. X                    (off_t)0)) == (caddr_t)-1)
  2734. X                    goto slow;
  2735. X                t->bt_cmap = t->bt_smap;
  2736. X                t->bt_emap = t->bt_smap + sb.st_size;
  2737. X                t->bt_irec = ISSET(t, R_FIXLEN) ?
  2738. X                    __rec_fmap : __rec_vmap;
  2739. X                SET(t, R_MEMMAPPED);
  2740. X            }
  2741. X        }
  2742. X    }
  2743. X
  2744. X    /* Use the recno routines. */
  2745. X    dbp->close = __rec_close;
  2746. X    dbp->del = __rec_delete;
  2747. X    dbp->fd = __rec_fd;
  2748. X    dbp->get = __rec_get;
  2749. X    dbp->put = __rec_put;
  2750. X    dbp->seq = __rec_seq;
  2751. X    dbp->sync = __rec_sync;
  2752. X
  2753. X    /* If the root page was created, reset the flags. */
  2754. X    if ((h = mpool_get(t->bt_mp, P_ROOT, 0)) == NULL)
  2755. X        goto err;
  2756. X    if ((h->flags & P_TYPE) == P_BLEAF) {
  2757. X        h->flags = h->flags & ~P_TYPE | P_RLEAF;
  2758. X        mpool_put(t->bt_mp, h, MPOOL_DIRTY);
  2759. X    } else
  2760. X        mpool_put(t->bt_mp, h, 0);
  2761. X
  2762. X    if (openinfo && openinfo->flags & R_SNAPSHOT &&
  2763. X        !ISSET(t, R_EOF | R_INMEM) &&
  2764. X        t->bt_irec(t, MAX_REC_NUMBER) == RET_ERROR)
  2765. X                goto err;
  2766. X    return (dbp);
  2767. X
  2768. Xeinval:    errno = EINVAL;
  2769. Xerr:    sverrno = errno;
  2770. X    if (dbp != NULL)
  2771. X        (void)__bt_close(dbp);
  2772. X    if (fname != NULL)
  2773. X        (void)close(rfd);
  2774. X    errno = sverrno;
  2775. X    return (NULL);
  2776. X}
  2777. X
  2778. Xint
  2779. X__rec_fd(dbp)
  2780. X    const DB *dbp;
  2781. X{
  2782. X    BTREE *t;
  2783. X
  2784. X    t = dbp->internal;
  2785. X
  2786. X    if (ISSET(t, R_INMEM)) {
  2787. X        errno = ENOENT;
  2788. X        return (-1);
  2789. X    }
  2790. X    return (t->bt_rfd);
  2791. X}
  2792. END_OF_FILE
  2793. if test 6244 -ne `wc -c <'recno/rec_open.c'`; then
  2794.     echo shar: \"'recno/rec_open.c'\" unpacked with wrong size!
  2795. fi
  2796. # end of 'recno/rec_open.c'
  2797. fi
  2798. if test -f 'recno/rec_put.c' -a "${1}" != "-c" ; then 
  2799.   echo shar: Will not clobber existing file \"'recno/rec_put.c'\"
  2800. else
  2801. echo shar: Extracting \"'recno/rec_put.c'\" \(6135 characters\)
  2802. sed "s/^X//" >'recno/rec_put.c' <<'END_OF_FILE'
  2803. X/*-
  2804. X * Copyright (c) 1990, 1993
  2805. X *    The Regents of the University of California.  All rights reserved.
  2806. X *
  2807. X * Redistribution and use in source and binary forms, with or without
  2808. X * modification, are permitted provided that the following conditions
  2809. X * are met:
  2810. X * 1. Redistributions of source code must retain the above copyright
  2811. X *    notice, this list of conditions and the following disclaimer.
  2812. X * 2. Redistributions in binary form must reproduce the above copyright
  2813. X *    notice, this list of conditions and the following disclaimer in the
  2814. X *    documentation and/or other materials provided with the distribution.
  2815. X * 3. All advertising materials mentioning features or use of this software
  2816. X *    must display the following acknowledgement:
  2817. X *    This product includes software developed by the University of
  2818. X *    California, Berkeley and its contributors.
  2819. X * 4. Neither the name of the University nor the names of its contributors
  2820. X *    may be used to endorse or promote products derived from this software
  2821. X *    without specific prior written permission.
  2822. X *
  2823. X * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  2824. X * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  2825. X * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  2826. X * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  2827. X * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  2828. X * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  2829. X * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  2830. X * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  2831. X * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  2832. X * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  2833. X * SUCH DAMAGE.
  2834. X */
  2835. X
  2836. X#if defined(LIBC_SCCS) && !defined(lint)
  2837. Xstatic char sccsid[] = "@(#)rec_put.c    8.1 (Berkeley) 6/4/93";
  2838. X#endif /* LIBC_SCCS and not lint */
  2839. X
  2840. X#include <sys/types.h>
  2841. X
  2842. X#include <errno.h>
  2843. X#include <stdio.h>
  2844. X#include <stdlib.h>
  2845. X#include <string.h>
  2846. X
  2847. X#include <db.h>
  2848. X#include "recno.h"
  2849. X
  2850. X/*
  2851. X * __REC_PUT -- Add a recno item to the tree.
  2852. X *
  2853. X * Parameters:
  2854. X *    dbp:    pointer to access method
  2855. X *    key:    key
  2856. X *    data:    data
  2857. X *    flag:    R_CURSOR, R_IAFTER, R_IBEFORE, R_NOOVERWRITE
  2858. X *
  2859. X * Returns:
  2860. X *    RET_ERROR, RET_SUCCESS and RET_SPECIAL if the key is
  2861. X *    already in the tree and R_NOOVERWRITE specified.
  2862. X */
  2863. Xint
  2864. X__rec_put(dbp, key, data, flags)
  2865. X    const DB *dbp;
  2866. X    DBT *key;
  2867. X    const DBT *data;
  2868. X    u_int flags;
  2869. X{
  2870. X    BTREE *t;
  2871. X    DBT tdata;
  2872. X    recno_t nrec;
  2873. X    int status;
  2874. X
  2875. X    t = dbp->internal;
  2876. X
  2877. X    switch (flags) {
  2878. X    case R_CURSOR:
  2879. X        if (!ISSET(t, B_SEQINIT))
  2880. X            goto einval;
  2881. X        nrec = t->bt_rcursor;
  2882. X        break;
  2883. X    case R_SETCURSOR:
  2884. X        if ((nrec = *(recno_t *)key->data) == 0)
  2885. X            goto einval;
  2886. X        break;
  2887. X    case R_IAFTER:
  2888. X        if ((nrec = *(recno_t *)key->data) == 0) {
  2889. X            nrec = 1;
  2890. X            flags = R_IBEFORE;
  2891. X        }
  2892. X        break;
  2893. X    case 0:
  2894. X    case R_IBEFORE:
  2895. X        if ((nrec = *(recno_t *)key->data) == 0)
  2896. X            goto einval;
  2897. X        break;
  2898. X    case R_NOOVERWRITE:
  2899. X        if ((nrec = *(recno_t *)key->data) == 0)
  2900. X            goto einval;
  2901. X        if (nrec <= t->bt_nrecs)
  2902. X            return (RET_SPECIAL);
  2903. X        break;
  2904. X    default:
  2905. Xeinval:        errno = EINVAL;
  2906. X        return (RET_ERROR);
  2907. X    }
  2908. X
  2909. X    /*
  2910. X     * Make sure that records up to and including the put record are
  2911. X     * already in the database.  If skipping records, create empty ones.
  2912. X     */
  2913. X    if (nrec > t->bt_nrecs) {
  2914. X        if (!ISSET(t, R_EOF | R_INMEM) &&
  2915. X            t->bt_irec(t, nrec) == RET_ERROR)
  2916. X            return (RET_ERROR);
  2917. X        if (nrec > t->bt_nrecs + 1) {
  2918. X            tdata.data = NULL;
  2919. X            tdata.size = 0;
  2920. X            while (nrec > t->bt_nrecs + 1)
  2921. X                if (__rec_iput(t,
  2922. X                    t->bt_nrecs, &tdata, 0) != RET_SUCCESS)
  2923. X                    return (RET_ERROR);
  2924. X        }
  2925. X    }
  2926. X
  2927. X    if ((status = __rec_iput(t, nrec - 1, data, flags)) != RET_SUCCESS)
  2928. X        return (status);
  2929. X
  2930. X    if (flags == R_SETCURSOR)
  2931. X        t->bt_rcursor = nrec;
  2932. X    
  2933. X    SET(t, R_MODIFIED);
  2934. X    return (__rec_ret(t, NULL, nrec, key, NULL));
  2935. X}
  2936. X
  2937. X/*
  2938. X * __REC_IPUT -- Add a recno item to the tree.
  2939. X *
  2940. X * Parameters:
  2941. X *    t:    tree
  2942. X *    nrec:    record number
  2943. X *    data:    data
  2944. X *
  2945. X * Returns:
  2946. X *    RET_ERROR, RET_SUCCESS
  2947. X */
  2948. Xint
  2949. X__rec_iput(t, nrec, data, flags)
  2950. X    BTREE *t;
  2951. X    recno_t nrec;
  2952. X    const DBT *data;
  2953. X    u_int flags;
  2954. X{
  2955. X    DBT tdata;
  2956. X    EPG *e;
  2957. X    PAGE *h;
  2958. X    indx_t index, nxtindex;
  2959. X    pgno_t pg;
  2960. X    size_t nbytes;
  2961. X    int dflags, status;
  2962. X    char *dest, db[NOVFLSIZE];
  2963. X
  2964. X    /*
  2965. X     * If the data won't fit on a page, store it on indirect pages.
  2966. X     *
  2967. X     * XXX
  2968. X     * If the insert fails later on, these pages aren't recovered.
  2969. X     */
  2970. X    if (data->size > t->bt_ovflsize) {
  2971. X        if (__ovfl_put(t, data, &pg) == RET_ERROR)
  2972. X            return (RET_ERROR);
  2973. X        tdata.data = db;
  2974. X        tdata.size = NOVFLSIZE;
  2975. X        *(pgno_t *)db = pg;
  2976. X        *(size_t *)(db + sizeof(pgno_t)) = data->size;
  2977. X        dflags = P_BIGDATA;
  2978. X        data = &tdata;
  2979. X    } else
  2980. X        dflags = 0;
  2981. X
  2982. X    /* __rec_search pins the returned page. */
  2983. X    if ((e = __rec_search(t, nrec,
  2984. X        nrec > t->bt_nrecs || flags == R_IAFTER || flags == R_IBEFORE ?
  2985. X        SINSERT : SEARCH)) == NULL)
  2986. X        return (RET_ERROR);
  2987. X
  2988. X    h = e->page;
  2989. X    index = e->index;
  2990. X
  2991. X    /*
  2992. X     * Add the specified key/data pair to the tree.  The R_IAFTER and
  2993. X     * R_IBEFORE flags insert the key after/before the specified key.
  2994. X     *
  2995. X     * Pages are split as required.
  2996. X     */
  2997. X    switch (flags) {
  2998. X    case R_IAFTER:
  2999. X        ++index;
  3000. X        break;
  3001. X    case R_IBEFORE:
  3002. X        break;
  3003. X    default:
  3004. X        if (nrec < t->bt_nrecs &&
  3005. X            __rec_dleaf(t, h, index) == RET_ERROR) {
  3006. X            mpool_put(t->bt_mp, h, 0);
  3007. X            return (RET_ERROR);
  3008. X        }
  3009. X        break;
  3010. X    }
  3011. X
  3012. X    /*
  3013. X     * If not enough room, split the page.  The split code will insert
  3014. X     * the key and data and unpin the current page.  If inserting into
  3015. X     * the offset array, shift the pointers up.
  3016. X     */
  3017. X    nbytes = NRLEAFDBT(data->size);
  3018. X    if (h->upper - h->lower < nbytes + sizeof(indx_t)) {
  3019. X        status = __bt_split(t, h, NULL, data, dflags, nbytes, index);
  3020. X        if (status == RET_SUCCESS)
  3021. X            ++t->bt_nrecs;
  3022. X        return (status);
  3023. X    }
  3024. X
  3025. X    if (index < (nxtindex = NEXTINDEX(h)))
  3026. X        memmove(h->linp + index + 1, h->linp + index,
  3027. X            (nxtindex - index) * sizeof(indx_t));
  3028. X    h->lower += sizeof(indx_t);
  3029. X
  3030. X    h->linp[index] = h->upper -= nbytes;
  3031. X    dest = (char *)h + h->upper;
  3032. X    WR_RLEAF(dest, data, dflags);
  3033. X
  3034. X    ++t->bt_nrecs;
  3035. X    SET(t, B_MODIFIED);
  3036. X    mpool_put(t->bt_mp, h, MPOOL_DIRTY);
  3037. X
  3038. X    return (RET_SUCCESS);
  3039. X}
  3040. END_OF_FILE
  3041. if test 6135 -ne `wc -c <'recno/rec_put.c'`; then
  3042.     echo shar: \"'recno/rec_put.c'\" unpacked with wrong size!
  3043. fi
  3044. # end of 'recno/rec_put.c'
  3045. fi
  3046. echo shar: End of archive 3 \(of 9\).
  3047. cp /dev/null ark3isdone
  3048. MISSING=""
  3049. for I in 1 2 3 4 5 6 7 8 9 ; do
  3050.     if test ! -f ark${I}isdone ; then
  3051.     MISSING="${MISSING} ${I}"
  3052.     fi
  3053. done
  3054. if test "${MISSING}" = "" ; then
  3055.     echo You have unpacked all 9 archives.
  3056.     rm -f ark[1-9]isdone ark[1-9][0-9]isdone
  3057. else
  3058.     echo You still need to unpack the following archives:
  3059.     echo "        " ${MISSING}
  3060. fi
  3061. ##  End of shell archive.
  3062. exit 0
  3063.