1
0
mirror of https://github.com/janet-lang/janet synced 2025-04-06 07:17:16 +00:00

Merge pull request #247 from andrewchambers/getenvdflt

Optional default value for os/getenv.
This commit is contained in:
Calvin Rose 2020-01-06 17:27:33 -06:00 committed by GitHub
commit c4047f3f88
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -145,7 +145,7 @@ static Janet os_exit(int32_t argc, Janet *argv) {
static Janet os_getenv(int32_t argc, Janet *argv) {
(void) argv;
janet_fixarity(argc, 1);
janet_arity(argc, 1, 2);
return janet_wrap_nil();
}
@ -407,11 +407,13 @@ static Janet os_environ(int32_t argc, Janet *argv) {
}
static Janet os_getenv(int32_t argc, Janet *argv) {
janet_fixarity(argc, 1);
janet_arity(argc, 1, 2);
const char *cstr = janet_getcstring(argv, 0);
const char *res = getenv(cstr);
return res
? janet_cstringv(res)
: argc == 2
? argv[1]
: janet_wrap_nil();
}
@ -926,7 +928,7 @@ static const JanetReg os_cfuns[] = {
},
{
"os/getenv", os_getenv,
JDOC("(os/getenv variable)\n\n"
JDOC("(os/getenv variable &opt dflt)\n\n"
"Get the string value of an environment variable.")
},
{