1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-14 08:54:58 +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

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