Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 2 | /* |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 3 | * Mini hostname implementation for busybox |
| 4 | * |
| 5 | * Copyright (C) 1999 by Randolph Chung <tausq@debian.org> |
| 6 | * |
Eric Andersen | cb81e64 | 2003-07-14 21:21:08 +0000 | [diff] [blame] | 7 | * adjusted by Erik Andersen <andersen@codepoet.org> to remove |
Eric Andersen | d29edf3 | 1999-12-08 04:13:44 +0000 | [diff] [blame] | 8 | * use of long options and GNU getopt. Improved the usage info. |
| 9 | * |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 10 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
"Robert P. J. Day" | 2819f75 | 2006-07-11 11:32:31 +0000 | [diff] [blame] | 11 | * |
| 12 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 13 | */ |
| 14 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 15 | #include "libbb.h" |
Eric Andersen | 3d61b10 | 2001-10-31 09:59:57 +0000 | [diff] [blame] | 16 | |
Eric Andersen | 3e6ff90 | 2001-03-09 21:24:12 +0000 | [diff] [blame] | 17 | static void do_sethostname(char *s, int isfile) |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 18 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 19 | if (!s) |
| 20 | return; |
Denis Vlasenko | 5415c85 | 2008-07-21 23:05:26 +0000 | [diff] [blame] | 21 | if (isfile) { |
| 22 | parser_t *parser = config_open2(s, xfopen_for_read); |
Denis Vlasenko | 084266e | 2008-07-26 23:08:31 +0000 | [diff] [blame] | 23 | while (config_read(parser, &s, 1, 1, "# \t", PARSE_NORMAL & ~PARSE_GREEDY)) { |
Denis Vlasenko | 5415c85 | 2008-07-21 23:05:26 +0000 | [diff] [blame] | 24 | do_sethostname(s, 0); |
Eric Andersen | 3d61b10 | 2001-10-31 09:59:57 +0000 | [diff] [blame] | 25 | } |
Bernhard Reutner-Fischer | f07fe62 | 2007-01-09 10:06:19 +0000 | [diff] [blame] | 26 | if (ENABLE_FEATURE_CLEAN_UP) |
Denis Vlasenko | 5415c85 | 2008-07-21 23:05:26 +0000 | [diff] [blame] | 27 | 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"); |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 32 | } |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 33 | } |
| 34 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 35 | int hostname_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 36 | int hostname_main(int argc, char **argv) |
| 37 | { |
Denis Vlasenko | 8514fc5 | 2006-09-22 08:53:14 +0000 | [diff] [blame] | 38 | enum { |
| 39 | OPT_d = 0x1, |
| 40 | OPT_f = 0x2, |
| 41 | OPT_i = 0x4, |
| 42 | OPT_s = 0x8, |
Denis Vlasenko | bc5262d | 2007-01-26 07:02:56 +0000 | [diff] [blame] | 43 | OPT_F = 0x10, |
Denis Vlasenko | 8514fc5 | 2006-09-22 08:53:14 +0000 | [diff] [blame] | 44 | OPT_dfis = 0xf, |
| 45 | }; |
| 46 | |
Denis Vlasenko | 6f1713f | 2008-02-25 23:23:58 +0000 | [diff] [blame] | 47 | char *buf; |
Denis Vlasenko | 90ec4dc | 2007-01-25 19:44:38 +0000 | [diff] [blame] | 48 | char *hostname_str; |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 49 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 50 | if (argc < 1) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 51 | bb_show_usage(); |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 52 | |
Denis Vlasenko | fe7cd64 | 2007-08-18 15:32:12 +0000 | [diff] [blame] | 53 | getopt32(argv, "dfisF:", &hostname_str); |
Denis Vlasenko | 6f1713f | 2008-02-25 23:23:58 +0000 | [diff] [blame] | 54 | argv += optind; |
| 55 | buf = safe_gethostname(); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 56 | |
Eric Andersen | 3d61b10 | 2001-10-31 09:59:57 +0000 | [diff] [blame] | 57 | /* Output in desired format */ |
Bernhard Reutner-Fischer | f07fe62 | 2007-01-09 10:06:19 +0000 | [diff] [blame] | 58 | if (option_mask32 & OPT_dfis) { |
Denis Vlasenko | 8514fc5 | 2006-09-22 08:53:14 +0000 | [diff] [blame] | 59 | struct hostent *hp; |
| 60 | char *p; |
Eric Andersen | 3d61b10 | 2001-10-31 09:59:57 +0000 | [diff] [blame] | 61 | hp = xgethostbyname(buf); |
| 62 | p = strchr(hp->h_name, '.'); |
Bernhard Reutner-Fischer | f07fe62 | 2007-01-09 10:06:19 +0000 | [diff] [blame] | 63 | if (option_mask32 & OPT_f) { |
Eric Andersen | 3d61b10 | 2001-10-31 09:59:57 +0000 | [diff] [blame] | 64 | puts(hp->h_name); |
Bernhard Reutner-Fischer | f07fe62 | 2007-01-09 10:06:19 +0000 | [diff] [blame] | 65 | } else if (option_mask32 & OPT_s) { |
Denis Vlasenko | 6f1713f | 2008-02-25 23:23:58 +0000 | [diff] [blame] | 66 | if (p) |
Denis Vlasenko | 90ec4dc | 2007-01-25 19:44:38 +0000 | [diff] [blame] | 67 | *p = '\0'; |
Glenn L McGrath | 3ff3f4a | 2002-11-10 22:07:48 +0000 | [diff] [blame] | 68 | puts(hp->h_name); |
Bernhard Reutner-Fischer | f07fe62 | 2007-01-09 10:06:19 +0000 | [diff] [blame] | 69 | } else if (option_mask32 & OPT_d) { |
| 70 | if (p) |
| 71 | puts(p + 1); |
| 72 | } else if (option_mask32 & OPT_i) { |
Eric Andersen | 3d61b10 | 2001-10-31 09:59:57 +0000 | [diff] [blame] | 73 | while (hp->h_addr_list[0]) { |
| 74 | printf("%s ", inet_ntoa(*(struct in_addr *) (*hp->h_addr_list++))); |
| 75 | } |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 76 | bb_putchar('\n'); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 77 | } |
| 78 | } |
Eric Andersen | 3d61b10 | 2001-10-31 09:59:57 +0000 | [diff] [blame] | 79 | /* Set the hostname */ |
Denis Vlasenko | 90ec4dc | 2007-01-25 19:44:38 +0000 | [diff] [blame] | 80 | else if (option_mask32 & OPT_F) { |
Denis Vlasenko | 8514fc5 | 2006-09-22 08:53:14 +0000 | [diff] [blame] | 81 | do_sethostname(hostname_str, 1); |
Denis Vlasenko | 6f1713f | 2008-02-25 23:23:58 +0000 | [diff] [blame] | 82 | } else if (argv[0]) { |
| 83 | do_sethostname(argv[0], 0); |
Eric Andersen | 3d61b10 | 2001-10-31 09:59:57 +0000 | [diff] [blame] | 84 | } |
| 85 | /* Or if all else fails, |
| 86 | * just print the current hostname */ |
Denis Vlasenko | 8514fc5 | 2006-09-22 08:53:14 +0000 | [diff] [blame] | 87 | else { |
Eric Andersen | 3d61b10 | 2001-10-31 09:59:57 +0000 | [diff] [blame] | 88 | puts(buf); |
| 89 | } |
Denis Vlasenko | 6f1713f | 2008-02-25 23:23:58 +0000 | [diff] [blame] | 90 | if (ENABLE_FEATURE_CLEAN_UP) |
| 91 | free(buf); |
Denis Vlasenko | 5415c85 | 2008-07-21 23:05:26 +0000 | [diff] [blame] | 92 | return EXIT_SUCCESS; |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 93 | } |