diff options
Diffstat (limited to 'hostname.c')
-rw-r--r-- | hostname.c | 193 |
1 files changed, 100 insertions, 93 deletions
diff --git a/hostname.c b/hostname.c index 68a560950..8cc334da0 100644 --- a/hostname.c +++ b/hostname.c | |||
@@ -1,5 +1,6 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
1 | /* | 2 | /* |
2 | * $Id: hostname.c,v 1.6 2000/02/07 05:29:42 erik Exp $ | 3 | * $Id: hostname.c,v 1.7 2000/02/08 19:58:47 erik Exp $ |
3 | * Mini hostname implementation for busybox | 4 | * Mini hostname implementation for busybox |
4 | * | 5 | * |
5 | * Copyright (C) 1999 by Randolph Chung <tausq@debian.org> | 6 | * Copyright (C) 1999 by Randolph Chung <tausq@debian.org> |
@@ -29,110 +30,116 @@ | |||
29 | #include <unistd.h> | 30 | #include <unistd.h> |
30 | #include <stdio.h> | 31 | #include <stdio.h> |
31 | 32 | ||
32 | static const char* hostname_usage = | 33 | static const char *hostname_usage = |
33 | "hostname [OPTION] {hostname | -F file}\n\n" | 34 | "hostname [OPTION] {hostname | -F file}\n\n" |
34 | "Get or set the hostname or DNS domain name. If a hostname is given\n" | 35 | "Get or set the hostname or DNS domain name. If a hostname is given\n" |
35 | "(or a file with the -F parameter), the host name will be set.\n\n" | 36 | "(or a file with the -F parameter), the host name will be set.\n\n" |
36 | "Options:\n" | 37 | "Options:\n" |
37 | "\t-s\t\tShort\n" | 38 | "\t-s\t\tShort\n" |
38 | "\t-i\t\tAddresses for the hostname\n" | 39 | |
39 | "\t-d\t\tDNS domain name\n" | 40 | "\t-i\t\tAddresses for the hostname\n" |
40 | "\t-F FILE\t\tUse the contents of FILE to specify 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"; | ||
41 | 43 | ||
42 | 44 | ||
43 | void do_sethostname(char *s, int isfile) | 45 | void do_sethostname(char *s, int isfile) |
44 | { | 46 | { |
45 | FILE *f; | 47 | FILE *f; |
46 | char buf[255]; | 48 | char buf[255]; |
47 | 49 | ||
48 | if (!s) return; | 50 | if (!s) |
49 | if (!isfile) { | 51 | return; |
50 | if (sethostname(s, strlen(s)) < 0) { | 52 | if (!isfile) { |
51 | if (errno == EPERM) | 53 | if (sethostname(s, strlen(s)) < 0) { |
52 | fprintf(stderr, "hostname: you must be root to change the hostname\n"); | 54 | if (errno == EPERM) |
53 | else | 55 | fprintf(stderr, |
54 | perror("sethostname"); | 56 | "hostname: you must be root to change the hostname\n"); |
55 | exit(1); | 57 | else |
56 | } | 58 | perror("sethostname"); |
57 | } else { | 59 | exit(1); |
58 | if ((f = fopen(s, "r")) == NULL) { | 60 | } |
59 | perror(s); | ||
60 | exit(1); | ||
61 | } else { | 61 | } else { |
62 | fgets(buf, 255, f); | 62 | if ((f = fopen(s, "r")) == NULL) { |
63 | fclose(f); | 63 | perror(s); |
64 | if (buf[strlen(buf)-1] == '\n') buf[strlen(buf)-1] = 0; | 64 | exit(1); |
65 | if (sethostname(buf, strlen(buf)) < 0) { | 65 | } else { |
66 | perror("sethostname"); | 66 | fgets(buf, 255, f); |
67 | exit(1); | 67 | fclose(f); |
68 | } | 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 | } | ||
69 | } | 75 | } |
70 | } | ||
71 | } | 76 | } |
72 | 77 | ||
73 | int hostname_main(int argc, char **argv) | 78 | int hostname_main(int argc, char **argv) |
74 | { | 79 | { |
75 | int opt_short = 0; | 80 | int opt_short = 0; |
76 | int opt_domain = 0; | 81 | int opt_domain = 0; |
77 | int opt_ip = 0; | 82 | int opt_ip = 0; |
78 | struct hostent *h; | 83 | struct hostent *h; |
79 | char *filename = NULL; | 84 | char *filename = NULL; |
80 | char buf[255]; | 85 | char buf[255]; |
81 | char *s = NULL; | 86 | char *s = NULL; |
82 | |||
83 | if (argc < 1) usage(hostname_usage); | ||
84 | 87 | ||
85 | while (--argc > 0 && **(++argv) == '-') { | 88 | if (argc < 1) |
86 | while (*(++(*argv))) { | ||
87 | switch (**argv) { | ||
88 | case 's': | ||
89 | opt_short = 1; | ||
90 | break; | ||
91 | case 'i': | ||
92 | opt_ip = 1; | ||
93 | break; | ||
94 | case 'd': | ||
95 | opt_domain = 1; | ||
96 | break; | ||
97 | case 'F': | ||
98 | filename = optarg; | ||
99 | if (--argc == 0) { | ||
100 | usage(hostname_usage); | ||
101 | } | ||
102 | filename = *(++argv); | ||
103 | break; | ||
104 | default: | ||
105 | usage(hostname_usage); | 89 | usage(hostname_usage); |
106 | } | 90 | |
107 | if (filename!=NULL) | 91 | while (--argc > 0 && **(++argv) == '-') { |
108 | break; | 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 | } | ||
109 | } | 116 | } |
110 | } | ||
111 | 117 | ||
112 | if (argc >= 1) { | 118 | if (argc >= 1) { |
113 | do_sethostname(*argv, 0); | 119 | do_sethostname(*argv, 0); |
114 | } else if (filename!=NULL) { | 120 | } else if (filename != NULL) { |
115 | do_sethostname(filename, 1); | 121 | do_sethostname(filename, 1); |
116 | } else { | 122 | } else { |
117 | gethostname(buf, 255); | 123 | gethostname(buf, 255); |
118 | if (opt_short) { | 124 | if (opt_short) { |
119 | s = strchr(buf, '.'); | 125 | s = strchr(buf, '.'); |
120 | if (!s) s = buf; *s = 0; | 126 | if (!s) |
121 | printf("%s\n", buf); | 127 | s = buf; |
122 | } else if (opt_domain) { | 128 | *s = 0; |
123 | s = strchr(buf, '.'); | 129 | printf("%s\n", buf); |
124 | printf("%s\n", (s ? s+1 : "")); | 130 | } else if (opt_domain) { |
125 | } else if (opt_ip) { | 131 | s = strchr(buf, '.'); |
126 | h = gethostbyname(buf); | 132 | printf("%s\n", (s ? s + 1 : "")); |
127 | if (!h) { | 133 | } else if (opt_ip) { |
128 | printf("Host not found\n"); | 134 | h = gethostbyname(buf); |
129 | exit(1); | 135 | if (!h) { |
130 | } | 136 | printf("Host not found\n"); |
131 | printf("%s\n", inet_ntoa(*(struct in_addr *)(h->h_addr))); | 137 | exit(1); |
132 | } else { | 138 | } |
133 | printf("%s\n", buf); | 139 | printf("%s\n", inet_ntoa(*(struct in_addr *) (h->h_addr))); |
134 | } | 140 | } else { |
135 | } | 141 | printf("%s\n", buf); |
136 | exit( 0); | 142 | } |
143 | } | ||
144 | exit(0); | ||
137 | } | 145 | } |
138 | |||