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 | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 16 | #include <errno.h> |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 17 | #include <arpa/inet.h> |
| 18 | #include <netdb.h> |
| 19 | #include <unistd.h> |
Eric Andersen | ed3ef50 | 2001-01-27 08:24:39 +0000 | [diff] [blame] | 20 | #include <string.h> |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 21 | #include <stdio.h> |
Eric Andersen | eba8ed7 | 2001-03-09 14:36:42 +0000 | [diff] [blame] | 22 | #include <stdlib.h> |
Eric Andersen | 3d61b10 | 2001-10-31 09:59:57 +0000 | [diff] [blame] | 23 | #include <getopt.h> |
Eric Andersen | e0c0757 | 2001-06-23 13:49:14 +0000 | [diff] [blame] | 24 | #include "busybox.h" |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 25 | |
Eric Andersen | 3d61b10 | 2001-10-31 09:59:57 +0000 | [diff] [blame] | 26 | extern char *optarg; /* in unistd.h */ |
| 27 | extern int optind, opterr, optopt; /* in unistd.h */ |
| 28 | |
Eric Andersen | 3e6ff90 | 2001-03-09 21:24:12 +0000 | [diff] [blame] | 29 | static void do_sethostname(char *s, int isfile) |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 30 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 31 | FILE *f; |
| 32 | char buf[255]; |
| 33 | |
| 34 | if (!s) |
| 35 | return; |
| 36 | if (!isfile) { |
| 37 | if (sethostname(s, strlen(s)) < 0) { |
| 38 | if (errno == EPERM) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 39 | 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] | 40 | else |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 41 | bb_perror_msg_and_die("sethostname"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 42 | } |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 43 | } else { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 44 | f = bb_xfopen(s, "r"); |
Eric Andersen | 3d61b10 | 2001-10-31 09:59:57 +0000 | [diff] [blame] | 45 | while (fgets(buf, 255, f) != NULL) { |
| 46 | if (buf[0] =='#') { |
| 47 | continue; |
| 48 | } |
| 49 | chomp(buf); |
| 50 | do_sethostname(buf, 0); |
| 51 | } |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 52 | #ifdef CONFIG_FEATURE_CLEAN_UP |
Matt Kraai | bbaef66 | 2000-09-27 02:43:35 +0000 | [diff] [blame] | 53 | fclose(f); |
Glenn L McGrath | 78b0e37 | 2001-06-26 02:06:08 +0000 | [diff] [blame] | 54 | #endif |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 55 | } |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | int hostname_main(int argc, char **argv) |
| 59 | { |
Eric Andersen | 3d61b10 | 2001-10-31 09:59:57 +0000 | [diff] [blame] | 60 | int opt; |
| 61 | int type = 0; |
| 62 | struct hostent *hp; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 63 | char *filename = NULL; |
| 64 | char buf[255]; |
Eric Andersen | 3d61b10 | 2001-10-31 09:59:57 +0000 | [diff] [blame] | 65 | char *p = NULL; |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 66 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 67 | if (argc < 1) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 68 | bb_show_usage(); |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 69 | |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 70 | while ((opt = getopt(argc, argv, "dfisF:")) > 0) { |
| 71 | switch (opt) { |
Eric Andersen | 3d61b10 | 2001-10-31 09:59:57 +0000 | [diff] [blame] | 72 | case 'd': |
| 73 | case 'f': |
| 74 | case 'i': |
| 75 | case 's': |
| 76 | type = opt; |
| 77 | break; |
| 78 | case 'F': |
| 79 | filename = optarg; |
| 80 | break; |
| 81 | default: |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 82 | bb_show_usage(); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | |
Eric Andersen | 3d61b10 | 2001-10-31 09:59:57 +0000 | [diff] [blame] | 86 | /* Output in desired format */ |
| 87 | if (type != 0) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 88 | gethostname(buf, 255); |
Eric Andersen | 3d61b10 | 2001-10-31 09:59:57 +0000 | [diff] [blame] | 89 | hp = xgethostbyname(buf); |
| 90 | p = strchr(hp->h_name, '.'); |
| 91 | if (type == 'f') { |
| 92 | puts(hp->h_name); |
| 93 | } else if (type == 's') { |
| 94 | if (p != NULL) { |
| 95 | *p = 0; |
| 96 | } |
Glenn L McGrath | 3ff3f4a | 2002-11-10 22:07:48 +0000 | [diff] [blame] | 97 | puts(hp->h_name); |
Eric Andersen | 3d61b10 | 2001-10-31 09:59:57 +0000 | [diff] [blame] | 98 | } else if (type == 'd') { |
Eric Andersen | d69e31f | 2002-10-18 22:13:23 +0000 | [diff] [blame] | 99 | if (p) puts(p + 1); |
Eric Andersen | 3d61b10 | 2001-10-31 09:59:57 +0000 | [diff] [blame] | 100 | } else if (type == 'i') { |
| 101 | while (hp->h_addr_list[0]) { |
| 102 | printf("%s ", inet_ntoa(*(struct in_addr *) (*hp->h_addr_list++))); |
| 103 | } |
| 104 | printf("\n"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 105 | } |
| 106 | } |
Eric Andersen | 3d61b10 | 2001-10-31 09:59:57 +0000 | [diff] [blame] | 107 | /* Set the hostname */ |
| 108 | else if (filename != NULL) { |
| 109 | do_sethostname(filename, 1); |
| 110 | } else if (optind < argc) { |
| 111 | do_sethostname(argv[optind], 0); |
| 112 | } |
| 113 | /* Or if all else fails, |
| 114 | * just print the current hostname */ |
| 115 | else { |
| 116 | gethostname(buf, 255); |
| 117 | puts(buf); |
| 118 | } |
Eric Andersen | b610615 | 2000-06-19 17:25:40 +0000 | [diff] [blame] | 119 | return(0); |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 120 | } |