home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / utils / disk-man / mtools-3.000 / mtools-3 / mtools-3.0 / streamcache.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-08  |  3.6 KB  |  203 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 "streamcache.h"
  12. #include "plain_io.h"
  13.  
  14. Stream_t *open_subdir(StreamCache_t *sc, char *arg, int flags, Stream_t **Fsp)
  15. {
  16.     int drive;
  17.     Stream_t *Fs;
  18.     char pathname[MAX_PATH];
  19.  
  20.     sc->drivename = drive = get_drive(arg, *(sc->mcwd));
  21.     
  22.     /* open the drive */
  23.     if(sc->fss[drive])
  24.         Fs = sc->fss[drive];
  25.     else {
  26.         Fs = fs_init(drive, flags);
  27.         if (!Fs){
  28.             fprintf(stderr, "Cannot initialize '%c:'\n", drive);
  29.             return NULL;
  30.         }
  31.  
  32.         sc->fss[drive] = Fs;
  33.     }
  34.  
  35.     if(Fsp)
  36.         *Fsp = Fs;
  37.  
  38.     get_name(arg, sc->filename, sc->mcwd);
  39.     get_path(arg, pathname, sc->mcwd);
  40.     if(sc->pathname)
  41.         strcpy(sc->pathname, pathname);
  42.  
  43.     if (sc->last_drive != drive || 
  44.         strcasecmp(pathname,sc->subdir_name)){
  45.         FREE(&sc->subdir);
  46.         
  47.         sc->subdir = subdir(Fs, pathname);
  48.         sc->last_drive = drive;
  49.         strcpy(sc->subdir_name, pathname);
  50.     }    
  51.     return COPY(sc->subdir);
  52. }
  53.  
  54. void init_sc(StreamCache_t *sc)
  55. {
  56.     int i;
  57.  
  58.     sc->last_drive = '\0';
  59.     fix_mcwd(sc->mcwd);
  60.  
  61.     for(i=0; i<256; i++)
  62.         sc->fss[i]=0;
  63.  
  64.     sc->subdir= NULL;
  65.     sc->subdir_name[0]='\0';
  66.     sc->unixcallback = NULL;
  67.     sc->newdoscallback = NULL;
  68.     sc->pathname = sc->outname = sc->shortname = sc->longname = 0;
  69.     sc->newdrive_cb = sc->olddrive_cb = 0;
  70. }
  71.  
  72.  
  73. void finish_sc(StreamCache_t *sc)
  74. {
  75.     int i;
  76.  
  77.     FREE(&sc->subdir);
  78.     for(i=0; i<256; i++){
  79.         if(sc->fss[i] && sc->fss[i]->refs != 1 )
  80.             fprintf(stderr,"Streamcache allocation problem:%c %d\n",
  81.                 i, sc->fss[i]->refs);
  82.         FREE(&(sc->fss[i]));
  83.     }
  84. }
  85.  
  86.  
  87. int unix_loop(StreamCache_t *sc, char *arg)
  88. {
  89.     char *tmp;
  90.     int ret;
  91.  
  92.     sc->File = NULL;
  93.     if((sc->lookupflags & DO_OPEN)){
  94.         sc->File = SimpleFloppyOpen(0, 0, arg, O_RDONLY, 0);
  95.         if(!sc->File){
  96.             perror(arg);
  97. #if 0
  98.             tmp = strrchr(arg,'/');
  99.             if(tmp)
  100.                 tmp++;
  101.             else
  102.                 tmp = arg;
  103.             strncpy(sc->filename, tmp, VBUFSIZE);
  104.             sc->filename[VBUFSIZE-1] = '\0';
  105. #endif
  106.             return MISSED_ONE;
  107.         }
  108.     }
  109.  
  110.     if(sc->outname){
  111.         tmp = strrchr(arg,'/');
  112.         if(tmp)
  113.             tmp++;
  114.         else
  115.             tmp = arg;
  116.         strncpy(sc->outname, tmp, VBUFSIZE-1);
  117.         sc->outname[VBUFSIZE-1]='\0';
  118.     }
  119.     ret = sc->unixcallback(arg,sc) | IS_MATCH;
  120.     FREE(&sc->File);
  121.     return ret;
  122. }
  123.  
  124. int dos_loop(StreamCache_t *sc, char *arg)
  125. {
  126.     Stream_t *Dir;
  127.     int ret;
  128.     int have_one;
  129.     int entry;
  130.     Stream_t *Fs;
  131.     char *filename;
  132.  
  133.     have_one = MISSED_ONE;
  134.     ret = 0;
  135.     Dir =  open_subdir(sc, arg, sc->openflags, &Fs);
  136.     if(!Dir)
  137.         return MISSED_ONE;
  138.     
  139.     entry = 0;
  140.     filename = sc->filename;
  141.     if(!filename[0])
  142.         filename="*";
  143.     while(entry >= 0){
  144.         if(got_signal)
  145.             break;
  146.         sc->File = NULL;
  147.         if(vfat_lookup(Dir, Fs,
  148.                    & (sc->dir), &entry, 0,
  149.                    filename,
  150.                    sc->lookupflags,
  151.                    sc->outname,
  152.                    sc->shortname,
  153.                    sc->longname,
  154.                    &sc->File) == 0 ){
  155.             if(got_signal)
  156.                 break;
  157.             have_one = IS_MATCH;
  158.             sc->Fs = Fs;
  159.             ret |= sc->callback(Dir, sc, entry - 1);
  160.             FREE(&sc->File);
  161.         }        
  162.         if (((FsPublic_t *) Fs)->fat_error)
  163.             ret |= MISSED_ONE;
  164.         if(ret & NEXT_DISK)
  165.             break;
  166.         
  167.     }
  168.     FREE(&Dir);
  169.     return ret | have_one;
  170. }
  171.  
  172. int main_loop(StreamCache_t *sc, char *progname, char **argv, int argc)
  173. {
  174.     int i;
  175.     int ret, Bret;
  176.     
  177.     Bret = 0;
  178.  
  179.     for (i = 0; i < argc; i++) {
  180.         if ( got_signal )
  181.             break;
  182.         if (sc->unixcallback && (!argv[i][0] || argv[i][1] != ':' ))
  183.             ret = unix_loop(sc, argv[i]);
  184.         else if (sc->newdoscallback)
  185.             ret = sc->newdoscallback(argv[i], sc) ;
  186.         else
  187.             ret = dos_loop(sc, argv[i]);
  188.         
  189.         if (! (ret & IS_MATCH) ) {
  190.             fprintf(stderr, "%s: File \"%s\" not found\n",
  191.                 progname, argv[i]);
  192.             ret |= MISSED_ONE;
  193.         }
  194.         Bret |= ret;
  195.     }
  196.     if ((Bret & GOT_ONE) && ( Bret & MISSED_ONE))
  197.         return 2;
  198.     if (Bret & MISSED_ONE)
  199.         return 1;
  200.     return 0;
  201. }
  202.  
  203.