home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 300-399 / ff376.lzh / AztecArp / Sources.LZH / scdir.c < prev    next >
C/C++ Source or Header  |  1990-08-01  |  2KB  |  111 lines

  1. /* Copyright (C) 1986,1987 Manx Software Systems, Inc.  */
  2.  
  3. /* not exactly re-entrant, is it?  it does make a simple directory scanner though */
  4.  
  5. #define MAXNAMELEN 256
  6.  
  7. static struct AnchorPath *    _ap = NULL;
  8.  
  9. char *                scdir(char *pat);
  10. static struct AnchorPath *    findfirst(char *path);
  11. static struct AnchorPath *    findrtn(ULONG rc);
  12. static struct AnchorPath *    allocap(VOID);
  13.  
  14. #define findnext() (findrtn(FindNext(_ap)))
  15.  
  16.     /* Olsen: these two defines were MISSING - nothing worked! */
  17.  
  18. #define    SET_ID(t,i) ((SHORT *) t)[-1]=i
  19. #define arpisdir(ap) (ap -> ap_Info . fib_DirEntryType > 0)
  20.  
  21. char *
  22. scdir(char *pat)
  23. {
  24.     struct AnchorPath *ap;
  25.     static char time = 0;
  26.  
  27.     Chk_Abort();
  28.  
  29.     do
  30.     {
  31.         /* new pattern */
  32.  
  33.         if(!time)
  34.         {
  35.             time = 1;
  36.             ap = findfirst(pat);
  37.         }
  38.         else    /* continue pattern */
  39.             ap = findnext();
  40.     }
  41.     while(ap && arpisdir(ap));
  42.  
  43.         /* no more (return null) */
  44.  
  45.     if(!ap)
  46.     {
  47.         time = 0;
  48.         return(NULL);
  49.     }
  50.  
  51.     return(ap -> ap_Buf);    /* return ptr to name */
  52. }
  53.  
  54. static struct AnchorPath *
  55. findfirst(char *path)
  56. {
  57.     if(!_ap)
  58.         if(!allocap())
  59.             return(NULL);
  60.  
  61.     return(findrtn(FindFirst(path,_ap)));
  62. }
  63.  
  64. static struct AnchorPath *
  65. findrtn(ULONG rc)
  66. {
  67.     switch(rc)
  68.     {
  69.         case 0:
  70.             return(_ap);
  71.  
  72.         case ERROR_BREAK:
  73.             _abort();
  74.  
  75.         case ERROR_NO_MORE_ENTRIES:
  76.             errno = 0;
  77.             break;
  78.  
  79.         default:
  80.             errno = rc;
  81.             break;
  82.     }
  83.  
  84.     FreeAnchorChain(_ap);
  85.  
  86.     return(NULL);
  87. }
  88.  
  89. static
  90. struct AnchorPath *
  91. allocap()
  92. {
  93.     struct AnchorPath *ap = NULL;
  94.  
  95.     if(ap = ArpAlloc(sizeof(struct AnchorPath) + MAXNAMELEN))
  96.     {
  97.         SET_ID(ap,TRAK_ANCHOR);
  98.  
  99.         if(Enable_Abort)
  100.             ap -> ap_BreakBits = SIGBREAKF_CTRL_C;
  101.  
  102.         ap -> ap_StrLen = MAXNAMELEN;
  103.  
  104.         _ap = ap;
  105.     }
  106.     else
  107.         errno = ENOMEM;
  108.  
  109.     return(ap);
  110. }
  111.