Skip to content

Commit 3894ec8

Browse files
committed
Add os:getenv
Signed-off-by: Mateusz Front <[email protected]>
1 parent df09ed8 commit 3894ec8

File tree

4 files changed

+60
-1
lines changed

4 files changed

+60
-1
lines changed

src/libAtomVM/nifs.c

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ static term nif_binary_split(Context *ctx, int argc, term argv[]);
100100
static term nif_binary_replace(Context *ctx, int argc, term argv[]);
101101
static term nif_binary_match(Context *ctx, int argc, term argv[]);
102102
static term nif_calendar_system_time_to_universal_time_2(Context *ctx, int argc, term argv[]);
103+
static term nif_os_getenv_1(Context *ctx, int argc, term argv[]);
103104
static term nif_erlang_delete_element_2(Context *ctx, int argc, term argv[]);
104105
static term nif_erlang_atom_to_binary(Context *ctx, int argc, term argv[]);
105106
static term nif_erlang_atom_to_list_1(Context *ctx, int argc, term argv[]);
@@ -535,6 +536,11 @@ static const struct Nif system_time_to_universal_time_nif =
535536
.nif_ptr = nif_calendar_system_time_to_universal_time_2
536537
};
537538

539+
const struct Nif os_getenv_nif = {
540+
.base.type = NIFFunctionType,
541+
.nif_ptr = nif_os_getenv_1
542+
};
543+
538544
static const struct Nif tuple_to_list_nif =
539545
{
540546
.base.type = NIFFunctionType,
@@ -1801,6 +1807,56 @@ term nif_calendar_system_time_to_universal_time_2(Context *ctx, int argc, term a
18011807
return build_datetime_from_tm(ctx, gmtime_r(&ts.tv_sec, &broken_down_time));
18021808
}
18031809

1810+
static term nif_os_getenv_1(Context *ctx, int argc, term argv[])
1811+
{
1812+
UNUSED(argc);
1813+
1814+
term result;
1815+
term env_var_list = argv[0];
1816+
VALIDATE_VALUE(env_var_list, term_is_list);
1817+
1818+
int ok;
1819+
char *env_var = interop_list_to_utf8_string(env_var_list, &ok);
1820+
if (UNLIKELY(!ok)) {
1821+
RAISE_ERROR(BADARG_ATOM);
1822+
}
1823+
1824+
#ifndef AVM_NO_SMP
1825+
smp_spinlock_lock(&ctx->global->env_spinlock);
1826+
#endif
1827+
1828+
const char *env_var_value_tmp = getenv(env_var);
1829+
free(env_var);
1830+
1831+
if (IS_NULL_PTR(env_var_value_tmp)) {
1832+
#ifndef AVM_NO_SMP
1833+
smp_spinlock_unlock(&ctx->global->env_spinlock);
1834+
#endif
1835+
return FALSE_ATOM;
1836+
}
1837+
1838+
char *env_var_value = strdup(env_var_value_tmp);
1839+
1840+
#ifndef AVM_NO_SMP
1841+
smp_spinlock_unlock(&ctx->global->env_spinlock);
1842+
#endif
1843+
1844+
if (IS_NULL_PTR(env_var_value)) {
1845+
RAISE_ERROR(OUT_OF_MEMORY_ATOM);
1846+
}
1847+
1848+
size_t env_value_len = strlen(env_var_value);
1849+
1850+
if (UNLIKELY(memory_ensure_free_opt(ctx, LIST_SIZE(env_value_len, 1), MEMORY_CAN_SHRINK) != MEMORY_GC_OK)) {
1851+
free(env_var_value);
1852+
RAISE_ERROR(OUT_OF_MEMORY_ATOM);
1853+
}
1854+
1855+
result = interop_bytes_to_list(env_var_value, env_value_len, &ctx->heap);
1856+
free(env_var_value);
1857+
return result;
1858+
}
1859+
18041860
static term nif_erlang_make_tuple_2(Context *ctx, int argc, term argv[])
18051861
{
18061862
UNUSED(argc);

src/libAtomVM/nifs.gperf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ binary:replace/4, &binary_replace_nif
4444
binary:match/2, &binary_match_nif
4545
binary:match/3, &binary_match_nif
4646
calendar:system_time_to_universal_time/2, &system_time_to_universal_time_nif
47+
os:getenv/1, &os_getenv_nif
4748
erlang:atom_to_binary/1, &atom_to_binary_nif
4849
erlang:atom_to_binary/2, &atom_to_binary_nif
4950
erlang:atom_to_list/1, &atom_to_list_nif

tests/libs/estdlib/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ set(ERLANG_MODULES
3939
test_maps
4040
test_net
4141
test_net_kernel
42+
test_os
4243
test_sets
4344
test_spawn
4445
test_ssl

tests/libs/estdlib/tests.erl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ get_non_networking_tests(_OTPVersion) ->
8080
test_timer,
8181
test_spawn,
8282
test_supervisor,
83-
test_lists_subtraction
83+
test_lists_subtraction,
84+
test_os
8485
].
8586

8687
get_networking_tests(OTPVersion) when

0 commit comments

Comments
 (0)