blob: 6975b2b307250a07431702ae9be02304d80b4f7d [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;
Erik Andersene49d5ec2000-02-08 19:58:47 +000021
22 if (!s)
23 return;
24 if (!isfile) {
25 if (sethostname(s, strlen(s)) < 0) {
26 if (errno == EPERM)
Bernhard Reutner-Fischerf07fe622007-01-09 10:06:19 +000027 bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
Erik Andersene49d5ec2000-02-08 19:58:47 +000028 else
Manuel Novoa III cad53642003-03-19 09:13:01 +000029 bb_perror_msg_and_die("sethostname");
Erik Andersene49d5ec2000-02-08 19:58:47 +000030 }
Eric Andersen485b9551999-12-07 23:14:59 +000031 } else {
Rob Landleyd921b2e2006-08-03 15:41:12 +000032 f = xfopen(s, "r");
Bernhard Reutner-Fischerf07fe622007-01-09 10:06:19 +000033 while (fgets(bb_common_bufsiz1, sizeof(bb_common_bufsiz1), f) != NULL) {
34 if (bb_common_bufsiz1[0] == '#') {
Eric Andersen3d61b102001-10-31 09:59:57 +000035 continue;
36 }
Bernhard Reutner-Fischerf07fe622007-01-09 10:06:19 +000037 chomp(bb_common_bufsiz1);
38 do_sethostname(bb_common_bufsiz1, 0);
Eric Andersen3d61b102001-10-31 09:59:57 +000039 }
Bernhard Reutner-Fischerf07fe622007-01-09 10:06:19 +000040 if (ENABLE_FEATURE_CLEAN_UP)
41 fclose(f);
Eric Andersen485b9551999-12-07 23:14:59 +000042 }
Eric Andersen485b9551999-12-07 23:14:59 +000043}
44
45int 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,
52 OPT_dfis = 0xf,
53 };
54
55 char buf[256];
Denis Vlasenko8514fc52006-09-22 08:53:14 +000056 char *hostname_str = NULL;
Eric Andersen485b9551999-12-07 23:14:59 +000057
Erik Andersene49d5ec2000-02-08 19:58:47 +000058 if (argc < 1)
Manuel Novoa III cad53642003-03-19 09:13:01 +000059 bb_show_usage();
Eric Andersen485b9551999-12-07 23:14:59 +000060
Bernhard Reutner-Fischerf07fe622007-01-09 10:06:19 +000061 getopt32(argc, argv, "dfisF:", &hostname_str);
Erik Andersene49d5ec2000-02-08 19:58:47 +000062
Eric Andersen3d61b102001-10-31 09:59:57 +000063 /* Output in desired format */
Bernhard Reutner-Fischerf07fe622007-01-09 10:06:19 +000064 if (option_mask32 & OPT_dfis) {
Denis Vlasenko8514fc52006-09-22 08:53:14 +000065 struct hostent *hp;
66 char *p;
67 gethostname(buf, sizeof(buf));
Eric Andersen3d61b102001-10-31 09:59:57 +000068 hp = xgethostbyname(buf);
69 p = strchr(hp->h_name, '.');
Bernhard Reutner-Fischerf07fe622007-01-09 10:06:19 +000070 if (option_mask32 & OPT_f) {
Eric Andersen3d61b102001-10-31 09:59:57 +000071 puts(hp->h_name);
Bernhard Reutner-Fischerf07fe622007-01-09 10:06:19 +000072 } else if (option_mask32 & OPT_s) {
Eric Andersen3d61b102001-10-31 09:59:57 +000073 if (p != NULL) {
74 *p = 0;
75 }
Glenn L McGrath3ff3f4a2002-11-10 22:07:48 +000076 puts(hp->h_name);
Bernhard Reutner-Fischerf07fe622007-01-09 10:06:19 +000077 } else if (option_mask32 & OPT_d) {
78 if (p)
79 puts(p + 1);
80 } else if (option_mask32 & OPT_i) {
Eric Andersen3d61b102001-10-31 09:59:57 +000081 while (hp->h_addr_list[0]) {
82 printf("%s ", inet_ntoa(*(struct in_addr *) (*hp->h_addr_list++)));
83 }
Denis Vlasenko8514fc52006-09-22 08:53:14 +000084 puts("");
Erik Andersene49d5ec2000-02-08 19:58:47 +000085 }
86 }
Eric Andersen3d61b102001-10-31 09:59:57 +000087 /* Set the hostname */
Denis Vlasenko8514fc52006-09-22 08:53:14 +000088 else if (hostname_str != NULL) {
89 do_sethostname(hostname_str, 1);
Eric Andersen3d61b102001-10-31 09:59:57 +000090 } else if (optind < argc) {
91 do_sethostname(argv[optind], 0);
92 }
93 /* Or if all else fails,
94 * just print the current hostname */
Denis Vlasenko8514fc52006-09-22 08:53:14 +000095 else {
96 gethostname(buf, sizeof(buf));
Eric Andersen3d61b102001-10-31 09:59:57 +000097 puts(buf);
98 }
Denis Vlasenko8514fc52006-09-22 08:53:14 +000099 return 0;
Eric Andersen485b9551999-12-07 23:14:59 +0000100}