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

  1. /*
  2.  * WildDemo.c -- Demonstration of how to use the arp.library wildcard
  3.  *         routines.  This program simulates a sort of dir command.
  4.  *
  5.  * -+=SDB=+-
  6.  *
  7.  * Copyright (c) 1987, Scott Ballantyne
  8.  * Use and abuse as you please.
  9.  */
  10.  
  11. #include <exec/types.h>
  12. #include <libraries/arpbase.h>
  13. #include <libraries/dos.h>    /* For ERROR_NO_MORE_ENTRIES, ^C, etc. */
  14. #include <arpfunctions.h>
  15.  
  16. /* A slightly extended AnchorPath structure */
  17. /* We extend the anchor structure in this way because we will be
  18.  * using the pattern matching functions to build complete path names.
  19.  * If you don't want to use this ability, then you don't need to extend
  20.  * the structure at all
  21.  */
  22.  
  23. struct UserAnchor {
  24.     struct    AnchorPath    ua_AP;
  25.     BYTE    moremem[255];    /* cheap way to extend ap_Buf[] */
  26. };
  27.  
  28. /* Our templates and help strings */
  29.  
  30. char *CLI_Template = "Pattern";
  31. char *CLI_Help = "List Files or directories by wildcard";
  32.  
  33. main(argc, argv)
  34. int argc;
  35. char **argv;
  36. {
  37.     struct    UserAnchor *Anchor;
  38.     char    *pat;
  39.     LONG    Result;
  40.  
  41.     if (argc < 2)
  42.         pat = "*";    /* All files/dirs in current directory */
  43.     else
  44.         pat = argv[1];
  45.  
  46.     /* Because a struct AnchorPath contains the BCPL structure
  47.      * FileInfoBlock, we must always insure that the AnchorPath is
  48.      * longword aligned.  A simple way to do this and also track it
  49.      * is to call ArpAlloc()
  50.      */
  51.  
  52.     if ( Anchor = (struct UserAnchor *)ArpAlloc( (ULONG)sizeof( *Anchor )) )
  53.     {
  54.         Anchor->ua_AP.ap_Length = 255;    /* Want full path built */
  55.         
  56.         /* We will be breaking on control 'C's */
  57.         /* and on control 'D's, just to show how */        
  58.         Anchor->ua_AP.ap_BreakBits |= SIGBREAKF_CTRL_C;
  59.         Anchor->ua_AP.ap_BreakBits |= SIGBREAKF_CTRL_D;
  60.     }
  61.     else
  62.     {
  63.         Puts("No memory!");
  64.         exit(20);
  65.     }
  66.  
  67.     Result = FindFirst( pat, Anchor);
  68.  
  69.     while ( Result == 0 )
  70.     {
  71.         Printf("%s", Anchor->ua_AP.ap_Buf);    /* Display name */
  72.  
  73.         if (Anchor->ua_AP.ap_Info.fib_DirEntryType >= 0)
  74.             Puts( "\t(dir)" );        /* Display type */
  75.         else
  76.             Puts("");    /* Just a newline */
  77.         Result = FindNext( Anchor );
  78.     }
  79.  
  80.     /* Free the Anchor chain built by the above functions */
  81.     FreeAnchorChain( Anchor );
  82.  
  83.     /* Finally, check the result - if it is ERROR_NO_MORE_ENTRIES then
  84.      * we are exiting normally, otherwise, it is either a ^C break
  85.      * or an actual error of some kind.
  86.      */
  87.     if (Result == ERROR_BREAK)    /* Control 'C' */
  88.     {
  89.         if ( Anchor->ua_AP.ap_FoundBreak & SIGBREAKF_CTRL_C)
  90.             Puts("***Break on '^C'");
  91.         else
  92.             Puts("***Break on '^D'");
  93.     }
  94.     else if (Result == ERROR_OBJECT_NOT_FOUND)
  95.     {
  96.         Printf("Can't find %s\n", pat);
  97.         exit(20);
  98.     }
  99.     else if (Result == ERROR_BUFFER_OVERFLOW)
  100.     {
  101.         Puts("I should have allocated a larger buffer. Sorry!");
  102.         exit(20);
  103.     }
  104.     else if (Result != ERROR_NO_MORE_ENTRIES)
  105.     {
  106.         Puts("An IO error occured!");
  107.         Printf("%ld\n", Result);
  108.         exit(20);
  109.     }
  110.     /* else */
  111.     exit(0);
  112. }
  113.  
  114.