"Fossies" - the Fresh Open Source Software Archive

Member "rgdbm-2.1.42/make.c" (20 Jun 2007, 3626 Bytes) of package /linux/privat/old/rgdbm-2.1.42.tgz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "make.c" see the Fossies "Dox" file reference documentation.

    1 #include "config.h"
    2 
    3 #if STDC_HEADERS
    4 # include <stdlib.h>
    5 # include <stdarg.h>
    6 # include <string.h>
    7 #else
    8 # ifndef HAVE_STRCHR
    9 #  define strchr index
   10 #  define strrchr rindex
   11 # endif
   12 char *strchr (), *strrchr ();
   13 # ifndef HAVE_MEMCPY
   14 #  define memcpy(d, s, n) bcopy ((s), (d), (n))
   15 #  define memmove(d, s, n) bcopy ((s), (d), (n))
   16 # endif
   17 #endif          /* STDC_HEADERS */
   18 
   19 #include <stdio.h>              /* for NULL */
   20 #include <sys/stat.h>           /* for stat struct */
   21 #include <errno.h>              /* for EINVAL etc. */
   22 
   23 #ifdef HAVE_GDBM_H         /* won't get far without gdbm.h */
   24 # include <gdbm.h>
   25 #elif defined(HAVE_NDBM_H) /* worth checking for ndbm too */
   26 # include <ndbm.h>
   27 #elif defined(HAVE_DBM_H)  /* even worth checking for dbm */
   28 # include <dbm.h>
   29 #endif
   30 
   31 #include "str.h"
   32 #include "files.h"
   33 #include "gdbmd.h"
   34 #include "make.h"
   35 
   36 /* 
   37  * Return 1 if @path1 is newer than @path2 by at least @tdiff seconds
   38  * Or if path2 does not exist and path1 does.
   39  *
   40  * Return 0 if path1 does not exist. Or path1 exists but is not newer
   41  * than path2 * by at least tdiff seconds. 
   42  */
   43 static int
   44 cnewer (char *path1, char *path2, int tdiff)
   45 {
   46 
   47     struct stat stat1, stat2;
   48 
   49     if (stat (path1, &stat1) < 0)
   50         return 0;
   51 
   52     if (stat (path2, &stat2) < 0)
   53         return 1;
   54 
   55     if (stat1.st_mtime > stat2.st_mtime + tdiff)
   56         return 1;
   57 
   58     return 0;
   59 }
   60 
   61 /*
   62  * load the files database from the files.txt file
   63  */
   64 static int
   65 loadtxt (void)
   66 {
   67 
   68     FILE *f;
   69     static char *filestxt;
   70     static char *files;
   71     struct stat fle;
   72     int err;
   73 #ifdef HAVE_GDBM_H
   74     GDBM_FILE flf;
   75 #elif defined(HAVE_NDBM_H)
   76     DBM* flf;
   77 #elif defined(HAVE_DBM_H)
   78     int flf;
   79 #endif
   80 
   81     if (!filestxt) {
   82         filestxt = catstr (rgdbm_ctrldir, "/" GDBMD_FILES_TXT, NULL);
   83         if (!filestxt)
   84             return -ENOMEM;     // give up quietly
   85     }
   86 
   87     if (!files) {
   88         files = catstr (rgdbm_ctrldir, "/" GDBMD_FILES, NULL);
   89         if (!files)
   90             return -ENOMEM;
   91     }
   92 
   93     f = fopen (filestxt, "r");
   94 
   95     if (!f)
   96         return -EINVAL;         // give up quietly
   97 
   98 #ifdef HAVE_GDBM_H
   99     flf = gdbm_open (files, 1024, GDBM_WRCREAT, 0640, NULL);
  100 #elif defined(HAVE_NDBM_H)
  101     flf = dbm_open (files, O_RDWR|O_CREAT, 0640);
  102 #elif defined(HAVE_DBM_H)
  103     flf = (dbminit (files) >= 0);
  104 #endif
  105     if (!flf) {
  106         fclose (f);
  107         return -EINVAL;
  108     }
  109 
  110     while (1) {
  111 
  112         char *name;
  113         char *buf;
  114 
  115         memset (&fle, 0, sizeof (fle));
  116 
  117         buf = fgettxt(f, &fle);
  118         if (!buf)
  119             break;
  120 
  121         name = catstr (rgdbm_datadir, "/", buf, NULL);
  122         free (buf);
  123 
  124         if (name) {
  125             err = fsetflnam ((void *)flf, name, &fle);
  126             free (name);
  127             if (err < 0) {
  128                 // shrug. Best effort.
  129             }
  130         }
  131     }
  132 
  133     fclose (f);
  134 #ifdef HAVE_GDBM_H
  135     gdbm_close (flf);
  136 #elif defined(HAVE_NDBM_H)
  137     dbm_close (flf);
  138 #elif defined(HAVE_DBM_H)
  139     dbmclose ();
  140 #endif
  141     return 0;
  142 }
  143 
  144 
  145 /*
  146  * build the files database  from the files.txt ascii text file, if the
  147  * latter is newer (by 10s or more)
  148  */
  149 int
  150 make (void)
  151 {
  152 
  153     char *files = catstr (rgdbm_ctrldir, "/" GDBMD_FILES, NULL);
  154     char *filestxt = catstr (rgdbm_ctrldir, "/" GDBMD_FILES_TXT, NULL);
  155 
  156     if (!files || !filestxt) {
  157         if (files)
  158             free (files);
  159         if (filestxt)
  160             free (filestxt);
  161         return -ENOMEM;
  162     }
  163     if (cnewer (filestxt, files, 10)) {
  164         // text is newer
  165         free (files);
  166         free (filestxt);
  167         return loadtxt ();
  168     }
  169     free (files);
  170     free (filestxt);
  171     return 0;
  172 }
  173