@@ -100,6 +100,7 @@ static term nif_binary_split(Context *ctx, int argc, term argv[]);
100100static term nif_binary_replace (Context * ctx , int argc , term argv []);
101101static term nif_binary_match (Context * ctx , int argc , term argv []);
102102static 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 []);
103104static term nif_erlang_delete_element_2 (Context * ctx , int argc , term argv []);
104105static term nif_erlang_atom_to_binary (Context * ctx , int argc , term argv []);
105106static 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+
538544static 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+
18041860static term nif_erlang_make_tuple_2 (Context * ctx , int argc , term argv [])
18051861{
18061862 UNUSED (argc );
0 commit comments