home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_01_04 / 1n04023a < prev    next >
Text File  |  1990-07-18  |  3KB  |  67 lines

  1. /*
  2. **  Figure 5 - Complex pattern matching
  3. */
  4.  
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include "dirent.h"
  8.  
  9. int patmat(const char *, const char *);
  10.  
  11. typedef enum {ERROR = -1, FALSE, TRUE} LOGICAL;
  12. #define SUCCESS 0
  13.  
  14. /****************************************************************/
  15. /*                                                              */
  16. /*  dirmask()                                                   */
  17. /*                                                              */
  18. /*  Tests a directory entry for matching patterns. Tests both   */
  19. /*  file name and attributes. Tests for both inclusion specs    */
  20. /*  and exclusion specs.                                        */
  21. /*                                                              */
  22. /*  Parameters: 1 - Pointer to the directory entry's FIND       */
  23. /*                  structure                                   */
  24. /*              2 - Filename for inclusion matching, i.e. if    */
  25. /*                  this spec matches the filename, we matched. */
  26. /*                  Use NULL to match anything.                 */
  27. /*              3 - Filename for exclusion matching, i.e. if    */
  28. /*                  this spec matches the filename, we failed.  */
  29. /*                  Use NULL to exclude nothing.                */
  30. /*              4 - Attribute for inclusion mask. Use FA_ANY    */
  31. /*                  to match anything).                         */
  32. /*              5 - Attribute for exclusion mask. Use zero to   */
  33. /*                  exclude nothing).                           */
  34. /*                                                              */
  35. /*  Returns: SUCCESS if name and attribute matched, else ERROR. */
  36. /*                                                              */
  37. /*  Side effects: Converts patterns to upper case               */
  38. /*                                                              */
  39. /****************************************************************/
  40.  
  41. int dirmask(struct DSTRUCT *dstruct,
  42.             char           *fname_inc,
  43.             char           *fname_exc,
  44.             unsigned        attr_inc,
  45.             unsigned        attr_exc)
  46. {
  47.         if (!dstruct)
  48.                 return ERROR;
  49.         strupr(fname_inc);
  50.         strupr(fname_exc);
  51.         if (fname_inc)
  52.         {
  53.                 if (TRUE != patmat(dstruct->NAME, fname_inc))
  54.                         return ERROR;
  55.         }
  56.         if (fname_exc)
  57.         {
  58.                 if (TRUE == patmat(dstruct->NAME, fname_exc))
  59.                         return ERROR;
  60.         }
  61.         if (!((dstruct->ATTRIBUTE | 0x80) & attr_inc))
  62.                 return ERROR;
  63.         if (dstruct->ATTRIBUTE & attr_exc)
  64.                 return ERROR;
  65.         return SUCCESS;
  66. }
  67.