blob: 03fd88edb535eda11adef30a844f15a2ce3ebd5d [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"
Rob Landleyd921b2e2006-08-03 15:41:12 +000017#include <getopt.h>
Eric Andersen485b9551999-12-07 23:14:59 +000018
Eric Andersen3d61b102001-10-31 09:59:57 +000019extern char *optarg; /* in unistd.h */
20extern int optind, opterr, optopt; /* in unistd.h */
21
Eric Andersen3e6ff902001-03-09 21:24:12 +000022static void do_sethostname(char *s, int isfile)
Eric Andersen485b9551999-12-07 23:14:59 +000023{
Erik Andersene49d5ec2000-02-08 19:58:47 +000024 FILE *f;
25 char buf[255];
26
27 if (!s)
28 return;
29 if (!isfile) {
30 if (sethostname(s, strlen(s)) < 0) {
31 if (errno == EPERM)
Manuel Novoa III cad53642003-03-19 09:13:01 +000032 bb_error_msg_and_die("you must be root to change the hostname");
Erik Andersene49d5ec2000-02-08 19:58:47 +000033 else
Manuel Novoa III cad53642003-03-19 09:13:01 +000034 bb_perror_msg_and_die("sethostname");
Erik Andersene49d5ec2000-02-08 19:58:47 +000035 }
Eric Andersen485b9551999-12-07 23:14:59 +000036 } else {
Rob Landleyd921b2e2006-08-03 15:41:12 +000037 f = xfopen(s, "r");
Eric Andersen3d61b102001-10-31 09:59:57 +000038 while (fgets(buf, 255, f) != NULL) {
39 if (buf[0] =='#') {
40 continue;
41 }
42 chomp(buf);
43 do_sethostname(buf, 0);
44 }
Eric Andersenbdfd0d72001-10-24 05:00:29 +000045#ifdef CONFIG_FEATURE_CLEAN_UP
Matt Kraaibbaef662000-09-27 02:43:35 +000046 fclose(f);
Glenn L McGrath78b0e372001-06-26 02:06:08 +000047#endif
Eric Andersen485b9551999-12-07 23:14:59 +000048 }
Eric Andersen485b9551999-12-07 23:14:59 +000049}
50
51int hostname_main(int argc, char **argv)
52{
Eric Andersen3d61b102001-10-31 09:59:57 +000053 int opt;
54 int type = 0;
55 struct hostent *hp;
Erik Andersene49d5ec2000-02-08 19:58:47 +000056 char *filename = NULL;
57 char buf[255];
Eric Andersen3d61b102001-10-31 09:59:57 +000058 char *p = NULL;
Eric Andersen485b9551999-12-07 23:14:59 +000059
Erik Andersene49d5ec2000-02-08 19:58:47 +000060 if (argc < 1)
Manuel Novoa III cad53642003-03-19 09:13:01 +000061 bb_show_usage();
Eric Andersen485b9551999-12-07 23:14:59 +000062
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000063 while ((opt = getopt(argc, argv, "dfisF:")) > 0) {
64 switch (opt) {
Eric Andersen3d61b102001-10-31 09:59:57 +000065 case 'd':
66 case 'f':
67 case 'i':
68 case 's':
69 type = opt;
70 break;
71 case 'F':
72 filename = optarg;
73 break;
74 default:
Manuel Novoa III cad53642003-03-19 09:13:01 +000075 bb_show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +000076 }
77 }
78
Eric Andersen3d61b102001-10-31 09:59:57 +000079 /* Output in desired format */
80 if (type != 0) {
Erik Andersene49d5ec2000-02-08 19:58:47 +000081 gethostname(buf, 255);
Eric Andersen3d61b102001-10-31 09:59:57 +000082 hp = xgethostbyname(buf);
83 p = strchr(hp->h_name, '.');
84 if (type == 'f') {
85 puts(hp->h_name);
86 } else if (type == 's') {
87 if (p != NULL) {
88 *p = 0;
89 }
Glenn L McGrath3ff3f4a2002-11-10 22:07:48 +000090 puts(hp->h_name);
Eric Andersen3d61b102001-10-31 09:59:57 +000091 } else if (type == 'd') {
Eric Andersend69e31f2002-10-18 22:13:23 +000092 if (p) puts(p + 1);
Eric Andersen3d61b102001-10-31 09:59:57 +000093 } else if (type == 'i') {
94 while (hp->h_addr_list[0]) {
95 printf("%s ", inet_ntoa(*(struct in_addr *) (*hp->h_addr_list++)));
96 }
97 printf("\n");
Erik Andersene49d5ec2000-02-08 19:58:47 +000098 }
99 }
Eric Andersen3d61b102001-10-31 09:59:57 +0000100 /* Set the hostname */
101 else if (filename != NULL) {
102 do_sethostname(filename, 1);
103 } else if (optind < argc) {
104 do_sethostname(argv[optind], 0);
105 }
106 /* Or if all else fails,
107 * just print the current hostname */
108 else {
109 gethostname(buf, 255);
110 puts(buf);
111 }
Eric Andersenb6106152000-06-19 17:25:40 +0000112 return(0);
Eric Andersen485b9551999-12-07 23:14:59 +0000113}