home *** CD-ROM | disk | FTP | other *** search
/ OpenStep (Enterprise) / OpenStepENTCD.toast / OEDEV / GNUSRC.Z / readdir.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-01-17  |  2.2 KB  |  129 lines

  1. #ifdef WIN32
  2.  
  3. #include <windows.h>
  4. #include <dirent.h>
  5. #include <string.h>
  6. #define BOOL short
  7. typedef struct tagWin32GnuDir {
  8.     DIR        dir;
  9.     struct dirent dent;
  10.     char          namefill[MAX_PATH];
  11. } Win32GnuDir;
  12.  
  13. typedef Win32GnuDir   * PW32GDIR;
  14.  
  15. DIR * opendir(path)
  16. char * path;
  17. {
  18.     DIR     *dir=0;
  19.     char     fpath[MAX_PATH],full[MAX_PATH];
  20.     LPTSTR    end,put;
  21.     DWORD    fulllen;
  22.     DWORD    fileattr,volattr;
  23.     char    tmp[4];
  24.     strcpy(fpath,path);
  25.     end=fpath;
  26.     while(((end=strchr(fpath,'/'))))
  27.     *end='\\';
  28.     GetFullPathName(fpath,
  29.             MAX_PATH,
  30.             full,
  31.             0);
  32.     if((fileattr=GetFileAttributes(fpath))!=0xffffffff) 
  33.     {
  34.     if (fileattr & FILE_ATTRIBUTE_DIRECTORY)
  35.     {
  36.         dir=calloc(sizeof(Win32GnuDir),1);
  37.         strcat(full,"\\*");
  38.         dir->__data=calloc(sizeof(WIN32_FIND_DATA),1);
  39.         dir->__fd=FindFirstFile(full,
  40.                     dir->__data);
  41.         dir->__allocation=dir->__size=sizeof(WIN32_FIND_DATA);
  42.     }
  43.     }
  44.     return(dir);
  45. }
  46.  
  47.  
  48.  
  49. int closedir(dir)
  50. DIR * dir;
  51. {
  52.     int retval=-1;
  53.     if (dir) 
  54.     {
  55.     if (FindClose(dir->__fd) || dir->__fd == 0xffffffff)
  56.         retval=0;
  57.     free(dir->__data);
  58.     free(dir);
  59.     }
  60.     return(retval);
  61. }
  62.  
  63. struct dirent *readdir(dir)
  64. DIR * dir;
  65. {
  66.     struct dirent *retdirent=0;
  67.     LPWIN32_FIND_DATA    find;
  68.     DWORD  diridx;
  69.     if     (dir) 
  70.     {    
  71.     diridx=dir->__offset/sizeof(WIN32_FIND_DATA);
  72.     find=dir->__data;
  73.     if (strlen(find[diridx].cFileName))
  74.     {
  75.         strcpy(&dir->__entry.d_name,&find[diridx].cFileName);
  76.         dir->__offset+=sizeof(WIN32_FIND_DATA);
  77.         if (dir->__offset>=dir->__size) 
  78.         {
  79.         dir->__size+=sizeof(WIN32_FIND_DATA);
  80.         dir->__allocation=dir->__size;
  81.         find=dir->__data=realloc(find,dir->__size);
  82.         memset(&find[diridx+1],0,sizeof(WIN32_FIND_DATA));
  83.         FindNextFile(dir->__fd,&find[diridx+1]);
  84.         }
  85.         retdirent=&dir->__entry;
  86.         dir->__entry.d_namlen=strlen(dir->__entry.d_name);
  87.     }
  88.     }
  89.     return(retdirent);
  90. }
  91.  
  92.  
  93.     
  94.  
  95. void rewinddir(dirp)
  96. DIR *dirp;
  97. {
  98.     if (dirp)
  99.     {    
  100.     dirp->__offset=0;
  101.     }
  102. }
  103.  
  104. void seekdir(dirp, pos)
  105. DIR *dirp;
  106. _off_t pos;
  107. {
  108.     if (dirp) 
  109.     {
  110.     pos=(pos/sizeof(WIN32_FIND_DATA))*sizeof(WIN32_FIND_DATA);
  111.     if (pos<dirp->__size)
  112.         dirp->__offset=pos;
  113.     }
  114. }
  115.  
  116.  
  117. _off_t telldir(dirp)
  118. DIR * dirp;
  119. {
  120.     if (dirp)
  121.     return(dirp->__offset);
  122.     else
  123.     return(0xffffffff);
  124. }
  125.  
  126. #endif
  127.            
  128.  
  129.