home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD1.bin / useful / disk / cdrom / mkisofs / unix / readdir.c < prev    next >
C/C++ Source or Header  |  1994-05-29  |  3KB  |  145 lines

  1. /* readdir.c: */
  2.  
  3. #include <dos/dos.h>
  4. #include <dos/dostags.h>
  5. #include <clib/dos_protos.h>
  6. #include <errno.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include "unixlib.h"
  11.  
  12. #define MAX_DIRS  16
  13.  
  14. #ifdef LATTICE
  15. #include <proto/dos.h>
  16. #endif
  17.  
  18. static short g_module_initialized = 0;
  19. static DIR *g_dir_array[MAX_DIRS];
  20.  
  21. static void Cleanup (void)
  22. {
  23.   int i;
  24.  
  25.   for (i=0; i<MAX_DIRS; i++)
  26.     if (g_dir_array[i]) {
  27.       if (g_dir_array[i]->lock)
  28.         UnLock ((BPTR) g_dir_array[i]->lock);
  29.       if (g_dir_array[i]->fib)
  30.         FreeDosObject (DOS_FIB, (struct FileInfoBlock *) g_dir_array[i]->fib);
  31.     }
  32. }
  33.  
  34. static int Register_Dir (DIR *p_dir)
  35. {
  36.   int i;
  37.   
  38.   for (i=0; i<MAX_DIRS; i++)
  39.     if (!g_dir_array[i]) {
  40.       g_dir_array[i] = p_dir;
  41.       return i+1;
  42.     }
  43.   
  44.   return 0;
  45. }
  46.  
  47. static void Initialize_Module (void)
  48. {
  49.   int i;
  50.  
  51.   g_module_initialized = 1;
  52.   
  53.   for (i=0; i<MAX_DIRS; i++)
  54.     g_dir_array[i] = NULL;
  55.  
  56.   atexit (Cleanup);
  57. }
  58.  
  59. DIR *opendir (const char *p_pathname)
  60. {
  61.   DIR *new = malloc (sizeof (*new));
  62.  
  63.   if (!malloc) {
  64.     errno = ENOMEM;
  65.     return NULL;
  66.   }
  67.   
  68.   new->lock = new->fib = NULL;
  69.  
  70.   if (!(new->dir_index = Register_Dir (new))) {
  71.     free (new);
  72.     errno = EMFILE;
  73.     return NULL;
  74.   }
  75.  
  76.   if (!g_module_initialized)
  77.     Initialize_Module ();
  78.  
  79.   new->lock = (char *) Lock ((UBYTE *) p_pathname, ACCESS_READ);
  80.   if (!new->lock) {
  81.     free (new);
  82.     errno = ENOENT;
  83.     return NULL;
  84.   }
  85.  
  86.   new->fib = (char *) AllocDosObject (DOS_FIB, TAG_DONE);
  87.   if (!new->fib) {
  88.     UnLock ((BPTR) new->lock);
  89.     g_dir_array[new->dir_index] = NULL;
  90.     free (new);
  91.     errno = ENOMEM;
  92.     return NULL;
  93.   }
  94.  
  95.   if (!Examine ((BPTR) new->lock, (struct FileInfoBlock *) new->fib) ||
  96.        ((struct FileInfoBlock *) new->fib)->fib_DirEntryType <= 0) {
  97.     FreeDosObject (DOS_FIB, (struct FileInfoBlock *) new->fib);
  98.     UnLock ((BPTR) new->lock);
  99.     g_dir_array[new->dir_index] = NULL;
  100.     free (new);
  101.     errno = ENOTDIR;
  102.     return NULL;
  103.   }
  104.  
  105.   new->phase = 0;
  106.   return new;
  107. }
  108.  
  109. struct dirent *readdir (DIR *p_dir_ptr)
  110. {
  111.   struct FileInfoBlock *fib = (struct FileInfoBlock *) p_dir_ptr->fib;
  112.  
  113.   switch (p_dir_ptr->phase) {
  114.   case 0:
  115.     strcpy (p_dir_ptr->dirent.d_name, ".");
  116.     p_dir_ptr->dirent.d_reclen = 1;
  117.     p_dir_ptr->phase++;
  118.     return &(p_dir_ptr->dirent);
  119.   case 1:
  120.     strcpy (p_dir_ptr->dirent.d_name, "..");
  121.     p_dir_ptr->dirent.d_reclen = 2;
  122.     p_dir_ptr->phase++;
  123.     return &(p_dir_ptr->dirent);
  124.   default:
  125.     if (!ExNext ((BPTR) p_dir_ptr->lock, fib)) {
  126.       errno = ENOENT;
  127.       return NULL;
  128.     }
  129.  
  130.     strcpy (p_dir_ptr->dirent.d_name, fib->fib_FileName);
  131.     p_dir_ptr->dirent.d_reclen = strlen (fib->fib_FileName);
  132.  
  133.     return &(p_dir_ptr->dirent);
  134.   }
  135. }
  136.  
  137. int closedir (DIR *p_dir_ptr)
  138. {
  139.   g_dir_array[p_dir_ptr->dir_index - 1] = NULL;
  140.   UnLock ((BPTR) p_dir_ptr->lock);
  141.   FreeDosObject (DOS_FIB, (struct FileInfoBlock *) p_dir_ptr->fib);
  142.   free (p_dir_ptr);
  143.   return 0;
  144. }
  145.