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