home *** CD-ROM | disk | FTP | other *** search
/ The Unsorted BBS Collection / thegreatunsorted.tar / thegreatunsorted / hacking / internet / desc02.sh / enddesc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-02-04  |  5.7 KB  |  281 lines

  1. /* enddesc.c -    Initialise/deallocate description database
  2.  *
  3.  * Copyright (c) 1991, 1992 Tim Cook.
  4.  * Non-profit distribution allowed.  See README for details.
  5.  */
  6.  
  7. static char rcsid[] = "$Id: enddesc.c,v 1.4 1993/01/29 04:17:43 tim Exp tim $";
  8.  
  9. #include <sys/param.h>
  10. #include <sys/errno.h>
  11. #include "config.h"
  12.  
  13. #ifdef TEST
  14. #include <stdio.h>
  15. #endif
  16.  
  17. #ifndef MAXPATHLEN
  18. #define MAXPATHLEN    1024
  19. #endif
  20.  
  21. extern int errno ;
  22. #ifdef NDBM
  23. DBM *_desc_database = (DBM *) NULL ;
  24. #define DB_OPEN        (_desc_database != (DBM *) NULL)
  25. #else
  26. static int _db_open = FALSE ;
  27. #define DB_OPEN        _db_open
  28. #endif
  29.  
  30.  
  31. VOID enddesc ()
  32. {
  33. #ifdef NDBM
  34.    if (_desc_database)
  35.       dbm_close (_desc_database) ;
  36. #else
  37.    dbmclose () ;
  38. #endif
  39.    }
  40.  
  41.  
  42. /*
  43.  * This is support routine for getdesc() and setdesc(), and should not
  44.  * normally be called by the programmer. 
  45.  */
  46.  
  47. int _initdesc (directory, open_flags)
  48.    char *directory ;
  49.    int open_flags ;
  50. {
  51.    static char desc_file[MAXPATHLEN+1] = "" ;
  52.    static int db_flags ;
  53.  
  54.    if (! directory || *directory == EOS)
  55.       directory = "." ;
  56.  
  57.    if (open_flags == db_flags && strcmp (directory, desc_file) == 0)
  58.       return DB_OPEN ;        /* Tell 'em what we said before */
  59.  
  60.    /* Otherwise, we need to try to open a database */
  61.    {
  62.       char *p ;
  63. #ifndef NDBM
  64.       char *q ;
  65. #include <sys/stat.h>
  66.       struct stat status ;
  67. #endif
  68.  
  69.       DBM_close (_desc_database) ;
  70.  
  71.       strcpy (desc_file, directory) ;
  72.       db_flags = open_flags ;
  73.  
  74.       /* Find end of "desc_file" */
  75.       for (p = desc_file ; *p != EOS ; p++) ;
  76.  
  77. #ifdef NDBM
  78.  
  79.       /* By crikey, it's easier this way! */
  80.  
  81.       strcpy (p, "/.desc") ;
  82.  
  83. #ifdef TEST
  84.       printf ("Opening new database\n") ;
  85. #endif
  86.       if ((_desc_database = dbm_open (desc_file, db_flags, 0666))
  87.                == (DBM *) NULL) {
  88.      return FALSE ; }
  89.  
  90. #else    /* NDBM */
  91.  
  92.       _db_open = FALSE ;    /* We just closed it above */
  93.  
  94.       strcpy (p, "/.desc.pag") ;
  95.       q = strrchr (p, '.') ;
  96.  
  97.       if (stat (desc_file, &status) != -1) {
  98.  
  99.      /* Database exists */
  100.  
  101.      *q = EOS ;        /* Hide ".pag" */
  102.      if (dbminit (desc_file) < 0) {
  103.         return FALSE ; }
  104.      else
  105.         _db_open = TRUE ; }
  106.       else {
  107.  
  108.      /* No database */
  109.  
  110. #include <fcntl.h>
  111.      if (db_flags & O_RDWR) {
  112.         int fd ;
  113.  
  114.         /* Create .desc.(pag|dir) files */
  115.         if ((fd = open (desc_file, db_flags | O_EXCL, 0666))
  116.           < 0)
  117.            return FALSE ;
  118.         close (fd) ;
  119.         strcpy (q, ".dir") ;
  120.         if ((fd = open (desc_file, db_flags | O_EXCL, 0666))
  121.           < 0)
  122.            return FALSE ;
  123.         close (fd) ;
  124.  
  125.         /* Start up DBM */
  126.         *q = EOS ;        /* Hide ".dir" */
  127.         if (dbminit (desc_file) == 0)
  128.            _db_open = TRUE ; }
  129.      else {
  130.         errno = ENOENT ;
  131.         return FALSE ; } }
  132. #endif    /* NDBM */
  133.  
  134.       /* Return "desc_file" to just a directory name */
  135.       *p = EOS ;
  136.       }
  137.    return TRUE ;
  138.    }
  139.  
  140.  
  141. /*
  142.  * Another support routine for getdesc() and setdesc().  This returns
  143.  * FALSE if unsuccessful.
  144.  */
  145.  
  146. char *_desc_pathname ;
  147. char *_desc_directory ;
  148. char *_desc_name ;
  149.  
  150.  
  151. _desc_parse_path (pathname, directory, name)
  152.    char *pathname ;
  153.    char *directory ;
  154.    char *name ;
  155. {
  156.    static char misc_static[MAXPATHLEN] ;
  157.    static char cwd[] = "." ;
  158.  
  159. #ifdef TEST
  160.    printf ("(_desc_parse_path called)\n") ;
  161. #endif
  162.  
  163.    if (pathname) {
  164.       _desc_pathname = pathname ;
  165.       if (name) {
  166.      if (directory && *directory)
  167.         _desc_directory = directory ;
  168.      else
  169.         _desc_directory = cwd ;
  170.      _desc_name = name ; }
  171.       else {    /* Path only */
  172.      if (strlen (_desc_pathname) >
  173. #ifdef __STDC__
  174.            (size_t)
  175. #endif
  176.            MAXPATHLEN)
  177.         return FALSE ;
  178.  
  179.      /* Split path into directory and name */
  180.  
  181.      _desc_directory = strcpy (misc_static, pathname) ;
  182.      _desc_name = strrchr (_desc_directory, '/') ;
  183.  
  184.      /* Clean out any superfluous trailing slashes */
  185.  
  186.      while (_desc_name > _desc_directory && !_desc_name[1]) {
  187.         *_desc_name = '\0' ;
  188.         _desc_name = strrchr (_desc_directory, '/') ; }
  189.  
  190.      if (_desc_name) {
  191.  
  192.         /* We have a slash and something after it */
  193.  
  194.         if (_desc_name == _desc_directory) {
  195.  
  196.            /* The only slash is at start of string, ie, directory is "/" */
  197.  
  198.            _desc_name = pathname + 1 ;
  199.            _desc_directory[1] = '\0' ; }
  200.  
  201.         else {
  202.  
  203.            *_desc_name = '\0' ;        /* Kill slash */
  204.            _desc_name++ ; } }
  205.  
  206.      else {
  207.  
  208.         if (_desc_name) {
  209.  
  210.            /* We have something that ends in "/"; delete it and recurse */
  211.  
  212.            *_desc_name = '\0' ;
  213.            return _desc_parse_path (pathname) ; }
  214.  
  215.         else {
  216.  
  217.            /* No slash */
  218.  
  219.            strcpy (_desc_directory, cwd) ;
  220.            _desc_name = pathname ; } } } }
  221.  
  222.    else {
  223.  
  224.       /* No pathname, but should have directory and name */
  225.  
  226.       if (! name) {
  227.      return FALSE ; }
  228.  
  229.       _desc_directory = directory ;
  230.       _desc_name = name ;
  231.  
  232.       if (directory && *directory) {
  233.  
  234.      /* Construct path from directory and name */
  235.  
  236.      if ((strlen (directory) + strlen (name)) >=
  237. #ifdef __STDC__
  238.            (size_t)
  239. #endif
  240.            MAXPATHLEN)
  241.         return FALSE ;
  242.      _desc_pathname = strcpy (misc_static, directory) ;
  243.      strcat (_desc_pathname, "/") ;
  244.      strcat (_desc_pathname, name) ; }
  245.  
  246.       else {
  247.  
  248.      /* Null directory; path == name */
  249.  
  250.      _desc_pathname = name ; } }
  251.  
  252.    return TRUE ;
  253.    }
  254.  
  255.  
  256. #ifdef TEST
  257.  
  258. int main (argc, argv)
  259.    int argc ;
  260.    char **argv ;
  261. {
  262.    char pathname[MAXPATHLEN] ;
  263.    char directory[MAXPATHLEN] ;
  264.    char name[MAXPATHLEN] ;
  265.  
  266.    while (! feof (stdin)) {
  267.       printf (" test pathname: ") ;
  268.       fgets (pathname, MAXPATHLEN, stdin) ;
  269.       printf ("test directory: ") ;
  270.       fgets (directory, MAXPATHLEN, stdin) ;
  271.       printf ("     test name: ") ;
  272.       fgets (name, MAXPATHLEN, stdin) ;
  273.       if (_desc_parse_path (pathname, directory, name))
  274.      printf (
  275. "---\n  _desc_pathname: %s\n _desc_directory: %s\n _desc_name: %s\n",
  276.          _desc_pathname, _desc_directory, _desc_name) ;
  277.       else
  278.      perror (" error") ; }
  279.    }
  280. #endif
  281.