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 / mainloop.c < prev    next >
Text File  |  1997-11-12  |  3KB  |  175 lines

  1. /*
  2.  * mainloop.c
  3.  * Iterating over all the command line parameters, and matching patterns
  4.  * where needed
  5.  */
  6.  
  7. #include "sysincludes.h"
  8. #include "msdos.h"
  9. #include "mtools.h"
  10. #include "vfat.h"
  11. #include "fs.h"
  12. #include "mainloop.h"
  13. #include "plain_io.h"
  14. #include "file.h"
  15.  
  16. static int unix_loop(MainParam_t *mp, char *arg)
  17. {
  18.     char *tmp;
  19.     int ret;
  20.     int isdir;
  21.  
  22.     mp->File = NULL;
  23.     if((mp->lookupflags & DO_OPEN)){
  24.         mp->File = SimpleFileOpen(0, 0, arg, O_RDONLY, 0, 0);
  25.  
  26.         if(!mp->File){
  27.  
  28.             perror(arg);
  29. #if 0
  30.             tmp = strrchr(arg,'/');
  31.             if(tmp)
  32.                 tmp++;
  33.             else
  34.                 tmp = arg;
  35.             strncpy(mp->filename, tmp, VBUFSIZE);
  36.             mp->filename[VBUFSIZE-1] = '\0';
  37. #endif
  38.             return MISSED_ONE;
  39.         }
  40.         GET_DATA(mp->File, 0, 0, &isdir, 0);
  41.         if(isdir)
  42.             return IS_MATCH;
  43.     }
  44.  
  45.     if(mp->outname){
  46.         tmp = strrchr(arg,'/');
  47.         if(tmp)
  48.             tmp++;
  49.         else
  50.             tmp = arg;
  51.         strncpy(mp->outname, tmp, VBUFSIZE-1);
  52.         mp->outname[VBUFSIZE-1]='\0';
  53.     }
  54.     ret = mp->unixcallback(arg,mp) | IS_MATCH;
  55.     FREE(&mp->File);
  56.     return ret;
  57. }
  58.  
  59. static int checkForDot(MainParam_t *mp, const char *name)
  60. {
  61.     if((mp->lookupflags & NO_DOTS) &&
  62.        (!name[0] ||
  63.         !strcmp(name,".") || 
  64.         !strcmp(name,"..")))
  65.         return 1;
  66.     return 0;
  67. }
  68.         
  69.         
  70. static int dos_loop(MainParam_t *mp, char *arg)
  71. {
  72.     Stream_t *Dir;
  73.     int ret;
  74.     int have_one;
  75.     int entry;
  76.     const char *filename;
  77.  
  78.     have_one = MISSED_ONE;
  79.     ret = 0;
  80.  
  81.     Dir =  open_subdir(mp, arg, mp->openflags, 0, 0);
  82.     if(!Dir)
  83.         return MISSED_ONE;
  84.     entry = 0;
  85.  
  86.     filename = mp->filename;
  87.     if(!filename[0])
  88.         filename="*";
  89.     if(checkForDot(mp, filename)){
  90.         fprintf(stderr,
  91.             "This command cannot be applied to \".\" or \"..\"\n");
  92.         FREE(&Dir);
  93.         return MISSED_ONE;
  94.     }
  95.  
  96.     while(entry >= 0){
  97.         if(got_signal)
  98.             break;
  99.         mp->File = NULL;
  100.         if(vfat_lookup(Dir,
  101.                    & (mp->dir), &entry, 0,
  102.                    filename,
  103.                    mp->lookupflags,
  104.                    mp->outname,
  105.                    mp->shortname,
  106.                    mp->longname) == 0 ){
  107.             if(checkForDot(mp,mp->outname))
  108.                 ret |= MISSED_ONE;
  109.             else {
  110.                 if(mp->lookupflags & DO_OPEN)
  111.                     mp->File = open_file(Dir, &mp->dir);
  112.                 if(got_signal)
  113.                     break;
  114.                 have_one = IS_MATCH;
  115.                 ret |= mp->callback(Dir, mp, entry - 1);
  116.                 FREE(&mp->File);
  117.             }
  118.         }        
  119.         if (fat_error(Dir))
  120.             ret |= MISSED_ONE;
  121.         if(ret & NEXT_DISK)
  122.             break;
  123.         if(mp->fast_quit && (ret & MISSED_ONE))
  124.             break;
  125.         
  126.     }
  127.     FREE(&Dir);
  128.     return ret | have_one;
  129. }
  130.  
  131. int main_loop(MainParam_t *mp, char **argv, int argc)
  132. {
  133.     int i;
  134.     int ret, Bret;
  135.     
  136.     Bret = 0;
  137.  
  138.     for (i = 0; i < argc; i++) {
  139.         if ( got_signal )
  140.             break;
  141.         if (mp->unixcallback && (!argv[i][0] || argv[i][1] != ':' ))
  142.             ret = unix_loop(mp, argv[i]);
  143.         else if (mp->newdoscallback)
  144.             ret = mp->newdoscallback(argv[i], mp) ;
  145.         else
  146.             ret = dos_loop(mp, argv[i]);
  147.         
  148.         if (! (ret & IS_MATCH) ) {
  149.             fprintf(stderr, "%s: File \"%s\" not found\n",
  150.                 progname, argv[i]);
  151.             ret |= MISSED_ONE;
  152.         }
  153.         Bret |= ret;
  154.         if(mp->fast_quit && (Bret & MISSED_ONE))
  155.             break;
  156.     }
  157.     if ((Bret & GOT_ONE) && ( Bret & MISSED_ONE))
  158.         return 2;
  159.     if (Bret & MISSED_ONE)
  160.         return 1;
  161.     return 0;
  162. }
  163.  
  164.  
  165. void init_mp(MainParam_t *mp)
  166. {
  167.     fix_mcwd(mp->mcwd);
  168.  
  169.     mp->unixcallback = NULL;
  170.     mp->newdoscallback = NULL;
  171.     mp->pathname = mp->outname = mp->shortname = mp->longname = 0;
  172.     mp->newdrive_cb = mp->olddrive_cb = 0;
  173.     mp->fast_quit = 0;
  174. }
  175.