home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 300-399 / ff353.lzh / AztecArp / scdir.c < prev    next >
C/C++ Source or Header  |  1990-06-02  |  2KB  |  127 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. struct AnchorPath *findfirst(), *findnext();
  8.  
  9.     /* Olsen: these two defines were MISSING - nothing worked! */
  10.  
  11. #define    SET_ID(t,i) ((SHORT *) t)[-1]=i
  12. #define arpisdir(ap) (ap -> ap_Info . fib_DirEntryType > 0)
  13.  
  14. char *scdir (pat)
  15. char *pat;
  16. {
  17.     register struct AnchorPath *ap;
  18.     static char time = 0;
  19.  
  20.     Chk_Abort();
  21.  
  22.     do {
  23.     if (!time) {        /* new pattern */
  24.         time = 1;
  25.         ap = findfirst(pat);
  26.     }
  27.     else {            /* continue pattern */
  28.         ap = findnext();
  29.     }
  30.     } while (ap && arpisdir(ap));
  31.  
  32.     if (!ap) {          /* no more (return null) */
  33.     time = 0;
  34.     return NULL;
  35.     }
  36.  
  37.     return ap->ap_Buf;    /* return ptr to name */
  38. }
  39.  
  40.  
  41. /* static struct DefaultTracker *_tr,*_tr2;  */
  42. static struct AnchorPath *_ap;
  43.  
  44. static
  45. struct AnchorPath *findfirst(path)
  46. char *path;
  47. {
  48.     struct AnchorPath *findrtn();
  49.     struct AnchorPath *allocap();
  50.  
  51.     if (!_ap && !allocap()) return NULL;
  52.  
  53.     return findrtn (FindFirst (path,_ap));
  54. }
  55.  
  56. static
  57. struct AnchorPath *findnext()
  58. {
  59.     struct AnchorPath *findrtn();
  60.  
  61.     return findrtn (FindNext (_ap));
  62. }
  63.  
  64. static
  65. struct AnchorPath *findrtn(rc)
  66. ULONG rc;
  67. {
  68.     switch (rc) {
  69.     case 0:
  70.         return _ap;
  71.  
  72.     case ERROR_BREAK:
  73.         _abort();
  74.     case ERROR_NO_MORE_ENTRIES:
  75.         errno = 0;
  76.         break;
  77.  
  78.     default:
  79.         errno = rc;
  80.         break;
  81.     }
  82.  
  83.     FreeAnchorChain (_ap);
  84.  
  85.     return NULL;
  86. }
  87.  
  88.  
  89. static
  90. struct AnchorPath *allocap()
  91. {
  92.     register struct AnchorPath *ap = NULL;
  93.  
  94.     if ( (ap = ArpAlloc((long)sizeof *ap + MAXNAMELEN)) ) {
  95.     SET_ID(ap,TRAK_ANCHOR);
  96.  
  97.     if (Enable_Abort) ap->ap_BreakBits = SIGBREAKF_CTRL_C;
  98.     ap->ap_StrLen = MAXNAMELEN;
  99.     _ap = ap;
  100.     }
  101.     else errno = ENOMEM;
  102.  
  103.     return ap;
  104.  
  105. #if 0
  106.     register struct DefaultTracker *tr;
  107.     register struct AnchorPath *ap = NULL;
  108.  
  109.     /* !!! the order of these two allocations is VERY important -
  110.            unless done in this order, FreeTaskResList() will not be
  111.            able to call FreeAnchorChain() correctly */
  112.  
  113.     if ( (ap = ArpAlloc((long)sizeof *ap + MAXNAMELEN)) && (tr = GetTracker(TRAK_ANCHOR)) ) {
  114.     tr->dt_Object.dt_Resource = (CPTR)ap;
  115.     if (Enable_Abort) ap->ap_BreakBits = SIGBREAKF_CTRL_C;
  116.     ap->ap_StrLen = MAXNAMELEN;
  117.     _ap = ap;
  118.     }
  119.     else {
  120.     if (tr) FreeTrackedItem(tr);
  121.     errno = ENOMEM;
  122.     }
  123.     return ap;
  124. #endif
  125.  
  126. }
  127.