static void do_sethostname(char *s, int isfile) { FILE *f; char buf[255]; if (!s) return; if (!isfile) { if (sethostname(s, strlen(s)) < 0) { if (errno == EPERM) bb_error_msg_and_die("you must be root to change the hostname"); else bb_perror_msg_and_die("sethostname"); } } else { f = bb_xfopen(s, "r"); while (fgets(buf, 255, f) != NULL) { if (buf[0] =='#') { continue; } chomp(buf); do_sethostname(buf, 0); } #ifdef CONFIG_FEATURE_CLEAN_UP fclose(f); #endif } }
static void do_sethostname(char *s, int isfile) { // if (!s) // return; 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))) { // if (errno == EPERM) // bb_error_msg_and_die("%s", bb_msg_perm_denied_are_you_root); bb_perror_msg_and_die("sethostname"); } }
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; #if ENABLE_LONG_OPTS applet_long_options = "domain\0" No_argument "d" "fqdn\0" No_argument "f" //Enable if seen in active use in some distro: // "long\0" No_argument "f" // "ip-address\0" No_argument "i" // "short\0" No_argument "s" // "verbose\0" No_argument "v" "file\0" No_argument "F" ; #endif /* dnsdomainname from net-tools 1.60, hostname 1.100 (2001-04-14), * supports hostname's options too (not just -v as manpage says) */ opts = getopt32(argv, "dfisF:v", &hostname_str); argv += optind; buf = safe_gethostname(); if (applet_name[0] == 'd') /* dnsdomainname? */ opts = OPT_d; if (opts & OPT_dfi) { /* Cases when we need full hostname (or its part) */ 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 (opts & OPT_i)*/ { 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) { /* Set the hostname */ do_sethostname(hostname_str, 1); } else if (argv[0]) { /* Set the hostname */ do_sethostname(argv[0], 0); } else { /* Just print the current hostname */ puts(buf); } if (ENABLE_FEATURE_CLEAN_UP) free(buf); return EXIT_SUCCESS; }
int hostname_main(int argc, char **argv) { int opt; int type = 0; struct hostent *hp; char *filename = NULL; char buf[255]; char *p = NULL; if (argc < 1) bb_show_usage(); while ((opt = getopt(argc, argv, "dfisF:")) > 0) { switch (opt) { case 'd': case 'f': case 'i': case 's': type = opt; break; case 'F': filename = optarg; break; default: bb_show_usage(); } } /* Output in desired format */ if (type != 0) { gethostname(buf, 255); hp = xgethostbyname(buf); p = strchr(hp->h_name, '.'); if (type == 'f') { puts(hp->h_name); } else if (type == 's') { if (p != NULL) { *p = 0; } puts(hp->h_name); } else if (type == 'd') { if (p) puts(p + 1); } else if (type == 'i') { while (hp->h_addr_list[0]) { printf("%s ", inet_ntoa(*(struct in_addr *) (*hp->h_addr_list++))); } printf("\n"); } } /* Set the hostname */ else if (filename != NULL) { do_sethostname(filename, 1); } else if (optind < argc) { do_sethostname(argv[optind], 0); } /* Or if all else fails, * just print the current hostname */ else { gethostname(buf, 255); puts(buf); } return(0); }
int hostname_main(int argc, char **argv) { int opt_short = 0; int opt_domain = 0; int opt_ip = 0; struct hostent *h; char *filename = NULL; char buf[255]; char *s = NULL; if (argc < 1) printf("%% Incomplete command\r\n"); while (--argc > 0 && **(++argv) == '-') { while (*(++(*argv))) { switch (**argv) { case 's': opt_short = 1; break; case 'i': opt_ip = 1; break; case 'd': opt_domain = 1; break; case 'F': if (--argc == 0) { printf("%% Incomplete command\r\n"); } filename = *(++argv); break; case '-': if (strcmp(++(*argv), "file") || --argc ==0 ) { printf("%% Incomplete command\r\n"); } filename = *(++argv); break; default: printf("%% Incomplete command\r\n"); } if (filename != NULL) break; } } if (argc >= 1) { do_sethostname(*argv, 0); } else if (filename != NULL) { do_sethostname(filename, 1); } else { gethostname(buf, 255); if (opt_short) { s = strchr(buf, '.'); if (!s) s = buf; *s = 0; printf("%s", buf); } else if (opt_domain) { s = strchr(buf, '.'); printf("%s", (s ? s + 1 : "")); } else if (opt_ip) { h = gethostbyname(buf); if (!h) { printf("%% Host not found\n"); return(-1); } printf("%s", inet_ntoa(*(struct in_addr *) (h->h_addr))); } else { printf("%s", buf); } } return(0); }