blob: 93cbc961f895983227909ab57755eaaca52b631b [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 FILE *f;
Erik Andersene49d5ec2000-02-08 19:58:47 +000020
21 if (!s)
22 return;
23 if (!isfile) {
24 if (sethostname(s, strlen(s)) < 0) {
25 if (errno == EPERM)
Bernhard Reutner-Fischerf07fe622007-01-09 10:06:19 +000026 bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
Denis Vlasenko6f1713f2008-02-25 23:23:58 +000027 bb_perror_msg_and_die("sethostname");
Erik Andersene49d5ec2000-02-08 19:58:47 +000028 }
Eric Andersen485b9551999-12-07 23:14:59 +000029 } else {
Rob Landleyd921b2e2006-08-03 15:41:12 +000030 f = xfopen(s, "r");
Denis Vlasenko74324c82007-06-04 10:16:52 +000031#define strbuf bb_common_bufsiz1
32 while (fgets(strbuf, sizeof(strbuf), f) != NULL) {
33 if (strbuf[0] == '#') {
Eric Andersen3d61b102001-10-31 09:59:57 +000034 continue;
35 }
Denis Vlasenko74324c82007-06-04 10:16:52 +000036 chomp(strbuf);
37 do_sethostname(strbuf, 0);
Eric Andersen3d61b102001-10-31 09:59:57 +000038 }
Bernhard Reutner-Fischerf07fe622007-01-09 10:06:19 +000039 if (ENABLE_FEATURE_CLEAN_UP)
40 fclose(f);
Eric Andersen485b9551999-12-07 23:14:59 +000041 }
Eric Andersen485b9551999-12-07 23:14:59 +000042}
43
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000044int hostname_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Eric Andersen485b9551999-12-07 23:14:59 +000045int hostname_main(int argc, char **argv)
46{
Denis Vlasenko8514fc52006-09-22 08:53:14 +000047 enum {
48 OPT_d = 0x1,
49 OPT_f = 0x2,
50 OPT_i = 0x4,
51 OPT_s = 0x8,
Denis Vlasenkobc5262d2007-01-26 07:02:56 +000052 OPT_F = 0x10,
Denis Vlasenko8514fc52006-09-22 08:53:14 +000053 OPT_dfis = 0xf,
54 };
55
Denis Vlasenko6f1713f2008-02-25 23:23:58 +000056 char *buf;
Denis Vlasenko90ec4dc2007-01-25 19:44:38 +000057 char *hostname_str;
Eric Andersen485b9551999-12-07 23:14:59 +000058
Erik Andersene49d5ec2000-02-08 19:58:47 +000059 if (argc < 1)
Manuel Novoa III cad53642003-03-19 09:13:01 +000060 bb_show_usage();
Eric Andersen485b9551999-12-07 23:14:59 +000061
Denis Vlasenkofe7cd642007-08-18 15:32:12 +000062 getopt32(argv, "dfisF:", &hostname_str);
Denis Vlasenko6f1713f2008-02-25 23:23:58 +000063 argv += optind;
64 buf = safe_gethostname();
Erik Andersene49d5ec2000-02-08 19:58:47 +000065
Eric Andersen3d61b102001-10-31 09:59:57 +000066 /* Output in desired format */
Bernhard Reutner-Fischerf07fe622007-01-09 10:06:19 +000067 if (option_mask32 & OPT_dfis) {
Denis Vlasenko8514fc52006-09-22 08:53:14 +000068 struct hostent *hp;
69 char *p;
Eric Andersen3d61b102001-10-31 09:59:57 +000070 hp = xgethostbyname(buf);
71 p = strchr(hp->h_name, '.');
Bernhard Reutner-Fischerf07fe622007-01-09 10:06:19 +000072 if (option_mask32 & OPT_f) {
Eric Andersen3d61b102001-10-31 09:59:57 +000073 puts(hp->h_name);
Bernhard Reutner-Fischerf07fe622007-01-09 10:06:19 +000074 } else if (option_mask32 & OPT_s) {
Denis Vlasenko6f1713f2008-02-25 23:23:58 +000075 if (p)
Denis Vlasenko90ec4dc2007-01-25 19:44:38 +000076 *p = '\0';
Glenn L McGrath3ff3f4a2002-11-10 22:07:48 +000077 puts(hp->h_name);
Bernhard Reutner-Fischerf07fe622007-01-09 10:06:19 +000078 } else if (option_mask32 & OPT_d) {
79 if (p)
80 puts(p + 1);
81 } else if (option_mask32 & OPT_i) {
Eric Andersen3d61b102001-10-31 09:59:57 +000082 while (hp->h_addr_list[0]) {
83 printf("%s ", inet_ntoa(*(struct in_addr *) (*hp->h_addr_list++)));
84 }
Denis Vlasenko4daad902007-09-27 10:20:47 +000085 bb_putchar('\n');
Erik Andersene49d5ec2000-02-08 19:58:47 +000086 }
87 }
Eric Andersen3d61b102001-10-31 09:59:57 +000088 /* Set the hostname */
Denis Vlasenko90ec4dc2007-01-25 19:44:38 +000089 else if (option_mask32 & OPT_F) {
Denis Vlasenko8514fc52006-09-22 08:53:14 +000090 do_sethostname(hostname_str, 1);
Denis Vlasenko6f1713f2008-02-25 23:23:58 +000091 } else if (argv[0]) {
92 do_sethostname(argv[0], 0);
Eric Andersen3d61b102001-10-31 09:59:57 +000093 }
94 /* Or if all else fails,
95 * just print the current hostname */
Denis Vlasenko8514fc52006-09-22 08:53:14 +000096 else {
Eric Andersen3d61b102001-10-31 09:59:57 +000097 puts(buf);
98 }
Denis Vlasenko6f1713f2008-02-25 23:23:58 +000099 if (ENABLE_FEATURE_CLEAN_UP)
100 free(buf);
Denis Vlasenko8514fc52006-09-22 08:53:14 +0000101 return 0;
Eric Andersen485b9551999-12-07 23:14:59 +0000102}