xref: /csrg-svn/old/libndbm/dbm.c (revision 38117)
123772Sbloom /*
2*38117Sbostic  * Copyright (c) 1985 The Regents of the University of California.
3*38117Sbostic  * All rights reserved.
4*38117Sbostic  *
5*38117Sbostic  * Redistribution and use in source and binary forms are permitted
6*38117Sbostic  * provided that the above copyright notice and this paragraph are
7*38117Sbostic  * duplicated in all such forms and that any documentation,
8*38117Sbostic  * advertising materials, and other materials related to such
9*38117Sbostic  * distribution and use acknowledge that the software was developed
10*38117Sbostic  * by the University of California, Berkeley.  The name of the
11*38117Sbostic  * University may not be used to endorse or promote products derived
12*38117Sbostic  * from this software without specific prior written permission.
13*38117Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14*38117Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15*38117Sbostic  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1623772Sbloom  */
1723772Sbloom 
1823760Skre #ifndef lint
19*38117Sbostic static char sccsid[] = "@(#)dbm.c	5.4 (Berkeley) 05/24/89";
20*38117Sbostic #endif /* not lint */
2123760Skre 
2223760Skre #include	"dbm.h"
2323760Skre 
2423760Skre #define	NODB	((DBM *)0)
2523760Skre 
2623760Skre static DBM *cur_db = NODB;
2723760Skre 
2823760Skre static char no_db[] = "dbm: no open database\n";
2923760Skre 
3023760Skre dbminit(file)
3123760Skre 	char *file;
3223760Skre {
3323760Skre 	if (cur_db != NODB)
3423760Skre 		dbm_close(cur_db);
3523760Skre 
3623760Skre 	cur_db = dbm_open(file, 2, 0);
3723760Skre 	if (cur_db == NODB) {
3823760Skre 		cur_db = dbm_open(file, 0, 0);
3924292Skre 		if (cur_db == NODB)
4024292Skre 			return (-1);
4123760Skre 	}
4223760Skre 	return (0);
4323760Skre }
4423760Skre 
4523760Skre long
4623760Skre forder(key)
4723760Skre datum key;
4823760Skre {
4923760Skre 	if (cur_db == NODB) {
5023760Skre 		printf(no_db);
5123760Skre 		return (0L);
5223760Skre 	}
5323760Skre 	return (dbm_forder(cur_db, key));
5423760Skre }
5523760Skre 
5623760Skre datum
5723760Skre fetch(key)
5823760Skre datum key;
5923760Skre {
6023760Skre 	datum item;
6123760Skre 
6223760Skre 	if (cur_db == NODB) {
6323760Skre 		printf(no_db);
6423760Skre 		item.dptr = 0;
6523760Skre 		return (item);
6623760Skre 	}
6723760Skre 	return (dbm_fetch(cur_db, key));
6823760Skre }
6923760Skre 
7023760Skre delete(key)
7123760Skre datum key;
7223760Skre {
7323760Skre 	if (cur_db == NODB) {
7423760Skre 		printf(no_db);
7523760Skre 		return (-1);
7623760Skre 	}
7723760Skre 	if (dbm_rdonly(cur_db))
7823760Skre 		return (-1);
7923760Skre 	return (dbm_delete(cur_db, key));
8023760Skre }
8123760Skre 
8223760Skre store(key, dat)
8323760Skre datum key, dat;
8423760Skre {
8523760Skre 	if (cur_db == NODB) {
8623760Skre 		printf(no_db);
8723760Skre 		return (-1);
8823760Skre 	}
8923760Skre 	if (dbm_rdonly(cur_db))
9023760Skre 		return (-1);
9123760Skre 
9223760Skre 	return (dbm_store(cur_db, key, dat, DBM_REPLACE));
9323760Skre }
9423760Skre 
9523760Skre datum
9623760Skre firstkey()
9723760Skre {
9823760Skre 	datum item;
9923760Skre 
10023760Skre 	if (cur_db == NODB) {
10123760Skre 		printf(no_db);
10223760Skre 		item.dptr = 0;
10323760Skre 		return (item);
10423760Skre 	}
10523760Skre 	return (dbm_firstkey(cur_db));
10623760Skre }
10723760Skre 
10823760Skre datum
10923760Skre nextkey(key)
11023760Skre datum key;
11123760Skre {
11223760Skre 	datum item;
11323760Skre 
11423760Skre 	if (cur_db == NODB) {
11523760Skre 		printf(no_db);
11623760Skre 		item.dptr = 0;
11723760Skre 		return (item);
11823760Skre 	}
11923760Skre 	return (dbm_nextkey(cur_db, key));
12023760Skre }
121