xref: /openbsd/lib/libc/db/db/db.c (revision e20a56a5)
1*e20a56a5Sotto /*	$OpenBSD: db.c,v 1.9 2005/03/23 19:34:59 otto Exp $	*/
21b727fc6Smillert 
3df930be7Sderaadt /*-
4df930be7Sderaadt  * Copyright (c) 1991, 1993
5df930be7Sderaadt  *	The Regents of the University of California.  All rights reserved.
6df930be7Sderaadt  *
7df930be7Sderaadt  * Redistribution and use in source and binary forms, with or without
8df930be7Sderaadt  * modification, are permitted provided that the following conditions
9df930be7Sderaadt  * are met:
10df930be7Sderaadt  * 1. Redistributions of source code must retain the above copyright
11df930be7Sderaadt  *    notice, this list of conditions and the following disclaimer.
12df930be7Sderaadt  * 2. Redistributions in binary form must reproduce the above copyright
13df930be7Sderaadt  *    notice, this list of conditions and the following disclaimer in the
14df930be7Sderaadt  *    documentation and/or other materials provided with the distribution.
156580fee3Smillert  * 3. Neither the name of the University nor the names of its contributors
16df930be7Sderaadt  *    may be used to endorse or promote products derived from this software
17df930be7Sderaadt  *    without specific prior written permission.
18df930be7Sderaadt  *
19df930be7Sderaadt  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20df930be7Sderaadt  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21df930be7Sderaadt  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22df930be7Sderaadt  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23df930be7Sderaadt  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24df930be7Sderaadt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25df930be7Sderaadt  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26df930be7Sderaadt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27df930be7Sderaadt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28df930be7Sderaadt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29df930be7Sderaadt  * SUCH DAMAGE.
30df930be7Sderaadt  */
31df930be7Sderaadt 
32df930be7Sderaadt #if defined(LIBC_SCCS) && !defined(lint)
331b727fc6Smillert #if 0
341b727fc6Smillert static char sccsid[] = "@(#)db.c	8.4 (Berkeley) 2/21/94";
351b727fc6Smillert #else
36*e20a56a5Sotto static const char rcsid[] = "$OpenBSD: db.c,v 1.9 2005/03/23 19:34:59 otto Exp $";
371b727fc6Smillert #endif
38df930be7Sderaadt #endif /* LIBC_SCCS and not lint */
39df930be7Sderaadt 
40df930be7Sderaadt #include <sys/types.h>
41df930be7Sderaadt 
42df930be7Sderaadt #include <errno.h>
43df930be7Sderaadt #include <fcntl.h>
44df930be7Sderaadt #include <stddef.h>
45df930be7Sderaadt #include <stdio.h>
46df930be7Sderaadt 
47df930be7Sderaadt #include <db.h>
48df930be7Sderaadt 
49b7fd35b8Sderaadt static int __dberr(void);
50b7fd35b8Sderaadt 
51df930be7Sderaadt DB *
52*e20a56a5Sotto dbopen(const char *fname, int flags, int mode, DBTYPE type,
53*e20a56a5Sotto     const void *openinfo)
54df930be7Sderaadt {
55df930be7Sderaadt 
56df930be7Sderaadt #define	DB_FLAGS	(DB_LOCK | DB_SHMEM | DB_TXN)
57df930be7Sderaadt #define	USE_OPEN_FLAGS							\
58ce037c02Smillert 	(O_CREAT | O_EXCL | O_EXLOCK | O_NOFOLLOW | O_NONBLOCK | 	\
59ce037c02Smillert 	 O_RDONLY | O_RDWR | O_SHLOCK | O_SYNC | O_TRUNC)
60df930be7Sderaadt 
61df930be7Sderaadt 	if ((flags & ~(USE_OPEN_FLAGS | DB_FLAGS)) == 0)
62df930be7Sderaadt 		switch (type) {
63df930be7Sderaadt 		case DB_BTREE:
64df930be7Sderaadt 			return (__bt_open(fname, flags & USE_OPEN_FLAGS,
65df930be7Sderaadt 			    mode, openinfo, flags & DB_FLAGS));
66df930be7Sderaadt 		case DB_HASH:
67df930be7Sderaadt 			return (__hash_open(fname, flags & USE_OPEN_FLAGS,
68df930be7Sderaadt 			    mode, openinfo, flags & DB_FLAGS));
69df930be7Sderaadt 		case DB_RECNO:
70df930be7Sderaadt 			return (__rec_open(fname, flags & USE_OPEN_FLAGS,
71df930be7Sderaadt 			    mode, openinfo, flags & DB_FLAGS));
72df930be7Sderaadt 		}
73df930be7Sderaadt 	errno = EINVAL;
74df930be7Sderaadt 	return (NULL);
75df930be7Sderaadt }
76df930be7Sderaadt 
77df930be7Sderaadt static int
78b7fd35b8Sderaadt __dberr(void)
79df930be7Sderaadt {
80df930be7Sderaadt 	return (RET_ERROR);
81df930be7Sderaadt }
82df930be7Sderaadt 
83df930be7Sderaadt /*
84df930be7Sderaadt  * __DBPANIC -- Stop.
85df930be7Sderaadt  *
86df930be7Sderaadt  * Parameters:
87df930be7Sderaadt  *	dbp:	pointer to the DB structure.
88df930be7Sderaadt  */
89df930be7Sderaadt void
90*e20a56a5Sotto __dbpanic(DB *dbp)
91df930be7Sderaadt {
92df930be7Sderaadt 	/* The only thing that can succeed is a close. */
93df95a199Smillert 	dbp->del = (int (*)(const struct __db *, const DBT*, u_int))__dberr;
94df95a199Smillert 	dbp->fd = (int (*)(const struct __db *))__dberr;
95df95a199Smillert 	dbp->get = (int (*)(const struct __db *, const DBT*, DBT *, u_int))__dberr;
96df95a199Smillert 	dbp->put = (int (*)(const struct __db *, DBT *, const DBT *, u_int))__dberr;
97df95a199Smillert 	dbp->seq = (int (*)(const struct __db *, DBT *, DBT *, u_int))__dberr;
98df95a199Smillert 	dbp->sync = (int (*)(const struct __db *, u_int))__dberr;
99df930be7Sderaadt }
100