Ver código fonte

LibCore: Add System::environment

Move this from an internal function of Core::Process so that it can be
used elsewhere.
Shannon Booth 2 anos atrás
pai
commit
cb920b23cc

+ 2 - 16
Userland/Libraries/LibCore/Process.cpp

@@ -21,20 +21,6 @@
 #    include <syscall.h>
 #endif
 
-#if defined(AK_OS_MACOS)
-#    include <crt_externs.h>
-#endif
-
-static char** environment()
-{
-#if defined(AK_OS_MACOS)
-    return *_NSGetEnviron();
-#else
-    extern char** environ;
-    return environ;
-#endif
-}
-
 namespace Core {
 
 struct ArgvList {
@@ -77,11 +63,11 @@ struct ArgvList {
         if (!m_working_directory.is_empty())
             posix_spawn_file_actions_addchdir(&spawn_actions, m_working_directory.characters());
 
-        auto pid = TRY(System::posix_spawn(m_path.view(), &spawn_actions, nullptr, const_cast<char**>(get().data()), environment()));
+        auto pid = TRY(System::posix_spawn(m_path.view(), &spawn_actions, nullptr, const_cast<char**>(get().data()), System::environment()));
         if (keep_as_child == Process::KeepAsChild::No)
             TRY(System::disown(pid));
 #else
-        auto pid = TRY(System::posix_spawn(m_path.view(), nullptr, nullptr, const_cast<char**>(get().data()), environment()));
+        auto pid = TRY(System::posix_spawn(m_path.view(), nullptr, nullptr, const_cast<char**>(get().data()), System::environment()));
         // FIXME: Support keep_as_child outside Serenity.
         (void)keep_as_child;
 #endif

+ 12 - 0
Userland/Libraries/LibCore/System.cpp

@@ -43,7 +43,10 @@ static int memfd_create(char const* name, unsigned int flags)
 #endif
 
 #if defined(AK_OS_MACOS)
+#    include <crt_externs.h>
 #    include <sys/mman.h>
+#else
+extern char** environ;
 #endif
 
 #define HANDLE_SYSCALL_RETURN_VALUE(syscall_name, rc, success_value) \
@@ -1714,4 +1717,13 @@ ErrorOr<String> resolve_executable_from_environment(StringView filename, int fla
     return Error::from_errno(ENOENT);
 }
 
+char** environment()
+{
+#if defined(AK_OS_MACOS)
+    return *_NSGetEnviron();
+#else
+    return environ;
+#endif
+}
+
 }

+ 2 - 0
Userland/Libraries/LibCore/System.h

@@ -271,4 +271,6 @@ ErrorOr<void> posix_fallocate(int fd, off_t offset, off_t length);
 
 ErrorOr<String> resolve_executable_from_environment(StringView filename, int flags = 0);
 
+char** environment();
+
 }