1*7f2fe78bSCy Schubert /*- 2*7f2fe78bSCy Schubert * Copyright (c) 1991, 1993, 2007 3*7f2fe78bSCy Schubert * The Regents of the University of California. All rights reserved. 4*7f2fe78bSCy Schubert * 5*7f2fe78bSCy Schubert * Redistribution and use in source and binary forms, with or without 6*7f2fe78bSCy Schubert * modification, are permitted provided that the following conditions 7*7f2fe78bSCy Schubert * are met: 8*7f2fe78bSCy Schubert * 1. Redistributions of source code must retain the above copyright 9*7f2fe78bSCy Schubert * notice, this list of conditions and the following disclaimer. 10*7f2fe78bSCy Schubert * 2. Redistributions in binary form must reproduce the above copyright 11*7f2fe78bSCy Schubert * notice, this list of conditions and the following disclaimer in the 12*7f2fe78bSCy Schubert * documentation and/or other materials provided with the distribution. 13*7f2fe78bSCy Schubert * 3. All advertising materials mentioning features or use of this software 14*7f2fe78bSCy Schubert * must display the following acknowledgement: 15*7f2fe78bSCy Schubert * This product includes software developed by the University of 16*7f2fe78bSCy Schubert * California, Berkeley and its contributors. 17*7f2fe78bSCy Schubert * 4. Neither the name of the University nor the names of its contributors 18*7f2fe78bSCy Schubert * may be used to endorse or promote products derived from this software 19*7f2fe78bSCy Schubert * without specific prior written permission. 20*7f2fe78bSCy Schubert * 21*7f2fe78bSCy Schubert * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22*7f2fe78bSCy Schubert * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23*7f2fe78bSCy Schubert * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24*7f2fe78bSCy Schubert * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25*7f2fe78bSCy Schubert * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26*7f2fe78bSCy Schubert * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27*7f2fe78bSCy Schubert * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28*7f2fe78bSCy Schubert * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29*7f2fe78bSCy Schubert * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30*7f2fe78bSCy Schubert * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31*7f2fe78bSCy Schubert * SUCH DAMAGE. 32*7f2fe78bSCy Schubert * 33*7f2fe78bSCy Schubert * @(#)compat.h 8.13 (Berkeley) 2/21/94 34*7f2fe78bSCy Schubert */ 35*7f2fe78bSCy Schubert 36*7f2fe78bSCy Schubert #ifndef _DB_INT_H_ 37*7f2fe78bSCy Schubert #define _DB_INT_H_ 38*7f2fe78bSCy Schubert 39*7f2fe78bSCy Schubert #include "config.h" 40*7f2fe78bSCy Schubert #include "db.h" 41*7f2fe78bSCy Schubert 42*7f2fe78bSCy Schubert /* deal with autoconf-based stuff */ 43*7f2fe78bSCy Schubert 44*7f2fe78bSCy Schubert #define DB_LITTLE_ENDIAN 1234 45*7f2fe78bSCy Schubert #define DB_BIG_ENDIAN 4321 46*7f2fe78bSCy Schubert 47*7f2fe78bSCy Schubert #include <stdlib.h> 48*7f2fe78bSCy Schubert #ifdef HAVE_ENDIAN_H 49*7f2fe78bSCy Schubert # include <endian.h> 50*7f2fe78bSCy Schubert #endif 51*7f2fe78bSCy Schubert #ifdef HAVE_MACHINE_ENDIAN_H 52*7f2fe78bSCy Schubert # include <machine/endian.h> 53*7f2fe78bSCy Schubert #endif 54*7f2fe78bSCy Schubert #ifdef HAVE_SYS_PARAM_H 55*7f2fe78bSCy Schubert # include <sys/param.h> 56*7f2fe78bSCy Schubert #endif 57*7f2fe78bSCy Schubert /* Handle both BIG and LITTLE defined and BYTE_ORDER matches one, or 58*7f2fe78bSCy Schubert just one defined; both with and without leading underscores. 59*7f2fe78bSCy Schubert 60*7f2fe78bSCy Schubert Ignore "PDP endian" machines, this code doesn't support them 61*7f2fe78bSCy Schubert anyways. */ 62*7f2fe78bSCy Schubert #if !defined(LITTLE_ENDIAN) && !defined(BIG_ENDIAN) && !defined(BYTE_ORDER) 63*7f2fe78bSCy Schubert # ifdef __LITTLE_ENDIAN__ 64*7f2fe78bSCy Schubert # define LITTLE_ENDIAN __LITTLE_ENDIAN__ 65*7f2fe78bSCy Schubert # endif 66*7f2fe78bSCy Schubert # ifdef __BIG_ENDIAN__ 67*7f2fe78bSCy Schubert # define BIG_ENDIAN __BIG_ENDIAN__ 68*7f2fe78bSCy Schubert # endif 69*7f2fe78bSCy Schubert #endif 70*7f2fe78bSCy Schubert #if !defined(LITTLE_ENDIAN) && !defined(BIG_ENDIAN) && !defined(BYTE_ORDER) 71*7f2fe78bSCy Schubert # ifdef _LITTLE_ENDIAN 72*7f2fe78bSCy Schubert # define LITTLE_ENDIAN _LITTLE_ENDIAN 73*7f2fe78bSCy Schubert # endif 74*7f2fe78bSCy Schubert # ifdef _BIG_ENDIAN 75*7f2fe78bSCy Schubert # define BIG_ENDIAN _BIG_ENDIAN 76*7f2fe78bSCy Schubert # endif 77*7f2fe78bSCy Schubert # ifdef _BYTE_ORDER 78*7f2fe78bSCy Schubert # define BYTE_ORDER _BYTE_ORDER 79*7f2fe78bSCy Schubert # endif 80*7f2fe78bSCy Schubert #endif 81*7f2fe78bSCy Schubert #if !defined(LITTLE_ENDIAN) && !defined(BIG_ENDIAN) && !defined(BYTE_ORDER) 82*7f2fe78bSCy Schubert # ifdef __LITTLE_ENDIAN 83*7f2fe78bSCy Schubert # define LITTLE_ENDIAN __LITTLE_ENDIAN 84*7f2fe78bSCy Schubert # endif 85*7f2fe78bSCy Schubert # ifdef __BIG_ENDIAN 86*7f2fe78bSCy Schubert # define BIG_ENDIAN __BIG_ENDIAN 87*7f2fe78bSCy Schubert # endif 88*7f2fe78bSCy Schubert # ifdef __BYTE_ORDER 89*7f2fe78bSCy Schubert # define BYTE_ORDER __BYTE_ORDER 90*7f2fe78bSCy Schubert # endif 91*7f2fe78bSCy Schubert #endif 92*7f2fe78bSCy Schubert 93*7f2fe78bSCy Schubert #if defined(_MIPSEL) && !defined(LITTLE_ENDIAN) 94*7f2fe78bSCy Schubert # define LITTLE_ENDIAN 95*7f2fe78bSCy Schubert #endif 96*7f2fe78bSCy Schubert #if defined(_MIPSEB) && !defined(BIG_ENDIAN) 97*7f2fe78bSCy Schubert # define BIG_ENDIAN 98*7f2fe78bSCy Schubert #endif 99*7f2fe78bSCy Schubert 100*7f2fe78bSCy Schubert #if defined(LITTLE_ENDIAN) && defined(BIG_ENDIAN) && defined(BYTE_ORDER) 101*7f2fe78bSCy Schubert # if LITTLE_ENDIAN == BYTE_ORDER 102*7f2fe78bSCy Schubert # define DB_BYTE_ORDER DB_LITTLE_ENDIAN 103*7f2fe78bSCy Schubert # elif BIG_ENDIAN == BYTE_ORDER 104*7f2fe78bSCy Schubert # define DB_BYTE_ORDER DB_BIG_ENDIAN 105*7f2fe78bSCy Schubert # else 106*7f2fe78bSCy Schubert # error "LITTLE_ENDIAN and BIG_ENDIAN defined, but can't determine byte order" 107*7f2fe78bSCy Schubert # endif 108*7f2fe78bSCy Schubert #elif defined(LITTLE_ENDIAN) && !defined(BIG_ENDIAN) 109*7f2fe78bSCy Schubert # define DB_BYTE_ORDER DB_LITTLE_ENDIAN 110*7f2fe78bSCy Schubert #elif defined(BIG_ENDIAN) && !defined(LITTLE_ENDIAN) 111*7f2fe78bSCy Schubert # define DB_BYTE_ORDER DB_BIG_ENDIAN 112*7f2fe78bSCy Schubert #else 113*7f2fe78bSCy Schubert # error "can't determine byte order from included system headers" 114*7f2fe78bSCy Schubert #endif 115*7f2fe78bSCy Schubert 116*7f2fe78bSCy Schubert #if 0 117*7f2fe78bSCy Schubert #ifdef WORDS_BIGENDIAN 118*7f2fe78bSCy Schubert #define DB_BYTE_ORDER DB_BIG_ENDIAN 119*7f2fe78bSCy Schubert #else 120*7f2fe78bSCy Schubert #define DB_BYTE_ORDER DB_LITTLE_ENDIAN 121*7f2fe78bSCy Schubert #endif 122*7f2fe78bSCy Schubert #endif 123*7f2fe78bSCy Schubert 124*7f2fe78bSCy Schubert /* end autoconf-based stuff */ 125*7f2fe78bSCy Schubert 126*7f2fe78bSCy Schubert /* include necessary system header files */ 127*7f2fe78bSCy Schubert 128*7f2fe78bSCy Schubert #ifdef HAVE_UNISTD_H 129*7f2fe78bSCy Schubert #include <unistd.h> 130*7f2fe78bSCy Schubert #endif 131*7f2fe78bSCy Schubert #include <limits.h> 132*7f2fe78bSCy Schubert #include <fcntl.h> 133*7f2fe78bSCy Schubert #include <stdio.h> 134*7f2fe78bSCy Schubert #include <errno.h> 135*7f2fe78bSCy Schubert #include <stdint.h> 136*7f2fe78bSCy Schubert #include <sys/types.h> 137*7f2fe78bSCy Schubert #include <sys/stat.h> 138*7f2fe78bSCy Schubert #include <sys/param.h> 139*7f2fe78bSCy Schubert 140*7f2fe78bSCy Schubert /* types and constants used for database structure */ 141*7f2fe78bSCy Schubert 142*7f2fe78bSCy Schubert #define MAX_PAGE_NUMBER 0xffffffff /* >= # of pages in a file */ 143*7f2fe78bSCy Schubert typedef u_int32_t db_pgno_t; 144*7f2fe78bSCy Schubert #define MAX_PAGE_OFFSET 65535 /* >= # of bytes in a page */ 145*7f2fe78bSCy Schubert typedef u_int16_t indx_t; 146*7f2fe78bSCy Schubert #define MAX_REC_NUMBER 0xffffffff /* >= # of records in a tree */ 147*7f2fe78bSCy Schubert typedef u_int32_t recno_t; 148*7f2fe78bSCy Schubert 149*7f2fe78bSCy Schubert /* 150*7f2fe78bSCy Schubert * Little endian <==> big endian 32-bit swap macros. 151*7f2fe78bSCy Schubert * M_32_SWAP swap a memory location 152*7f2fe78bSCy Schubert * P_32_SWAP swap a referenced memory location 153*7f2fe78bSCy Schubert * P_32_COPY swap from one location to another 154*7f2fe78bSCy Schubert */ 155*7f2fe78bSCy Schubert #define M_32_SWAP(a) { \ 156*7f2fe78bSCy Schubert u_int32_t _tmp = a; \ 157*7f2fe78bSCy Schubert ((char *)&a)[0] = ((char *)&_tmp)[3]; \ 158*7f2fe78bSCy Schubert ((char *)&a)[1] = ((char *)&_tmp)[2]; \ 159*7f2fe78bSCy Schubert ((char *)&a)[2] = ((char *)&_tmp)[1]; \ 160*7f2fe78bSCy Schubert ((char *)&a)[3] = ((char *)&_tmp)[0]; \ 161*7f2fe78bSCy Schubert } 162*7f2fe78bSCy Schubert #define P_32_SWAP(a) { \ 163*7f2fe78bSCy Schubert char _tmp[4]; \ 164*7f2fe78bSCy Schubert _tmp[0] = ((char *)a)[0]; \ 165*7f2fe78bSCy Schubert _tmp[1] = ((char *)a)[1]; \ 166*7f2fe78bSCy Schubert _tmp[2] = ((char *)a)[2]; \ 167*7f2fe78bSCy Schubert _tmp[3] = ((char *)a)[3]; \ 168*7f2fe78bSCy Schubert ((char *)a)[0] = _tmp[3]; \ 169*7f2fe78bSCy Schubert ((char *)a)[1] = _tmp[2]; \ 170*7f2fe78bSCy Schubert ((char *)a)[2] = _tmp[1]; \ 171*7f2fe78bSCy Schubert ((char *)a)[3] = _tmp[0]; \ 172*7f2fe78bSCy Schubert } 173*7f2fe78bSCy Schubert #define P_32_COPY(a, b) { \ 174*7f2fe78bSCy Schubert ((char *)&(b))[0] = ((char *)&(a))[3]; \ 175*7f2fe78bSCy Schubert ((char *)&(b))[1] = ((char *)&(a))[2]; \ 176*7f2fe78bSCy Schubert ((char *)&(b))[2] = ((char *)&(a))[1]; \ 177*7f2fe78bSCy Schubert ((char *)&(b))[3] = ((char *)&(a))[0]; \ 178*7f2fe78bSCy Schubert } 179*7f2fe78bSCy Schubert 180*7f2fe78bSCy Schubert /* 181*7f2fe78bSCy Schubert * Little endian <==> big endian 16-bit swap macros. 182*7f2fe78bSCy Schubert * M_16_SWAP swap a memory location 183*7f2fe78bSCy Schubert * P_16_SWAP swap a referenced memory location 184*7f2fe78bSCy Schubert * P_16_COPY swap from one location to another 185*7f2fe78bSCy Schubert */ 186*7f2fe78bSCy Schubert #define M_16_SWAP(a) { \ 187*7f2fe78bSCy Schubert u_int16_t _tmp = a; \ 188*7f2fe78bSCy Schubert ((char *)&a)[0] = ((char *)&_tmp)[1]; \ 189*7f2fe78bSCy Schubert ((char *)&a)[1] = ((char *)&_tmp)[0]; \ 190*7f2fe78bSCy Schubert } 191*7f2fe78bSCy Schubert #define P_16_SWAP(a) { \ 192*7f2fe78bSCy Schubert char _tmp[2]; \ 193*7f2fe78bSCy Schubert _tmp[0] = ((char *)a)[0]; \ 194*7f2fe78bSCy Schubert _tmp[1] = ((char *)a)[1]; \ 195*7f2fe78bSCy Schubert ((char *)a)[0] = _tmp[1]; \ 196*7f2fe78bSCy Schubert ((char *)a)[1] = _tmp[0]; \ 197*7f2fe78bSCy Schubert } 198*7f2fe78bSCy Schubert #define P_16_COPY(a, b) { \ 199*7f2fe78bSCy Schubert ((char *)&(b))[0] = ((char *)&(a))[1]; \ 200*7f2fe78bSCy Schubert ((char *)&(b))[1] = ((char *)&(a))[0]; \ 201*7f2fe78bSCy Schubert } 202*7f2fe78bSCy Schubert 203*7f2fe78bSCy Schubert /* open functions for each database type, used in dbopen() */ 204*7f2fe78bSCy Schubert 205*7f2fe78bSCy Schubert #define __bt_open __kdb2_bt_open 206*7f2fe78bSCy Schubert #define __hash_open __kdb2_hash_open 207*7f2fe78bSCy Schubert #define __rec_open __kdb2_rec_open 208*7f2fe78bSCy Schubert #define __dbpanic __kdb2_dbpanic 209*7f2fe78bSCy Schubert 210*7f2fe78bSCy Schubert DB *__bt_open __P((const char *, int, int, const BTREEINFO *, int)); 211*7f2fe78bSCy Schubert DB *__hash_open __P((const char *, int, int, const HASHINFO *, int)); 212*7f2fe78bSCy Schubert DB *__rec_open __P((const char *, int, int, const RECNOINFO *, int)); 213*7f2fe78bSCy Schubert void __dbpanic __P((DB *dbp)); 214*7f2fe78bSCy Schubert 215*7f2fe78bSCy Schubert /* 216*7f2fe78bSCy Schubert * There is no portable way to figure out the maximum value of a file 217*7f2fe78bSCy Schubert * offset, so we put it here. 218*7f2fe78bSCy Schubert */ 219*7f2fe78bSCy Schubert #ifdef OFF_T_MAX 220*7f2fe78bSCy Schubert #define DB_OFF_T_MAX OFF_T_MAX 221*7f2fe78bSCy Schubert #else 222*7f2fe78bSCy Schubert #define DB_OFF_T_MAX LONG_MAX 223*7f2fe78bSCy Schubert #endif 224*7f2fe78bSCy Schubert 225*7f2fe78bSCy Schubert #ifndef O_ACCMODE /* POSIX 1003.1 access mode mask. */ 226*7f2fe78bSCy Schubert #define O_ACCMODE (O_RDONLY|O_WRONLY|O_RDWR) 227*7f2fe78bSCy Schubert #endif 228*7f2fe78bSCy Schubert 229*7f2fe78bSCy Schubert /* 230*7f2fe78bSCy Schubert * If you can't provide lock values in the open(2) call. Note, this 231*7f2fe78bSCy Schubert * allows races to happen. 232*7f2fe78bSCy Schubert */ 233*7f2fe78bSCy Schubert #ifndef O_EXLOCK /* 4.4BSD extension. */ 234*7f2fe78bSCy Schubert #define O_EXLOCK 0 235*7f2fe78bSCy Schubert #endif 236*7f2fe78bSCy Schubert 237*7f2fe78bSCy Schubert #ifndef O_SHLOCK /* 4.4BSD extension. */ 238*7f2fe78bSCy Schubert #define O_SHLOCK 0 239*7f2fe78bSCy Schubert #endif 240*7f2fe78bSCy Schubert 241*7f2fe78bSCy Schubert #ifndef EFTYPE 242*7f2fe78bSCy Schubert #define EFTYPE EINVAL /* POSIX 1003.1 format errno. */ 243*7f2fe78bSCy Schubert #endif 244*7f2fe78bSCy Schubert 245*7f2fe78bSCy Schubert #ifndef STDERR_FILENO 246*7f2fe78bSCy Schubert #define STDIN_FILENO 0 /* ANSI C #defines */ 247*7f2fe78bSCy Schubert #define STDOUT_FILENO 1 248*7f2fe78bSCy Schubert #define STDERR_FILENO 2 249*7f2fe78bSCy Schubert #endif 250*7f2fe78bSCy Schubert 251*7f2fe78bSCy Schubert #ifndef SEEK_END 252*7f2fe78bSCy Schubert #define SEEK_SET 0 /* POSIX 1003.1 seek values */ 253*7f2fe78bSCy Schubert #define SEEK_CUR 1 254*7f2fe78bSCy Schubert #define SEEK_END 2 255*7f2fe78bSCy Schubert #endif 256*7f2fe78bSCy Schubert 257*7f2fe78bSCy Schubert #ifndef NULL /* ANSI C #defines NULL everywhere. */ 258*7f2fe78bSCy Schubert #define NULL 0 259*7f2fe78bSCy Schubert #endif 260*7f2fe78bSCy Schubert 261*7f2fe78bSCy Schubert #ifndef MAX /* Usually found in <sys/param.h>. */ 262*7f2fe78bSCy Schubert #define MAX(_a,_b) ((_a)<(_b)?(_b):(_a)) 263*7f2fe78bSCy Schubert #endif 264*7f2fe78bSCy Schubert #ifndef MIN /* Usually found in <sys/param.h>. */ 265*7f2fe78bSCy Schubert #define MIN(_a,_b) ((_a)<(_b)?(_a):(_b)) 266*7f2fe78bSCy Schubert #endif 267*7f2fe78bSCy Schubert 268*7f2fe78bSCy Schubert #ifndef S_ISDIR /* POSIX 1003.1 file type tests. */ 269*7f2fe78bSCy Schubert #define S_ISDIR(m) ((m & 0170000) == 0040000) /* directory */ 270*7f2fe78bSCy Schubert #define S_ISCHR(m) ((m & 0170000) == 0020000) /* char special */ 271*7f2fe78bSCy Schubert #define S_ISBLK(m) ((m & 0170000) == 0060000) /* block special */ 272*7f2fe78bSCy Schubert #define S_ISREG(m) ((m & 0170000) == 0100000) /* regular file */ 273*7f2fe78bSCy Schubert #define S_ISFIFO(m) ((m & 0170000) == 0010000) /* fifo */ 274*7f2fe78bSCy Schubert #endif 275*7f2fe78bSCy Schubert #ifndef S_ISLNK /* BSD POSIX 1003.1 extensions */ 276*7f2fe78bSCy Schubert #define S_ISLNK(m) ((m & 0170000) == 0120000) /* symbolic link */ 277*7f2fe78bSCy Schubert #define S_ISSOCK(m) ((m & 0170000) == 0140000) /* socket */ 278*7f2fe78bSCy Schubert #endif 279*7f2fe78bSCy Schubert 280*7f2fe78bSCy Schubert #ifndef O_BINARY 281*7f2fe78bSCy Schubert #define O_BINARY 0 /* Needed for Win32 compiles */ 282*7f2fe78bSCy Schubert #endif 283*7f2fe78bSCy Schubert #endif /* _DB_INT_H_ */ 284