[comp.sys.amiga.tech] scdir

jbwaters@bsu-cs.UUCP (J. Brian Waters) (09/22/88)

After running into some problems with the Manx supplied scdir() function I
wanted to write my own replacment for it.  I used the arp.library function
to be able to take advantage of its wildcard features.  The routine below
worked until I tried to use the resource tracking features of arp.  Can
anyone see what I am doing wrong?  It bombs on the free(Anchor) command
with the guru for AN_Free_Twice.

#include <exec/types.h>
#include <libraries/arpbase.h>

extern void *calloc();
extern long FindNext(), FindFirst();
extern struct DefaultTracker *GetTracker();
extern struct DefaultTracker *LastTracker;


struct UserAnchor {
	struct	AnchorPath	ua_AP;
	BYTE	moremem[255];	/* cheap way to extend ap_Buf[] */
};

char *
scdir(pat)
char *pat;
{
   static int first_time = 1;
   static struct  UserAnchor *Anchor;
   static struct DefaultTracker *anchor_tracker;  /* track so we can always clean up */
   LONG    Result;

/*   Chk_Abort(); */
   
   if (first_time) {
       /* calloc works as Manx passes [cm]alloc() on to AllocMem so longword
           alignment requirment is satisfied */
        if ( Anchor = calloc( 1, sizeof( *Anchor )) ) {
           Anchor->ua_AP.ap_Length = 255;   /* Want full path info */
          }
        else
          return(0L);
    }

   while (1) {
     if (first_time) {
       first_time = 0;
       Result = FindFirst(pat, Anchor);
       if ( anchor_tracker = GetTracker( TRAK_ANCHOR ) )
          anchor_tracker->dt_Object.dt_Resource = (CPTR)Anchor;
       else 
          Result = 0;  /* can not get tracker so force failure */ 
      }
     else Result = FindNext(Anchor);

     if ((Result == 0) && Anchor->ua_AP.ap_Info.fib_DirEntryType >= 0) continue; /* skip dirs */

     if (Result == 0) return(Anchor->ua_AP.ap_Buf);
     else {   /* failed to find more files */
       first_time = 1;  /* reset first_time to TRUE */
       FreeTrackedItem(anchor_tracker);
       free(Anchor);  /* bombs here with AN_FreeTwice is arp freeing it on me? */
       return(0L);
      }

  }  /* end of while (1) */

} /* end of scdir() */
 -- 
Brian Waters              <backbone>!{iuvax|pur-ee}!bsu-cs!jbwaters
                                          uunet!---/