1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-25 17:57:17 +00:00

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

View File

@ -464,6 +464,23 @@ static Janet os_stat(int32_t argc, Janet *argv) {
return janet_wrap_table(tab); 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 */ #endif /* JANET_REDUCED_OS */
static const JanetReg os_cfuns[] = { static const JanetReg os_cfuns[] = {
@ -487,9 +504,15 @@ static const JanetReg os_cfuns[] = {
"Get the string value of an environment variable.") "Get the string value of an environment variable.")
}, },
#ifndef JANET_REDUCED_OS #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, "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.") "Gets information about a file or directory. Returns a table.")
}, },
{ {