Skip to content

Commit d0349bc

Browse files
committed
Refactor environment handling in posix_process_args for improved clarity
- Updated the `cstr_guard` return logic to enhance readability by removing unnecessary return statements. - Introduced a new namespace `posix` to encapsulate platform-specific environment variable handling, including `_NSGetEnviron` for Darwin and `environ` for other platforms. - Streamlined the `get_envs` function to utilize the new `posix` namespace, improving code organization and maintainability.
1 parent c74a312 commit d0349bc

File tree

1 file changed

+17
-13
lines changed
  • include/fast_io_hosted/process/process

1 file changed

+17
-13
lines changed

include/fast_io_hosted/process/process/arg_env.h

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ struct cstr_guard FAST_IO_TRIVIALLY_RELOCATABLE_IF_ELIGIBLE
295295
if (others.cstr == nullptr)
296296
{
297297
cstr = nullptr;
298-
return *this;
298+
return;
299299
}
300300

301301
::std::size_t str_size{::fast_io::cstr_len(others.cstr)};
@@ -473,6 +473,20 @@ struct posix_process_args FAST_IO_TRIVIALLY_RELOCATABLE_IF_ELIGIBLE
473473
return reinterpret_cast<char_const_p_const_p_may_alias_ptr>(arg_envs.data());
474474
}
475475

476+
namespace posix
477+
{
478+
#if defined(__APPLE__) || defined(__DARWIN_C_LEVEL)
479+
// Darwin does not provide an `environ` function; here we use `_NSGetEnviron` to obtain it.
480+
extern char*** _NSGetEnviron() noexcept __asm__("__NSGetEnviron");
481+
#elif defined(__MSDOS__) || defined(__DJGPP__)
482+
// djgpp only provides `char** _environ`. For consistency, a symbolic link is used here.
483+
extern char** environ __asm__("__environ");
484+
#elif !(defined(_WIN32) || defined(__CYGWIN__))
485+
// Reference to the global `environ` variable
486+
extern "C" char** environ;
487+
#endif
488+
} // namespace details
489+
476490
inline char const *const *get_envs() const noexcept
477491
{
478492
using char_const_p_const_p_may_alias_ptr
@@ -484,19 +498,9 @@ struct posix_process_args FAST_IO_TRIVIALLY_RELOCATABLE_IF_ELIGIBLE
484498
if (arg_envs.size() < 2u)
485499
{
486500
#if defined(__APPLE__) && defined(__MACH__)
487-
extern char ***_NSGetEnviron() noexcept __asm__("__NSGetEnviron");
488-
489-
return reinterpret_cast<char_const_p_const_p_may_alias_ptr>(*_NSGetEnviron());
501+
return reinterpret_cast<char_const_p_const_p_may_alias_ptr>(*posix::_NSGetEnviron());
490502
#else
491-
#if defined(__MSDOS__) || defined(__DJGPP__)
492-
// djgpp only provides `char** _environ`. For consistency, a symbolic link is used here.
493-
extern char **environ __asm__("__environ");
494-
#elif !(defined(_WIN32) || defined(__CYGWIN__))
495-
// Reference to the global `environ` variable
496-
extern "C" char **environ;
497-
#endif
498-
499-
return reinterpret_cast<char_const_p_const_p_may_alias_ptr>(environ);
503+
return reinterpret_cast<char_const_p_const_p_may_alias_ptr>(posix::environ);
500504
#endif
501505
}
502506
else

0 commit comments

Comments
 (0)