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 | bbaef66 | 2000-09-27 02:43:35 +0000 | [diff] [blame^] | 3 | * $Id: hostname.c,v 1.14 2000/09/27 02:43:35 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 | |
Eric Andersen | 3570a34 | 2000-09-25 21:45:58 +0000 | [diff] [blame] | 26 | #include "busybox.h" |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 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 | |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 33 | void do_sethostname(char *s, int isfile) |
| 34 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 35 | FILE *f; |
| 36 | char buf[255]; |
| 37 | |
| 38 | if (!s) |
| 39 | return; |
| 40 | if (!isfile) { |
| 41 | if (sethostname(s, strlen(s)) < 0) { |
| 42 | if (errno == EPERM) |
Matt Kraai | d537a95 | 2000-07-14 01:51:25 +0000 | [diff] [blame] | 43 | errorMsg("you must be root to change the hostname\n"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 44 | else |
| 45 | perror("sethostname"); |
| 46 | exit(1); |
| 47 | } |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 48 | } else { |
Matt Kraai | bbaef66 | 2000-09-27 02:43:35 +0000 | [diff] [blame^] | 49 | f = xfopen(s, "r"); |
| 50 | fgets(buf, 255, f); |
| 51 | fclose(f); |
| 52 | if (buf[strlen(buf) - 1] == '\n') |
| 53 | buf[strlen(buf) - 1] = 0; |
| 54 | if (sethostname(buf, strlen(buf)) < 0) { |
| 55 | perror("sethostname"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 56 | exit(1); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 57 | } |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 58 | } |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | int hostname_main(int argc, char **argv) |
| 62 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 63 | int opt_short = 0; |
| 64 | int opt_domain = 0; |
| 65 | int opt_ip = 0; |
| 66 | struct hostent *h; |
| 67 | char *filename = NULL; |
| 68 | char buf[255]; |
| 69 | char *s = NULL; |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 70 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 71 | if (argc < 1) |
Eric Andersen | d29edf3 | 1999-12-08 04:13:44 +0000 | [diff] [blame] | 72 | usage(hostname_usage); |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 73 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 74 | while (--argc > 0 && **(++argv) == '-') { |
| 75 | while (*(++(*argv))) { |
| 76 | switch (**argv) { |
| 77 | case 's': |
| 78 | opt_short = 1; |
| 79 | break; |
| 80 | case 'i': |
| 81 | opt_ip = 1; |
| 82 | break; |
| 83 | case 'd': |
| 84 | opt_domain = 1; |
| 85 | break; |
| 86 | case 'F': |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 87 | if (--argc == 0) { |
| 88 | usage(hostname_usage); |
| 89 | } |
| 90 | filename = *(++argv); |
| 91 | break; |
| 92 | default: |
| 93 | usage(hostname_usage); |
| 94 | } |
| 95 | if (filename != NULL) |
| 96 | break; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | if (argc >= 1) { |
| 101 | do_sethostname(*argv, 0); |
| 102 | } else if (filename != NULL) { |
| 103 | do_sethostname(filename, 1); |
| 104 | } else { |
| 105 | gethostname(buf, 255); |
| 106 | if (opt_short) { |
| 107 | s = strchr(buf, '.'); |
| 108 | if (!s) |
| 109 | s = buf; |
| 110 | *s = 0; |
| 111 | printf("%s\n", buf); |
| 112 | } else if (opt_domain) { |
| 113 | s = strchr(buf, '.'); |
| 114 | printf("%s\n", (s ? s + 1 : "")); |
| 115 | } else if (opt_ip) { |
| 116 | h = gethostbyname(buf); |
| 117 | if (!h) { |
| 118 | printf("Host not found\n"); |
| 119 | exit(1); |
| 120 | } |
| 121 | printf("%s\n", inet_ntoa(*(struct in_addr *) (h->h_addr))); |
| 122 | } else { |
| 123 | printf("%s\n", buf); |
| 124 | } |
| 125 | } |
Eric Andersen | b610615 | 2000-06-19 17:25:40 +0000 | [diff] [blame] | 126 | return(0); |
Eric Andersen | 485b955 | 1999-12-07 23:14:59 +0000 | [diff] [blame] | 127 | } |