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 | cb81e64 | 2003-07-14 21:21:08 +0000 | [diff] [blame] | 3 | * $Id: hostname.c,v 1.36 2003/07/14 21:21:01 andersen Exp $ |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 4 | * Mini hostname implementation for busybox |
| 5 | * |
| 6 | * Copyright (C) 1999 by Randolph Chung <tausq@debian.org> |
| 7 | * |
Eric Andersen | cb81e64 | 2003-07-14 21:21:08 +0000 | [diff] [blame] | 8 | * adjusted by Erik Andersen <andersen@codepoet.org> to remove |
Eric Andersen | d29edf3 | 1999-12-08 04:13:44 +0000 | [diff] [blame] | 9 | * use of long options and GNU getopt. Improved the usage info. |
| 10 | * |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 11 | * 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] | 12 | * |
| 13 | * 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] | 14 | */ |
| 15 | |
Eric Andersen | e0c0757 | 2001-06-23 13:49:14 +0000 | [diff] [blame] | 16 | #include "busybox.h" |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 17 | #include <getopt.h> |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 18 | |
Eric Andersen | 3d61b10 | 2001-10-31 09:59:57 +0000 | [diff] [blame] | 19 | extern char *optarg; /* in unistd.h */ |
| 20 | extern int optind, opterr, optopt; /* in unistd.h */ |
| 21 | |
Eric Andersen | 3e6ff90 | 2001-03-09 21:24:12 +0000 | [diff] [blame] | 22 | static void do_sethostname(char *s, int isfile) |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 23 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 24 | FILE *f; |
| 25 | char buf[255]; |
| 26 | |
| 27 | if (!s) |
| 28 | return; |
| 29 | if (!isfile) { |
| 30 | if (sethostname(s, strlen(s)) < 0) { |
| 31 | if (errno == EPERM) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 32 | bb_error_msg_and_die("you must be root to change the hostname"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 33 | else |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 34 | bb_perror_msg_and_die("sethostname"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 35 | } |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 36 | } else { |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 37 | f = xfopen(s, "r"); |
Eric Andersen | 3d61b10 | 2001-10-31 09:59:57 +0000 | [diff] [blame] | 38 | while (fgets(buf, 255, f) != NULL) { |
| 39 | if (buf[0] =='#') { |
| 40 | continue; |
| 41 | } |
| 42 | chomp(buf); |
| 43 | do_sethostname(buf, 0); |
| 44 | } |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 45 | #ifdef CONFIG_FEATURE_CLEAN_UP |
Matt Kraai | bbaef66 | 2000-09-27 02:43:35 +0000 | [diff] [blame] | 46 | fclose(f); |
Glenn L McGrath | 78b0e37 | 2001-06-26 02:06:08 +0000 | [diff] [blame] | 47 | #endif |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 48 | } |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | int hostname_main(int argc, char **argv) |
| 52 | { |
Eric Andersen | 3d61b10 | 2001-10-31 09:59:57 +0000 | [diff] [blame] | 53 | int opt; |
| 54 | int type = 0; |
| 55 | struct hostent *hp; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 56 | char *filename = NULL; |
| 57 | char buf[255]; |
Eric Andersen | 3d61b10 | 2001-10-31 09:59:57 +0000 | [diff] [blame] | 58 | char *p = NULL; |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 59 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 60 | if (argc < 1) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 61 | bb_show_usage(); |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 62 | |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 63 | while ((opt = getopt(argc, argv, "dfisF:")) > 0) { |
| 64 | switch (opt) { |
Eric Andersen | 3d61b10 | 2001-10-31 09:59:57 +0000 | [diff] [blame] | 65 | case 'd': |
| 66 | case 'f': |
| 67 | case 'i': |
| 68 | case 's': |
| 69 | type = opt; |
| 70 | break; |
| 71 | case 'F': |
| 72 | filename = optarg; |
| 73 | break; |
| 74 | default: |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 75 | bb_show_usage(); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 76 | } |
| 77 | } |
| 78 | |
Eric Andersen | 3d61b10 | 2001-10-31 09:59:57 +0000 | [diff] [blame] | 79 | /* Output in desired format */ |
| 80 | if (type != 0) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 81 | gethostname(buf, 255); |
Eric Andersen | 3d61b10 | 2001-10-31 09:59:57 +0000 | [diff] [blame] | 82 | hp = xgethostbyname(buf); |
| 83 | p = strchr(hp->h_name, '.'); |
| 84 | if (type == 'f') { |
| 85 | puts(hp->h_name); |
| 86 | } else if (type == 's') { |
| 87 | if (p != NULL) { |
| 88 | *p = 0; |
| 89 | } |
Glenn L McGrath | 3ff3f4a | 2002-11-10 22:07:48 +0000 | [diff] [blame] | 90 | puts(hp->h_name); |
Eric Andersen | 3d61b10 | 2001-10-31 09:59:57 +0000 | [diff] [blame] | 91 | } else if (type == 'd') { |
Eric Andersen | d69e31f | 2002-10-18 22:13:23 +0000 | [diff] [blame] | 92 | if (p) puts(p + 1); |
Eric Andersen | 3d61b10 | 2001-10-31 09:59:57 +0000 | [diff] [blame] | 93 | } else if (type == 'i') { |
| 94 | while (hp->h_addr_list[0]) { |
| 95 | printf("%s ", inet_ntoa(*(struct in_addr *) (*hp->h_addr_list++))); |
| 96 | } |
| 97 | printf("\n"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 98 | } |
| 99 | } |
Eric Andersen | 3d61b10 | 2001-10-31 09:59:57 +0000 | [diff] [blame] | 100 | /* Set the hostname */ |
| 101 | else if (filename != NULL) { |
| 102 | do_sethostname(filename, 1); |
| 103 | } else if (optind < argc) { |
| 104 | do_sethostname(argv[optind], 0); |
| 105 | } |
| 106 | /* Or if all else fails, |
| 107 | * just print the current hostname */ |
| 108 | else { |
| 109 | gethostname(buf, 255); |
| 110 | puts(buf); |
| 111 | } |
Eric Andersen | b610615 | 2000-06-19 17:25:40 +0000 | [diff] [blame] | 112 | return(0); |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 113 | } |