123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197 |
- #include "libbb.h"
- static void do_sethostname(char *s, int isfile)
- {
- if (isfile) {
- parser_t *parser = config_open2(s, xfopen_for_read);
- while (config_read(parser, &s, 1, 1, "# \t", PARSE_NORMAL & ~PARSE_GREEDY)) {
- do_sethostname(s, 0);
- }
- if (ENABLE_FEATURE_CLEAN_UP)
- config_close(parser);
- } else if (sethostname(s, strlen(s))) {
- bb_perror_msg_and_die("sethostname");
- }
- }
- int hostname_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
- int hostname_main(int argc UNUSED_PARAM, char **argv)
- {
- enum {
- OPT_d = 0x1,
- OPT_f = 0x2,
- OPT_i = 0x4,
- OPT_s = 0x8,
- OPT_F = 0x10,
- OPT_dfi = 0x7,
- };
- unsigned opts;
- char *buf;
- char *hostname_str;
-
- opts = getopt32(argv, "dfisF:v", &hostname_str,
- "domain\0" No_argument "d"
- "fqdn\0" No_argument "f"
-
-
-
-
-
- "file\0" No_argument "F"
- );
- argv += optind;
- buf = safe_gethostname();
- if (ENABLE_DNSDOMAINNAME) {
- if (!ENABLE_HOSTNAME || applet_name[0] == 'd') {
-
- opts = OPT_d;
- }
- }
- if (opts & OPT_dfi) {
-
- struct hostent *hp;
- char *p;
- hp = xgethostbyname(buf);
- p = strchrnul(hp->h_name, '.');
- if (opts & OPT_f) {
- puts(hp->h_name);
- } else if (opts & OPT_s) {
- *p = '\0';
- puts(hp->h_name);
- } else if (opts & OPT_d) {
- if (*p)
- puts(p + 1);
- } else {
- if (hp->h_length == sizeof(struct in_addr)) {
- struct in_addr **h_addr_list = (struct in_addr **)hp->h_addr_list;
- while (*h_addr_list) {
- printf(h_addr_list[1] ? "%s " : "%s", inet_ntoa(**h_addr_list));
- h_addr_list++;
- }
- bb_putchar('\n');
- }
- }
- } else if (opts & OPT_s) {
- strchrnul(buf, '.')[0] = '\0';
- puts(buf);
- } else if (opts & OPT_F) {
-
- do_sethostname(hostname_str, 1);
- } else if (argv[0]) {
-
- do_sethostname(argv[0], 0);
- } else {
-
- puts(buf);
- }
- if (ENABLE_FEATURE_CLEAN_UP)
- free(buf);
- return EXIT_SUCCESS;
- }
|