xref: /openbsd/lib/libc/db/db/db.c (revision 181ba704)
1*181ba704Sguenther /*	$OpenBSD: db.c,v 1.13 2015/09/05 11:28:35 guenther 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 #include <sys/types.h>
33df930be7Sderaadt 
34df930be7Sderaadt #include <errno.h>
35df930be7Sderaadt #include <fcntl.h>
36df930be7Sderaadt #include <stddef.h>
37df930be7Sderaadt #include <stdio.h>
38df930be7Sderaadt 
39df930be7Sderaadt #include <db.h>
40df930be7Sderaadt 
41b7fd35b8Sderaadt static int __dberr(void);
42b7fd35b8Sderaadt 
43df930be7Sderaadt DB *
dbopen(const char * fname,int flags,int mode,DBTYPE type,const void * openinfo)44e20a56a5Sotto dbopen(const char *fname, int flags, int mode, DBTYPE type,
45e20a56a5Sotto     const void *openinfo)
46df930be7Sderaadt {
47df930be7Sderaadt 
48df930be7Sderaadt #define	DB_FLAGS	(DB_LOCK | DB_SHMEM | DB_TXN)
49df930be7Sderaadt #define	USE_OPEN_FLAGS							\
50ce037c02Smillert 	(O_CREAT | O_EXCL | O_EXLOCK | O_NOFOLLOW | O_NONBLOCK | 	\
517870f2a0Sguenther 	 O_ACCMODE | O_SHLOCK | O_SYNC | O_TRUNC)
52df930be7Sderaadt 
53dc519336Sguenther 	if (((flags & O_ACCMODE) == O_RDONLY || (flags & O_ACCMODE) == O_RDWR)
547870f2a0Sguenther 	    && (flags & ~(USE_OPEN_FLAGS | DB_FLAGS)) == 0)
55df930be7Sderaadt 		switch (type) {
56df930be7Sderaadt 		case DB_BTREE:
57df930be7Sderaadt 			return (__bt_open(fname, flags & USE_OPEN_FLAGS,
58df930be7Sderaadt 			    mode, openinfo, flags & DB_FLAGS));
59df930be7Sderaadt 		case DB_HASH:
60df930be7Sderaadt 			return (__hash_open(fname, flags & USE_OPEN_FLAGS,
61df930be7Sderaadt 			    mode, openinfo, flags & DB_FLAGS));
62df930be7Sderaadt 		case DB_RECNO:
63df930be7Sderaadt 			return (__rec_open(fname, flags & USE_OPEN_FLAGS,
64df930be7Sderaadt 			    mode, openinfo, flags & DB_FLAGS));
65df930be7Sderaadt 		}
66df930be7Sderaadt 	errno = EINVAL;
67df930be7Sderaadt 	return (NULL);
68df930be7Sderaadt }
69*181ba704Sguenther DEF_WEAK(dbopen);
70df930be7Sderaadt 
71df930be7Sderaadt static int
__dberr(void)72b7fd35b8Sderaadt __dberr(void)
73df930be7Sderaadt {
74df930be7Sderaadt 	return (RET_ERROR);
75df930be7Sderaadt }
76df930be7Sderaadt 
77df930be7Sderaadt /*
78df930be7Sderaadt  * __DBPANIC -- Stop.
79df930be7Sderaadt  *
80df930be7Sderaadt  * Parameters:
81df930be7Sderaadt  *	dbp:	pointer to the DB structure.
82df930be7Sderaadt  */
83df930be7Sderaadt void
__dbpanic(DB * dbp)84e20a56a5Sotto __dbpanic(DB *dbp)
85df930be7Sderaadt {
86df930be7Sderaadt 	/* The only thing that can succeed is a close. */
87df95a199Smillert 	dbp->del = (int (*)(const struct __db *, const DBT*, u_int))__dberr;
88df95a199Smillert 	dbp->fd = (int (*)(const struct __db *))__dberr;
89df95a199Smillert 	dbp->get = (int (*)(const struct __db *, const DBT*, DBT *, u_int))__dberr;
90df95a199Smillert 	dbp->put = (int (*)(const struct __db *, DBT *, const DBT *, u_int))__dberr;
91df95a199Smillert 	dbp->seq = (int (*)(const struct __db *, DBT *, DBT *, u_int))__dberr;
92df95a199Smillert 	dbp->sync = (int (*)(const struct __db *, u_int))__dberr;
93df930be7Sderaadt }
94