From f5fea69ef9cb8edd6644a123d9100e5b7282f2a8 Mon Sep 17 00:00:00 2001 From: Dmitry Atamanov Date: Thu, 24 Aug 2023 16:10:28 +0500 Subject: [PATCH] Move `get_var`, `set_var` and `clear_var` to `os::env` module. --- lib/std/core/env.c3 | 57 +-------------------------------------------- lib/std/os/env.c3 | 57 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 56 deletions(-) create mode 100644 lib/std/os/env.c3 diff --git a/lib/std/core/env.c3 b/lib/std/core/env.c3 index 4a5817f75..59623cd2d 100644 --- a/lib/std/core/env.c3 +++ b/lib/std/core/env.c3 @@ -2,7 +2,7 @@ // Use of this source code is governed by the MIT license // a copy of which can be found in the LICENSE_STDLIB file. module std::core::env; -import libc; + enum CompilerOptLevel { O0, @@ -179,58 +179,3 @@ macro bool os_is_posix() return false; $endswitch } - - -/** - * @param [in] name - * @require name.len > 0 - **/ -fn String! get_var(String name) -{ - $if LIBC && !WIN32: - @pool() - { - ZString val = libc::getenv(name.zstr_tcopy()); - return val ? val.as_str() : SearchResult.MISSING?; - }; - $else - return ""; - $endif -} - - -/** - * @param [in] name - * @param [in] value - * @require name.len > 0 - **/ -fn void set_var(String name, String value, bool overwrite = true) -{ - $if LIBC && !WIN32: - @pool() - { - if (libc::setenv(name.zstr_tcopy(), value.zstr_copy(), (int)overwrite)) - { - unreachable(); - } - }; - $endif -} - -/** - * @param [in] name - * @require name.len > 0 - **/ -fn void clear_var(String name) -{ - $if LIBC && !WIN32: - @pool() - { - if (libc::unsetenv(name.zstr_tcopy())) - { - unreachable(); - } - }; - $endif -} - diff --git a/lib/std/os/env.c3 b/lib/std/os/env.c3 new file mode 100644 index 000000000..d4ab784b7 --- /dev/null +++ b/lib/std/os/env.c3 @@ -0,0 +1,57 @@ +// Copyright (c) 2021 Christoffer Lerno. All rights reserved. +// Use of this source code is governed by the MIT license +// a copy of which can be found in the LICENSE_STDLIB file. +module std::os::env; +import libc; + +/** + * @param [in] name + * @require name.len > 0 + **/ +fn String! get_var(String name) +{ + $if env::LIBC && !env::WIN32: + @pool() + { + ZString val = libc::getenv(name.zstr_tcopy()); + return val ? val.as_str() : SearchResult.MISSING?; + }; + $else + return ""; + $endif +} + +/** + * @param [in] name + * @param [in] value + * @require name.len > 0 + **/ +fn void set_var(String name, String value, bool overwrite = true) +{ + $if env::LIBC && !env::WIN32: + @pool() + { + if (libc::setenv(name.zstr_tcopy(), value.zstr_copy(), (int)overwrite)) + { + unreachable(); + } + }; + $endif +} + +/** + * @param [in] name + * @require name.len > 0 + **/ +fn void clear_var(String name) +{ + $if env::LIBC && !env::WIN32: + @pool() + { + if (libc::unsetenv(name.zstr_tcopy())) + { + unreachable(); + } + }; + $endif +}