home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 9 / FreshFishVol9-CD2.bin / bbs / gnu / libnix-0.8-src.lha / libnix-0.8 / sources / nix / extra / dirent.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-12  |  1.9 KB  |  83 lines

  1. #include <dirent.h>
  2. #include <stdlib.h>
  3. #include <exec/memory.h>
  4. #include <proto/exec.h>
  5. #include <proto/dos.h>
  6. #include <strsup.h>
  7.  
  8. DIR *opendir(const char *dirname)
  9. {
  10.   DIR *dirp;
  11.  
  12.   if ((dirp=(DIR *)AllocVec(sizeof(DIR),MEMF_PUBLIC)) != NULL)
  13.   {
  14.     if ((dirp->d_lock=(Lock((STRPTR)dirname,SHARED_LOCK))) != 0ul)
  15.     {
  16.       dirp->d_count=0; dirp->d_more = DOSTRUE;
  17.  
  18.       if ((dirp->d_eac=AllocDosObject(DOS_EXALLCONTROL,NULL)) != NULL)
  19.       {
  20. #if 0
  21.         dirp->d_eac->eac_LastKey=0;
  22.         dirp->d_eac->eac_MatchString=NULL;
  23.         dirp->d_eac->eac_MatchFunc=NULL;
  24. #endif
  25.         if (Examine(dirp->d_lock,&dirp->d_info))
  26.         {
  27.           if (dirp->d_info.fib_EntryType>=0)
  28.             return (DIR *)dirp;
  29.         }
  30.         FreeDosObject(DOS_EXALLCONTROL,dirp->d_eac);
  31.       }
  32.       UnLock(dirp->d_lock);
  33.     }
  34.     FreeVec(dirp); dirp=NULL;
  35.   }
  36.   return dirp;
  37. }
  38.  
  39. struct dirent *readdir(DIR *dirp)
  40. {
  41.   struct dirent *result;
  42.  
  43.   if (dirp->d_count==0 && dirp->d_more!=DOSFALSE)
  44.   {
  45.     dirp->d_more=ExAll(dirp->d_lock,(APTR)&dirp->d_ead[0],sizeof(dirp->d_ead),ED_NAME,dirp->d_eac);
  46.     dirp->current=(struct ExAllData *)&dirp->d_ead[0];
  47.     dirp->d_count=dirp->d_eac->eac_Entries;
  48.   }
  49.  
  50.   result = NULL;
  51.  
  52.   if (dirp->d_count)
  53.   {
  54.     dirp->dd_ent.d_fileno = dirp->dd_ent.d_reclen = 1;
  55.     strcpy(dirp->dd_ent.d_name,dirp->current->ed_Name);
  56.     dirp->dd_ent.d_namlen = strlen(dirp->dd_ent.d_name);
  57.     dirp->current=dirp->current->ed_Next;
  58.     dirp->d_count--;
  59.     result=&dirp->dd_ent;
  60.   }
  61.   return result;
  62. }
  63.  
  64. void rewinddir(DIR *dirp)
  65. {
  66.   if (dirp->d_more!=DOSFALSE)
  67.     do
  68.      {
  69.        dirp->d_more=ExAll(dirp->d_lock,(APTR)&dirp->d_ead[0],sizeof(dirp->d_ead),ED_NAME,dirp->d_eac);
  70.      }
  71.     while(dirp->d_more!=DOSFALSE);
  72.   dirp->d_count=0; dirp->d_more = DOSTRUE; dirp->d_eac->eac_LastKey=0;
  73. }
  74.  
  75. int closedir(DIR *dirp)
  76. {
  77.   rewinddir(dirp);
  78.   FreeDosObject(DOS_EXALLCONTROL,dirp->d_eac);
  79.   UnLock(dirp->d_lock);
  80.   FreeVec(dirp);
  81.   return 0;
  82. }
  83.