1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 11:57:35 +00:00

groupdel: Port to LibMain

This commit is contained in:
TheOddGarlic 2022-01-15 20:08:56 +03:00 committed by Andreas Kling
parent 6499699d6d
commit 6cda142c28
2 changed files with 13 additions and 31 deletions

View file

@ -104,6 +104,7 @@ target_link_libraries(gml-format LibGUI LibMain)
target_link_libraries(grep LibRegex LibMain) target_link_libraries(grep LibRegex LibMain)
target_link_libraries(gron LibMain) target_link_libraries(gron LibMain)
target_link_libraries(groupadd LibMain) target_link_libraries(groupadd LibMain)
target_link_libraries(groupdel LibMain)
target_link_libraries(groups LibMain) target_link_libraries(groups LibMain)
target_link_libraries(gunzip LibCompress LibMain) target_link_libraries(gunzip LibCompress LibMain)
target_link_libraries(gzip LibCompress LibMain) target_link_libraries(gzip LibCompress LibMain)

View file

@ -2,38 +2,30 @@
* Copyright (c) 2020, Fei Wu <f.eiwu@yahoo.com> * Copyright (c) 2020, Fei Wu <f.eiwu@yahoo.com>
* Copyright (c) 2021, Brandon Pruitt <brapru@pm.me> * Copyright (c) 2021, Brandon Pruitt <brapru@pm.me>
* Copyright (c) 2021, Maxime Friess <M4x1me@pm.me> * Copyright (c) 2021, Maxime Friess <M4x1me@pm.me>
* Copyright (c) 2022, Umut İnan Erdoğan <umutinanerdogan62@gmail.com>
* *
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
#include <LibCore/ArgsParser.h> #include <LibCore/ArgsParser.h>
#include <LibCore/File.h> #include <LibCore/File.h>
#include <LibCore/System.h>
#include <LibMain/Main.h>
#include <grp.h> #include <grp.h>
#include <pwd.h> #include <pwd.h>
#include <unistd.h> #include <unistd.h>
int main(int argc, char** argv) ErrorOr<int> serenity_main(Main::Arguments arguments)
{ {
if (pledge("stdio wpath rpath cpath fattr proc exec", nullptr) < 0) { TRY(Core::System::pledge("stdio wpath rpath cpath fattr proc exec"));
perror("pledge"); TRY(Core::System::unveil("/etc/", "rwc"));
return 1; TRY(Core::System::unveil("/bin/rm", "x"));
}
if (unveil("/etc/", "rwc") < 0) {
perror("unveil");
return 1;
}
if (unveil("/bin/rm", "x") < 0) {
perror("unveil");
return 1;
}
char const* groupname = nullptr; char const* groupname = nullptr;
Core::ArgsParser args_parser; Core::ArgsParser args_parser;
args_parser.add_positional_argument(groupname, "Group name", "group"); args_parser.add_positional_argument(groupname, "Group name", "group");
args_parser.parse(argc, argv); args_parser.parse(arguments);
setgrent(); setgrent();
auto* g = getgrnam(groupname); auto* g = getgrnam(groupname);
@ -68,7 +60,7 @@ int main(int argc, char** argv)
char temp_group[] = "/etc/group.XXXXXX"; char temp_group[] = "/etc/group.XXXXXX";
auto unlink_temp_files = [&] { auto unlink_temp_files = [&] {
if (unlink(temp_group) < 0) if (Core::System::unlink(temp_group).is_error())
perror("unlink"); perror("unlink");
}; };
@ -76,11 +68,7 @@ int main(int argc, char** argv)
unlink_temp_files(); unlink_temp_files();
}; };
auto temp_group_fd = mkstemp(temp_group); int temp_group_fd = TRY(Core::System::mkstemp(temp_group));
if (temp_group_fd == -1) {
perror("failed to create temporary group file");
return 1;
}
FILE* temp_group_file = fdopen(temp_group_fd, "w"); FILE* temp_group_file = fdopen(temp_group_fd, "w");
if (!temp_group_file) { if (!temp_group_file) {
@ -104,15 +92,8 @@ int main(int argc, char** argv)
return 1; return 1;
} }
if (chmod(temp_group, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) { TRY(Core::System::chmod(temp_group, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH));
perror("chmod"); TRY(Core::System::rename(temp_group, "/etc/group"));
return 1;
}
if (rename(temp_group, "/etc/group") < 0) {
perror("failed to rename the temporary group file");
return 1;
}
unlink_temp_files_guard.disarm(); unlink_temp_files_guard.disarm();