home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 122.lha / Arp_v1.1 / Demos / WildDemo2.c < prev   
Encoding:
C/C++ Source or Header  |  1986-11-21  |  2.8 KB  |  150 lines

  1. /*
  2.  * WildDemo2.c - Search filesystem for patterns, and separate into directories
  3.  *         and files, sorting each separately using DA lists.
  4.  *
  5.  * -+=SDB=+-
  6.  *
  7.  * Copyright (c) 1987, Scott Ballantyne
  8.  * Use and abuse as you please.
  9.  *
  10.  * Manx:
  11.  * cc WildDemo2.c
  12.  * ln WildDemo2.o -larp -lc
  13.  *
  14.  * Lattice:
  15.  * lc WildDemo2.c
  16.  * blink lib:c.o WildDemo2.o to WildDemo2 lib lib:arp.lib lib:lc.lib lib:amiga.lib
  17.  */
  18.  
  19. #include <exec/types.h>
  20. #include "libraries/arpbase.h"
  21. #include <libraries/dos.h>
  22. #include "arpfunctions.h"
  23.  
  24.  
  25. struct DirectoryEntry    *FileList = NULL;    /* Head of DAList */
  26.  
  27. /* Our secondary keys for sorting the list, we want the directories to bunch
  28.  * up at the beginning.
  29.  */
  30.  
  31. #define DIRECTORY    0L
  32. #define FILE        1L
  33.  
  34. /* Our templates and help strings */
  35. char *CLI_Template = "Pattern";
  36. char *CLI_Help = "List Files or directories by wildcard";
  37.  
  38. BOOL lflag;    /* Cosmetics */
  39.  
  40. VOID columnize();
  41.  
  42. VOID main(argc, argv)
  43. int argc;
  44. char **argv;
  45. {
  46.     struct AnchorPath *Anchor;
  47.     register struct DirectoryEntry    *de;    /* For later */
  48.     register LONG fcount = 0, dcount = 0;
  49.     LONG    key;
  50.     char    *pat;
  51.     LONG    Result;
  52.  
  53.     if (argc < 2)
  54.         pat = "*";    /* All files/dirs in current directory */
  55.     else
  56.         pat = argv[1];
  57.  
  58.     /* Get our AnchorBase */
  59.  
  60.     if ( Anchor = (struct AnchorPath *)ArpAlloc( (ULONG)sizeof( *Anchor )) )
  61.     {
  62.         Anchor->ap_Length = 0;
  63.         Anchor->ap_BreakBits |= SIGBREAKF_CTRL_C;
  64.     }
  65.     else
  66.     {
  67.         Puts("No memory!");
  68.         exit(20);
  69.     }
  70.  
  71.     Result = FindFirst( pat, Anchor);
  72.  
  73.     while ( Result == 0 )
  74.     {
  75.  
  76.         if (Anchor->ap_Info.fib_DirEntryType >= 0)
  77.         {
  78.             key = DIRECTORY;
  79.             dcount++;
  80.         }
  81.         else
  82.         {
  83.             key = FILE;
  84.             fcount++;
  85.         }
  86.  
  87.         if ( !AddDANode( Anchor->ap_Info.fib_FileName, &FileList, 0L, key))
  88.         {
  89.             Puts("Out of memory!");
  90.             FreeDAList( FileList );
  91.             exit(20);
  92.         }
  93.         Result = FindNext( Anchor );
  94.     }
  95.  
  96.     /* Free the Anchor chain built by the above functions */
  97.       FreeAnchorChain( Anchor );
  98.  
  99.     if (Result == ERROR_BREAK)    /* Control 'C' */
  100.         Puts("***Break");
  101.     else if (Result == ERROR_OBJECT_NOT_FOUND)
  102.     {
  103.         Printf("Can't find %s\n", pat);
  104.         exit(20);
  105.     }
  106.     else if (Result != ERROR_NO_MORE_ENTRIES)
  107.     {
  108.         Printf("ERROR #%ld\n", Result);
  109.         exit(20);
  110.     }
  111.  
  112.     /* Now munge through the DA list, displaying the sorted files */
  113.  
  114.     de = FileList;
  115.     Printf("%ld Directories:", dcount);
  116.     
  117.     if (de->de_Type == DIRECTORY)    /* While we have directories */
  118.     {
  119.         lflag = 1;
  120.         for ( ; de ; de = de->de_Next)
  121.         {
  122.             if (de->de_Type != DIRECTORY)
  123.                 break;
  124.             columnize(de->de_Name);
  125.         }
  126.     }
  127.     /* And now do the files */
  128.     Printf("\n%ld Files:", fcount);
  129.     if ( de )    /* Still more entries */
  130.     {
  131.         lflag = 1;
  132.         for ( ; de ; de = de->de_Next)
  133.             columnize(de->de_Name);
  134.     }
  135.     Puts("");
  136.     FreeDAList( FileList );
  137. }
  138.  
  139. /* Simple routine to print a file or directory name in 2 columns */
  140.  
  141. VOID columnize(name)
  142. char *name;
  143. {
  144.  
  145.     if (lflag)
  146.         Printf("\n\t");
  147.     Printf("%-32s", name);
  148.     lflag = !lflag;
  149. }
  150.