home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD v1.2 / amidev_cd_12.iso / reference / amiga_mail_vol2 / ii-49 / listdir2.c < prev    next >
C/C++ Source or Header  |  1996-01-30  |  8KB  |  245 lines

  1. ;/* ListDir2.c   AmigaMail seconds ExAll() example.
  2. sc NMINC STRMERGE NOSTKCHK NODEBUG DATA=FAR IGNORE=73 ListDir2.c
  3. slink from ListDir2.o to ListDir2 lib lib:amiga.lib ;if you don't have pragmas
  4. quit
  5.  *
  6.  * Pure code if pragmas are used.
  7.  * Tuesday, 16-Jul-91 16:21:14, Ewout
  8.  *
  9.  * Compiled with SAS/C 6.56
  10.  */
  11.  
  12. /*
  13. Copyright (c) 1991 Commodore-Amiga, Inc.
  14.  
  15. This example is provided in electronic form by Commodore-Amiga,
  16. Inc. for use with the Amiga Mail Volume II technical publication.
  17. Amiga Mail Volume II contains additional information on the correct
  18. usage of the techniques and operating system functions presented in
  19. these examples.  The source and executable code of these examples may
  20. only be distributed in free electronic form, via bulletin board or
  21. as part of a fully non-commercial and freely redistributable
  22. diskette.  Both the source and executable code (including comments)
  23. must be included, without modification, in any copy.  This example
  24. may not be published in printed form or distributed with any
  25. commercial product. However, the programming techniques and support
  26. routines set forth in these examples may be used in the development
  27. of original executable software products for Commodore Amiga
  28. computers.
  29.  
  30. All other rights reserved.
  31.  
  32. This example is provided "as-is" and is subject to change; no
  33. warranties are made.  All use is at your own risk. No liability or
  34. responsibility is assumed.
  35. */
  36.  
  37. #include <exec/memory.h>
  38. #include <dos/dosextens.h>
  39. #include <dos/rdargs.h>
  40. #include <dos/exall.h>
  41. #include <utility/hooks.h>
  42.  
  43. #include <clib/exec_protos.h>
  44. #include <clib/dos_protos.h>
  45. #include <clib/utility_protos.h>
  46.  
  47. /* undef PRAGMAS if you don't have them */
  48. #define PRAGMAS
  49. #undef PRAGMAS
  50. #ifdef PRAGMAS
  51. #include <pragmas/exec_pragmas.h>
  52. #include <pragmas/dos_pragmas.h>
  53. #include <pragmas/utility_pragmas.h>
  54. #else
  55. struct ExecBase *SysBase;
  56. struct DosLibrary *DOSBase;
  57. struct Library *UtilityBase;
  58.  
  59. #endif
  60.  
  61. /* Buffersize to receive filenames in */
  62. #define BUFFERSIZE 512
  63.  
  64.  
  65. VOID            main(VOID);
  66. UWORD           StrLen(UBYTE *);
  67.  
  68. /* SAS/C specific, use asm stub otherwise */
  69. #define ASM __asm
  70. #define REG(x) register __## x
  71. BOOL ASM  ExAllHook(REG(a0) struct Hook * hook,
  72.                     REG(a1) struct ExAllData * data,
  73.                     REG(a2) LONG * datatype);
  74.  
  75. VOID
  76. main(VOID)
  77. {
  78. #ifdef PRAGMAS
  79.     struct DosLibrary *DOSBase;
  80.     struct Library *UtilityBase;
  81.  
  82. #endif
  83.     struct RDArgs  *readargs;
  84.     LONG            rargs[4];
  85.     struct ExAllControl *excontrol;
  86.     struct ExAllData *ead, *buffer;
  87.     struct Hook     exallhook;
  88.     UBYTE          *pattern, *parsebuffer;
  89.     BPTR            sourcelock;
  90.     BOOL            exmore;
  91.     COUNT           i;
  92.     LONG            parselength, type, error;
  93.  
  94. #ifndef PRAGMAS
  95.     /* set up SysBase */
  96.     SysBase = (*((struct Library **) 4));
  97. #endif
  98.  
  99.     /* Fail silently if < 37 */
  100.     if (DOSBase = (struct DosLibrary *) OpenLibrary("dos.library", 37))
  101.     {
  102.         UtilityBase = DOSBase->dl_UtilityBase;
  103.  
  104.         rargs[1] = 0L;
  105.         rargs[2] = 0L;
  106.         if (readargs = ReadArgs("PATTERN/A,DIRS/S,FILES/S", rargs, NULL))
  107.         {
  108.  
  109.             pattern = (UBYTE *) rargs[0];
  110.  
  111.             /*
  112.              * If DIRS or files not specified or both, accept
  113.              * both files and directories
  114.              */
  115.             if (rargs[1] == rargs[2])
  116.                 type = 0;
  117.             else
  118.             {
  119.                 /* Accept only directories */
  120.                 if (rargs[1])
  121.                     type = 1;
  122.                 /* Accept only files */
  123.                 else
  124.                     type = -1;
  125.             }
  126.  
  127.             parselength = StrLen(pattern) * 3;
  128.             if (parsebuffer = AllocMem(parselength, MEMF_CLEAR))
  129.             {
  130.  
  131.                 /* Make pattern uppercase for possible character classes */
  132.                 i = 0;
  133.                 while (pattern[i])
  134.                     pattern[i] = ToUpper(pattern[i++]);
  135.  
  136.                 if ((ParsePatternNoCase(pattern, parsebuffer, parselength)) != -1)
  137.                 {
  138.  
  139.                     if (buffer = AllocMem(BUFFERSIZE, MEMF_CLEAR))
  140.                     {
  141.  
  142.                         sourcelock =
  143.                                 ((struct Process *) FindTask(NULL))->pr_CurrentDir;
  144.  
  145.                         if (excontrol = AllocDosObject(DOS_EXALLCONTROL, NULL))
  146.                         {
  147.  
  148.                             exallhook.h_Entry = (ULONG (*)())ExAllHook;
  149.                             exallhook.h_Data = (VOID *) type;
  150.  
  151.                             excontrol->eac_MatchString = parsebuffer;
  152.                             excontrol->eac_MatchFunc = &exallhook;
  153.  
  154.                             do
  155.                             {
  156.  
  157.                                 exmore = ExAll(sourcelock,
  158.                                                buffer,
  159.                                                BUFFERSIZE,
  160.                                                ED_TYPE,
  161.                                                excontrol);
  162.                                 error = IoErr();
  163.                                 if ((exmore == NULL &&
  164.                                     (error != ERROR_NO_MORE_ENTRIES)))
  165.                                     break;
  166.  
  167.                                 if (excontrol->eac_Entries == 0)
  168.                                     continue;
  169.  
  170.                                 ead = buffer;
  171.                                 do
  172.                                 {
  173.  
  174.                                     /* Check for CTRL-C */
  175.                                     if (SetSignal(0L, SIGBREAKF_CTRL_C) &
  176.                                         SIGBREAKF_CTRL_C)
  177.                                     {
  178.                                         error = ERROR_BREAK;
  179.                                         exmore = FALSE;
  180.                                         break;
  181.                                     }
  182.  
  183.                                     rargs[0] = (LONG) ead->ed_Name;
  184.                                     VFPrintf(Output(), "%s\n", rargs);
  185.  
  186.                                     ead = ead->ed_Next;
  187.                                 } while (ead);
  188.                             } while (exmore);
  189.  
  190.                             if (error != ERROR_NO_MORE_ENTRIES)
  191.                                 PrintFault(error, NULL);
  192.  
  193.                             FreeDosObject(DOS_EXALLCONTROL, excontrol);
  194.                         }
  195.                         else
  196.                             PrintFault(ERROR_NO_FREE_STORE, NULL);
  197.  
  198.                         FreeMem(buffer, BUFFERSIZE);
  199.                     }
  200.                     else
  201.                         PrintFault(ERROR_NO_FREE_STORE, NULL);
  202.                 }
  203.                 else
  204.                     PrintFault(ERROR_BAD_TEMPLATE, NULL);
  205.                 FreeMem(parsebuffer, parselength);
  206.             }
  207.             else
  208.                 PrintFault(ERROR_NO_FREE_STORE, NULL);
  209.             FreeArgs(readargs);
  210.  
  211.         }
  212.         else
  213.             PrintFault(IoErr(), NULL);
  214.         CloseLibrary((struct Library *) DOSBase);
  215.     }
  216. }
  217.  
  218. BOOL ASM
  219. ExAllHook(REG(a0) struct Hook * hook,
  220.           REG(a1) struct ExAllData * data,
  221.           REG(a2) LONG * datatype)
  222. {
  223.     LONG            neededfiletype = (LONG) hook->h_Data;
  224.     BOOL            success = TRUE;
  225.  
  226.     if (neededfiletype != 0)
  227.     {
  228.         if (data->ed_Type > 0 && neededfiletype < 0)
  229.             success = FALSE;
  230.         if (data->ed_Type < 0 && neededfiletype > 0)
  231.             success = FALSE;
  232.     }
  233.     return (success);
  234. }
  235.  
  236.  
  237. UWORD
  238. StrLen(UBYTE * string)
  239. {
  240.     UBYTE          *length = string + 1;
  241.  
  242.     while (*string++ != '\0');
  243.     return ((UWORD) (string - length));
  244. }
  245.