serenity
serenity copied to clipboard
LibCore: Introduce and use Core::Environment wrapper for FOOenv() functions
Another LibC thing that I felt deserved a nicer wrapper. A couple of these we had wrappers for in Core::System, but not all of them, and the environment is a separate enough thing to belong on its own. So, the existing wrapper functions are moved to Core::Environment, and the others filled in.
Initially I thought of keeping a HashMap around of the environment variables, but given that the environ can be modified from other places, it made more sense to just wrap the LibC functions and provide a nicer API.
Of course, "nicer" is my opinion, so I could be wrong. Feedback welcome.
CI failure is legitimate, I need to put replacement implementations in for clearenv() and secure_getenv() on MacOS.
Changes:
- Rebase!
- Fix MacOS (hopefully???): Fall back to regular getenv(), and copy the implementation of clearenv() from LibC.
- Replace the goofy
for_each_entry()with an iterator. I know very little about iterators so I might have made a boo-boo so look out for that. - Use StringBuilders instead of
to_byte_string(). This technically can produce OOM errors, and the other places TRY() those because they already return ErrorOr, but having getenv() be fallible feels weird so I just made it use the non-errory functions. - Added
Core::Environment::has(name)for that one case. It's pretty nice, I won't lie. - Correct that
outln()usage. - Remove that now-unneeded MacOS include from System.cpp
@alimpfard does this fix your ownership concerns? I'm not 100% sure I understood that.
Use StringBuilders instead of to_byte_string().
And
for (auto entry : entries())
if (entry.name == name)
return entry.value;
return {};
would be both always infallible and shorter...
And it's also not like Serenity's, GNU's, or LLVM's libc implements something smarter than this loop: 1, 2, 3.
Changes:
- Iterate
entries()instead of callinggetenv() - Replace
get_secure()with a second parameter toEnvironment::get(), which defaults to non-secure. - Remove unnecessary const qualifiers in the Entry iterator.
- Rebase on master to make sure nothing's broken. (Second push)
Changes:
- Go back to calling
getenv()/secure_getenv()directly, because otherwise we have to include<sys/auxv.h>which doesn't always exist, including on CI! - Rename
Environment::SecuretoEnvironment::SecureOnly.
Missing a header for environ on macOS
/Users/runner/work/1/s/Userland/Libraries/LibCore/Environment.cpp:61:24: error: use of undeclared identifier 'environ'
https://dev.azure.com/SerenityOS/SerenityOS/_build/results?buildId=44741&view=logs&j=2351e412-c793-598f-27f8-72c4f2d55ba1&t=9c50333a-ada2-5596-d44b-d8dcee270ad2&l=99
Missing a header for
environon macOS
/Users/runner/work/1/s/Userland/Libraries/LibCore/Environment.cpp:61:24: error: use of undeclared identifier 'environ'
Changes:
- Use the
environwrapper so things work on macOS. I'm... really surprised this didn't come up before, I was usingenvironin multiple places.
Changes:
- Another macOS fix, marking a parameter as
[[maybe_unused]]which isn't used in the macOS code. Deary me, macOS will be the end of me. :laughing:
Changes:
- Fixed the conflict. :smiling_face_with_tear: