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

  1. /* setdesc.c -    Set a file description
  2.  *
  3.  * Copyright (c) 1991, 1992 Tim Cook.
  4.  * Non-profit distribution allowed.  See README for details.
  5.  */
  6.  
  7. static char rcsid[] = "$Id: setdesc.c,v 1.3 1992/12/02 03:51:00 tim Exp $";
  8.  
  9. #include "config.h"
  10. #include <fcntl.h>
  11. #include <sys/stat.h>
  12. #include <sys/errno.h>
  13.  
  14. extern int errno ;
  15. extern int _desc_parse_path () ;
  16. extern char *_desc_pathname ;
  17. extern char *_desc_directory ;
  18. extern char *_desc_name ;
  19. extern int _initdesc () ;
  20. #ifdef NDBM
  21. extern DBM *_desc_database ;
  22. #endif
  23.  
  24.  
  25. int setdesc (pathname, directory, name, inode, description)
  26.    char *pathname ;
  27.    char *directory ;
  28.    char *name ;
  29.    ino_t inode ;
  30.    char *description ;
  31. {
  32.    datum key, content ;
  33.  
  34.    if (! _desc_parse_path (pathname, directory, name)) {
  35.       errno = EINVAL ;
  36.       return FALSE ; }
  37.  
  38.    if (! inode) {
  39.       struct stat file_status ;
  40.  
  41.       if (stat (_desc_pathname, &file_status) == 0)
  42.      inode = file_status.st_ino ; }
  43.  
  44.    if (description == NULL_CP || *description == EOS) {
  45.       /* Set the description to null?  This means delete */
  46.  
  47.       if (! _initdesc (_desc_directory, O_RDWR))
  48.          /* If there is no database, that's OK */
  49.          return (errno == ENOENT) ;
  50.  
  51.       if (inode) {
  52.      key.dptr = (char *) &inode ;
  53.      key.dsize = sizeof (ino_t) ;
  54.      DBM_delete (_desc_database, key) ; }
  55.       key.dptr = _desc_name ;
  56.       key.dsize = strlen (_desc_name) ;
  57.       if (key.dsize == sizeof (ino_t))
  58.      key.dsize++ ;
  59.       DBM_delete (_desc_database, key) ;
  60.       return TRUE ; }
  61.  
  62.    if (! _initdesc (_desc_directory, O_RDWR | O_CREAT))
  63.       return FALSE ;
  64.  
  65.    /* Store the description, indexed by the file name */
  66.  
  67.    key.dptr = _desc_name ;
  68.    key.dsize = strlen (_desc_name) ;
  69.  
  70.    /*
  71.     * Name and inode keys are distinguished by the key length.  Inode
  72.     * keys are all "sizeof (ino_t)" long, while all other keys are
  73.     * name keys.
  74.     */
  75.  
  76.    if (key.dsize == sizeof (ino_t))
  77.       /*
  78.        * To distinguish this from an inode key, we'll store the
  79.        * terminating null byte as well
  80.        */
  81.       key.dsize++ ;
  82.  
  83.    content.dptr = description ;
  84.    content.dsize = strlen (description) + 1 ;
  85.    if (DBM_store (_desc_database, key, content) < 0)
  86.       return FALSE ;
  87.  
  88.    if (inode) {
  89.  
  90.       /* Store the file name, indexed by the inode number */
  91.  
  92.       key.dptr = (char *) &inode ;
  93.       key.dsize = sizeof (ino_t) ;
  94.       content.dptr = _desc_name ;
  95.       content.dsize = strlen (_desc_name) ;
  96.       if (DBM_store (_desc_database, key, content) < 0)
  97.      return FALSE ;
  98.       }
  99.    return TRUE ;
  100.    }
  101.