diff --git a/src/modules/files/efuns.c b/src/modules/files/efuns.c index 821d86f8df72c30df1075fd661f5dbdd45e8c3de..1274095b3ce191040e14f2f53b83e1d7b5f06705 100644 --- a/src/modules/files/efuns.c +++ b/src/modules/files/efuns.c @@ -1192,40 +1192,66 @@ void f_get_dir(INT32 args) free_string_builder(&sb); if (dir == DO_NOT_WARN(INVALID_HANDLE_VALUE)) { + int err = GetLastError(); #ifdef READDIR_DEBUG - fprintf(stderr, " INVALID_HANDLE_VALUE\n"); + fprintf(stderr, " INVALID_HANDLE_VALUE, error %d\n", err); #endif /* READDIR_DEBUG */ pop_n_elems(args); - push_int(0); + if (err == ERROR_FILE_NOT_FOUND) { + /* Normally there should at least be a "." entry, so this seldom + * happens. But it seems unwise to count on it, considering this + * being Windows and all.. + * + * Note: The error is ERROR_PATH_NOT_FOUND if the directory + * doesn't exist. + */ + push_empty_array(); + } + else { + set_errno_from_win32_error (err); + push_int(0); + } return; } - BEGIN_AGGREGATE_ARRAY(10); + { + int err; - do { + BEGIN_AGGREGATE_ARRAY(10); + + do { #ifdef READDIR_DEBUG - fprintf(stderr, " \"%S\"\n", d.cFileName); + fprintf(stderr, " \"%S\"\n", d.cFileName); #endif /* READDIR_DEBUG */ - /* Filter "." and ".." from the list. */ - if(d.cFileName[0]=='.') - { - if(!d.cFileName[1]) continue; - if(d.cFileName[1]=='.' && !d.cFileName[2]) continue; - } - push_string(make_shared_binary_string1(d.cFileName, wcslen(d.cFileName))); - DO_AGGREGATE_ARRAY(120); - } while(FindNextFileW(dir, &d)); + /* Filter "." and ".." from the list. */ + if(d.cFileName[0]=='.') + { + if(!d.cFileName[1]) continue; + if(d.cFileName[1]=='.' && !d.cFileName[2]) continue; + } + push_string(make_shared_binary_string1(d.cFileName, wcslen(d.cFileName))); + DO_AGGREGATE_ARRAY(120); + } while(FindNextFileW(dir, &d)); + err = GetLastError(); #ifdef READDIR_DEBUG - fprintf(stderr, " DONE\n"); + fprintf(stderr, " DONE, error %d\n", err); #endif /* READDIR_DEBUG */ - FindClose(dir); + FindClose(dir); - END_AGGREGATE_ARRAY; + END_AGGREGATE_ARRAY; + + stack_pop_n_elems_keep_top(args); - stack_pop_n_elems_keep_top(args); + if (err != ERROR_SUCCESS && err != ERROR_NO_MORE_FILES) { + set_errno_from_win32_error (err); + pop_stack(); + push_int (0); + return; + } + } #else /* !__NT__ */