10
opendir(const char* pDirName)
22
if (stat(pDirName, &sb) != 0) {
26
if ((sb.st_mode & S_IFMT) != S_IFDIR) {
31
/* allocate a DIR structure to return */
32
pDir = (DIR *) malloc(sizeof (DIR));
37
/* input directory name length */
38
nBufferLen = strlen(pDirName);
40
/* copy input directory name to DIR buffer */
41
strcpy(pDir->dir_pDirectoryName, pDirName);
43
/* point to end of the copied directory name */
44
pEndDirName = &pDir->dir_pDirectoryName[nBufferLen - 1];
46
/* if directory name did not end in '/' or '\', add '/' */
47
if ((*pEndDirName != '/') && (*pEndDirName != '\\')) {
52
/* now append the wildcard character to the buffer */
58
/* other values defaulted */
59
pDir->dir_nNumFiles = 0;
60
pDir->dir_hDirHandle = INVALID_HANDLE_VALUE;
61
pDir->dir_ulCookie = __DIRENT_COOKIE;
69
/* got a valid pointer? */
75
/* sanity check that this is a DIR pointer */
76
if (pDir->dir_ulCookie != __DIRENT_COOKIE) {
81
/* close the WINDOWS32 directory handle */
82
if (pDir->dir_hDirHandle != INVALID_HANDLE_VALUE)
83
FindClose(pDir->dir_hDirHandle);
93
WIN32_FIND_DATA wfdFindData;
100
/* sanity check that this is a DIR pointer */
101
if (pDir->dir_ulCookie != __DIRENT_COOKIE) {
106
if (pDir->dir_nNumFiles == 0) {
107
pDir->dir_hDirHandle = FindFirstFile(pDir->dir_pDirectoryName, &wfdFindData);
108
if (pDir->dir_hDirHandle == INVALID_HANDLE_VALUE)
110
} else if (!FindNextFile(pDir->dir_hDirHandle, &wfdFindData))
113
/* bump count for next call to readdir() or telldir() */
114
pDir->dir_nNumFiles++;
116
/* fill in struct dirent values */
117
pDir->dir_sdReturn.d_ino = -1;
118
strcpy(pDir->dir_sdReturn.d_name, wfdFindData.cFileName);
120
return &pDir->dir_sdReturn;
131
/* sanity check that this is a DIR pointer */
132
if (pDir->dir_ulCookie != __DIRENT_COOKIE) {
137
/* close the WINDOWS32 directory handle */
138
if (pDir->dir_hDirHandle != INVALID_HANDLE_VALUE)
139
if (!FindClose(pDir->dir_hDirHandle))
142
/* reset members which control readdir() */
143
pDir->dir_hDirHandle = INVALID_HANDLE_VALUE;
144
pDir->dir_nNumFiles = 0;
157
/* sanity check that this is a DIR pointer */
158
if (pDir->dir_ulCookie != __DIRENT_COOKIE) {
163
/* return number of times readdir() called */
164
return pDir->dir_nNumFiles;
168
seekdir(DIR* pDir, long nPosition)
173
/* sanity check that this is a DIR pointer */
174
if (pDir->dir_ulCookie != __DIRENT_COOKIE)
177
/* go back to beginning of directory */
180
/* loop until we have found position we care about */
181
for (--nPosition; nPosition && readdir(pDir); nPosition--);
183
/* flag invalid nPosition value */