Serenity Operating System
1/*
2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 */
6
7#include <LibCore/ArgsParser.h>
8#include <LibCore/System.h>
9#include <LibMain/Main.h>
10#include <limits.h>
11#include <stdio.h>
12#include <string.h>
13#include <unistd.h>
14
15ErrorOr<int> serenity_main(Main::Arguments args)
16{
17 StringView hostname {};
18
19 Core::ArgsParser args_parser;
20 args_parser.add_positional_argument(hostname, "Hostname to set", "hostname", Core::ArgsParser::Required::No);
21 args_parser.parse(args);
22
23 if (hostname.is_empty()) {
24 outln("{}", TRY(Core::System::gethostname()));
25 } else {
26 if (hostname.length() >= HOST_NAME_MAX) {
27 warnln("Hostname must be less than {} characters", HOST_NAME_MAX);
28 return 1;
29 }
30 TRY(Core::System::sethostname(hostname));
31 }
32 return 0;
33}