home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 200-299 / ff274.lzh / LookFor / source / lfmemkey.c < prev    next >
C/C++ Source or Header  |  1989-11-16  |  11KB  |  363 lines

  1. /* LookFor the file whose name|pattern matches that passed as first argument.
  2.  * Print the filename and its path to the screen
  3.  */
  4. #include <stdio.h>
  5. #include <ctype.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <exec/types.h>
  9. #include <libraries/dosextens.h>
  10. #include <proto/exec.h>
  11. #include <proto/dos.h>
  12. #define LONGEST_PATH_NAME  130
  13. #define LONGEST_NAME  32
  14. #define MAXDEV        30
  15.  
  16. /*
  17.  *                 F U N C T I O N     D E C L A R A T I O N S
  18.  */
  19. extern BPTR Lock(char *,long);
  20.  
  21. BOOL ifoundit;
  22. void chkabort(void);
  23. int breakout(void);
  24. void close_things (void);
  25. void scan_directory (char *,char *);
  26. char * adddir (char *);
  27. void do_usage(void);
  28. void do_searchfile(char *);
  29. void do_devname(char *);
  30. void usage_exit(void);
  31. void getdisks(struct List *);
  32. void freedisks(struct List *);
  33. int MakeDevArray(char *);
  34. int searchforcommas(char *);
  35. int searchforALL(char *);
  36. /*
  37.  *                  E X T E R N A L S
  38.  */
  39. int notANY,notON,alive;
  40. char devname[LONGEST_NAME][MAXDEV];
  41. char searchfile[LONGEST_NAME];
  42. /*
  43.  *                  B R E A K   O U T    O F    S E A R C H
  44.  */
  45. int breakout()
  46. {
  47. printf("LookFor .......... terminated with \033[2mBREAK\033[0m.\n");
  48. close_things();
  49. alive = 0;
  50. return(alive);       /* we return 0 so that we can clean up memory */
  51. }
  52.  
  53. /*
  54.  *                  M A I N     P R O G R A M     M O D U L E
  55.  */
  56. void
  57. main( argc,argv)
  58. int argc;
  59. char * argv[];
  60. {
  61.         void scan_directory ();
  62.         void UnLock ();
  63.         void usage_exit();
  64.         void do_usage();
  65.  
  66.         char path[130];           /* hold volume name */
  67.         int ndev,i,dummy;
  68.  
  69.         notANY = notON = 1;
  70.         switch(argc)
  71.         {
  72.         case 2:    /*
  73.                     *     LookFor filename
  74.                     */
  75.             if (!(strcmp(argv[1],"?"))) usage_exit();
  76.             devname[0][0] = '\0';
  77.             do_searchfile(argv[1]);
  78.             break;
  79.  
  80.         case 3:    /*  argv[]     0     1          2
  81.                     *          LookFor ANY      filename
  82.                     *          LookFor filename devname
  83.                     */
  84.             do_searchfile(argv[2]);
  85.             devname[0][0] = '\0';
  86.             notANY = stricmp(argv[1],"ANY");
  87.             if (notANY)
  88.                 {
  89.                     do_devname(argv[2]);
  90.                     do_searchfile(argv[1]);
  91.                 }
  92.             break;
  93.  
  94.         case 4:    /*  argv[]     0     1          2         3
  95.                     *          LookFor ANY      filename  devname
  96.                     *          LookFor filename    ON     devname
  97.                     */
  98.             do_devname(argv[3]);
  99.             do_searchfile(argv[2]);
  100.             if(notANY = stricmp(argv[1],"ANY"))
  101.                 {
  102.                     if( notON = stricmp(argv[2],"ON")) usage_exit();
  103.                     do_searchfile(argv[1]);
  104.                 }
  105.             else do_searchfile(argv[2]);
  106.             break;
  107.         case 5:    /*  argv[]     0     1      2      3      4
  108.                     *          LookFor ANY  filename  ON  devname
  109.                     */
  110.             notANY = stricmp(argv[1],"ANY");
  111.             notON  = stricmp(argv[3],"ON");
  112.             if(notANY || notON) usage_exit();
  113.             do_devname(argv[4]);
  114.             do_searchfile(argv[2]);
  115.             break;
  116.         default:
  117.             usage_exit();
  118.             break;
  119.         }                    /* end of switch */
  120.  
  121.         ifoundit = FALSE;
  122.         dummy = onbreak(&breakout);     /*  Set break trap */
  123.         alive = 1;            /*  alive is 0 when we've been aborted */
  124.         if (notON)
  125.             {
  126.                strcpy (path, devname[0]);
  127.                /* Find it */
  128.                scan_directory (searchfile,path);
  129.                close_things ();
  130.                exit(0);
  131.             }
  132.         if ( (ndev = MakeDevArray(devname[0])) < 0 ) close_things();
  133.         for ( i = 0 ; i <= ndev; i++)
  134.             {
  135.               strcpy (path,devname[i]);
  136.               printf("Checking %s\n",path);
  137.               scan_directory(searchfile,path);
  138.             }
  139.         close_things ();
  140.         exit(0);
  141. } /* end main */
  142.  
  143. int MakeDevArray(devices)
  144. char * devices;
  145. {
  146.     int ndev;
  147.     if(stricmp(devices,"ALL"))  /* if notALL search for commas */
  148.         {
  149.         ndev = searchforcommas(devices);
  150.         return(ndev);
  151.         }
  152.     /*  ALL keyword used, therefore */
  153.     ndev = searchforALL(devices);
  154.     return(ndev);
  155. }
  156. /********************************************************************/
  157. int searchforALL(all)
  158. char * all;
  159. {
  160.    struct List disks;
  161.    struct Node *disk;
  162.    static char Name_Disk[LONGEST_NAME] ;
  163.    extern void getdisks(),freedisks();
  164.    int count;
  165.  
  166.    count = 0;
  167.    NewList(&disks);         /* Macro in exec ( ROM Kernel)  */
  168.    getdisks(&disks);
  169.  
  170.  for ( disk = disks.lh_Head;disk->ln_Succ;disk=disk->ln_Succ)
  171.      {
  172.         strcpy(Name_Disk, disk->ln_Name) ;
  173.         strcat(Name_Disk, ":");
  174.         strcpy(devname[count++],Name_Disk);
  175.         if (count > (int)MAXDEV) break;
  176.      }
  177.    freedisks(&disks);
  178.    return(--count);
  179. }
  180.  
  181. int searchforcommas(strofCommas)
  182. char * strofCommas;
  183. {
  184.     char * dummy;
  185.     char * token;
  186.     int count;
  187.  
  188.     count = -1;
  189.     dummy = strofCommas;
  190.     token = strtok(dummy,",");
  191.     while ( (token != NULL) && (count <= (int)MAXDEV) )
  192.         {
  193.            strcpy(devname[++count],token);
  194.            token = strtok(NULL,",");
  195.         }
  196.     return(count);
  197. }
  198.  
  199. /********************************************************************/
  200. /*
  201. Do whatever final clean up is needed to leave the program.
  202. */
  203. void
  204. close_things()
  205. {
  206.         if (!ifoundit)
  207.                 printf ("File not found.\n");
  208.         return;
  209. } /* end close_things */
  210.  
  211.  
  212. /******************************
  213.        scan_directory
  214. *******************************/
  215. void
  216. scan_directory (searchfile,path)
  217. char * searchfile;
  218. char * path;
  219.         {
  220.         char * strcpy ();
  221.         void UnLock ();
  222.         register BPTR dir;
  223.         struct FileInfoBlock * fb;
  224.         char * adddir ();
  225.  
  226. #       define MAXSUB 50
  227.         char subdir [LONGEST_NAME][MAXSUB]; /* Pointer area for Sub-Directories */
  228.         char pathname[LONGEST_PATH_NAME]; /* Pointers to AmigaDOS sub-directories */
  229.         register UBYTE countdir,indexdir,i;
  230.         int thistest;
  231.         char testname[LONGEST_NAME];
  232.         char * ch;
  233.         char * match;
  234.  
  235.         /* Get Lock on this directory */
  236.         alive = 1;
  237.         if ( (dir = Lock (path,ACCESS_READ)) == NULL)
  238.                 {
  239.                 printf ("Couldn't get %s\n",path);
  240.                 close_things ();
  241.                 exit (4);
  242.                 }
  243.  
  244.         /* Examine lock and obtain FileInfoBlock */
  245.         fb=(struct FileInfoBlock *) AllocMem(sizeof(struct FileInfoBlock),0);
  246.         if (!Examine (dir,fb))
  247.                 {
  248.                 printf ("Couldn't Examine files on lock\n");
  249.                 FreeMem ((char *)fb, sizeof (struct FileInfoBlock));
  250.                 UnLock (dir);
  251.                 close_things ();
  252.                 exit (4);
  253.                 }
  254.         /* we now have dir and fb set up,
  255.          * scan all files at this level
  256.          * (remembering subdirectories)
  257.          */
  258.         countdir = 0;
  259.         while ((ExNext(dir,fb) || IoErr() != ERROR_NO_MORE_ENTRIES)
  260.                 && (countdir <= MAXSUB)
  261.                 )
  262.                 {
  263.                 chkabort();
  264.                 if (!alive) goto abortMem;
  265.                 if (fb->fib_DirEntryType > 0)
  266.                         {
  267.                         /* we have a subdirectory here... */
  268.                         if (countdir < MAXSUB) strcpy(subdir[countdir],fb->fib_FileName);
  269.                         else printf ("Too many subdirectories for LookFor\n");
  270.                         countdir++;
  271.                         }
  272.                 else
  273.                         {
  274.                         /* compare fb->filename with searchfile  */
  275.                         for (i=0;i<32;i++)
  276.                                 testname[i] = '\0';
  277.                         strncpy (testname,fb->fib_FileName, 32);
  278.                         testname[31] = '\0';
  279.                         for (i = 0,ch=testname; (ch != '\0')&&(i<32);
  280.                                 i++,ch++)
  281.                                 *ch = toupper (*ch);
  282. /******                 printf ("%s =?= %s\n",testname, searchfile); *****/
  283.  
  284.                         thistest = ( (notANY) ? stcpma(testname,searchfile) : stcpm(testname,searchfile,&match) );
  285.                         if (thistest)
  286.                                 {
  287.                                 /*   printf("\033[2m%-20s\033[0m",fb->fib_FileName);  */
  288.                                 /*   printf ("  \033[2m%s/\033[0m%-30s   %9d bytes\n",path,fb->fib_FileName,fb->fib_Size); */
  289.                                 /*   printf ("  \033[2m%s/\033[0m%s\t%9d bytes\n",path,fb->fib_FileName,fb->fib_Size);     */
  290.                                 printf ("  %s/%s\t%9d bytes\n",path,fb->fib_FileName,fb->fib_Size);
  291.                                 ifoundit = TRUE;
  292.                                 }
  293.                         }
  294.                 } /* end while */
  295.         /* return these now, we're done, thank you. */
  296.         FreeMem ((char *)fb, sizeof (struct FileInfoBlock));
  297.         UnLock (dir);
  298.  
  299.         /* finished with this level, try one level down */
  300.         indexdir = 0;
  301.         while (indexdir < countdir)
  302.                 {
  303.                 chkabort();
  304.                 if (!alive) goto abortMem;
  305.                 strcpy (pathname,path); /*  , LONGEST_PATH_NAME - strlen(pathname) - 1);  */
  306.                 /* if no path delimiter, add one */
  307.                 if (       (path[strlen(path)-1] != ':')
  308.                         && (path[strlen(path)-1] != '/')
  309.                         && (strlen(path) > 0))
  310.                         strcat (pathname,"/");
  311.                 strcat (pathname,subdir[indexdir]);  /*  , LONGEST_PATH_NAME - strlen(pathname) - 1);  */
  312.                 scan_directory (searchfile,pathname);
  313.                 indexdir++;
  314.                 }
  315.  
  316.         return;   /* normal end of scan_directory  */
  317.  
  318. abortMem: /* we've been aborted so attempt to clean up */
  319.    if(fb)  FreeMem ((char *)fb, sizeof (struct FileInfoBlock));
  320.    if(dir) UnLock (dir);
  321.    exit(0);
  322. }       /* end scan_directory */
  323.  
  324.  
  325. void do_searchfile(string)
  326. char *string;
  327. {
  328.     char * ch;
  329.  
  330.     strcpy (searchfile,string);
  331.     for (ch=searchfile; *ch != '\0'; ch++)
  332.         *ch = toupper (*ch);
  333.     return;
  334. }
  335.  
  336. void do_devname(string)
  337. char *string;
  338. {
  339.     int i;
  340.  
  341.     for (i=0;i<32;i++)
  342.         devname[i][0] = '\0';
  343.     strncpy (devname[0],string,31);
  344.     return;
  345. }
  346.  
  347. void do_usage()
  348. {
  349.    printf ("\nLookFor by\033[2m Mark Schretlen\033[0m 88-12\n\n");
  350.    printf ("usage: LookFor <ANY> <filename> <ON> <drive(s)|ALL>\n\n");
  351.    printf ("patterns: (filename only)\n?  any single character ( LookFor ? gives this help message)\n*  zero or more occurrences \n+  one or more occurrences\n\\  override the above patterns\n\n");
  352.    printf ("keywords: (optional)\nANY looks for the pattern anywhere within the filename (unanchored).\nON must be specified for a multi-drive search (separate drives with commas).\nON ALL looks for the file on all drives.\n\n");
  353.    printf ("\nLookFor is a \033[2mpublic domain \033[0mprogram. Please distribute freely.\n\n");
  354.    return;
  355. }
  356.  
  357. void usage_exit()
  358. {
  359.     do_usage();
  360.     exit(0);
  361.     return;
  362. }
  363.