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