1 |
/* vi: set sw=4 ts=4: */ |
/* vi: set sw=4 ts=4: */ |
2 |
/* |
/* |
|
* $Id: hostname.c,v 1.1 2007-09-01 22:43:53 niro Exp $ |
|
3 |
* Mini hostname implementation for busybox |
* Mini hostname implementation for busybox |
4 |
* |
* |
5 |
* Copyright (C) 1999 by Randolph Chung <tausq@debian.org> |
* Copyright (C) 1999 by Randolph Chung <tausq@debian.org> |
12 |
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
13 |
*/ |
*/ |
14 |
|
|
15 |
#include "busybox.h" |
#include "libbb.h" |
16 |
|
|
17 |
static void do_sethostname(char *s, int isfile) |
static void do_sethostname(char *s, int isfile) |
18 |
{ |
{ |
|
FILE *f; |
|
|
|
|
19 |
if (!s) |
if (!s) |
20 |
return; |
return; |
21 |
if (!isfile) { |
if (isfile) { |
22 |
if (sethostname(s, strlen(s)) < 0) { |
parser_t *parser = config_open2(s, xfopen_for_read); |
23 |
if (errno == EPERM) |
while (config_read(parser, &s, 1, 1, "# \t", PARSE_NORMAL & ~PARSE_GREEDY)) { |
24 |
bb_error_msg_and_die(bb_msg_perm_denied_are_you_root); |
do_sethostname(s, 0); |
|
else |
|
|
bb_perror_msg_and_die("sethostname"); |
|
|
} |
|
|
} else { |
|
|
f = xfopen(s, "r"); |
|
|
while (fgets(bb_common_bufsiz1, sizeof(bb_common_bufsiz1), f) != NULL) { |
|
|
if (bb_common_bufsiz1[0] == '#') { |
|
|
continue; |
|
|
} |
|
|
chomp(bb_common_bufsiz1); |
|
|
do_sethostname(bb_common_bufsiz1, 0); |
|
25 |
} |
} |
26 |
if (ENABLE_FEATURE_CLEAN_UP) |
if (ENABLE_FEATURE_CLEAN_UP) |
27 |
fclose(f); |
config_close(parser); |
28 |
|
} else if (sethostname(s, strlen(s)) < 0) { |
29 |
|
if (errno == EPERM) |
30 |
|
bb_error_msg_and_die(bb_msg_perm_denied_are_you_root); |
31 |
|
bb_perror_msg_and_die("sethostname"); |
32 |
} |
} |
33 |
} |
} |
34 |
|
|
35 |
|
int hostname_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
36 |
int hostname_main(int argc, char **argv) |
int hostname_main(int argc, char **argv) |
37 |
{ |
{ |
38 |
enum { |
enum { |
40 |
OPT_f = 0x2, |
OPT_f = 0x2, |
41 |
OPT_i = 0x4, |
OPT_i = 0x4, |
42 |
OPT_s = 0x8, |
OPT_s = 0x8, |
43 |
|
OPT_F = 0x10, |
44 |
OPT_dfis = 0xf, |
OPT_dfis = 0xf, |
45 |
}; |
}; |
46 |
|
|
47 |
char buf[256]; |
char *buf; |
48 |
char *hostname_str = NULL; |
char *hostname_str; |
49 |
|
|
50 |
if (argc < 1) |
if (argc < 1) |
51 |
bb_show_usage(); |
bb_show_usage(); |
52 |
|
|
53 |
getopt32(argc, argv, "dfisF:", &hostname_str); |
getopt32(argv, "dfisF:", &hostname_str); |
54 |
|
argv += optind; |
55 |
|
buf = safe_gethostname(); |
56 |
|
|
57 |
/* Output in desired format */ |
/* Output in desired format */ |
58 |
if (option_mask32 & OPT_dfis) { |
if (option_mask32 & OPT_dfis) { |
59 |
struct hostent *hp; |
struct hostent *hp; |
60 |
char *p; |
char *p; |
|
gethostname(buf, sizeof(buf)); |
|
61 |
hp = xgethostbyname(buf); |
hp = xgethostbyname(buf); |
62 |
p = strchr(hp->h_name, '.'); |
p = strchr(hp->h_name, '.'); |
63 |
if (option_mask32 & OPT_f) { |
if (option_mask32 & OPT_f) { |
64 |
puts(hp->h_name); |
puts(hp->h_name); |
65 |
} else if (option_mask32 & OPT_s) { |
} else if (option_mask32 & OPT_s) { |
66 |
if (p != NULL) { |
if (p) |
67 |
*p = 0; |
*p = '\0'; |
|
} |
|
68 |
puts(hp->h_name); |
puts(hp->h_name); |
69 |
} else if (option_mask32 & OPT_d) { |
} else if (option_mask32 & OPT_d) { |
70 |
if (p) |
if (p) |
73 |
while (hp->h_addr_list[0]) { |
while (hp->h_addr_list[0]) { |
74 |
printf("%s ", inet_ntoa(*(struct in_addr *) (*hp->h_addr_list++))); |
printf("%s ", inet_ntoa(*(struct in_addr *) (*hp->h_addr_list++))); |
75 |
} |
} |
76 |
puts(""); |
bb_putchar('\n'); |
77 |
} |
} |
78 |
} |
} |
79 |
/* Set the hostname */ |
/* Set the hostname */ |
80 |
else if (hostname_str != NULL) { |
else if (option_mask32 & OPT_F) { |
81 |
do_sethostname(hostname_str, 1); |
do_sethostname(hostname_str, 1); |
82 |
} else if (optind < argc) { |
} else if (argv[0]) { |
83 |
do_sethostname(argv[optind], 0); |
do_sethostname(argv[0], 0); |
84 |
} |
} |
85 |
/* Or if all else fails, |
/* Or if all else fails, |
86 |
* just print the current hostname */ |
* just print the current hostname */ |
87 |
else { |
else { |
|
gethostname(buf, sizeof(buf)); |
|
88 |
puts(buf); |
puts(buf); |
89 |
} |
} |
90 |
return 0; |
if (ENABLE_FEATURE_CLEAN_UP) |
91 |
|
free(buf); |
92 |
|
return EXIT_SUCCESS; |
93 |
} |
} |