version(Win32) { void listdir(char[] pathname, bool delegate(char[] filename) callback) { HANDLE h; pathname = std.path.join(pathname, "*"); if(useWfuncs) { WIN32_FIND_DATAW fdata; h = FindFirstFileW(std.utf.toUTF16z(pathname), &fdata); if(INVALID_HANDLE_VALUE != h) { do { // Skip "." and ".." if(!std.string.wcscmp(fdata.cFileName, ".") || !std.string.wcscmp(fdata.cFileName, "..")) continue; int len = std.string.wcslen(fdata.cFileName); // Assumes toUTF8() returns a new buffer. if(!callback(std.utf.toUTF8(fdata.cFileName[0 .. len]))) break; } while(FindNextFileW(h, &fdata)); FindClose(h); } } else { WIN32_FIND_DATA fdata; h = FindFirstFileA(toMBSz(pathname), &fdata); if(INVALID_HANDLE_VALUE != h) { do { // Skip "." and ".." if(!std.string.strcmp(fdata.cFileName, ".") || !std.string.strcmp(fdata.cFileName, "..")) continue; int len = std.string.strlen(fdata.cFileName); if(!callback(fdata.cFileName[0 .. len].dup)) break; } while(FindNextFileA(h, &fdata)); FindClose(h); } } } } version(linux) { // This should be added to std/c/linux/dirent.d or std/c/linux/linux.d extern(C) { struct dirent { int d_ino; off_t d_off; ushort d_reclen; ubyte d_type; char[256] d_name; } struct DIR { // Managed by OS. } DIR* opendir(char* name); // Returns null on failure. int closedir(DIR* dir); // Returns 0 on success, -1 on failure. dirent* readdir(DIR* dir); // Returns null if error or EOF. void rewinddir(DIR* dir); // Go back to beginning of directory. off_t telldir(DIR* dir); // Returns current location, or -1 if error. void seekdir(DIR* dir, off_t offset); // Set location for next readdir(). } void listdir(char[] pathname, bool delegate(char[] filename) callback) { DIR* h; dirent* fdata; h = opendir(toStringz(pathname)); if(h) { while((fdata = readdir(h)) != null) { // Skip "." and ".." if(!std.string.strcmp(fdata.d_name, ".") || !std.string.strcmp(fdata.d_name, "..")) continue; int len = std.string.strlen(fdata.d_name); if(!callback(fdata.d_name[0 .. len].dup)) break; } closedir(h); } } } char[][] listdir(char[] pathname) { char[][] result; bool listing(char[] filename) { result ~= filename; return true; // continue } listdir(pathname, &listing); return result; } version(LISTDIR_TEST) int main() { foreach(char[] s; listdir(".")) { printf("'%.*s'\n", s); } return 0; }