diff options
author | Rob Landley <rob@landley.net> | 2005-10-28 09:24:33 +0000 |
---|---|---|
committer | Rob Landley <rob@landley.net> | 2005-10-28 09:24:33 +0000 |
commit | 1b751c894b520846af8575d09187ce342e88778c (patch) | |
tree | ea1bfb15742becbaab52960ee9fad82aba9fbcee /util-linux | |
parent | 0f34a821ab99e4936c7aa4974f58784442172211 (diff) | |
download | busybox-w32-1b751c894b520846af8575d09187ce342e88778c.tar.gz busybox-w32-1b751c894b520846af8575d09187ce342e88778c.tar.bz2 busybox-w32-1b751c894b520846af8575d09187ce342e88778c.zip |
Rob Sullivan sent in some cleanups, which I beat on slightly.
Diffstat (limited to 'util-linux')
-rw-r--r-- | util-linux/rdate.c | 63 |
1 files changed, 15 insertions, 48 deletions
diff --git a/util-linux/rdate.c b/util-linux/rdate.c index a73e8eebf..e2824607e 100644 --- a/util-linux/rdate.c +++ b/util-linux/rdate.c | |||
@@ -5,20 +5,7 @@ | |||
5 | * | 5 | * |
6 | * by Sterling Huxley <sterling@europa.com> | 6 | * by Sterling Huxley <sterling@europa.com> |
7 | * | 7 | * |
8 | * This program is free software; you can redistribute it and/or modify | 8 | * Licensed under GPL v2 or later, see file License for details. |
9 | * it under the terms of the GNU General Public License as published by | ||
10 | * the Free Software Foundation; either version 2 of the License, or | ||
11 | * (at your option) any later version. | ||
12 | * | ||
13 | * This program is distributed in the hope that it will be useful, | ||
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
16 | * General Public License for more details. | ||
17 | * | ||
18 | * You should have received a copy of the GNU General Public License | ||
19 | * along with this program; if not, write to the Free Software | ||
20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
21 | * | ||
22 | */ | 9 | */ |
23 | 10 | ||
24 | #include <sys/time.h> | 11 | #include <sys/time.h> |
@@ -27,7 +14,6 @@ | |||
27 | #include <netinet/in.h> | 14 | #include <netinet/in.h> |
28 | #include <netdb.h> | 15 | #include <netdb.h> |
29 | #include <stdio.h> | 16 | #include <stdio.h> |
30 | #include <getopt.h> | ||
31 | #include <string.h> | 17 | #include <string.h> |
32 | #include <time.h> | 18 | #include <time.h> |
33 | #include <stdlib.h> | 19 | #include <stdlib.h> |
@@ -46,14 +32,14 @@ static void socket_timeout(int sig) | |||
46 | 32 | ||
47 | static time_t askremotedate(const char *host) | 33 | static time_t askremotedate(const char *host) |
48 | { | 34 | { |
49 | unsigned long int nett, localt; | 35 | unsigned long nett; |
50 | struct sockaddr_in s_in; | 36 | struct sockaddr_in s_in; |
51 | int fd; | 37 | int fd; |
52 | 38 | ||
53 | bb_lookup_host(&s_in, host); | 39 | bb_lookup_host(&s_in, host); |
54 | s_in.sin_port = bb_lookup_port("time", "tcp", 37); | 40 | s_in.sin_port = bb_lookup_port("time", "tcp", 37); |
55 | 41 | ||
56 | /* Add a timeout for dead or non accessable servers */ | 42 | /* Add a timeout for dead or inaccessible servers */ |
57 | alarm(10); | 43 | alarm(10); |
58 | signal(SIGALRM, socket_timeout); | 44 | signal(SIGALRM, socket_timeout); |
59 | 45 | ||
@@ -61,7 +47,6 @@ static time_t askremotedate(const char *host) | |||
61 | 47 | ||
62 | if (safe_read(fd, (void *)&nett, 4) != 4) /* read time from server */ | 48 | if (safe_read(fd, (void *)&nett, 4) != 4) /* read time from server */ |
63 | bb_error_msg_and_die("%s did not send the complete time", host); | 49 | bb_error_msg_and_die("%s did not send the complete time", host); |
64 | |||
65 | close(fd); | 50 | close(fd); |
66 | 51 | ||
67 | /* convert from network byte order to local byte order. | 52 | /* convert from network byte order to local byte order. |
@@ -70,40 +55,22 @@ static time_t askremotedate(const char *host) | |||
70 | * the RFC 868 time 2,208,988,800 corresponds to 00:00 1 Jan 1970 GMT | 55 | * the RFC 868 time 2,208,988,800 corresponds to 00:00 1 Jan 1970 GMT |
71 | * Subtract the RFC 868 time to get Linux epoch | 56 | * Subtract the RFC 868 time to get Linux epoch |
72 | */ | 57 | */ |
73 | localt= ntohl(nett) - RFC_868_BIAS; | 58 | |
74 | 59 | return(ntohl(nett) - RFC_868_BIAS); | |
75 | return(localt); | ||
76 | } | 60 | } |
77 | 61 | ||
78 | int rdate_main(int argc, char **argv) | 62 | int rdate_main(int argc, char **argv) |
79 | { | 63 | { |
80 | time_t remote_time; | 64 | time_t remote_time; |
81 | int opt; | 65 | |
82 | int setdate = 1; | 66 | unsigned long flags = bb_getopt_ulflags(argc, argv, "sp"); |
83 | int printdate = 1; | 67 | |
84 | 68 | if (!flags || argc == optind) | |
85 | /* Interpret command line args */ | ||
86 | while ((opt = getopt(argc, argv, "sp")) > 0) { | ||
87 | switch (opt) { | ||
88 | case 's': | ||
89 | printdate = 0; | ||
90 | setdate = 1; | ||
91 | break; | ||
92 | case 'p': | ||
93 | printdate = 1; | ||
94 | setdate = 0; | ||
95 | break; | ||
96 | default: | ||
97 | bb_show_usage(); | ||
98 | } | ||
99 | } | ||
100 | |||
101 | if (optind == argc) | ||
102 | bb_show_usage(); | 69 | bb_show_usage(); |
103 | 70 | ||
104 | remote_time = askremotedate(argv[optind]); | 71 | remote_time = askremotedate(argv[optind]); |
105 | 72 | ||
106 | if (setdate) { | 73 | if (flags & 1) { |
107 | time_t current_time; | 74 | time_t current_time; |
108 | 75 | ||
109 | time(¤t_time); | 76 | time(¤t_time); |
@@ -112,10 +79,10 @@ int rdate_main(int argc, char **argv) | |||
112 | else | 79 | else |
113 | if (stime(&remote_time) < 0) | 80 | if (stime(&remote_time) < 0) |
114 | bb_perror_msg_and_die("Could not set time of day"); | 81 | bb_perror_msg_and_die("Could not set time of day"); |
115 | } | 82 | |
116 | 83 | /* No need to check for the -p flag as it's the only option left */ | |
117 | if (printdate) | 84 | |
118 | printf("%s", ctime(&remote_time)); | 85 | } else printf("%s", ctime(&remote_time)); |
119 | 86 | ||
120 | return EXIT_SUCCESS; | 87 | return EXIT_SUCCESS; |
121 | } | 88 | } |