summaryrefslogtreecommitdiff
path: root/Userland/Utilities/env.cpp
blob: 3b4bfcf668a20b89ab639598d3fb8a32fa58dbf2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
 * Copyright (c) 2020, the SerenityOS developers.
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <LibCore/ArgsParser.h>
#include <LibCore/DirIterator.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(int argc, char** argv)
{
    if (pledge("stdio rpath exec", nullptr) < 0) {
        perror("pledge");
        return 1;
    }

    bool ignore_env = false;
    const char* split_string = nullptr;
    Vector<const char*> values;

    Core::ArgsParser args_parser;
    args_parser.set_stop_on_first_non_option(true);

    args_parser.add_option(ignore_env, "Start with an empty environment", "ignore-environment", 'i');
    args_parser.add_option(split_string, "Process and split S into separate arguments; used to pass multiple arguments on shebang lines", "split-string", 'S', "S");

    args_parser.add_positional_argument(values, "Environment and commands", "env/command", Core::ArgsParser::Required::No);
    args_parser.parse(argc, argv);

    if (ignore_env)
        clearenv();

    size_t argv_start;
    for (argv_start = 0; argv_start < values.size(); ++argv_start) {
        if (StringView { values[argv_start] }.contains('=')) {
            putenv(const_cast<char*>(values[argv_start]));
        } else {
            break;
        }
    }

    Vector<String> split_string_storage;
    Vector<const char*> new_argv;
    if (split_string) {
        for (auto view : StringView(split_string).split_view(' ')) {
            split_string_storage.append(view);
        }
        for (auto& str : split_string_storage) {
            new_argv.append(str.characters());
        }
    }

    for (size_t i = argv_start; i < values.size(); ++i) {
        new_argv.append(values[i]);
    }

    if (new_argv.size() == 0) {
        for (auto entry = environ; *entry != nullptr; ++entry)
            outln("{}", *entry);

        return 0;
    }

    new_argv.append(nullptr);

    const char* executable = new_argv[0];
    char* const* new_argv_ptr = const_cast<char* const*>(&new_argv[0]);

    execvp(executable, new_argv_ptr);
    perror("execvp");
    return 1;
}