Magellan Linux

Contents of /trunk/mkinitrd-magellan/busybox/networking/hostname.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 532 - (show annotations) (download)
Sat Sep 1 22:45:15 2007 UTC (16 years, 8 months ago) by niro
File MIME type: text/plain
File size: 2197 byte(s)
-import if magellan mkinitrd; it is a fork of redhats mkinitrd-5.0.8 with all magellan patches and features; deprecates magellan-src/mkinitrd

1 /* vi: set sw=4 ts=4: */
2 /*
3 * $Id: hostname.c,v 1.1 2007-09-01 22:43:53 niro Exp $
4 * Mini hostname implementation for busybox
5 *
6 * Copyright (C) 1999 by Randolph Chung <tausq@debian.org>
7 *
8 * adjusted by Erik Andersen <andersen@codepoet.org> to remove
9 * use of long options and GNU getopt. Improved the usage info.
10 *
11 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
12 *
13 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
14 */
15
16 #include "busybox.h"
17
18 static void do_sethostname(char *s, int isfile)
19 {
20 FILE *f;
21
22 if (!s)
23 return;
24 if (!isfile) {
25 if (sethostname(s, strlen(s)) < 0) {
26 if (errno == EPERM)
27 bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
28 else
29 bb_perror_msg_and_die("sethostname");
30 }
31 } else {
32 f = xfopen(s, "r");
33 while (fgets(bb_common_bufsiz1, sizeof(bb_common_bufsiz1), f) != NULL) {
34 if (bb_common_bufsiz1[0] == '#') {
35 continue;
36 }
37 chomp(bb_common_bufsiz1);
38 do_sethostname(bb_common_bufsiz1, 0);
39 }
40 if (ENABLE_FEATURE_CLEAN_UP)
41 fclose(f);
42 }
43 }
44
45 int hostname_main(int argc, char **argv)
46 {
47 enum {
48 OPT_d = 0x1,
49 OPT_f = 0x2,
50 OPT_i = 0x4,
51 OPT_s = 0x8,
52 OPT_dfis = 0xf,
53 };
54
55 char buf[256];
56 char *hostname_str = NULL;
57
58 if (argc < 1)
59 bb_show_usage();
60
61 getopt32(argc, argv, "dfisF:", &hostname_str);
62
63 /* Output in desired format */
64 if (option_mask32 & OPT_dfis) {
65 struct hostent *hp;
66 char *p;
67 gethostname(buf, sizeof(buf));
68 hp = xgethostbyname(buf);
69 p = strchr(hp->h_name, '.');
70 if (option_mask32 & OPT_f) {
71 puts(hp->h_name);
72 } else if (option_mask32 & OPT_s) {
73 if (p != NULL) {
74 *p = 0;
75 }
76 puts(hp->h_name);
77 } else if (option_mask32 & OPT_d) {
78 if (p)
79 puts(p + 1);
80 } else if (option_mask32 & OPT_i) {
81 while (hp->h_addr_list[0]) {
82 printf("%s ", inet_ntoa(*(struct in_addr *) (*hp->h_addr_list++)));
83 }
84 puts("");
85 }
86 }
87 /* Set the hostname */
88 else if (hostname_str != NULL) {
89 do_sethostname(hostname_str, 1);
90 } else if (optind < argc) {
91 do_sethostname(argv[optind], 0);
92 }
93 /* Or if all else fails,
94 * just print the current hostname */
95 else {
96 gethostname(buf, sizeof(buf));
97 puts(buf);
98 }
99 return 0;
100 }