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 | |
Eric Andersen | e0c0757 | 2001-06-23 13:49:14 +0000 | [diff] [blame] | 15 | #include "busybox.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 | FILE *f; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 20 | |
| 21 | if (!s) |
| 22 | return; |
| 23 | if (!isfile) { |
| 24 | if (sethostname(s, strlen(s)) < 0) { |
| 25 | if (errno == EPERM) |
Bernhard Reutner-Fischer | f07fe62 | 2007-01-09 10:06:19 +0000 | [diff] [blame] | 26 | bb_error_msg_and_die(bb_msg_perm_denied_are_you_root); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 27 | else |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 28 | bb_perror_msg_and_die("sethostname"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 29 | } |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 30 | } else { |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 31 | f = xfopen(s, "r"); |
Bernhard Reutner-Fischer | f07fe62 | 2007-01-09 10:06:19 +0000 | [diff] [blame] | 32 | while (fgets(bb_common_bufsiz1, sizeof(bb_common_bufsiz1), f) != NULL) { |
| 33 | if (bb_common_bufsiz1[0] == '#') { |
Eric Andersen | 3d61b10 | 2001-10-31 09:59:57 +0000 | [diff] [blame] | 34 | continue; |
| 35 | } |
Bernhard Reutner-Fischer | f07fe62 | 2007-01-09 10:06:19 +0000 | [diff] [blame] | 36 | chomp(bb_common_bufsiz1); |
| 37 | do_sethostname(bb_common_bufsiz1, 0); |
Eric Andersen | 3d61b10 | 2001-10-31 09:59:57 +0000 | [diff] [blame] | 38 | } |
Bernhard Reutner-Fischer | f07fe62 | 2007-01-09 10:06:19 +0000 | [diff] [blame] | 39 | if (ENABLE_FEATURE_CLEAN_UP) |
| 40 | fclose(f); |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 41 | } |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 42 | } |
| 43 | |
Denis Vlasenko | 06af216 | 2007-02-03 17:28:39 +0000 | [diff] [blame] | 44 | int hostname_main(int argc, char **argv); |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 45 | int hostname_main(int argc, char **argv) |
| 46 | { |
Denis Vlasenko | 8514fc5 | 2006-09-22 08:53:14 +0000 | [diff] [blame] | 47 | enum { |
| 48 | OPT_d = 0x1, |
| 49 | OPT_f = 0x2, |
| 50 | OPT_i = 0x4, |
| 51 | OPT_s = 0x8, |
Denis Vlasenko | bc5262d | 2007-01-26 07:02:56 +0000 | [diff] [blame] | 52 | OPT_F = 0x10, |
Denis Vlasenko | 8514fc5 | 2006-09-22 08:53:14 +0000 | [diff] [blame] | 53 | OPT_dfis = 0xf, |
| 54 | }; |
| 55 | |
| 56 | char buf[256]; |
Denis Vlasenko | 90ec4dc | 2007-01-25 19:44:38 +0000 | [diff] [blame] | 57 | char *hostname_str; |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 58 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 59 | if (argc < 1) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 60 | bb_show_usage(); |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 61 | |
Bernhard Reutner-Fischer | f07fe62 | 2007-01-09 10:06:19 +0000 | [diff] [blame] | 62 | getopt32(argc, argv, "dfisF:", &hostname_str); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 63 | |
Eric Andersen | 3d61b10 | 2001-10-31 09:59:57 +0000 | [diff] [blame] | 64 | /* Output in desired format */ |
Bernhard Reutner-Fischer | f07fe62 | 2007-01-09 10:06:19 +0000 | [diff] [blame] | 65 | if (option_mask32 & OPT_dfis) { |
Denis Vlasenko | 8514fc5 | 2006-09-22 08:53:14 +0000 | [diff] [blame] | 66 | struct hostent *hp; |
| 67 | char *p; |
| 68 | gethostname(buf, sizeof(buf)); |
Eric Andersen | 3d61b10 | 2001-10-31 09:59:57 +0000 | [diff] [blame] | 69 | hp = xgethostbyname(buf); |
| 70 | p = strchr(hp->h_name, '.'); |
Bernhard Reutner-Fischer | f07fe62 | 2007-01-09 10:06:19 +0000 | [diff] [blame] | 71 | if (option_mask32 & OPT_f) { |
Eric Andersen | 3d61b10 | 2001-10-31 09:59:57 +0000 | [diff] [blame] | 72 | puts(hp->h_name); |
Bernhard Reutner-Fischer | f07fe62 | 2007-01-09 10:06:19 +0000 | [diff] [blame] | 73 | } else if (option_mask32 & OPT_s) { |
Eric Andersen | 3d61b10 | 2001-10-31 09:59:57 +0000 | [diff] [blame] | 74 | if (p != NULL) { |
Denis Vlasenko | 90ec4dc | 2007-01-25 19:44:38 +0000 | [diff] [blame] | 75 | *p = '\0'; |
Eric Andersen | 3d61b10 | 2001-10-31 09:59:57 +0000 | [diff] [blame] | 76 | } |
Glenn L McGrath | 3ff3f4a | 2002-11-10 22:07:48 +0000 | [diff] [blame] | 77 | puts(hp->h_name); |
Bernhard Reutner-Fischer | f07fe62 | 2007-01-09 10:06:19 +0000 | [diff] [blame] | 78 | } else if (option_mask32 & OPT_d) { |
| 79 | if (p) |
| 80 | puts(p + 1); |
| 81 | } else if (option_mask32 & OPT_i) { |
Eric Andersen | 3d61b10 | 2001-10-31 09:59:57 +0000 | [diff] [blame] | 82 | while (hp->h_addr_list[0]) { |
| 83 | printf("%s ", inet_ntoa(*(struct in_addr *) (*hp->h_addr_list++))); |
| 84 | } |
Denis Vlasenko | 8514fc5 | 2006-09-22 08:53:14 +0000 | [diff] [blame] | 85 | puts(""); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 86 | } |
| 87 | } |
Eric Andersen | 3d61b10 | 2001-10-31 09:59:57 +0000 | [diff] [blame] | 88 | /* Set the hostname */ |
Denis Vlasenko | 90ec4dc | 2007-01-25 19:44:38 +0000 | [diff] [blame] | 89 | else if (option_mask32 & OPT_F) { |
Denis Vlasenko | 8514fc5 | 2006-09-22 08:53:14 +0000 | [diff] [blame] | 90 | do_sethostname(hostname_str, 1); |
Eric Andersen | 3d61b10 | 2001-10-31 09:59:57 +0000 | [diff] [blame] | 91 | } else if (optind < argc) { |
| 92 | do_sethostname(argv[optind], 0); |
| 93 | } |
| 94 | /* Or if all else fails, |
| 95 | * just print the current hostname */ |
Denis Vlasenko | 8514fc5 | 2006-09-22 08:53:14 +0000 | [diff] [blame] | 96 | else { |
| 97 | gethostname(buf, sizeof(buf)); |
Eric Andersen | 3d61b10 | 2001-10-31 09:59:57 +0000 | [diff] [blame] | 98 | puts(buf); |
| 99 | } |
Denis Vlasenko | 8514fc5 | 2006-09-22 08:53:14 +0000 | [diff] [blame] | 100 | return 0; |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 101 | } |