int efile_readdir(Efile_error* errInfo, /* Where to return error codes. */ char* name, /* Name of directory to open. */ EFILE_DIR_HANDLE* p_dir_handle, /* Pointer to directory handle of open directory.*/ char* buffer, /* Pointer to buffer for one filename. */ size_t *size) /* in-out Size of buffer, length of name. */ { DIR *dp; /* Pointer to directory structure. */ struct dirent* dirp; /* Pointer to directory entry. */ /* * If this is the first call, we must open the directory. */ if (*p_dir_handle == NULL) { dp = opendir(name); if (dp == NULL) return check_error(-1, errInfo); *p_dir_handle = (EFILE_DIR_HANDLE) dp; } /* * Retrieve the name of the next file using the directory handle. */ dp = *((DIR **)((void *)p_dir_handle)); for (;;) { dirp = readdir(dp); if (dirp == NULL) { closedir(dp); return 0; } if (IS_DOT_OR_DOTDOT(dirp->d_name)) continue; buffer[0] = '\0'; strncat(buffer, dirp->d_name, (*size)-1); *size = strlen(dirp->d_name); return 1; } }
int efile_readdir(Efile_error* errInfo, /* Where to return error codes. */ char* name, /* Name of directory to list */ EFILE_DIR_HANDLE* dir_handle, /* Handle of opened directory or NULL */ char* buffer, /* Buffer to put one filename in */ size_t *size) /* in-out size of buffer/size of filename excluding zero termination in bytes*/ { HANDLE dir; /* Handle to directory. */ WCHAR wildcard[MAX_PATH]; /* Wildcard to search for. */ WIN32_FIND_DATAW findData; /* Data found by FindFirstFile() or FindNext(). */ /* Alignment is not honored, this works on x86 because of alignment fixup by processor. Not perfect, but faster than alinging by hand (really) */ WCHAR *wname = (WCHAR *) name; WCHAR *wbuffer = (WCHAR *) buffer; /* * First time we must setup everything. */ if (*dir_handle == NULL) { int length = wcslen(wname); WCHAR* s; if (length+3 >= MAX_PATH) { errno = ENAMETOOLONG; return check_error(-1, errInfo); } wcscpy(wildcard, wname); s = wildcard+length-1; if (*s != L'/' && *s != L'\\') *++s = L'\\'; *++s = L'*'; *++s = L'\0'; DEBUGF(("Reading %ws\n", wildcard)); dir = FindFirstFileW(wildcard, &findData); if (dir == INVALID_HANDLE_VALUE) return set_error(errInfo); *dir_handle = (EFILE_DIR_HANDLE) dir; if (!IS_DOT_OR_DOTDOT(findData.cFileName)) { wcscpy(wbuffer, findData.cFileName); *size = wcslen(wbuffer)*2; return 1; } } /* * Retrieve the name of the next file using the directory handle. */ dir = (HANDLE) *dir_handle; for (;;) { if (FindNextFileW(dir, &findData)) { if (IS_DOT_OR_DOTDOT(findData.cFileName)) continue; wcscpy(wbuffer, findData.cFileName); *size = wcslen(wbuffer)*2; return 1; } if (GetLastError() == ERROR_NO_MORE_FILES) { FindClose(dir); errInfo->posix_errno = errInfo->os_errno = 0; return 0; } set_error(errInfo); FindClose(dir); return 0; } }