diff options
author | Eric Andersen <andersen@codepoet.org> | 2001-10-31 09:59:57 +0000 |
---|---|---|
committer | Eric Andersen <andersen@codepoet.org> | 2001-10-31 09:59:57 +0000 |
commit | 3d61b10595a101ec60926728c9d1988a2db6db59 (patch) | |
tree | f169419a8c4873167d301d4b5eaf48aeffb6330c /networking/hostname.c | |
parent | 950d8b496f39c8474ef1944f18de4e52ca7b412a (diff) | |
download | busybox-w32-3d61b10595a101ec60926728c9d1988a2db6db59.tar.gz busybox-w32-3d61b10595a101ec60926728c9d1988a2db6db59.tar.bz2 busybox-w32-3d61b10595a101ec60926728c9d1988a2db6db59.zip |
Major cleanup from Charles Steinkuehler <charles@steinkuehler.net>:
- Switched to getopt argument parsing
- Added -f option to get fully qualified domain name
- Fixed the -s (short) and -d (domain) options, which were not
doing a gethostbyname lookup to get the FQDN before trying to
separate the local and domain portions of the hostname.
- Fixed probem with 'agressive setting' of the hostname...the
previous busybox version would try to set the hostname if called
with a non-option argument, or the -F option, even if another
option (like -i or -s) was given. This behavior does not match
the net-tools hostname, which does not attempt to set anything if
given a 'display' option, regardless of the presence/absence of
the -F option or additional command line arguments.
- When using a file to set the hostname, behavior now matches
net-tools version...previous busybox version did not handle
comments, and simply grabbed the first line from the file.
Diffstat (limited to 'networking/hostname.c')
-rw-r--r-- | networking/hostname.c | 114 |
1 files changed, 58 insertions, 56 deletions
diff --git a/networking/hostname.c b/networking/hostname.c index 7a26c1b2c..16de86adb 100644 --- a/networking/hostname.c +++ b/networking/hostname.c | |||
@@ -1,6 +1,6 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | 1 | /* vi: set sw=4 ts=4: */ |
2 | /* | 2 | /* |
3 | * $Id: hostname.c,v 1.31 2001/10/24 04:59:56 andersen Exp $ | 3 | * $Id: hostname.c,v 1.32 2001/10/31 09:59:57 andersen Exp $ |
4 | * Mini hostname implementation for busybox | 4 | * Mini hostname implementation for busybox |
5 | * | 5 | * |
6 | * Copyright (C) 1999 by Randolph Chung <tausq@debian.org> | 6 | * Copyright (C) 1999 by Randolph Chung <tausq@debian.org> |
@@ -30,8 +30,12 @@ | |||
30 | #include <string.h> | 30 | #include <string.h> |
31 | #include <stdio.h> | 31 | #include <stdio.h> |
32 | #include <stdlib.h> | 32 | #include <stdlib.h> |
33 | #include <getopt.h> | ||
33 | #include "busybox.h" | 34 | #include "busybox.h" |
34 | 35 | ||
36 | extern char *optarg; /* in unistd.h */ | ||
37 | extern int optind, opterr, optopt; /* in unistd.h */ | ||
38 | |||
35 | static void do_sethostname(char *s, int isfile) | 39 | static void do_sethostname(char *s, int isfile) |
36 | { | 40 | { |
37 | FILE *f; | 41 | FILE *f; |
@@ -48,81 +52,79 @@ static void do_sethostname(char *s, int isfile) | |||
48 | } | 52 | } |
49 | } else { | 53 | } else { |
50 | f = xfopen(s, "r"); | 54 | f = xfopen(s, "r"); |
51 | fgets(buf, 255, f); | 55 | while (fgets(buf, 255, f) != NULL) { |
56 | if (buf[0] =='#') { | ||
57 | continue; | ||
58 | } | ||
59 | chomp(buf); | ||
60 | do_sethostname(buf, 0); | ||
61 | } | ||
52 | #ifdef CONFIG_FEATURE_CLEAN_UP | 62 | #ifdef CONFIG_FEATURE_CLEAN_UP |
53 | fclose(f); | 63 | fclose(f); |
54 | #endif | 64 | #endif |
55 | chomp(buf); | ||
56 | do_sethostname(buf, 0); | ||
57 | } | 65 | } |
58 | } | 66 | } |
59 | 67 | ||
60 | int hostname_main(int argc, char **argv) | 68 | int hostname_main(int argc, char **argv) |
61 | { | 69 | { |
62 | int opt_short = 0; | 70 | int opt; |
63 | int opt_domain = 0; | 71 | int type = 0; |
64 | int opt_ip = 0; | 72 | struct hostent *hp; |
65 | struct hostent *h; | ||
66 | char *filename = NULL; | 73 | char *filename = NULL; |
67 | char buf[255]; | 74 | char buf[255]; |
68 | char *s = NULL; | 75 | char *p = NULL; |
69 | 76 | ||
70 | if (argc < 1) | 77 | if (argc < 1) |
71 | show_usage(); | 78 | show_usage(); |
72 | 79 | ||
73 | while (--argc > 0 && **(++argv) == '-') { | 80 | while ((opt = getopt(argc, argv, "dfisF:")) > 0) { |
74 | while (*(++(*argv))) { | 81 | switch (opt) { |
75 | switch (**argv) { | 82 | case 'd': |
76 | case 's': | 83 | case 'f': |
77 | opt_short = 1; | 84 | case 'i': |
78 | break; | 85 | case 's': |
79 | case 'i': | 86 | type = opt; |
80 | opt_ip = 1; | 87 | break; |
81 | break; | 88 | case 'F': |
82 | case 'd': | 89 | filename = optarg; |
83 | opt_domain = 1; | 90 | break; |
84 | break; | 91 | default: |
85 | case 'F': | 92 | show_usage(); |
86 | if (--argc == 0) { | ||
87 | show_usage(); | ||
88 | } | ||
89 | filename = *(++argv); | ||
90 | break; | ||
91 | case '-': | ||
92 | if (strcmp(++(*argv), "file") || --argc ==0 ) { | ||
93 | show_usage(); | ||
94 | } | ||
95 | filename = *(++argv); | ||
96 | break; | ||
97 | default: | ||
98 | show_usage(); | ||
99 | } | ||
100 | if (filename != NULL) | ||
101 | break; | ||
102 | } | 93 | } |
103 | } | 94 | } |
104 | 95 | ||
105 | if (argc >= 1) { | 96 | /* Output in desired format */ |
106 | do_sethostname(*argv, 0); | 97 | if (type != 0) { |
107 | } else if (filename != NULL) { | ||
108 | do_sethostname(filename, 1); | ||
109 | } else { | ||
110 | gethostname(buf, 255); | 98 | gethostname(buf, 255); |
111 | if (opt_short) { | 99 | hp = xgethostbyname(buf); |
112 | s = strchr(buf, '.'); | 100 | p = strchr(hp->h_name, '.'); |
113 | if (!s) | 101 | if (type == 'f') { |
114 | s = buf; | 102 | puts(hp->h_name); |
115 | *s = 0; | 103 | } else if (type == 's') { |
116 | puts(buf); | 104 | if (p != NULL) { |
117 | } else if (opt_domain) { | 105 | *p = 0; |
118 | s = strchr(buf, '.'); | 106 | } |
119 | puts(s ? s + 1 : ""); | ||
120 | } else if (opt_ip) { | ||
121 | h = xgethostbyname(buf); | ||
122 | puts(inet_ntoa(*(struct in_addr *) (h->h_addr))); | ||
123 | } else { | ||
124 | puts(buf); | 107 | puts(buf); |
108 | } else if (type == 'd') { | ||
109 | puts(p ? p + 1 : ""); | ||
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"); | ||
125 | } | 115 | } |
126 | } | 116 | } |
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 | } | ||
127 | return(0); | 129 | return(0); |
128 | } | 130 | } |