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 | d69e31f | 2002-10-18 22:13:23 +0000 | [diff] [blame] | 3 | * $Id: hostname.c,v 1.33 2002/10/18 22:13:23 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 | d29edf3 | 1999-12-08 04:13:44 +0000 | [diff] [blame] | 8 | * adjusted by Erik Andersen <andersee@debian.org> to remove |
| 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 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License as published by |
| 13 | * the Free Software Foundation; either version 2 of the License, or |
| 14 | * (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 19 | * General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License |
| 22 | * along with this program; if not, write to the Free Software |
| 23 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 24 | */ |
| 25 | |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 26 | #include <errno.h> |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 27 | #include <arpa/inet.h> |
| 28 | #include <netdb.h> |
| 29 | #include <unistd.h> |
Eric Andersen | ed3ef50 | 2001-01-27 08:24:39 +0000 | [diff] [blame] | 30 | #include <string.h> |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 31 | #include <stdio.h> |
Eric Andersen | eba8ed7 | 2001-03-09 14:36:42 +0000 | [diff] [blame] | 32 | #include <stdlib.h> |
Eric Andersen | 3d61b10 | 2001-10-31 09:59:57 +0000 | [diff] [blame] | 33 | #include <getopt.h> |
Eric Andersen | e0c0757 | 2001-06-23 13:49:14 +0000 | [diff] [blame] | 34 | #include "busybox.h" |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 35 | |
Eric Andersen | 3d61b10 | 2001-10-31 09:59:57 +0000 | [diff] [blame] | 36 | extern char *optarg; /* in unistd.h */ |
| 37 | extern int optind, opterr, optopt; /* in unistd.h */ |
| 38 | |
Eric Andersen | 3e6ff90 | 2001-03-09 21:24:12 +0000 | [diff] [blame] | 39 | static void do_sethostname(char *s, int isfile) |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 40 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 41 | FILE *f; |
| 42 | char buf[255]; |
| 43 | |
| 44 | if (!s) |
| 45 | return; |
| 46 | if (!isfile) { |
| 47 | if (sethostname(s, strlen(s)) < 0) { |
| 48 | if (errno == EPERM) |
Matt Kraai | dd19c69 | 2001-01-31 19:00:21 +0000 | [diff] [blame] | 49 | error_msg_and_die("you must be root to change the hostname"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 50 | else |
Matt Kraai | a9819b2 | 2000-12-22 01:48:07 +0000 | [diff] [blame] | 51 | perror_msg_and_die("sethostname"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 52 | } |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 53 | } else { |
Matt Kraai | bbaef66 | 2000-09-27 02:43:35 +0000 | [diff] [blame] | 54 | f = xfopen(s, "r"); |
Eric Andersen | 3d61b10 | 2001-10-31 09:59:57 +0000 | [diff] [blame] | 55 | while (fgets(buf, 255, f) != NULL) { |
| 56 | if (buf[0] =='#') { |
| 57 | continue; |
| 58 | } |
| 59 | chomp(buf); |
| 60 | do_sethostname(buf, 0); |
| 61 | } |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 62 | #ifdef CONFIG_FEATURE_CLEAN_UP |
Matt Kraai | bbaef66 | 2000-09-27 02:43:35 +0000 | [diff] [blame] | 63 | fclose(f); |
Glenn L McGrath | 78b0e37 | 2001-06-26 02:06:08 +0000 | [diff] [blame] | 64 | #endif |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 65 | } |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | int hostname_main(int argc, char **argv) |
| 69 | { |
Eric Andersen | 3d61b10 | 2001-10-31 09:59:57 +0000 | [diff] [blame] | 70 | int opt; |
| 71 | int type = 0; |
| 72 | struct hostent *hp; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 73 | char *filename = NULL; |
| 74 | char buf[255]; |
Eric Andersen | 3d61b10 | 2001-10-31 09:59:57 +0000 | [diff] [blame] | 75 | char *p = NULL; |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 76 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 77 | if (argc < 1) |
Eric Andersen | 67991cf | 2001-02-14 21:23:06 +0000 | [diff] [blame] | 78 | show_usage(); |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 79 | |
Eric Andersen | 3d61b10 | 2001-10-31 09:59:57 +0000 | [diff] [blame] | 80 | while ((opt = getopt(argc, argv, "dfisF:")) > 0) { |
| 81 | switch (opt) { |
| 82 | case 'd': |
| 83 | case 'f': |
| 84 | case 'i': |
| 85 | case 's': |
| 86 | type = opt; |
| 87 | break; |
| 88 | case 'F': |
| 89 | filename = optarg; |
| 90 | break; |
| 91 | default: |
| 92 | show_usage(); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | |
Eric Andersen | 3d61b10 | 2001-10-31 09:59:57 +0000 | [diff] [blame] | 96 | /* Output in desired format */ |
| 97 | if (type != 0) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 98 | gethostname(buf, 255); |
Eric Andersen | 3d61b10 | 2001-10-31 09:59:57 +0000 | [diff] [blame] | 99 | hp = xgethostbyname(buf); |
| 100 | p = strchr(hp->h_name, '.'); |
| 101 | if (type == 'f') { |
| 102 | puts(hp->h_name); |
| 103 | } else if (type == 's') { |
| 104 | if (p != NULL) { |
| 105 | *p = 0; |
| 106 | } |
Matt Kraai | 59df6f7 | 2001-05-16 14:21:09 +0000 | [diff] [blame] | 107 | puts(buf); |
Eric Andersen | 3d61b10 | 2001-10-31 09:59:57 +0000 | [diff] [blame] | 108 | } else if (type == 'd') { |
Eric Andersen | d69e31f | 2002-10-18 22:13:23 +0000 | [diff] [blame] | 109 | if (p) puts(p + 1); |
Eric Andersen | 3d61b10 | 2001-10-31 09:59:57 +0000 | [diff] [blame] | 110 | } else if (type == 'i') { |
| 111 | while (hp->h_addr_list[0]) { |
| 112 | printf("%s ", inet_ntoa(*(struct in_addr *) (*hp->h_addr_list++))); |
| 113 | } |
| 114 | printf("\n"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 115 | } |
| 116 | } |
Eric Andersen | 3d61b10 | 2001-10-31 09:59:57 +0000 | [diff] [blame] | 117 | /* Set the hostname */ |
| 118 | else if (filename != NULL) { |
| 119 | do_sethostname(filename, 1); |
| 120 | } else if (optind < argc) { |
| 121 | do_sethostname(argv[optind], 0); |
| 122 | } |
| 123 | /* Or if all else fails, |
| 124 | * just print the current hostname */ |
| 125 | else { |
| 126 | gethostname(buf, 255); |
| 127 | puts(buf); |
| 128 | } |
Eric Andersen | b610615 | 2000-06-19 17:25:40 +0000 | [diff] [blame] | 129 | return(0); |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 130 | } |