source: main/trunk/greenstone2/common-src/packages/gdbm/gdbm-1.8.3/dbminit.c@ 22259

Last change on this file since 22259 was 22259, checked in by davidb, 14 years ago

Tie Makefile and source code in with -DMSDOS so MinGW/MSys compilation works with GDBM

File size: 4.1 KB
Line 
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, by Philip A. Nelson.
5 Copyright (C) 1990, 1991, 1993 Free Software Foundation, Inc.
6
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)
10 any later version.
11
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.
16
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.
20
21 You may contact the author by:
22 e-mail: [email protected]
23 us-mail: Philip A. Nelson
24 Computer Science Department
25 Western Washington University
26 Bellingham, WA 98226
27
28*************************************************************************/
29
30
31/* include system configuration before all else. */
32#include "autoconf.h"
33
34#include "gdbmdefs.h"
35#include "gdbmerrno.h"
36#include "extern.h"
37
38/* Initialize dbm system. FILE is a pointer to the file name. In
39 standard dbm, the database is found in files called FILE.pag and
40 FILE.dir. To make gdbm compatable with dbm using the dbminit call,
41 the same file names are used. Specifically, dbminit will use the file
42 name FILE.pag in its call to gdbm open. If the file (FILE.pag) has a
43 size of zero bytes, a file initialization procedure is performed,
44 setting up the initial structure in the file. Any error detected will
45 cause a return value of -1. No errors cause the return value of 0.
46 NOTE: file.dir will be linked to file.pag. */
47
48int
49dbminit (file)
50 char *file;
51{
52 char* pag_file; /* Used to construct "file.pag". */
53 char* dir_file; /* Used to construct "file.dir". */
54 struct stat dir_stat; /* Stat information for "file.dir". */
55 int ret;
56
57
58 ret = 0; /* Default return value. */
59
60 /* Prepare the correct names of "file.pag" and "file.dir". */
61 pag_file = (char *) malloc (strlen (file)+5);
62 dir_file = (char *) malloc (strlen (file)+5);
63 if ((pag_file == NULL) || (dir_file == NULL))
64 {
65 gdbm_errno = GDBM_MALLOC_ERROR; /* For the hell of it. */
66 return -1;
67 }
68
69 strcpy (pag_file, file);
70 strcat (pag_file, ".pag");
71 strcpy (dir_file, file);
72 strcat (dir_file, ".dir");
73
74 if (_gdbm_file != NULL)
75 gdbm_close (_gdbm_file);
76
77 /* Try to open the file as a writer. DBM never created a file. */
78#ifdef MSDOS
79 /* Not clear if final argument for Windows should be to obtain a
80 file lock or not. Play it safe for now and ask for a lock
81 */
82 _gdbm_file = gdbm_open (pag_file, 0, GDBM_WRITER, 0, NULL, TRUE);
83#else
84 _gdbm_file = gdbm_open (pag_file, 0, GDBM_WRITER, 0, NULL);
85#endif
86
87 /* If it was not opened, try opening it as a reader. */
88 if (_gdbm_file == NULL)
89 {
90#ifdef MSDOS
91 _gdbm_file = gdbm_open (pag_file, 0, GDBM_READER, 0, NULL, TRUE);
92#else
93 _gdbm_file = gdbm_open (pag_file, 0, GDBM_READER, 0, NULL);
94#endif
95 /* Did we successfully open the file? */
96 if (_gdbm_file == NULL)
97 {
98 gdbm_errno = GDBM_FILE_OPEN_ERROR;
99 ret = -1;
100 goto done;
101 }
102 }
103
104 /* If the database is new, link "file.dir" to "file.pag". This is done
105 so the time stamp on both files is the same. */
106 if (stat (dir_file, &dir_stat) == 0)
107 {
108 if (dir_stat.st_size == 0)
109 if (unlink (dir_file) != 0 || link (pag_file, dir_file) != 0)
110 {
111 gdbm_errno = GDBM_FILE_OPEN_ERROR;
112 gdbm_close (_gdbm_file);
113 ret = -1;
114 goto done;
115 }
116 }
117 else
118 {
119 /* Since we can't stat it, we assume it is not there and try
120 to link the dir_file to the pag_file. */
121 if (link (pag_file, dir_file) != 0)
122 {
123 gdbm_errno = GDBM_FILE_OPEN_ERROR;
124 gdbm_close (_gdbm_file);
125 ret = -1;
126 goto done;
127 }
128 }
129
130 ret = 0;
131
132done:
133 free (dir_file);
134 free (pag_file);
135 return ret;
136}
Note: See TracBrowser for help on using the repository browser.