1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-28 17:45:09 +00:00

hostname: Port to LibMain

This commit is contained in:
Lucas CHOLLET 2022-01-13 23:15:25 +01:00 committed by Idan Horowitz
parent b3b40ae1fa
commit 8bc1a9e946
2 changed files with 7 additions and 14 deletions

View file

@ -108,6 +108,7 @@ target_link_libraries(gzip LibCompress LibMain)
target_link_libraries(head LibMain) target_link_libraries(head LibMain)
target_link_libraries(hexdump LibMain) target_link_libraries(hexdump LibMain)
target_link_libraries(host LibMain) target_link_libraries(host LibMain)
target_link_libraries(hostname LibMain)
target_link_libraries(id LibMain) target_link_libraries(id LibMain)
target_link_libraries(ini LibMain) target_link_libraries(ini LibMain)
target_link_libraries(jp LibMain) target_link_libraries(jp LibMain)

View file

@ -5,37 +5,29 @@
*/ */
#include <LibCore/ArgsParser.h> #include <LibCore/ArgsParser.h>
#include <LibCore/System.h>
#include <LibMain/Main.h>
#include <limits.h> #include <limits.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
int main(int argc, char** argv) ErrorOr<int> serenity_main(Main::Arguments args)
{ {
const char* hostname = nullptr; const char* hostname = nullptr;
Core::ArgsParser args_parser; Core::ArgsParser args_parser;
args_parser.add_positional_argument(hostname, "Hostname to set", "hostname", Core::ArgsParser::Required::No); args_parser.add_positional_argument(hostname, "Hostname to set", "hostname", Core::ArgsParser::Required::No);
args_parser.parse(argc, argv); args_parser.parse(args);
if (!hostname) { if (!hostname) {
char buffer[HOST_NAME_MAX]; outln("{}", TRY(Core::System::gethostname()));
int rc = gethostname(buffer, sizeof(buffer));
if (rc < 0) {
perror("gethostname");
return 1;
}
outln("{}", buffer);
} else { } else {
if (strlen(hostname) >= HOST_NAME_MAX) { if (strlen(hostname) >= HOST_NAME_MAX) {
warnln("Hostname must be less than {} characters", HOST_NAME_MAX); warnln("Hostname must be less than {} characters", HOST_NAME_MAX);
return 1; return 1;
} }
int rc = sethostname(hostname, strlen(hostname)); TRY(Core::System::sethostname(hostname));
if (rc < 0) {
perror("sethostname");
return 1;
}
} }
return 0; return 0;
} }