chown: Port to LibMain :^)

This commit is contained in:
Kenneth Myhra 2021-11-27 16:11:35 +01:00 committed by Brian Gianforcaro
parent c4bd46023b
commit cc3710e19f
Notes: sideshowbarker 2024-07-17 22:56:38 +09:00
2 changed files with 12 additions and 24 deletions

View file

@ -65,6 +65,7 @@ target_link_libraries(cat LibMain)
target_link_libraries(checksum LibCrypto LibMain)
target_link_libraries(chmod LibMain)
target_link_libraries(chgrp LibMain)
target_link_libraries(chown LibMain)
target_link_libraries(chres LibGUI)
target_link_libraries(cksum LibCrypto)
target_link_libraries(config LibConfig)

View file

@ -6,6 +6,8 @@
#include <AK/String.h>
#include <AK/Vector.h>
#include <LibCore/System.h>
#include <LibMain/Main.h>
#include <grp.h>
#include <pwd.h>
#include <stdio.h>
@ -13,14 +15,11 @@
#include <sys/stat.h>
#include <unistd.h>
int main(int argc, char** argv)
ErrorOr<int> serenity_main(Main::Arguments arguments)
{
if (pledge("stdio rpath chown", nullptr) < 0) {
perror("pledge");
return 1;
}
TRY(Core::System::pledge("stdio rpath chown", nullptr));
if (argc < 3) {
if (arguments.strings.size() < 3) {
warnln("usage: chown <uid[:gid]> <path>");
return 1;
}
@ -28,7 +27,7 @@ int main(int argc, char** argv)
uid_t new_uid = -1;
gid_t new_gid = -1;
auto parts = String(argv[1]).split(':', true);
auto parts = arguments.strings[1].split_view(':', true);
if (parts.is_empty()) {
warnln("Empty uid/gid spec");
return 1;
@ -42,12 +41,8 @@ int main(int argc, char** argv)
if (number.has_value()) {
new_uid = number.value();
} else {
auto* passwd = getpwnam(parts[0].characters());
if (!passwd) {
warnln("Unknown user '{}'", parts[0]);
return 1;
}
new_uid = passwd->pw_uid;
auto passwd = TRY(Core::System::getpwnam(parts[0]));
new_uid = passwd.pw_uid;
}
if (parts.size() == 2) {
@ -55,20 +50,12 @@ int main(int argc, char** argv)
if (number.has_value()) {
new_gid = number.value();
} else {
auto* group = getgrnam(parts[1].characters());
if (!group) {
warnln("Unknown group '{}'", parts[1]);
return 1;
}
new_gid = group->gr_gid;
auto group = TRY(Core::System::getgrnam(parts[1]));
new_gid = group.gr_gid;
}
}
int rc = chown(argv[2], new_uid, new_gid);
if (rc < 0) {
perror("chown");
return 1;
}
TRY(Core::System::chown(arguments.strings[2], new_uid, new_gid));
return 0;
}