directory listing, return table of files in a directory */
| 855 | directory listing, return table of files in a directory |
| 856 | */ |
| 857 | int lua_dirlist(lua_State *L) { |
| 858 | binding_argcheck(L, 1); |
| 859 | |
| 860 | struct dirent *entry; |
| 861 | int i; |
| 862 | const char *path = luaL_checkstring(L, 1); |
| 863 | |
| 864 | /* open directory */ |
| 865 | auto dir = AP::FS().opendir(path); |
| 866 | if (dir == nullptr) { /* error opening the directory? */ |
| 867 | lua_pushnil(L); /* return nil and ... */ |
| 868 | lua_pushstring(L, strerror(errno)); /* error message */ |
| 869 | return 2; /* number of results */ |
| 870 | } |
| 871 | |
| 872 | /* create result table */ |
| 873 | lua_newtable(L); |
| 874 | i = 1; |
| 875 | while ((entry = AP::FS().readdir(dir)) != nullptr) { |
| 876 | lua_pushnumber(L, i++); /* push key */ |
| 877 | lua_pushstring(L, entry->d_name); /* push value */ |
| 878 | lua_settable(L, -3); |
| 879 | } |
| 880 | |
| 881 | AP::FS().closedir(dir); |
| 882 | return 1; /* table is already on top */ |
| 883 | } |
| 884 | |
| 885 | /* |
| 886 | remove a file |
nothing calls this directly
no test coverage detected