Skip to content

Commit 39a3775

Browse files
timschumilinusg
authored andcommitted
Userland: Rely on a single authoritative source for the default PATH
1 parent 61e18c6 commit 39a3775

File tree

7 files changed

+14
-7
lines changed

7 files changed

+14
-7
lines changed

Userland/Applications/Terminal/main.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <LibConfig/Listener.h>
1212
#include <LibCore/ArgsParser.h>
1313
#include <LibCore/DirIterator.h>
14+
#include <LibCore/File.h>
1415
#include <LibCore/System.h>
1516
#include <LibDesktop/Launcher.h>
1617
#include <LibGUI/Action.h>
@@ -162,7 +163,7 @@ static ErrorOr<void> run_command(String command, bool keep_open)
162163
arguments.append("-c"sv);
163164
arguments.append(command);
164165
}
165-
auto env = TRY(FixedArray<StringView>::try_create({ "TERM=xterm"sv, "PAGER=more"sv, "PATH=/usr/local/sbin:/usr/local/bin:/usr/bin:/bin"sv }));
166+
auto env = TRY(FixedArray<StringView>::try_create({ "TERM=xterm"sv, "PAGER=more"sv, "PATH="sv DEFAULT_PATH_SV }));
166167
TRY(Core::System::exec(shell, arguments, Core::System::SearchInPath::No, env.span()));
167168
VERIFY_NOT_REACHED();
168169
}

Userland/DevTools/HackStudio/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ static void update_path_environment_variable()
128128

129129
if (path.length())
130130
path.append(':');
131-
path.append("/usr/local/sbin:/usr/local/bin:/usr/bin:/bin"sv);
131+
path.append(DEFAULT_PATH_SV);
132132
setenv("PATH", path.to_string().characters(), true);
133133
}
134134

Userland/Libraries/LibC/unistd.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <AK/ScopedValueRollback.h>
99
#include <AK/String.h>
1010
#include <AK/Vector.h>
11+
#include <LibCore/File.h>
1112
#include <alloca.h>
1213
#include <assert.h>
1314
#include <bits/pthread_cancel.h>
@@ -186,7 +187,7 @@ int execvpe(char const* filename, char* const argv[], char* const envp[])
186187
ScopedValueRollback errno_rollback(errno);
187188
String path = getenv("PATH");
188189
if (path.is_empty())
189-
path = "/bin:/usr/bin";
190+
path = DEFAULT_PATH;
190191
auto parts = path.split(':');
191192
for (auto& part : parts) {
192193
auto candidate = String::formatted("{}/{}", part, filename);

Userland/Libraries/LibCore/File.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
#include <LibCore/IODevice.h>
1212
#include <sys/stat.h>
1313

14+
// FIXME: Make this a bit prettier.
15+
#define DEFAULT_PATH "/usr/local/sbin:/usr/local/bin:/usr/bin:/bin"
16+
#define DEFAULT_PATH_SV "/usr/local/sbin:/usr/local/bin:/usr/bin:/bin"sv
17+
1418
namespace Core {
1519

1620
class File final : public IODevice {

Userland/Libraries/LibCore/System.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -958,7 +958,7 @@ ErrorOr<String> find_file_in_path(StringView filename)
958958
auto const* path_ptr = getenv("PATH");
959959
StringView path { path_ptr, strlen(path_ptr) };
960960
if (path.is_empty())
961-
path = "/bin:/usr/bin"sv;
961+
path = DEFAULT_PATH_SV;
962962
auto parts = path.split_view(':');
963963
for (auto& part : parts) {
964964
auto candidate = String::formatted("{}/{}", part, filename);
@@ -1043,7 +1043,7 @@ ErrorOr<void> exec(StringView filename, Span<StringView> arguments, SearchInPath
10431043
ScopedValueRollback errno_rollback(errno);
10441044
String path = getenv("PATH");
10451045
if (path.is_empty())
1046-
path = "/bin:/usr/bin";
1046+
path = DEFAULT_PATH;
10471047
auto parts = path.split(':');
10481048
for (auto& part : parts) {
10491049
auto candidate = String::formatted("{}/{}", part, filename);

Userland/Services/TelnetServer/main.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <AK/Types.h>
1111
#include <LibCore/ArgsParser.h>
1212
#include <LibCore/EventLoop.h>
13+
#include <LibCore/File.h>
1314
#include <LibCore/TCPServer.h>
1415
#include <LibMain/Main.h>
1516
#include <fcntl.h>
@@ -71,7 +72,7 @@ static void run_command(int ptm_fd, String command)
7172
args[1] = "-c";
7273
args[2] = command.characters();
7374
}
74-
char const* envs[] = { "TERM=xterm", "PATH=/usr/local/sbin:/usr/local/bin:/usr/bin:/bin", nullptr };
75+
char const* envs[] = { "TERM=xterm", "PATH=" DEFAULT_PATH, nullptr };
7576
rc = execve("/bin/Shell", const_cast<char**>(args), const_cast<char**>(envs));
7677
if (rc < 0) {
7778
perror("execve");

Userland/Shell/Shell.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2189,7 +2189,7 @@ Shell::Shell()
21892189
path.append({ path_env_ptr, strlen(path_env_ptr) });
21902190
if (path.length())
21912191
path.append(":"sv);
2192-
path.append("/usr/local/sbin:/usr/local/bin:/usr/bin:/bin"sv);
2192+
path.append(DEFAULT_PATH_SV);
21932193
setenv("PATH", path.to_string().characters(), true);
21942194
}
21952195

0 commit comments

Comments
 (0)