1
/* dbminit.c - Open the file for dbm operations. This looks like the
4
/* This file is part of GDBM, the GNU data base manager, by Philip A. Nelson.
5
Copyright (C) 1990, 1991, 1993 Free Software Foundation, Inc.
7
GDBM is free software; you can redistribute it and/or modify
8
it under the terms of the GNU General Public License as published by
9
the Free Software Foundation; either version 2, or (at your option)
12
GDBM is distributed in the hope that it will be useful,
13
but WITHOUT ANY WARRANTY; without even the implied warranty of
14
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
GNU General Public License for more details.
17
You should have received a copy of the GNU General Public License
18
along with GDBM; see the file COPYING. If not, write to
19
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
21
You may contact the author by:
22
e-mail: phil@cs.wwu.edu
23
us-mail: Philip A. Nelson
24
Computer Science Department
25
Western Washington University
28
*************************************************************************/
31
/* AIX demands this be the very first thing in the file. */
32
#if !defined(__GNUC__) && defined(_AIX)
36
/* include system configuration before all else. */
40
#include "gdbmerrno.h"
43
/* Initialize dbm system. FILE is a pointer to the file name. In
44
standard dbm, the database is found in files called FILE.pag and
45
FILE.dir. To make gdbm compatable with dbm using the dbminit call,
46
the same file names are used. Specifically, dbminit will use the file
47
name FILE.pag in its call to gdbm open. If the file (FILE.pag) has a
48
size of zero bytes, a file initialization procedure is performed,
49
setting up the initial structure in the file. Any error detected will
50
cause a return value of -1. No errors cause the return value of 0.
51
NOTE: file.dir will be linked to file.pag. */
57
char* pag_file; /* Used to construct "file.pag". */
58
char* dir_file; /* Used to construct "file.dir". */
59
struct stat dir_stat; /* Stat information for "file.dir". */
62
/* Prepare the correct names of "file.pag" and "file.dir". */
63
pag_file = (char *) alloca (strlen (file)+5);
64
dir_file = (char *) alloca (strlen (file)+5);
66
strcpy (pag_file, file);
67
strcat (pag_file, ".pag");
68
strcpy (dir_file, file);
69
strcat (dir_file, ".dir");
71
if (_gdbm_file != NULL)
72
gdbm_close (_gdbm_file);
74
/* Try to open the file as a writer. DBM never created a file. */
75
_gdbm_file = gdbm_open (pag_file, 0, GDBM_WRITER, 0, NULL);
77
/* If it was not opened, try opening it as a reader. */
78
if (_gdbm_file == NULL)
80
_gdbm_file = gdbm_open (pag_file, 0, GDBM_READER, 0, NULL);
82
/* Did we successfully open the file? */
83
if (_gdbm_file == NULL)
85
gdbm_errno = GDBM_FILE_OPEN_ERROR;
90
/* If the database is new, link "file.dir" to "file.pag". This is done
91
so the time stamp on both files is the same. */
92
if (stat (dir_file, &dir_stat) == 0)
94
if (dir_stat.st_size == 0)
95
if (unlink (dir_file) != 0 || link (pag_file, dir_file) != 0)
97
gdbm_errno = GDBM_FILE_OPEN_ERROR;
98
gdbm_close (_gdbm_file);
104
/* Since we can't stat it, we assume it is not there and try
105
to link the dir_file to the pag_file. */
106
if (link (pag_file, dir_file) != 0)
108
gdbm_errno = GDBM_FILE_OPEN_ERROR;
109
gdbm_close (_gdbm_file);