mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 18:07:35 +00:00
Everywhere: Replace some uses of fork/exec with posix_spawn
It's less code, and it's potentially more efficient once posix_spawn is a real syscall.
This commit is contained in:
parent
301ac3c7e5
commit
12cbc4ad0d
11 changed files with 65 additions and 101 deletions
|
@ -58,6 +58,7 @@
|
|||
#include <LibGUI/Window.h>
|
||||
#include <LibGfx/Palette.h>
|
||||
#include <signal.h>
|
||||
#include <spawn.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
@ -337,16 +338,13 @@ int run_in_windowed_mode(RefPtr<Core::ConfigFile> config, String initial_locatio
|
|||
});
|
||||
|
||||
auto open_terminal_action = GUI::Action::create("Open Terminal here...", Gfx::Bitmap::load_from_file("/res/icons/16x16/app-terminal.png"), [&](const GUI::Action&) {
|
||||
if (!fork()) {
|
||||
if (chdir(directory_view.path().characters()) < 0) {
|
||||
perror("chdir");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (execl("/bin/Terminal", "Terminal", nullptr) < 0)
|
||||
perror("execl");
|
||||
exit(1);
|
||||
}
|
||||
posix_spawn_file_actions_t spawn_actions;
|
||||
posix_spawn_file_actions_init(&spawn_actions);
|
||||
posix_spawn_file_actions_addchdir(&spawn_actions, directory_view.path().characters());
|
||||
pid_t pid;
|
||||
const char* argv[] = { "Terminal", nullptr };
|
||||
posix_spawn(&pid, "/bin/Terminal", &spawn_actions, nullptr, const_cast<char**>(argv), environ);
|
||||
posix_spawn_file_actions_destroy(&spawn_actions);
|
||||
});
|
||||
|
||||
RefPtr<GUI::Action> view_as_table_action;
|
||||
|
@ -719,13 +717,10 @@ int run_in_windowed_mode(RefPtr<Core::ConfigFile> config, String initial_locatio
|
|||
};
|
||||
|
||||
auto open_in_text_editor_action = GUI::Action::create("Open in TextEditor...", Gfx::Bitmap::load_from_file("/res/icons/TextEditor16.png"), [&](auto&) {
|
||||
pid_t child;
|
||||
for (auto& path : selected_file_paths()) {
|
||||
if (!fork()) {
|
||||
int rc = execl("/bin/TextEditor", "TextEditor", path.characters(), nullptr);
|
||||
if (rc < 0)
|
||||
perror("execl");
|
||||
exit(1);
|
||||
}
|
||||
const char* argv[] = { "TextEditor", path.characters(), nullptr };
|
||||
posix_spawn(&child, "/bin/TextEditor", nullptr, nullptr, const_cast<char**>(argv), environ);
|
||||
}
|
||||
});
|
||||
|
||||
|
|
|
@ -40,6 +40,7 @@
|
|||
#include <LibGUI/MessageBox.h>
|
||||
#include <LibGUI/WindowServerConnection.h>
|
||||
#include <LibKeyboard/CharacterMap.h>
|
||||
#include <spawn.h>
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
|
@ -123,18 +124,12 @@ int main(int argc, char** argv)
|
|||
GUI::MessageBox::show("Please select character mapping file.", "Keyboard settings", GUI::MessageBox::Type::Error, GUI::MessageBox::InputType::OK, window);
|
||||
return;
|
||||
}
|
||||
|
||||
pid_t child_pid = fork();
|
||||
if (child_pid < 0) {
|
||||
perror("fork");
|
||||
pid_t child_pid;
|
||||
const char* argv[] = { "/bin/keymap", character_map_file.characters(), nullptr };
|
||||
if ((errno = posix_spawn(&child_pid, "/bin/keymap", nullptr, nullptr, const_cast<char**>(argv), environ))) {
|
||||
perror("posix_spawn");
|
||||
exit(1);
|
||||
}
|
||||
if (child_pid == 0) {
|
||||
if (execl("/bin/keymap", "/bin/keymap", character_map_file.characters(), nullptr) < 0) {
|
||||
perror("execl");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
if (quit)
|
||||
app.quit();
|
||||
};
|
||||
|
|
|
@ -44,6 +44,7 @@
|
|||
#include <LibGfx/Bitmap.h>
|
||||
#include <LibGfx/Palette.h>
|
||||
#include <LibGfx/Rect.h>
|
||||
#include <spawn.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
|
@ -110,11 +111,10 @@ int main(int argc, char** argv)
|
|||
widget.load_from_file(url.path());
|
||||
}
|
||||
|
||||
pid_t child;
|
||||
for (size_t i = 1; i < urls.size(); ++i) {
|
||||
if (fork() == 0) {
|
||||
execl("/bin/QuickShow", "/bin/QuickShow", urls[i].path().characters(), nullptr);
|
||||
ASSERT_NOT_REACHED();
|
||||
}
|
||||
const char* argv[] = { "/bin/QuickShow", urls[i].path().characters(), nullptr };
|
||||
posix_spawn(&child, "/bin/QuickShow", nullptr, nullptr, const_cast<char**>(argv), environ);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
|
@ -47,6 +47,7 @@
|
|||
#include <fcntl.h>
|
||||
#include <pwd.h>
|
||||
#include <signal.h>
|
||||
#include <spawn.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
@ -257,10 +258,9 @@ int main(int argc, char** argv)
|
|||
|
||||
auto& app_menu = menubar->add_menu("Terminal");
|
||||
app_menu.add_action(GUI::Action::create("Open new terminal", { Mod_Ctrl | Mod_Shift, Key_N }, Gfx::Bitmap::load_from_file("/res/icons/16x16/app-terminal.png"), [&](auto&) {
|
||||
if (!fork()) {
|
||||
execl("/bin/Terminal", "Terminal", nullptr);
|
||||
exit(1);
|
||||
}
|
||||
pid_t child;
|
||||
const char* argv[] = { "Terminal", nullptr };
|
||||
posix_spawn(&child, "/bin/Terminal", nullptr, nullptr, const_cast<char**>(argv), environ);
|
||||
}));
|
||||
app_menu.add_action(GUI::Action::create("Settings...", Gfx::Bitmap::load_from_file("/res/icons/gear16.png"),
|
||||
[&](const GUI::Action&) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue