hostname.cpp 873 B

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