Imported Upstream version 1.10
[platform/upstream/gdbm.git] / compat / dbminit.c
1 /* dbminit.c - Open the file for dbm operations. This looks like the
2    DBM interface. */
3
4 /* This file is part of GDBM, the GNU data base manager.
5    Copyright (C) 1990, 1991, 1993, 2007, 2011 Free Software Foundation,
6    Inc.
7
8    GDBM is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3, or (at your option)
11    any later version.
12
13    GDBM is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with GDBM. If not, see <http://www.gnu.org/licenses/>.   */
20
21 /* Include system configuration before all else. */
22 #include "autoconf.h"
23 #include "dbm-priv.h"
24
25 DBM *_gdbm_file;
26
27 /* Initialize dbm system.  FILE is a pointer to the file name.  In
28    standard dbm, the database is found in files called FILE.pag and
29    FILE.dir.  To make gdbm compatable with dbm using the dbminit call,
30    the same file names are used.  Specifically, dbminit will use the file
31    name FILE.pag in its call to gdbm open.  If the file (FILE.pag) has a
32    size of zero bytes, a file initialization procedure is performed,
33    setting up the initial structure in the file.  Any error detected will
34    cause a return value of -1.  No errors cause the return value of 0.
35    NOTE: file.dir will be linked to file.pag. */
36
37 int
38 dbminit (char *file)
39 {
40   if (_gdbm_file != NULL)
41     dbm_close (_gdbm_file);
42   /* Try to open the file as a writer.  DBM never created a file. */
43   _gdbm_file = dbm_open (file, O_RDWR, 0644);
44   /* If it was not opened, try opening it as a reader. */
45   if (_gdbm_file == NULL)
46     {
47       _gdbm_file = dbm_open (file, O_RDONLY, 0644);
48       /* Did we successfully open the file? */
49       if (_gdbm_file == NULL)
50         {
51           gdbm_errno = GDBM_FILE_OPEN_ERROR;
52           return -1;
53         }
54     }
55   return 0;
56 }