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.
18 static void do_sethostname(char *s, int isfile)
26 if (sethostname(s, strlen(s)) < 0) {
28 bb_error_msg_and_die("you must be root to change the hostname");
30 bb_perror_msg_and_die("sethostname");
34 while (fgets(buf, sizeof(buf), f) != NULL) {
39 do_sethostname(buf, 0);
41 #ifdef CONFIG_FEATURE_CLEAN_UP
47 int hostname_main(int argc, char **argv)
59 char *hostname_str = NULL;
64 opt = getopt32(argc, argv, "dfisF:", &hostname_str);
66 /* Output in desired format */
70 gethostname(buf, sizeof(buf));
71 hp = xgethostbyname(buf);
72 p = strchr(hp->h_name, '.');
75 } else if (opt & OPT_s) {
80 } else if (opt & OPT_d) {
82 } else if (opt & OPT_i) {
83 while (hp->h_addr_list[0]) {
84 printf("%s ", inet_ntoa(*(struct in_addr *) (*hp->h_addr_list++)));
89 /* Set the hostname */
90 else if (hostname_str != NULL) {
91 do_sethostname(hostname_str, 1);
92 } else if (optind < argc) {
93 do_sethostname(argv[optind], 0);
95 /* Or if all else fails,
96 * just print the current hostname */
98 gethostname(buf, sizeof(buf));