home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / CMDS / mtools_3.6.src.lzh / MTOOLS_3.6 / subdir.c < prev    next >
C/C++ Source or Header  |  1997-11-12  |  2KB  |  119 lines

  1. #include "sysincludes.h"
  2. #include "msdos.h"
  3. #include "mtools.h"
  4. #include "vfat.h"
  5. #include "file.h"
  6. #include "buffer.h"
  7.  
  8. /*
  9.  * Find the directory and load a new dir_chain[].  A null directory
  10.  * is OK.  Returns a 1 on error.
  11.  */
  12.  
  13.  
  14. static void bufferize(Stream_t **Dir)
  15. {
  16.     Stream_t *BDir;
  17.  
  18. #if 1    
  19.     if(!*Dir)
  20.         return;
  21.     BDir = buf_init(*Dir, 16384, MDIR_SIZE, MDIR_SIZE);
  22.     if(!BDir){
  23.         FREE(Dir);
  24.         *Dir = NULL;
  25.     } else
  26.         *Dir = BDir;
  27. #endif
  28. }
  29.     
  30.     
  31. Stream_t *descend(Stream_t *Dir, char *path, int barf,char *outname, int lock)
  32. {
  33.     /* this function makes its own copy of the Next pointers */
  34.     int entry;
  35.     struct directory dir;
  36.     Stream_t *SubDir;
  37.     int ret;
  38.  
  39.     if(path[0] == '\0' || !strcmp(path, "."))
  40.         /* don't waste timing scanning through the directory if
  41.          * we look for dot anyways */
  42.         return COPY(Dir);
  43.  
  44.     entry = 0;
  45.     ret = vfat_lookup(Dir, &dir, &entry, 0, path,
  46.               ACCEPT_DIR | SINGLE | DO_OPEN | (barf ? 0 : NO_MSG),
  47.               outname, 0, 0);
  48.  
  49.     if(ret == 0){
  50.         SubDir = open_file(Dir, &dir);
  51.         if(lock)
  52.             LockFile(SubDir);
  53.         bufferize(&SubDir);
  54.         return SubDir;
  55.     }
  56.  
  57.     /*
  58.      * If path is '..', but it wasn't found, then you must be
  59.      * at root.
  60.      */
  61.     if (!strcmp(path, "..")){
  62.         SubDir = open_root(Dir);
  63.         bufferize(&SubDir);
  64.         if(lock)
  65.             LockFile(SubDir);
  66.         return SubDir;
  67.     }
  68.     if (barf)
  69.         fprintf(stderr, "Path component \"%s\" is not a directory\n",
  70.             path);
  71.     return NULL;
  72. }
  73.  
  74.  
  75. /*
  76.  * Descends the directory tree.  Returns 1 on error.  Attempts to optimize by
  77.  * remembering the last path it parsed
  78.  */
  79. Stream_t *subdir(Stream_t *Fs, char *pathname, int lock)
  80. {
  81.     /* this function makes its own copy of the Next pointers */
  82.  
  83.     char *s, *tmp, tbuf[MAX_PATH], *path, terminator;
  84.     Stream_t *Dir, *NewDir;
  85.  
  86.     /* FIXME: path caching */
  87.  
  88.     strcpy(tbuf, pathname);
  89.  
  90.     /* start at root */
  91.     Dir = open_root(Fs);
  92.     bufferize(&Dir);
  93.     if (!Dir){
  94.         FREE(&Fs);
  95.         return Dir;
  96.     }
  97.  
  98.     /* separate the parts */
  99.     tmp = &tbuf[1];
  100.     for (s = tmp; ; ++s) {
  101.         if (*s == '/' || *s == '\0') {
  102.             path = tmp;
  103.             terminator = *s;
  104.             *s = '\0';
  105.             if (s != tmp && strcmp(path,".") && path[0]){
  106.                 NewDir = descend(Dir, path, 1, 0, lock);
  107.                 FREE(&Dir);
  108.                 if(!NewDir)
  109.                     return NewDir;
  110.                 Dir = NewDir;
  111.             }
  112.             if (!terminator)
  113.                 break;
  114.             tmp = s + 1;
  115.         }
  116.     }
  117.     return Dir;
  118. }
  119.