Luna/shell/builtin/set.cpp

25 lines
861 B
C++
Raw Normal View History

2023-07-21 19:06:10 +00:00
#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 {};
};