home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD v1.2 / amidev_cd_12.iso / reference / amiga_mail_vol2 / ii-113 / multilist.c < prev   
C/C++ Source or Header  |  1996-01-30  |  5KB  |  142 lines

  1. ;/* multilist.c -- execute me to compile me
  2. sc data=near nominc strmer streq nostkchk saveds ign=73 multilist
  3. slink FROM LIB:c.o multilist.o TO multilist LIB LIB:sc.lib LIB:amiga.lib
  4. quit
  5. */
  6.  
  7. /* This example illustrates how to scan DOS file names without      */
  8. /* having to make a special case for assigns and multiassigns.      */
  9. /* The DoAllAssigns() routine accepts an arbitrary dos path and     */
  10. /* a pointer to a function.  DoAllAssigns() will call this function */
  11. /* passing it a lock to the object named in the path.               */
  12.  
  13. #define BUFSIZE 1024
  14.  
  15. #include <exec/types.h>
  16. #include <exec/memory.h>
  17. #include <dos/dosextens.h>
  18. #include <dos/exall.h>
  19.  
  20. #include <clib/dos_protos.h>
  21. #include <clib/exec_protos.h>
  22. #include <stdio.h>
  23. #include <strings.h>
  24.  
  25. BOOL DoAllAssigns(char *, BOOL (*)());
  26. BOOL ListContents(BPTR);
  27.  
  28. extern struct DosLibrary *DOSBase;
  29.  
  30.  
  31. void main(int argc, char **argv)
  32. {
  33.     if (DOSBase->dl_lib.lib_Version >= 37)
  34.     {
  35.         if (argc > 1)
  36.         {
  37.             (void) DoAllAssigns(argv[1], &ListContents);
  38.         }
  39.     }
  40. }
  41.  
  42.        /* Pass this routine a directory lock and it prints the names of the  */
  43.        /* files and directories in that directory.  If you pass this routine */
  44.        /* a file lock, it just prints the file's name.                       */
  45. BOOL ListContents(BPTR lock)
  46. {
  47.     struct ExAllControl  *myeac;
  48.     struct ExAllData     *myead;
  49.     APTR                 buffer;
  50.     BOOL                 done;
  51.     struct FileInfoBlock *myfib;
  52.  
  53.     if (myfib = AllocDosObject(DOS_FIB, NULL))
  54.     {
  55.         if (Examine(lock, myfib) == DOSTRUE)
  56.         {
  57.             if (myfib->fib_DirEntryType > 0)
  58.             {
  59.                 if (buffer = AllocVec(BUFSIZE, MEMF_PUBLIC))
  60.                 {
  61.                     if (myeac = AllocDosObject(DOS_EXALLCONTROL, NULL))
  62.                     {
  63.                         myeac->eac_LastKey = 0;
  64.  
  65.                         do
  66.                         {
  67.                             done = ExAll(lock, buffer, BUFSIZE, ED_NAME, myeac);
  68.                             myead = buffer;
  69.                             while (myead)
  70.                             {
  71.                                 printf("%s\n", myead->ed_Name);
  72.                                 myead = myead->ed_Next;
  73.                             }
  74.                         } while (done != 0);
  75.                         FreeDosObject(DOS_EXALLCONTROL, myeac);
  76.                     }
  77.                     FreeVec(buffer);
  78.                 }
  79.             }
  80.             else printf("%s\n", myfib->fib_FileName);
  81.         }
  82.         FreeDosObject(DOS_FIB, myfib);
  83.     }
  84.     return TRUE;
  85. }
  86.  
  87.  
  88.  
  89. /* This routine accepts a path string that may include a device name. From   */
  90. /* that string, this routine locks the object named in the path and calls    */
  91. /* the function passback_func() on the lock.  DoAllAssigns() should work on  */
  92. /* paths with assigns and multiassigns, as well as a filesystem-based device */
  93. /* (i.e. df0:, work:, ram:, etc.)                                            */
  94.  
  95. BOOL DoAllAssigns(char *dos_path, BOOL (*passback_func)(BPTR lock))
  96. {
  97.     struct DevProc *dp=NULL;
  98.     struct MsgPort *old_fsport;
  99.     BPTR           lock, old_curdir;
  100.     char           *rest_of_path;
  101.  
  102.  
  103.     while(dp = GetDeviceProc(dos_path, dp))
  104.     {                                        /* I need to cut the device name from  */
  105.         rest_of_path = strchr(dos_path,':'); /* the front of dos_path so I can give */
  106.                                              /* that substring to Lock().           */
  107.         if (rest_of_path == NULL)                 /* There was no device name to    */
  108.             rest_of_path = dos_path;              /* cut off, use the whole string. */
  109.         else
  110.             rest_of_path++;     /* Increment string pointer to just past the colon. */
  111.  
  112.  
  113.  
  114.         old_fsport = SetFileSysTask(dp->dvp_Port); /* in case dp->dvp_Lock is NULL. */
  115.         old_curdir = CurrentDir(dp->dvp_Lock);     /* Lock() locks relative to the  */
  116.                                 /* current directory and falls back to the root of  */
  117.                                 /* the current file system if dp->dvp_Lock is NULL. */
  118.  
  119.         lock = Lock(rest_of_path,SHARED_LOCK);
  120.  
  121.         (void) SetFileSysTask(old_fsport); /* reset the process' default filesystem */
  122.         (void) CurrentDir(old_curdir);     /* port and current dir to their initial */
  123.                                            /* values for clean up later.            */
  124.  
  125.         if (lock)
  126.         {
  127.             if (!(*passback_func)(lock))
  128.             {
  129.                 printf("function returned false\n");
  130.                 UnLock(lock);         /* UnLock() will ignore NULL lock */
  131.                 FreeDeviceProc(dp);
  132.                 return FALSE;
  133.             }
  134.             UnLock(lock);
  135.         }
  136.     }
  137.     if (IoErr() == ERROR_NO_MORE_ENTRIES)
  138.         return TRUE;               /* At present, a bug in DOS prevents this case, */
  139.     else                           /* so DoAllAssigns() always returns FALSE.      */
  140.         return FALSE;
  141. }
  142.