home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d1xx / d168 / dillonstuff.lha / src / com / findit.c < prev    next >
C/C++ Source or Header  |  1988-11-22  |  3KB  |  164 lines

  1.  
  2. /*
  3.  *  FINDIT()
  4.  *
  5.  *  FINDIT [-ddir] <programname> <programname> ...
  6.  *
  7.  *  Search specified directories (those specified in FINDITVOLS if none
  8.  *  spcified on the command line) for the specified program names.  Wildcards
  9.  *  are acceptable
  10.  *
  11.  *  Format for FINDITVOLS:    dir,dir,dir ...
  12.  */
  13.  
  14. #include <local/typedefs.h>
  15. #include <local/xmisc.h>
  16. #include <stdio.h>
  17.  
  18. #define ENVVAR    "FINDITVOLS"
  19.  
  20. MLIST    DList;
  21. MLIST    FList;
  22. char *ErrorString = "unable to open dres.library";
  23. char *EnvStr;
  24. char Path[1024];
  25.  
  26. extern int Enable_Abort;
  27.  
  28. main(ac,av)
  29. char *av[];
  30. {
  31.     NewList(&DList);
  32.     NewList(&FList);
  33.  
  34.     Enable_Abort = 0;
  35.     {
  36.     register short i;
  37.     for (i = 1; i < ac; ++i) {
  38.         register char *str = av[i];
  39.         if (*str != '-') {
  40.         register NODE *node = malloc(sizeof(NODE));
  41.         node->ln_Name = str;
  42.         AddTail(&FList, node);
  43.         continue;
  44.         }
  45.         for (++str; *str; ++str) {
  46.         switch(*str) {
  47.         case 'd':
  48.             {
  49.             register NODE *node = malloc(sizeof(NODE));
  50.             node->ln_Name = str + 1;
  51.             AddTail(&DList, node);
  52.             }
  53.             str = "\0";
  54.             break;
  55.         default:
  56.             printf("Unknown option: -%c\n", *str);
  57.             exit(1);
  58.         }
  59.         }
  60.     }
  61.     }
  62.     if (openlibs(DRES_LIB) == 0) {
  63.     puts(ErrorString);
  64.     exit(1);
  65.     }
  66.     mountrequest(0);
  67.     if (!GetHead(&DList)) {         /*  scan enviroment variable for dirs */
  68.     char *str;
  69.     register char *ptr;
  70.     register NODE *node;
  71.     register char c;
  72.  
  73.     str = EnvStr = GetDEnv(ENVVAR);
  74.     if (!str) {
  75.         printf("Env. Var %s not found, format:  dir,dir,dir...\n", ENVVAR);
  76.         goto fail;
  77.     }
  78.     for (c = 1; c; str = ptr + 1) {
  79.         for (ptr = str; *ptr && *ptr != ','; ++ptr);
  80.         c = *ptr;
  81.         *ptr = 0;
  82.         node = malloc(sizeof(NODE));
  83.         node->ln_Name = str;
  84.         AddTail(&DList, node);
  85.     }
  86.     }
  87.     {
  88.     register NODE *node;
  89.     register FIB *fib = malloc(sizeof(FIB));
  90.     while (node = RemHead(&DList)) {
  91.         long lock;
  92.  
  93.         if (lock = Lock(node->ln_Name, SHARED_LOCK)) {
  94.         strcpy(Path, node->ln_Name);
  95.         if (Examine(lock, fib))
  96.             SearchTree(lock, fib, strlen(Path));
  97.         UnLock(lock);
  98.         }
  99.         free(node);
  100.         if (checkbreak())
  101.         break;
  102.     }
  103.     free(fib);
  104.     puts("");
  105.     }
  106. fail:
  107.     if (checkbreak())
  108.     puts("^C");
  109.     {
  110.     register NODE *node;
  111.     while (node = RemHead(&DList))
  112.         free(node);
  113.     while (node = RemHead(&FList))
  114.         free(node);
  115.     }
  116.     if (EnvStr)
  117.     free(EnvStr);
  118.     mountrequest(1);
  119.     closelibs(-1);
  120. }
  121.  
  122. /*
  123.  *  Search the specified directory for the wildcarded names in FList.
  124.  */
  125.  
  126. SearchTree(dirlock, dirfib, idx)
  127. long dirlock;
  128. FIB *dirfib;
  129. {
  130.     long oldlock;
  131.     long lock;
  132.     register FIB *fib = malloc(sizeof(FIB));
  133.  
  134.     oldlock = CurrentDir(dirlock);
  135.     while (ExNext(dirlock, dirfib)) {
  136.     if (idx && Path[idx-1] != ':' && Path[idx-1] != '/') {
  137.         Path[idx] = '/';
  138.         strcpy(Path+idx+1, dirfib->fib_FileName);
  139.     } else {
  140.         strcpy(Path+idx, dirfib->fib_FileName);
  141.     }
  142.     if (dirfib->fib_DirEntryType > 0) {
  143.         if (lock = Lock(dirfib->fib_FileName, SHARED_LOCK)) {
  144.         if (Examine(lock, fib))
  145.             SearchTree(lock, fib, idx + strlen(Path+idx));
  146.         UnLock(lock);
  147.         if (checkbreak())
  148.             break;
  149.         }
  150.     } else {
  151.         register NODE *node;
  152.         for (node = GetHead(&FList); node; node = GetSucc(node)) {
  153.         if (WildCmp(node->ln_Name, dirfib->fib_FileName)) {
  154.             printf("%s ", Path);
  155.             fflush(stdout);
  156.         }
  157.         }
  158.     }
  159.     }
  160.     CurrentDir(oldlock);
  161.     free(fib);
  162. }
  163.  
  164.