blob: 48e70db91a9abaa74f472ad7ba3967b94841b2b9 [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersen485b9551999-12-07 23:14:59 +00002/*
Eric Andersen485b9551999-12-07 23:14:59 +00003 * Mini hostname implementation for busybox
4 *
5 * Copyright (C) 1999 by Randolph Chung <tausq@debian.org>
6 *
Eric Andersencb81e642003-07-14 21:21:08 +00007 * adjusted by Erik Andersen <andersen@codepoet.org> to remove
Eric Andersend29edf31999-12-08 04:13:44 +00008 * use of long options and GNU getopt. Improved the usage info.
9 *
Eric Andersen485b9551999-12-07 23:14:59 +000010 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
"Robert P. J. Day"2819f752006-07-11 11:32:31 +000011 *
12 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Eric Andersen485b9551999-12-07 23:14:59 +000013 */
14
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000015#include "libbb.h"
Eric Andersen3d61b102001-10-31 09:59:57 +000016
Eric Andersen3e6ff902001-03-09 21:24:12 +000017static void do_sethostname(char *s, int isfile)
Eric Andersen485b9551999-12-07 23:14:59 +000018{
Erik Andersene49d5ec2000-02-08 19:58:47 +000019 if (!s)
20 return;
Denis Vlasenko5415c852008-07-21 23:05:26 +000021 if (isfile) {
22 parser_t *parser = config_open2(s, xfopen_for_read);
Denis Vlasenko084266e2008-07-26 23:08:31 +000023 while (config_read(parser, &s, 1, 1, "# \t", PARSE_NORMAL & ~PARSE_GREEDY)) {
Denis Vlasenko5415c852008-07-21 23:05:26 +000024 do_sethostname(s, 0);
Eric Andersen3d61b102001-10-31 09:59:57 +000025 }
Bernhard Reutner-Fischerf07fe622007-01-09 10:06:19 +000026 if (ENABLE_FEATURE_CLEAN_UP)
Denis Vlasenko5415c852008-07-21 23:05:26 +000027 config_close(parser);
28 } else if (sethostname(s, strlen(s)) < 0) {
29 if (errno == EPERM)
30 bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
31 bb_perror_msg_and_die("sethostname");
Eric Andersen485b9551999-12-07 23:14:59 +000032 }
Eric Andersen485b9551999-12-07 23:14:59 +000033}
34
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000035int hostname_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Eric Andersen485b9551999-12-07 23:14:59 +000036int hostname_main(int argc, char **argv)
37{
Denis Vlasenko8514fc52006-09-22 08:53:14 +000038 enum {
39 OPT_d = 0x1,
40 OPT_f = 0x2,
41 OPT_i = 0x4,
42 OPT_s = 0x8,
Denis Vlasenkobc5262d2007-01-26 07:02:56 +000043 OPT_F = 0x10,
Denis Vlasenko8514fc52006-09-22 08:53:14 +000044 OPT_dfis = 0xf,
45 };
46
Denis Vlasenko6f1713f2008-02-25 23:23:58 +000047 char *buf;
Denis Vlasenko90ec4dc2007-01-25 19:44:38 +000048 char *hostname_str;
Eric Andersen485b9551999-12-07 23:14:59 +000049
Erik Andersene49d5ec2000-02-08 19:58:47 +000050 if (argc < 1)
Manuel Novoa III cad53642003-03-19 09:13:01 +000051 bb_show_usage();
Eric Andersen485b9551999-12-07 23:14:59 +000052
Denis Vlasenkofe7cd642007-08-18 15:32:12 +000053 getopt32(argv, "dfisF:", &hostname_str);
Denis Vlasenko6f1713f2008-02-25 23:23:58 +000054 argv += optind;
55 buf = safe_gethostname();
Erik Andersene49d5ec2000-02-08 19:58:47 +000056
Eric Andersen3d61b102001-10-31 09:59:57 +000057 /* Output in desired format */
Bernhard Reutner-Fischerf07fe622007-01-09 10:06:19 +000058 if (option_mask32 & OPT_dfis) {
Denis Vlasenko8514fc52006-09-22 08:53:14 +000059 struct hostent *hp;
60 char *p;
Eric Andersen3d61b102001-10-31 09:59:57 +000061 hp = xgethostbyname(buf);
62 p = strchr(hp->h_name, '.');
Bernhard Reutner-Fischerf07fe622007-01-09 10:06:19 +000063 if (option_mask32 & OPT_f) {
Eric Andersen3d61b102001-10-31 09:59:57 +000064 puts(hp->h_name);
Bernhard Reutner-Fischerf07fe622007-01-09 10:06:19 +000065 } else if (option_mask32 & OPT_s) {
Denis Vlasenko6f1713f2008-02-25 23:23:58 +000066 if (p)
Denis Vlasenko90ec4dc2007-01-25 19:44:38 +000067 *p = '\0';
Glenn L McGrath3ff3f4a2002-11-10 22:07:48 +000068 puts(hp->h_name);
Bernhard Reutner-Fischerf07fe622007-01-09 10:06:19 +000069 } else if (option_mask32 & OPT_d) {
70 if (p)
71 puts(p + 1);
72 } else if (option_mask32 & OPT_i) {
Eric Andersen3d61b102001-10-31 09:59:57 +000073 while (hp->h_addr_list[0]) {
74 printf("%s ", inet_ntoa(*(struct in_addr *) (*hp->h_addr_list++)));
75 }
Denis Vlasenko4daad902007-09-27 10:20:47 +000076 bb_putchar('\n');
Erik Andersene49d5ec2000-02-08 19:58:47 +000077 }
78 }
Eric Andersen3d61b102001-10-31 09:59:57 +000079 /* Set the hostname */
Denis Vlasenko90ec4dc2007-01-25 19:44:38 +000080 else if (option_mask32 & OPT_F) {
Denis Vlasenko8514fc52006-09-22 08:53:14 +000081 do_sethostname(hostname_str, 1);
Denis Vlasenko6f1713f2008-02-25 23:23:58 +000082 } else if (argv[0]) {
83 do_sethostname(argv[0], 0);
Eric Andersen3d61b102001-10-31 09:59:57 +000084 }
85 /* Or if all else fails,
86 * just print the current hostname */
Denis Vlasenko8514fc52006-09-22 08:53:14 +000087 else {
Eric Andersen3d61b102001-10-31 09:59:57 +000088 puts(buf);
89 }
Denis Vlasenko6f1713f2008-02-25 23:23:58 +000090 if (ENABLE_FEATURE_CLEAN_UP)
91 free(buf);
Denis Vlasenko5415c852008-07-21 23:05:26 +000092 return EXIT_SUCCESS;
Eric Andersen485b9551999-12-07 23:14:59 +000093}