1 /* vi: set sw=4 ts=4: */
3 * $Id: hostname.c,v 1.36 2003/07/14 21:21:01 andersen Exp $
4 * Mini hostname implementation for busybox
6 * Copyright (C) 1999 by Randolph Chung <tausq@debian.org>
8 * adjusted by Erik Andersen <andersen@codepoet.org> to remove
9 * use of long options and GNU getopt. Improved the usage info.
11 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
13 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
19 extern char *optarg; /* in unistd.h */
20 extern int optind, opterr, optopt; /* in unistd.h */
22 static void do_sethostname(char *s, int isfile)
30 if (sethostname(s, strlen(s)) < 0) {
32 bb_error_msg_and_die("you must be root to change the hostname");
34 bb_perror_msg_and_die("sethostname");
38 while (fgets(buf, 255, f) != NULL) {
43 do_sethostname(buf, 0);
45 #ifdef CONFIG_FEATURE_CLEAN_UP
51 int hostname_main(int argc, char **argv)
56 char *filename = NULL;
63 while ((opt = getopt(argc, argv, "dfisF:")) > 0) {
79 /* Output in desired format */
81 gethostname(buf, 255);
82 hp = xgethostbyname(buf);
83 p = strchr(hp->h_name, '.');
86 } else if (type == 's') {
91 } else if (type == 'd') {
93 } else if (type == 'i') {
94 while (hp->h_addr_list[0]) {
95 printf("%s ", inet_ntoa(*(struct in_addr *) (*hp->h_addr_list++)));
100 /* Set the hostname */
101 else if (filename != NULL) {
102 do_sethostname(filename, 1);
103 } else if (optind < argc) {
104 do_sethostname(argv[optind], 0);
106 /* Or if all else fails,
107 * just print the current hostname */
109 gethostname(buf, 255);