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 | /* |
Matt Kraai | d537a95 | 2000-07-14 01:51:25 +0000 | [diff] [blame^] | 3 | * $Id: hostname.c,v 1.11 2000/07/14 01:51:25 kraai 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 | |
| 26 | #include "internal.h" |
| 27 | #include <errno.h> |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 28 | #include <arpa/inet.h> |
| 29 | #include <netdb.h> |
| 30 | #include <unistd.h> |
| 31 | #include <stdio.h> |
| 32 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 33 | static const char *hostname_usage = |
Erik Andersen | 7ab9c7e | 2000-05-12 19:41:47 +0000 | [diff] [blame] | 34 | "hostname [OPTION] {hostname | -F file}\n" |
| 35 | #ifndef BB_FEATURE_TRIVIAL_HELP |
| 36 | "\nGet or set the hostname or DNS domain name. If a hostname is given\n" |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 37 | "(or a file with the -F parameter), the host name will be set.\n\n" |
| 38 | "Options:\n" |
| 39 | "\t-s\t\tShort\n" |
| 40 | |
| 41 | "\t-i\t\tAddresses for the hostname\n" |
| 42 | "\t-d\t\tDNS domain name\n" |
Erik Andersen | 7ab9c7e | 2000-05-12 19:41:47 +0000 | [diff] [blame] | 43 | "\t-F FILE\t\tUse the contents of FILE to specify the hostname\n" |
| 44 | #endif |
| 45 | ; |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 46 | |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 47 | |
| 48 | void do_sethostname(char *s, int isfile) |
| 49 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 50 | FILE *f; |
| 51 | char buf[255]; |
| 52 | |
| 53 | if (!s) |
| 54 | return; |
| 55 | if (!isfile) { |
| 56 | if (sethostname(s, strlen(s)) < 0) { |
| 57 | if (errno == EPERM) |
Matt Kraai | d537a95 | 2000-07-14 01:51:25 +0000 | [diff] [blame^] | 58 | errorMsg("you must be root to change the hostname\n"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 59 | else |
| 60 | perror("sethostname"); |
| 61 | exit(1); |
| 62 | } |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 63 | } else { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 64 | if ((f = fopen(s, "r")) == NULL) { |
| 65 | perror(s); |
| 66 | exit(1); |
| 67 | } else { |
| 68 | fgets(buf, 255, f); |
| 69 | fclose(f); |
| 70 | if (buf[strlen(buf) - 1] == '\n') |
| 71 | buf[strlen(buf) - 1] = 0; |
| 72 | if (sethostname(buf, strlen(buf)) < 0) { |
| 73 | perror("sethostname"); |
| 74 | exit(1); |
| 75 | } |
| 76 | } |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 77 | } |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | int hostname_main(int argc, char **argv) |
| 81 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 82 | int opt_short = 0; |
| 83 | int opt_domain = 0; |
| 84 | int opt_ip = 0; |
| 85 | struct hostent *h; |
| 86 | char *filename = NULL; |
| 87 | char buf[255]; |
| 88 | char *s = NULL; |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 89 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 90 | if (argc < 1) |
Eric Andersen | d29edf3 | 1999-12-08 04:13:44 +0000 | [diff] [blame] | 91 | usage(hostname_usage); |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 92 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 93 | while (--argc > 0 && **(++argv) == '-') { |
| 94 | while (*(++(*argv))) { |
| 95 | switch (**argv) { |
| 96 | case 's': |
| 97 | opt_short = 1; |
| 98 | break; |
| 99 | case 'i': |
| 100 | opt_ip = 1; |
| 101 | break; |
| 102 | case 'd': |
| 103 | opt_domain = 1; |
| 104 | break; |
| 105 | case 'F': |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 106 | if (--argc == 0) { |
| 107 | usage(hostname_usage); |
| 108 | } |
| 109 | filename = *(++argv); |
| 110 | break; |
| 111 | default: |
| 112 | usage(hostname_usage); |
| 113 | } |
| 114 | if (filename != NULL) |
| 115 | break; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | if (argc >= 1) { |
| 120 | do_sethostname(*argv, 0); |
| 121 | } else if (filename != NULL) { |
| 122 | do_sethostname(filename, 1); |
| 123 | } else { |
| 124 | gethostname(buf, 255); |
| 125 | if (opt_short) { |
| 126 | s = strchr(buf, '.'); |
| 127 | if (!s) |
| 128 | s = buf; |
| 129 | *s = 0; |
| 130 | printf("%s\n", buf); |
| 131 | } else if (opt_domain) { |
| 132 | s = strchr(buf, '.'); |
| 133 | printf("%s\n", (s ? s + 1 : "")); |
| 134 | } else if (opt_ip) { |
| 135 | h = gethostbyname(buf); |
| 136 | if (!h) { |
| 137 | printf("Host not found\n"); |
| 138 | exit(1); |
| 139 | } |
| 140 | printf("%s\n", inet_ntoa(*(struct in_addr *) (h->h_addr))); |
| 141 | } else { |
| 142 | printf("%s\n", buf); |
| 143 | } |
| 144 | } |
Eric Andersen | b610615 | 2000-06-19 17:25:40 +0000 | [diff] [blame] | 145 | return(0); |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 146 | } |