home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / prgramer / rcs / sources / dbmopen.c < prev    next >
C/C++ Source or Header  |  1992-01-19  |  5KB  |  137 lines

  1. /* dbmopen.c - Open the file for dbm operations. This looks like the
  2.    NDBM interface for dbm use. */
  3.  
  4. /*  This file is part of GDBM, the GNU data base manager, by Philip A. Nelson.
  5.     Copyright (C) 1990, 1991  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 1, 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:  phil@cs.wwu.edu
  23.       us-mail:  Philip A. Nelson
  24.                 Computer Science Department
  25.                 Western Washington University
  26.                 Bellingham, WA 98226
  27.         phone:  (206) 676-3035
  28.  
  29. *************************************************************************/
  30.  
  31.  
  32. #include "gdbmdefs.h"
  33. #include "gdbmerrno.h"
  34. #include "extern.h"
  35.  
  36. extern gdbm_error gdbm_errno;
  37.  
  38. /* Initialize ndbm 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 ignored and will always have a size of zero.
  47.    FLAGS and MODE are the same as the open (2) call.  This call will
  48.    look at the FLAGS and decide what call to make to gdbm_open.  For
  49.    FLAGS == O_RDONLY, it will be a GDBM_READER, if FLAGS == O_RDWR|O_CREAT,
  50.    it will be a GDBM_WRCREAT (creater and writer) and if the FLAGS == O_RDWR,
  51.    it will be a GDBM_WRITER and if FLAGS contain O_TRUNC then it will be
  52.    a GDBM_NEWDB.  All other values of FLAGS in the flags are
  53.    ignored. */
  54.  
  55. gdbm_file_info *
  56. dbm_open (file, flags, mode)
  57.      char *file;
  58.      int flags;
  59.      int mode;
  60. {
  61.   char* pag_file;        /* Used to construct "file.pag". */
  62.   char* dir_file;        /* Used to construct "file.dir". */
  63.   struct stat dir_stat;        /* Stat information for "file.dir". */
  64.   gdbm_file_info *temp_dbf;  /* Temporary file pointer storage. */
  65.  
  66.  
  67.   /* Prepare the correct names of "file.pag" and "file.dir". */
  68.   pag_file = (char *) alloca (strlen (file)+5);
  69.   dir_file = (char *) alloca (strlen (file)+5);
  70.  
  71.   strcpy (pag_file, file);
  72.   strcat (pag_file, ".pag");
  73.   strcpy (dir_file, file);
  74.   strcat (dir_file, ".dir");
  75.  
  76.  
  77.   /* Call the actual routine, saving the pointer to the file information. */
  78.   flags &= O_RDONLY | O_RDWR | O_CREAT | O_TRUNC;
  79.   if (flags == O_RDONLY)
  80.     {
  81.       temp_dbf = gdbm_open (pag_file, 0, GDBM_READER, 0, NULL);
  82.     }
  83.   else if (flags == (O_RDWR | O_CREAT))
  84.     {
  85.       temp_dbf = gdbm_open (pag_file, 0, GDBM_WRCREAT, mode, NULL);
  86.     }
  87.   else if ( (flags & O_TRUNC) == O_TRUNC)
  88.     {
  89.       temp_dbf = gdbm_open (pag_file, 0, GDBM_NEWDB, mode, NULL);
  90.     }
  91.   else
  92.     {
  93.       temp_dbf = gdbm_open (pag_file, 0, GDBM_WRITER, 0, NULL);
  94.     }
  95.  
  96.   /* Did we successfully open the file? */
  97.   if (temp_dbf == NULL)
  98.     {
  99.       gdbm_errno = GDBM_FILE_OPEN_ERROR;
  100.       return NULL;
  101.     }
  102.  
  103.   /* If the database is new, link "file.dir" to "file.pag". This is done
  104.      so the time stamp on both files is the same. */
  105.   if (stat (dir_file, &dir_stat) == 0)
  106.     {
  107.       if (dir_stat.st_size == 0)
  108. #ifdef OS2
  109.         if ( 0 /* open (dir_file, O_RDWR|O_TRUNC, S_IREAD|S_IWRITE) < 0 */ )
  110. #else /* not OS2 */
  111.     if (unlink (dir_file) != 0 || link (pag_file, dir_file) != 0)
  112. #endif /* not OS2 */
  113.       {
  114.         gdbm_errno = GDBM_FILE_OPEN_ERROR;
  115.         gdbm_close (temp_dbf);
  116.         return NULL;
  117.       }
  118.     }
  119.   else
  120.     {
  121.       /* Since we can't stat it, we assume it is not there and try
  122.          to link the dir_file to the pag_file. */
  123. #ifdef OS2
  124.       if ( 0 /* open (dir_file, O_RDWR|O_CREAT, S_IREAD|S_IWRITE) < 0 */ )
  125. #else /* not OS2 */
  126.       if (link (pag_file, dir_file) != 0)
  127. #endif /* not OS2 */
  128.     {
  129.       gdbm_errno = GDBM_FILE_OPEN_ERROR;
  130.       gdbm_close (temp_dbf);
  131.       return NULL;
  132.     }
  133.     }
  134.  
  135.   return temp_dbf;
  136. }
  137.