blob: ec4a0e8d815a065eb80d8717e937232ed47935bb [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 Andersen485b9551999-12-07 23:14:59 +000016#include <errno.h>
Eric Andersen485b9551999-12-07 23:14:59 +000017#include <arpa/inet.h>
18#include <netdb.h>
19#include <unistd.h>
Eric Andersened3ef502001-01-27 08:24:39 +000020#include <string.h>
Eric Andersen485b9551999-12-07 23:14:59 +000021#include <stdio.h>
Eric Anderseneba8ed72001-03-09 14:36:42 +000022#include <stdlib.h>
Eric Andersen3d61b102001-10-31 09:59:57 +000023#include <getopt.h>
Eric Andersene0c07572001-06-23 13:49:14 +000024#include "busybox.h"
Eric Andersen485b9551999-12-07 23:14:59 +000025
Eric Andersen3d61b102001-10-31 09:59:57 +000026extern char *optarg; /* in unistd.h */
27extern int optind, opterr, optopt; /* in unistd.h */
28
Eric Andersen3e6ff902001-03-09 21:24:12 +000029static void do_sethostname(char *s, int isfile)
Eric Andersen485b9551999-12-07 23:14:59 +000030{
Erik Andersene49d5ec2000-02-08 19:58:47 +000031 FILE *f;
32 char buf[255];
33
34 if (!s)
35 return;
36 if (!isfile) {
37 if (sethostname(s, strlen(s)) < 0) {
38 if (errno == EPERM)
Manuel Novoa III cad53642003-03-19 09:13:01 +000039 bb_error_msg_and_die("you must be root to change the hostname");
Erik Andersene49d5ec2000-02-08 19:58:47 +000040 else
Manuel Novoa III cad53642003-03-19 09:13:01 +000041 bb_perror_msg_and_die("sethostname");
Erik Andersene49d5ec2000-02-08 19:58:47 +000042 }
Eric Andersen485b9551999-12-07 23:14:59 +000043 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +000044 f = bb_xfopen(s, "r");
Eric Andersen3d61b102001-10-31 09:59:57 +000045 while (fgets(buf, 255, f) != NULL) {
46 if (buf[0] =='#') {
47 continue;
48 }
49 chomp(buf);
50 do_sethostname(buf, 0);
51 }
Eric Andersenbdfd0d72001-10-24 05:00:29 +000052#ifdef CONFIG_FEATURE_CLEAN_UP
Matt Kraaibbaef662000-09-27 02:43:35 +000053 fclose(f);
Glenn L McGrath78b0e372001-06-26 02:06:08 +000054#endif
Eric Andersen485b9551999-12-07 23:14:59 +000055 }
Eric Andersen485b9551999-12-07 23:14:59 +000056}
57
58int hostname_main(int argc, char **argv)
59{
Eric Andersen3d61b102001-10-31 09:59:57 +000060 int opt;
61 int type = 0;
62 struct hostent *hp;
Erik Andersene49d5ec2000-02-08 19:58:47 +000063 char *filename = NULL;
64 char buf[255];
Eric Andersen3d61b102001-10-31 09:59:57 +000065 char *p = NULL;
Eric Andersen485b9551999-12-07 23:14:59 +000066
Erik Andersene49d5ec2000-02-08 19:58:47 +000067 if (argc < 1)
Manuel Novoa III cad53642003-03-19 09:13:01 +000068 bb_show_usage();
Eric Andersen485b9551999-12-07 23:14:59 +000069
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000070 while ((opt = getopt(argc, argv, "dfisF:")) > 0) {
71 switch (opt) {
Eric Andersen3d61b102001-10-31 09:59:57 +000072 case 'd':
73 case 'f':
74 case 'i':
75 case 's':
76 type = opt;
77 break;
78 case 'F':
79 filename = optarg;
80 break;
81 default:
Manuel Novoa III cad53642003-03-19 09:13:01 +000082 bb_show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +000083 }
84 }
85
Eric Andersen3d61b102001-10-31 09:59:57 +000086 /* Output in desired format */
87 if (type != 0) {
Erik Andersene49d5ec2000-02-08 19:58:47 +000088 gethostname(buf, 255);
Eric Andersen3d61b102001-10-31 09:59:57 +000089 hp = xgethostbyname(buf);
90 p = strchr(hp->h_name, '.');
91 if (type == 'f') {
92 puts(hp->h_name);
93 } else if (type == 's') {
94 if (p != NULL) {
95 *p = 0;
96 }
Glenn L McGrath3ff3f4a2002-11-10 22:07:48 +000097 puts(hp->h_name);
Eric Andersen3d61b102001-10-31 09:59:57 +000098 } else if (type == 'd') {
Eric Andersend69e31f2002-10-18 22:13:23 +000099 if (p) puts(p + 1);
Eric Andersen3d61b102001-10-31 09:59:57 +0000100 } else if (type == 'i') {
101 while (hp->h_addr_list[0]) {
102 printf("%s ", inet_ntoa(*(struct in_addr *) (*hp->h_addr_list++)));
103 }
104 printf("\n");
Erik Andersene49d5ec2000-02-08 19:58:47 +0000105 }
106 }
Eric Andersen3d61b102001-10-31 09:59:57 +0000107 /* Set the hostname */
108 else if (filename != NULL) {
109 do_sethostname(filename, 1);
110 } else if (optind < argc) {
111 do_sethostname(argv[optind], 0);
112 }
113 /* Or if all else fails,
114 * just print the current hostname */
115 else {
116 gethostname(buf, 255);
117 puts(buf);
118 }
Eric Andersenb6106152000-06-19 17:25:40 +0000119 return(0);
Eric Andersen485b9551999-12-07 23:14:59 +0000120}