Luna/shell/builtin/set.cpp
apio 7a4d3ba495
All checks were successful
continuous-integration/drone/push Build is passing
sh: Add a few more shell builtins
2023-07-21 21:06:10 +02:00

25 lines
861 B
C++

#include "sh.h"
#include <errno.h>
#include <os/ArgumentParser.h>
#include <stdlib.h>
shell_builtin_t builtin_set = [](int argc, char** argv) -> Result<void> {
StringView name;
StringView value;
bool preserve { false };
os::ArgumentParser parser;
parser.set_should_exit_on_bad_usage(false);
parser.add_description("Set an environment variable (shell builtin)"_sv);
parser.add_system_program_info("set"_sv);
parser.add_positional_argument(name, "name"_sv, true);
parser.add_positional_argument(value, "value"_sv, true);
parser.add_switch_argument(preserve, 'p', "preserve"_sv,
"preserve the environment variable if it already exists"_sv);
if (!TRY(parser.parse(argc, argv))) return {};
if (setenv(name.chars(), value.chars(), !preserve) < 0) return err(errno);
return {};
};