diff --git a/os/example_getenv.py b/os/example_getenv.py new file mode 100644 index 00000000..6c4a98ee --- /dev/null +++ b/os/example_getenv.py @@ -0,0 +1,3 @@ +import os + +print(os.getenv("HOME", "def")) diff --git a/os/os/__init__.py b/os/os/__init__.py index 82dcf0e4..8e057d51 100644 --- a/os/os/__init__.py +++ b/os/os/__init__.py @@ -32,6 +32,7 @@ _exit_ = libc.func("v", "_exit", "i") getpid_ = libc.func("i", "getpid", "") waitpid_ = libc.func("i", "waitpid", "ipi") system_ = libc.func("i", "system", "s") +getenv_ = libc.func("s", "getenv", "P") R_OK = const(4) W_OK = const(2) @@ -204,6 +205,12 @@ def system(command): check_error(r) return r +def getenv(var, default=None): + var = getenv_(var) + if var is None: + return default + return var + def fsencode(s): if type(s) is bytes: return s