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 / streamcache.c < prev    next >
Text File  |  1997-11-12  |  2KB  |  91 lines

  1. /*
  2.  * streamcache.c
  3.  * Managing a cache of open disks
  4.  */
  5.  
  6. #include "sysincludes.h"
  7. #include "msdos.h"
  8. #include "mtools.h"
  9. #include "vfat.h"
  10. #include "fs.h"
  11. #include "mainloop.h"
  12. #include "plain_io.h"
  13.  
  14. static int is_initialized = 0;
  15. static Stream_t *Subdir;    
  16. static char subdir_name[VBUFSIZE];
  17. static Stream_t *fss[256]; /* open drives */
  18. static char last_drive; /* last opened drive */    
  19.  
  20.  
  21. static void finish_sc(void)
  22. {
  23.     int i;
  24.  
  25.     FREE(&Subdir);
  26.     for(i=0; i<256; i++){
  27.         if(fss[i] && fss[i]->refs != 1 )
  28.             fprintf(stderr,"Streamcache allocation problem:%c %d\n",
  29.                 i, fss[i]->refs);
  30.         FREE(&(fss[i]));
  31.     }
  32. }
  33.  
  34.  
  35.  
  36. static void init_streamcache(void)
  37. {
  38.     int i;
  39.  
  40.     if(is_initialized)
  41.         return;
  42.     is_initialized = 1;
  43.     last_drive = '\0';
  44.     for(i=0; i<256; i++)
  45.         fss[i]=0;
  46.     Subdir= NULL;
  47.     subdir_name[0]='\0';
  48.     atexit(finish_sc);
  49. }
  50.  
  51.  
  52. Stream_t *open_subdir(MainParam_t *mp, const char *arg, 
  53.               int flags,int mode, int lock)
  54. {
  55.     int drive;
  56.     Stream_t *Fs;
  57.     char pathname[MAX_PATH];
  58.  
  59.     init_streamcache();
  60.  
  61.     mp->drivename = drive = get_drive(arg, *(mp->mcwd));
  62.     
  63.     /* open the drive */
  64.     if(fss[drive])
  65.         Fs = fss[drive];
  66.     else {
  67.         Fs = fs_init(drive, flags);
  68.         if (!Fs){
  69.             fprintf(stderr, "Cannot initialize '%c:'\n", drive);
  70.             return NULL;
  71.         }
  72.  
  73.         fss[drive] = Fs;
  74.     }
  75.  
  76.     get_name(arg, mp->filename, mp->mcwd);
  77.     get_path(arg, pathname, mp->mcwd, mode);
  78.     if(mp->pathname)
  79.         strcpy(mp->pathname, pathname);
  80.  
  81.     if (last_drive != drive || 
  82.         strcasecmp(pathname,subdir_name)){
  83.         FREE(&Subdir);
  84.         
  85.         Subdir = subdir(Fs, pathname, lock);
  86.         last_drive = drive;
  87.         strcpy(subdir_name, pathname);
  88.     }
  89.     return COPY(Subdir);
  90. }
  91.