2 Public domain. No warranty. Akira Kakuto
6 DIR * opendir(char *name);
7 struct dirent * readdir(DIR * dirp);
8 int closedir(DIR * dirp);
10 to be used on VC++ 2.0
11 ( by A. Kakuto, 1995 )
12 rewritten on Jan. 18, 1997 ( A.K. )
14 dirent.h should be included
21 #include "kpathsea/dirent.h"
23 static int find(DIR *dp, char *name)
28 pfd = (WIN32_FIND_DATA *)(dp->_d_buf);
29 if((h = FindFirstFile(name,pfd)) == INVALID_HANDLE_VALUE)
31 dp->_d_hdir = (unsigned)h;
32 pfd->dwReserved1 = strlen(pfd->cFileName);
33 dp->_d_magic = pfd->dwFileAttributes;
34 pfd->dwReserved0 = (dp->_d_magic & FILE_ATTRIBUTE_DIRECTORY) ? 1 : 0;
35 dp->_d_direntptr = (struct dirent *)&(pfd->dwReserved0);
40 static int findn(DIR *dp)
46 h = (HANDLE)dp->_d_hdir;
47 pfd = (WIN32_FIND_DATA *)(dp->_d_buf);
48 if((res = FindNextFile(h,pfd)) == FALSE)
50 pfd->dwReserved1 = strlen(pfd->cFileName);
51 dp->_d_magic = pfd->dwFileAttributes;
52 pfd->dwReserved0 = (dp->_d_magic & FILE_ATTRIBUTE_DIRECTORY) ? 1 : 0;
53 dp->_d_direntptr = (struct dirent *)&(pfd->dwReserved0);
58 DIR *opendir(char *fname)
64 if(!(dirptr = (DIR *)malloc(sizeof(DIR))))
69 if(*name != '\0' && *(name+1) == ':')
73 *name='/'; *(name+1)='\0'; goto add_ast;
75 if(*(name+1) == '\0' && (*name == '/' || *name == '\\')) {
76 *name='/'; goto add_ast;
83 if(*lastpos == '/' || *lastpos == '\\') {
84 *lastpos = '\0'; lastpos--;
87 if(*name == '.' && name[1] == '\0') {
88 name[1]='/'; name[2]='\0'; goto add_ast;
90 if(*name == '.' && name[1] == '.' && name[2] == '\0') {
91 name[2]='/'; name[3]='\0'; goto add_ast;
93 if(*lastpos == '.' && (*(lastpos-1) == '/' || *(lastpos-1) == '\\')) {
94 *(lastpos+1)='/'; *(lastpos+2)='\0'; goto add_ast;
96 if(*lastpos == '.' && *(lastpos-1) == '.' &&
97 (*(lastpos-2) == '/' || *(lastpos-2) == '\\')) {
98 *(lastpos+1)='/'; *(lastpos+2)='\0'; goto add_ast;
101 if(find(dirptr, nbuf) != 0) {
105 if(strpbrk(name,"*?") == NULL) {
106 if(dirptr->_d_magic & FILE_ATTRIBUTE_DIRECTORY) {
107 FindClose((HANDLE)dirptr->_d_hdir);
111 if(find(dirptr,nbuf) != 0) {
117 dirptr->_d_nfiles = 0;
121 int closedir(DIR *dirp)
126 h = (HANDLE)dirp->_d_hdir;
136 struct dirent * readdir(DIR *dirp)
140 if(++(dirp->_d_nfiles) == 1) {
141 return (dirp->_d_direntptr);
145 return (dirp->_d_direntptr);