blob: 2850bd5bd16b16d9254268c7830273e15cdb9ad1 [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 Andersencb81e642003-07-14 21:21:08 +00003 * $Id: hostname.c,v 1.36 2003/07/14 21:21:01 andersen Exp $
Eric Andersen485b9551999-12-07 23:14:59 +00004 * Mini hostname implementation for busybox
5 *
6 * Copyright (C) 1999 by Randolph Chung <tausq@debian.org>
7 *
Eric Andersencb81e642003-07-14 21:21:08 +00008 * adjusted by Erik Andersen <andersen@codepoet.org> to remove
Eric Andersend29edf31999-12-08 04:13:44 +00009 * use of long options and GNU getopt. Improved the usage info.
10 *
Eric Andersen485b9551999-12-07 23:14:59 +000011 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
"Robert P. J. Day"2819f752006-07-11 11:32:31 +000012 *
13 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Eric Andersen485b9551999-12-07 23:14:59 +000014 */
15
Eric Andersene0c07572001-06-23 13:49:14 +000016#include "busybox.h"
Eric Andersen3d61b102001-10-31 09:59:57 +000017
Eric Andersen3e6ff902001-03-09 21:24:12 +000018static void do_sethostname(char *s, int isfile)
Eric Andersen485b9551999-12-07 23:14:59 +000019{
Erik Andersene49d5ec2000-02-08 19:58:47 +000020 FILE *f;
Denis Vlasenko8514fc52006-09-22 08:53:14 +000021 char buf[256];
Erik Andersene49d5ec2000-02-08 19:58:47 +000022
23 if (!s)
24 return;
25 if (!isfile) {
26 if (sethostname(s, strlen(s)) < 0) {
27 if (errno == EPERM)
Manuel Novoa III cad53642003-03-19 09:13:01 +000028 bb_error_msg_and_die("you must be root to change the hostname");
Erik Andersene49d5ec2000-02-08 19:58:47 +000029 else
Manuel Novoa III cad53642003-03-19 09:13:01 +000030 bb_perror_msg_and_die("sethostname");
Erik Andersene49d5ec2000-02-08 19:58:47 +000031 }
Eric Andersen485b9551999-12-07 23:14:59 +000032 } else {
Rob Landleyd921b2e2006-08-03 15:41:12 +000033 f = xfopen(s, "r");
Denis Vlasenko8514fc52006-09-22 08:53:14 +000034 while (fgets(buf, sizeof(buf), f) != NULL) {
Eric Andersen3d61b102001-10-31 09:59:57 +000035 if (buf[0] =='#') {
36 continue;
37 }
38 chomp(buf);
39 do_sethostname(buf, 0);
40 }
Eric Andersenbdfd0d72001-10-24 05:00:29 +000041#ifdef CONFIG_FEATURE_CLEAN_UP
Matt Kraaibbaef662000-09-27 02:43:35 +000042 fclose(f);
Glenn L McGrath78b0e372001-06-26 02:06:08 +000043#endif
Eric Andersen485b9551999-12-07 23:14:59 +000044 }
Eric Andersen485b9551999-12-07 23:14:59 +000045}
46
47int hostname_main(int argc, char **argv)
48{
Denis Vlasenko8514fc52006-09-22 08:53:14 +000049 enum {
50 OPT_d = 0x1,
51 OPT_f = 0x2,
52 OPT_i = 0x4,
53 OPT_s = 0x8,
54 OPT_dfis = 0xf,
55 };
56
57 char buf[256];
58 unsigned long opt;
59 char *hostname_str = NULL;
Eric Andersen485b9551999-12-07 23:14:59 +000060
Erik Andersene49d5ec2000-02-08 19:58:47 +000061 if (argc < 1)
Manuel Novoa III cad53642003-03-19 09:13:01 +000062 bb_show_usage();
Eric Andersen485b9551999-12-07 23:14:59 +000063
Denis Vlasenko8514fc52006-09-22 08:53:14 +000064 opt = bb_getopt_ulflags(argc, argv, "dfisF:", &hostname_str);
Erik Andersene49d5ec2000-02-08 19:58:47 +000065
Eric Andersen3d61b102001-10-31 09:59:57 +000066 /* Output in desired format */
Denis Vlasenko8514fc52006-09-22 08:53:14 +000067 if (opt & OPT_dfis) {
68 struct hostent *hp;
69 char *p;
70 gethostname(buf, sizeof(buf));
Eric Andersen3d61b102001-10-31 09:59:57 +000071 hp = xgethostbyname(buf);
72 p = strchr(hp->h_name, '.');
Denis Vlasenko8514fc52006-09-22 08:53:14 +000073 if (opt & OPT_f) {
Eric Andersen3d61b102001-10-31 09:59:57 +000074 puts(hp->h_name);
Denis Vlasenko8514fc52006-09-22 08:53:14 +000075 } else if (opt & OPT_s) {
Eric Andersen3d61b102001-10-31 09:59:57 +000076 if (p != NULL) {
77 *p = 0;
78 }
Glenn L McGrath3ff3f4a2002-11-10 22:07:48 +000079 puts(hp->h_name);
Denis Vlasenko8514fc52006-09-22 08:53:14 +000080 } else if (opt & OPT_d) {
Eric Andersend69e31f2002-10-18 22:13:23 +000081 if (p) puts(p + 1);
Denis Vlasenko8514fc52006-09-22 08:53:14 +000082 } else if (opt & OPT_i) {
Eric Andersen3d61b102001-10-31 09:59:57 +000083 while (hp->h_addr_list[0]) {
84 printf("%s ", inet_ntoa(*(struct in_addr *) (*hp->h_addr_list++)));
85 }
Denis Vlasenko8514fc52006-09-22 08:53:14 +000086 puts("");
Erik Andersene49d5ec2000-02-08 19:58:47 +000087 }
88 }
Eric Andersen3d61b102001-10-31 09:59:57 +000089 /* Set the hostname */
Denis Vlasenko8514fc52006-09-22 08:53:14 +000090 else if (hostname_str != NULL) {
91 do_sethostname(hostname_str, 1);
Eric Andersen3d61b102001-10-31 09:59:57 +000092 } else if (optind < argc) {
93 do_sethostname(argv[optind], 0);
94 }
95 /* Or if all else fails,
96 * just print the current hostname */
Denis Vlasenko8514fc52006-09-22 08:53:14 +000097 else {
98 gethostname(buf, sizeof(buf));
Eric Andersen3d61b102001-10-31 09:59:57 +000099 puts(buf);
100 }
Denis Vlasenko8514fc52006-09-22 08:53:14 +0000101 return 0;
Eric Andersen485b9551999-12-07 23:14:59 +0000102}