xref: /openbsd-src/usr.sbin/amd/amd/info_ndbm.c (revision 29295d1c5caf158d3a732746704292e9a80a32d2)
1 /*
2  * Copyright (c) 1989 Jan-Simon Pendry
3  * Copyright (c) 1989 Imperial College of Science, Technology & Medicine
4  * Copyright (c) 1989, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Jan-Simon Pendry at Imperial College, London.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  *	from: @(#)info_ndbm.c	8.1 (Berkeley) 6/6/93
35  *	$Id: info_ndbm.c,v 1.4 2003/06/02 23:36:51 millert Exp $
36  */
37 
38 /*
39  * Get info from NDBM map
40  */
41 
42 #include "am.h"
43 
44 #ifdef HAS_NDBM_MAPS
45 
46 #include <ndbm.h>
47 #include <fcntl.h>
48 #include <sys/stat.h>
49 
50 static int
search_ndbm(DBM * db,char * key,char ** val)51 search_ndbm(DBM *db, char *key, char **val)
52 {
53 	datum k, v;
54 
55 	k.dptr = key;
56 	k.dsize = strlen(key) + 1;
57 	v = dbm_fetch(db, k);
58 	if (v.dptr) {
59 		*val = strdup(v.dptr);
60 		return 0;
61 	}
62 	return ENOENT;
63 }
64 
65 int
ndbm_search(mnt_map * m,char * map,char * key,char ** pval,time_t * tp)66 ndbm_search(mnt_map *m, char *map, char *key, char **pval, time_t *tp)
67 {
68 	DBM *db;
69 
70 	db = dbm_open(map, O_RDONLY, 0);
71 	if (db) {
72 		struct stat stb;
73 		int error;
74 
75 		error = fstat(dbm_pagfno(db), &stb);
76 		if (!error && *tp < stb.st_mtime) {
77 			*tp = stb.st_mtime;
78 			error = -1;
79 		} else {
80 			error = search_ndbm(db, key, pval);
81 		}
82 		(void) dbm_close(db);
83 		return error;
84 	}
85 
86 	return errno;
87 }
88 
89 int
ndbm_init(char * map,time_t * tp)90 ndbm_init(char *map, time_t *tp)
91 {
92 	DBM *db;
93 
94 	db = dbm_open(map, O_RDONLY, 0);
95 	if (db) {
96 		struct stat stb;
97 
98 		if (fstat(dbm_pagfno(db), &stb) < 0)
99 			*tp = clocktime();
100 		else
101 			*tp = stb.st_mtime;
102 		dbm_close(db);
103 		return 0;
104 	}
105 
106 	return errno;
107 }
108 
109 #endif /* HAS_NDBM_MAPS */
110