- if (optind < argc) {
- do_sethostname(argv[optind], 0);
- } else if (opt_file) {
- do_sethostname(filename, 1);
- } else {
- gethostname(buf, 255);
- if (opt_short) {
- s = strchr(buf, '.');
- if (!s) s = buf; *s = 0;
- printf("%s\n", buf);
- } else if (opt_domain) {
- s = strchr(buf, '.');
- printf("%s\n", (s ? s+1 : ""));
- } else if (opt_ip) {
- h = gethostbyname(buf);
- if (!h) {
- printf("Host not found\n");
- exit(1);
- }
- printf("%s\n", inet_ntoa(*(struct in_addr *)(h->h_addr)));
- } else {
- printf("%s\n", buf);
- }
- }
- return 0;
+ 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);