blob: 16de86adbd3bcd7757aa6d3d61b34e6d53cf6406 [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 Andersen3d61b102001-10-31 09:59:57 +00003 * $Id: hostname.c,v 1.32 2001/10/31 09:59:57 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 Andersend29edf31999-12-08 04:13:44 +00008 * adjusted by Erik Andersen <andersee@debian.org> to remove
9 * use of long options and GNU getopt. Improved the usage info.
10 *
Eric Andersen485b9551999-12-07 23:14:59 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 */
25
Eric Andersen485b9551999-12-07 23:14:59 +000026#include <errno.h>
Eric Andersen485b9551999-12-07 23:14:59 +000027#include <arpa/inet.h>
28#include <netdb.h>
29#include <unistd.h>
Eric Andersened3ef502001-01-27 08:24:39 +000030#include <string.h>
Eric Andersen485b9551999-12-07 23:14:59 +000031#include <stdio.h>
Eric Anderseneba8ed72001-03-09 14:36:42 +000032#include <stdlib.h>
Eric Andersen3d61b102001-10-31 09:59:57 +000033#include <getopt.h>
Eric Andersene0c07572001-06-23 13:49:14 +000034#include "busybox.h"
Eric Andersen485b9551999-12-07 23:14:59 +000035
Eric Andersen3d61b102001-10-31 09:59:57 +000036extern char *optarg; /* in unistd.h */
37extern int optind, opterr, optopt; /* in unistd.h */
38
Eric Andersen3e6ff902001-03-09 21:24:12 +000039static void do_sethostname(char *s, int isfile)
Eric Andersen485b9551999-12-07 23:14:59 +000040{
Erik Andersene49d5ec2000-02-08 19:58:47 +000041 FILE *f;
42 char buf[255];
43
44 if (!s)
45 return;
46 if (!isfile) {
47 if (sethostname(s, strlen(s)) < 0) {
48 if (errno == EPERM)
Matt Kraaidd19c692001-01-31 19:00:21 +000049 error_msg_and_die("you must be root to change the hostname");
Erik Andersene49d5ec2000-02-08 19:58:47 +000050 else
Matt Kraaia9819b22000-12-22 01:48:07 +000051 perror_msg_and_die("sethostname");
Erik Andersene49d5ec2000-02-08 19:58:47 +000052 }
Eric Andersen485b9551999-12-07 23:14:59 +000053 } else {
Matt Kraaibbaef662000-09-27 02:43:35 +000054 f = xfopen(s, "r");
Eric Andersen3d61b102001-10-31 09:59:57 +000055 while (fgets(buf, 255, f) != NULL) {
56 if (buf[0] =='#') {
57 continue;
58 }
59 chomp(buf);
60 do_sethostname(buf, 0);
61 }
Eric Andersenbdfd0d72001-10-24 05:00:29 +000062#ifdef CONFIG_FEATURE_CLEAN_UP
Matt Kraaibbaef662000-09-27 02:43:35 +000063 fclose(f);
Glenn L McGrath78b0e372001-06-26 02:06:08 +000064#endif
Eric Andersen485b9551999-12-07 23:14:59 +000065 }
Eric Andersen485b9551999-12-07 23:14:59 +000066}
67
68int hostname_main(int argc, char **argv)
69{
Eric Andersen3d61b102001-10-31 09:59:57 +000070 int opt;
71 int type = 0;
72 struct hostent *hp;
Erik Andersene49d5ec2000-02-08 19:58:47 +000073 char *filename = NULL;
74 char buf[255];
Eric Andersen3d61b102001-10-31 09:59:57 +000075 char *p = NULL;
Eric Andersen485b9551999-12-07 23:14:59 +000076
Erik Andersene49d5ec2000-02-08 19:58:47 +000077 if (argc < 1)
Eric Andersen67991cf2001-02-14 21:23:06 +000078 show_usage();
Eric Andersen485b9551999-12-07 23:14:59 +000079
Eric Andersen3d61b102001-10-31 09:59:57 +000080 while ((opt = getopt(argc, argv, "dfisF:")) > 0) {
81 switch (opt) {
82 case 'd':
83 case 'f':
84 case 'i':
85 case 's':
86 type = opt;
87 break;
88 case 'F':
89 filename = optarg;
90 break;
91 default:
92 show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +000093 }
94 }
95
Eric Andersen3d61b102001-10-31 09:59:57 +000096 /* Output in desired format */
97 if (type != 0) {
Erik Andersene49d5ec2000-02-08 19:58:47 +000098 gethostname(buf, 255);
Eric Andersen3d61b102001-10-31 09:59:57 +000099 hp = xgethostbyname(buf);
100 p = strchr(hp->h_name, '.');
101 if (type == 'f') {
102 puts(hp->h_name);
103 } else if (type == 's') {
104 if (p != NULL) {
105 *p = 0;
106 }
Matt Kraai59df6f72001-05-16 14:21:09 +0000107 puts(buf);
Eric Andersen3d61b102001-10-31 09:59:57 +0000108 } else if (type == 'd') {
109 puts(p ? p + 1 : "");
110 } else if (type == 'i') {
111 while (hp->h_addr_list[0]) {
112 printf("%s ", inet_ntoa(*(struct in_addr *) (*hp->h_addr_list++)));
113 }
114 printf("\n");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000115 }
116 }
Eric Andersen3d61b102001-10-31 09:59:57 +0000117 /* Set the hostname */
118 else if (filename != NULL) {
119 do_sethostname(filename, 1);
120 } else if (optind < argc) {
121 do_sethostname(argv[optind], 0);
122 }
123 /* Or if all else fails,
124 * just print the current hostname */
125 else {
126 gethostname(buf, 255);
127 puts(buf);
128 }
Eric Andersenb6106152000-06-19 17:25:40 +0000129 return(0);
Eric Andersen485b9551999-12-07 23:14:59 +0000130}