138494Sobrien/*
2310490Scy * Copyright (c) 1997-2014 Erez Zadok
338494Sobrien * Copyright (c) 1989 Jan-Simon Pendry
438494Sobrien * Copyright (c) 1989 Imperial College of Science, Technology & Medicine
538494Sobrien * Copyright (c) 1989 The Regents of the University of California.
638494Sobrien * All rights reserved.
738494Sobrien *
838494Sobrien * This code is derived from software contributed to Berkeley by
938494Sobrien * Jan-Simon Pendry at Imperial College, London.
1038494Sobrien *
1138494Sobrien * Redistribution and use in source and binary forms, with or without
1238494Sobrien * modification, are permitted provided that the following conditions
1338494Sobrien * are met:
1438494Sobrien * 1. Redistributions of source code must retain the above copyright
1538494Sobrien *    notice, this list of conditions and the following disclaimer.
1638494Sobrien * 2. Redistributions in binary form must reproduce the above copyright
1738494Sobrien *    notice, this list of conditions and the following disclaimer in the
1838494Sobrien *    documentation and/or other materials provided with the distribution.
19310490Scy * 3. Neither the name of the University nor the names of its contributors
2038494Sobrien *    may be used to endorse or promote products derived from this software
2138494Sobrien *    without specific prior written permission.
2238494Sobrien *
2338494Sobrien * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2438494Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2538494Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2638494Sobrien * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2738494Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2838494Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2938494Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3038494Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3138494Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3238494Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3338494Sobrien * SUCH DAMAGE.
3438494Sobrien *
3538494Sobrien *
36174294Sobrien * File: am-utils/amd/info_ndbm.c
3738494Sobrien *
3838494Sobrien */
3938494Sobrien
4038494Sobrien/*
4138494Sobrien * Get info from NDBM map
4238494Sobrien */
4338494Sobrien
4438494Sobrien#ifdef HAVE_CONFIG_H
4538494Sobrien# include <config.h>
4638494Sobrien#endif /* HAVE_CONFIG_H */
4738494Sobrien#include <am_defs.h>
4838494Sobrien#include <amd.h>
49310490Scy#include <sun_map.h>
5038494Sobrien
5138494Sobrien/* forward declarations */
5238494Sobrienint ndbm_init(mnt_map *m, char *map, time_t *tp);
5338494Sobrienint ndbm_mtime(mnt_map *m, char *map, time_t *tp);
5438494Sobrienint ndbm_search(mnt_map *m, char *map, char *key, char **pval, time_t *tp);
5538494Sobrien
5638494Sobrien
5738494Sobrienstatic int
58310490Scysearch_ndbm(mnt_map *m, DBM *db, char *key, char **val)
5938494Sobrien{
6038494Sobrien  datum k, v;
6138494Sobrien
6238494Sobrien  k.dptr = key;
6338494Sobrien  k.dsize = strlen(key) + 1;
6438494Sobrien  v = dbm_fetch(db, k);
6538494Sobrien  if (v.dptr) {
66310490Scy    if (m->cfm && (m->cfm->cfm_flags & CFM_SUN_MAP_SYNTAX))
67310490Scy      *val = sun_entry2amd(key, v.dptr);
68310490Scy    else
69310490Scy      *val = xstrdup(v.dptr);
7038494Sobrien    return 0;
7138494Sobrien  }
7238494Sobrien  return ENOENT;
7338494Sobrien}
7438494Sobrien
7538494Sobrien
7638494Sobrienint
7738494Sobrienndbm_search(mnt_map *m, char *map, char *key, char **pval, time_t *tp)
7838494Sobrien{
7938494Sobrien  DBM *db;
8038494Sobrien
8138494Sobrien  db = dbm_open(map, O_RDONLY, 0);
8238494Sobrien  if (db) {
8338494Sobrien    struct stat stb;
8438494Sobrien    int error;
8538494Sobrien#ifdef DBM_SUFFIX
8638494Sobrien    char dbfilename[256];
8738494Sobrien
88174294Sobrien    xstrlcpy(dbfilename, map, sizeof(dbfilename));
89174294Sobrien    xstrlcat(dbfilename, DBM_SUFFIX, sizeof(dbfilename));
9038494Sobrien    error = stat(dbfilename, &stb);
9138494Sobrien#else /* not DBM_SUFFIX */
9238494Sobrien    error = fstat(dbm_pagfno(db), &stb);
9338494Sobrien#endif /* not DBM_SUFFIX */
9438494Sobrien    if (!error && *tp < stb.st_mtime) {
9538494Sobrien      *tp = stb.st_mtime;
9638494Sobrien      error = -1;
9738494Sobrien    } else {
98310490Scy      error = search_ndbm(m, db, key, pval);
9938494Sobrien    }
10038494Sobrien    (void) dbm_close(db);
10138494Sobrien    return error;
10238494Sobrien  }
10338494Sobrien  return errno;
10438494Sobrien}
10538494Sobrien
10638494Sobrien
10738494Sobrienint
10838494Sobrienndbm_init(mnt_map *m, char *map, time_t *tp)
10938494Sobrien{
11038494Sobrien  DBM *db;
11138494Sobrien
11238494Sobrien  db = dbm_open(map, O_RDONLY, 0);
11338494Sobrien  if (db) {
11438494Sobrien    struct stat stb;
11538494Sobrien    int error;
11638494Sobrien#ifdef DBM_SUFFIX
11738494Sobrien    char dbfilename[256];
11838494Sobrien
119174294Sobrien    xstrlcpy(dbfilename, map, sizeof(dbfilename));
120174294Sobrien    xstrlcat(dbfilename, DBM_SUFFIX, sizeof(dbfilename));
12138494Sobrien    error = stat(dbfilename, &stb);
12238494Sobrien#else /* not DBM_SUFFIX */
12338494Sobrien    error = fstat(dbm_pagfno(db), &stb);
12438494Sobrien#endif /* not DBM_SUFFIX */
12538494Sobrien    if (error < 0)
126174294Sobrien      *tp = clocktime(NULL);
12738494Sobrien    else
12838494Sobrien      *tp = stb.st_mtime;
12938494Sobrien    dbm_close(db);
13038494Sobrien    return 0;
13138494Sobrien  }
13238494Sobrien  return errno;
13338494Sobrien}
13438494Sobrien
13538494Sobrien
13638494Sobrienint
13738494Sobrienndbm_mtime(mnt_map *m, char *map, time_t *tp)
13838494Sobrien{
13938494Sobrien  return ndbm_init(m,map, tp);
14038494Sobrien}
141