home *** CD-ROM | disk | FTP | other *** search
- /* enddesc.c - Initialise/deallocate description database
- *
- * Copyright (c) 1991, 1992 Tim Cook.
- * Non-profit distribution allowed. See README for details.
- */
-
- static char rcsid[] = "$Id: enddesc.c,v 1.4 1993/01/29 04:17:43 tim Exp tim $";
-
- #include <sys/param.h>
- #include <sys/errno.h>
- #include "config.h"
-
- #ifdef TEST
- #include <stdio.h>
- #endif
-
- #ifndef MAXPATHLEN
- #define MAXPATHLEN 1024
- #endif
-
- extern int errno ;
- #ifdef NDBM
- DBM *_desc_database = (DBM *) NULL ;
- #define DB_OPEN (_desc_database != (DBM *) NULL)
- #else
- static int _db_open = FALSE ;
- #define DB_OPEN _db_open
- #endif
-
-
- VOID enddesc ()
- {
- #ifdef NDBM
- if (_desc_database)
- dbm_close (_desc_database) ;
- #else
- dbmclose () ;
- #endif
- }
-
-
- /*
- * This is support routine for getdesc() and setdesc(), and should not
- * normally be called by the programmer.
- */
-
- int _initdesc (directory, open_flags)
- char *directory ;
- int open_flags ;
- {
- static char desc_file[MAXPATHLEN+1] = "" ;
- static int db_flags ;
-
- if (! directory || *directory == EOS)
- directory = "." ;
-
- if (open_flags == db_flags && strcmp (directory, desc_file) == 0)
- return DB_OPEN ; /* Tell 'em what we said before */
-
- /* Otherwise, we need to try to open a database */
- {
- char *p ;
- #ifndef NDBM
- char *q ;
- #include <sys/stat.h>
- struct stat status ;
- #endif
-
- DBM_close (_desc_database) ;
-
- strcpy (desc_file, directory) ;
- db_flags = open_flags ;
-
- /* Find end of "desc_file" */
- for (p = desc_file ; *p != EOS ; p++) ;
-
- #ifdef NDBM
-
- /* By crikey, it's easier this way! */
-
- strcpy (p, "/.desc") ;
-
- #ifdef TEST
- printf ("Opening new database\n") ;
- #endif
- if ((_desc_database = dbm_open (desc_file, db_flags, 0666))
- == (DBM *) NULL) {
- return FALSE ; }
-
- #else /* NDBM */
-
- _db_open = FALSE ; /* We just closed it above */
-
- strcpy (p, "/.desc.pag") ;
- q = strrchr (p, '.') ;
-
- if (stat (desc_file, &status) != -1) {
-
- /* Database exists */
-
- *q = EOS ; /* Hide ".pag" */
- if (dbminit (desc_file) < 0) {
- return FALSE ; }
- else
- _db_open = TRUE ; }
- else {
-
- /* No database */
-
- #include <fcntl.h>
- if (db_flags & O_RDWR) {
- int fd ;
-
- /* Create .desc.(pag|dir) files */
- if ((fd = open (desc_file, db_flags | O_EXCL, 0666))
- < 0)
- return FALSE ;
- close (fd) ;
- strcpy (q, ".dir") ;
- if ((fd = open (desc_file, db_flags | O_EXCL, 0666))
- < 0)
- return FALSE ;
- close (fd) ;
-
- /* Start up DBM */
- *q = EOS ; /* Hide ".dir" */
- if (dbminit (desc_file) == 0)
- _db_open = TRUE ; }
- else {
- errno = ENOENT ;
- return FALSE ; } }
- #endif /* NDBM */
-
- /* Return "desc_file" to just a directory name */
- *p = EOS ;
- }
- return TRUE ;
- }
-
-
- /*
- * Another support routine for getdesc() and setdesc(). This returns
- * FALSE if unsuccessful.
- */
-
- char *_desc_pathname ;
- char *_desc_directory ;
- char *_desc_name ;
-
-
- _desc_parse_path (pathname, directory, name)
- char *pathname ;
- char *directory ;
- char *name ;
- {
- static char misc_static[MAXPATHLEN] ;
- static char cwd[] = "." ;
-
- #ifdef TEST
- printf ("(_desc_parse_path called)\n") ;
- #endif
-
- if (pathname) {
- _desc_pathname = pathname ;
- if (name) {
- if (directory && *directory)
- _desc_directory = directory ;
- else
- _desc_directory = cwd ;
- _desc_name = name ; }
- else { /* Path only */
- if (strlen (_desc_pathname) >
- #ifdef __STDC__
- (size_t)
- #endif
- MAXPATHLEN)
- return FALSE ;
-
- /* Split path into directory and name */
-
- _desc_directory = strcpy (misc_static, pathname) ;
- _desc_name = strrchr (_desc_directory, '/') ;
-
- /* Clean out any superfluous trailing slashes */
-
- while (_desc_name > _desc_directory && !_desc_name[1]) {
- *_desc_name = '\0' ;
- _desc_name = strrchr (_desc_directory, '/') ; }
-
- if (_desc_name) {
-
- /* We have a slash and something after it */
-
- if (_desc_name == _desc_directory) {
-
- /* The only slash is at start of string, ie, directory is "/" */
-
- _desc_name = pathname + 1 ;
- _desc_directory[1] = '\0' ; }
-
- else {
-
- *_desc_name = '\0' ; /* Kill slash */
- _desc_name++ ; } }
-
- else {
-
- if (_desc_name) {
-
- /* We have something that ends in "/"; delete it and recurse */
-
- *_desc_name = '\0' ;
- return _desc_parse_path (pathname) ; }
-
- else {
-
- /* No slash */
-
- strcpy (_desc_directory, cwd) ;
- _desc_name = pathname ; } } } }
-
- else {
-
- /* No pathname, but should have directory and name */
-
- if (! name) {
- return FALSE ; }
-
- _desc_directory = directory ;
- _desc_name = name ;
-
- if (directory && *directory) {
-
- /* Construct path from directory and name */
-
- if ((strlen (directory) + strlen (name)) >=
- #ifdef __STDC__
- (size_t)
- #endif
- MAXPATHLEN)
- return FALSE ;
- _desc_pathname = strcpy (misc_static, directory) ;
- strcat (_desc_pathname, "/") ;
- strcat (_desc_pathname, name) ; }
-
- else {
-
- /* Null directory; path == name */
-
- _desc_pathname = name ; } }
-
- return TRUE ;
- }
-
-
- #ifdef TEST
-
- int main (argc, argv)
- int argc ;
- char **argv ;
- {
- char pathname[MAXPATHLEN] ;
- char directory[MAXPATHLEN] ;
- char name[MAXPATHLEN] ;
-
- while (! feof (stdin)) {
- printf (" test pathname: ") ;
- fgets (pathname, MAXPATHLEN, stdin) ;
- printf ("test directory: ") ;
- fgets (directory, MAXPATHLEN, stdin) ;
- printf (" test name: ") ;
- fgets (name, MAXPATHLEN, stdin) ;
- if (_desc_parse_path (pathname, directory, name))
- printf (
- "---\n _desc_pathname: %s\n _desc_directory: %s\n _desc_name: %s\n",
- _desc_pathname, _desc_directory, _desc_name) ;
- else
- perror (" error") ; }
- }
- #endif
-