Add os/dir support for linux/posix.

This commit is contained in:
Calvin Rose 2019-03-30 12:36:27 -04:00
parent c5090606a4
commit 5d7dc0a57c
1 changed files with 24 additions and 1 deletions

View File

@ -464,6 +464,23 @@ static Janet os_stat(int32_t argc, Janet *argv) {
return janet_wrap_table(tab);
}
static Janet os_dir(int32_t argc, Janet *argv) {
struct dirent *dp;
DIR *dfd;
janet_arity(argc, 1, 2);
const char *dir = janet_getcstring(argv, 0);
JanetArray *paths = (argc == 2) ? janet_getarray(argv, 1) : janet_array(0);
if ((dfd = opendir(dir)) == NULL) janet_panicf("cannot open directory %s", dir);
/* Read directory items */
while ((dp = readdir(dfd)) != NULL) {
if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, "..")) {
continue;
}
janet_array_push(paths, janet_cstringv(dp->d_name));
}
return janet_wrap_array(paths);
}
#endif /* JANET_REDUCED_OS */
static const JanetReg os_cfuns[] = {
@ -487,9 +504,15 @@ static const JanetReg os_cfuns[] = {
"Get the string value of an environment variable.")
},
#ifndef JANET_REDUCED_OS
{
"os/dir", os_dir,
JDOC("(os/stat dir [, array])\n\n"
"Iterate over files and subdirectories in a directory. Returns an array of paths parts, "
"with only the filename or directory name and no prefix.")
},
{
"os/stat", os_stat,
JDOC("(os/stat path)\n\n"
JDOC("(os/stat path [, tab])\n\n"
"Gets information about a file or directory. Returns a table.")
},
{