diff options
author | Eric Andersen <andersen@codepoet.org> | 2001-03-19 19:41:54 +0000 |
---|---|---|
committer | Eric Andersen <andersen@codepoet.org> | 2001-03-19 19:41:54 +0000 |
commit | 40eaa9f0bb2c689552e39890592ed773d870c1fa (patch) | |
tree | f88f8fcebcdddf62365a40eab70da80bdceff8db | |
parent | 8269396491913ab551b558756f6581a4d12bd00f (diff) | |
download | busybox-w32-40eaa9f0bb2c689552e39890592ed773d870c1fa.tar.gz busybox-w32-40eaa9f0bb2c689552e39890592ed773d870c1fa.tar.bz2 busybox-w32-40eaa9f0bb2c689552e39890592ed773d870c1fa.zip |
Size optimization for rdate from Vladimir
-rw-r--r-- | rdate.c | 56 | ||||
-rw-r--r-- | util-linux/rdate.c | 56 |
2 files changed, 44 insertions, 68 deletions
@@ -33,16 +33,11 @@ | |||
33 | #include <stdlib.h> | 33 | #include <stdlib.h> |
34 | #include <unistd.h> | 34 | #include <unistd.h> |
35 | #include "busybox.h" | 35 | #include "busybox.h" |
36 | #define BB_DECLARE_EXTERN | ||
37 | #include "messages.c" | ||
38 | 36 | ||
39 | 37 | ||
40 | static const int RFC_868_BIAS = 2208988800UL; | 38 | static const int RFC_868_BIAS = 2208988800UL; |
41 | 39 | ||
42 | static int setdate= 0; | 40 | static time_t askremotedate(const char *host) |
43 | static int printdate= 0; | ||
44 | |||
45 | static time_t askremotedate(char *host) | ||
46 | { | 41 | { |
47 | struct hostent *h; | 42 | struct hostent *h; |
48 | struct sockaddr_in sin; | 43 | struct sockaddr_in sin; |
@@ -50,32 +45,25 @@ static time_t askremotedate(char *host) | |||
50 | unsigned long int nett, localt; | 45 | unsigned long int nett, localt; |
51 | int fd; | 46 | int fd; |
52 | 47 | ||
53 | if (!(h = gethostbyname(host))) { /* get the IP addr */ | 48 | if (!(h = gethostbyname(host))) /* get the IP addr */ |
54 | perror_msg("%s", host); | 49 | perror_msg_and_die("%s", host); |
55 | return(-1); | 50 | |
56 | } | 51 | if ((tserv = getservbyname("time", "tcp")) == NULL) /* find port # */ |
57 | if ((tserv = getservbyname("time", "tcp")) == NULL) { /* find port # */ | 52 | perror_msg_and_die("%s", "time"); |
58 | perror_msg("%s", "time"); | 53 | |
59 | return(-1); | 54 | if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) /* get net connection */ |
60 | } | 55 | perror_msg_and_die("%s", "socket"); |
61 | if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { /* get net connection */ | ||
62 | perror_msg("%s", "socket"); | ||
63 | return(-1); | ||
64 | } | ||
65 | 56 | ||
66 | memcpy(&sin.sin_addr, h->h_addr, sizeof(sin.sin_addr)); | 57 | memcpy(&sin.sin_addr, h->h_addr, sizeof(sin.sin_addr)); |
67 | sin.sin_port= tserv->s_port; | 58 | sin.sin_port= tserv->s_port; |
68 | sin.sin_family = AF_INET; | 59 | sin.sin_family = AF_INET; |
69 | 60 | ||
70 | if (connect(fd, (struct sockaddr *)&sin, sizeof(sin)) < 0) { /* connect to time server */ | 61 | if (connect(fd, (struct sockaddr *)&sin, sizeof(sin)) < 0) /* connect to time server */ |
71 | perror_msg("%s", host); | 62 | perror_msg_and_die("%s", host); |
72 | close(fd); | 63 | |
73 | return(-1); | 64 | if (read(fd, (void *)&nett, 4) != 4) /* read time from server */ |
74 | } | 65 | error_msg_and_die("%s did not send the complete time", host); |
75 | if (read(fd, (void *)&nett, 4) != 4) { /* read time from server */ | 66 | |
76 | close(fd); | ||
77 | error_msg("%s did not send the complete time", host); | ||
78 | } | ||
79 | close(fd); | 67 | close(fd); |
80 | 68 | ||
81 | /* convert from network byte order to local byte order. | 69 | /* convert from network byte order to local byte order. |
@@ -93,6 +81,8 @@ int rdate_main(int argc, char **argv) | |||
93 | { | 81 | { |
94 | time_t time; | 82 | time_t time; |
95 | int opt; | 83 | int opt; |
84 | int setdate = 0; | ||
85 | int printdate= 0; | ||
96 | 86 | ||
97 | /* Interpret command line args */ | 87 | /* Interpret command line args */ |
98 | /* do special-case option parsing */ | 88 | /* do special-case option parsing */ |
@@ -118,20 +108,18 @@ int rdate_main(int argc, char **argv) | |||
118 | /* the default action is to set the date */ | 108 | /* the default action is to set the date */ |
119 | if (printdate==0 && setdate==0) setdate++; | 109 | if (printdate==0 && setdate==0) setdate++; |
120 | 110 | ||
121 | if (optind == argc) { | 111 | if (optind == argc) |
122 | show_usage(); | 112 | show_usage(); |
123 | } | ||
124 | 113 | ||
125 | if ((time= askremotedate(argv[optind])) == (time_t)-1) { | 114 | time = askremotedate(argv[optind]); |
126 | return EXIT_FAILURE; | 115 | |
127 | } | ||
128 | if (setdate) { | 116 | if (setdate) { |
129 | if (stime(&time) < 0) | 117 | if (stime(&time) < 0) |
130 | perror_msg_and_die("Could not set time of day"); | 118 | perror_msg_and_die("Could not set time of day"); |
131 | } | 119 | } |
132 | if (printdate) { | 120 | |
121 | if (printdate) | ||
133 | printf("%s", ctime(&time)); | 122 | printf("%s", ctime(&time)); |
134 | } | ||
135 | 123 | ||
136 | return EXIT_SUCCESS; | 124 | return EXIT_SUCCESS; |
137 | } | 125 | } |
diff --git a/util-linux/rdate.c b/util-linux/rdate.c index 28e87323d..5f31282fc 100644 --- a/util-linux/rdate.c +++ b/util-linux/rdate.c | |||
@@ -33,16 +33,11 @@ | |||
33 | #include <stdlib.h> | 33 | #include <stdlib.h> |
34 | #include <unistd.h> | 34 | #include <unistd.h> |
35 | #include "busybox.h" | 35 | #include "busybox.h" |
36 | #define BB_DECLARE_EXTERN | ||
37 | #include "messages.c" | ||
38 | 36 | ||
39 | 37 | ||
40 | static const int RFC_868_BIAS = 2208988800UL; | 38 | static const int RFC_868_BIAS = 2208988800UL; |
41 | 39 | ||
42 | static int setdate= 0; | 40 | static time_t askremotedate(const char *host) |
43 | static int printdate= 0; | ||
44 | |||
45 | static time_t askremotedate(char *host) | ||
46 | { | 41 | { |
47 | struct hostent *h; | 42 | struct hostent *h; |
48 | struct sockaddr_in sin; | 43 | struct sockaddr_in sin; |
@@ -50,32 +45,25 @@ static time_t askremotedate(char *host) | |||
50 | unsigned long int nett, localt; | 45 | unsigned long int nett, localt; |
51 | int fd; | 46 | int fd; |
52 | 47 | ||
53 | if (!(h = gethostbyname(host))) { /* get the IP addr */ | 48 | if (!(h = gethostbyname(host))) /* get the IP addr */ |
54 | perror_msg("%s", host); | 49 | perror_msg_and_die("%s", host); |
55 | return(-1); | 50 | |
56 | } | 51 | if ((tserv = getservbyname("time", "tcp")) == NULL) /* find port # */ |
57 | if ((tserv = getservbyname("time", "tcp")) == NULL) { /* find port # */ | 52 | perror_msg_and_die("%s", "time"); |
58 | perror_msg("%s", "time"); | 53 | |
59 | return(-1); | 54 | if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) /* get net connection */ |
60 | } | 55 | perror_msg_and_die("%s", "socket"); |
61 | if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { /* get net connection */ | ||
62 | perror_msg("%s", "socket"); | ||
63 | return(-1); | ||
64 | } | ||
65 | 56 | ||
66 | memcpy(&sin.sin_addr, h->h_addr, sizeof(sin.sin_addr)); | 57 | memcpy(&sin.sin_addr, h->h_addr, sizeof(sin.sin_addr)); |
67 | sin.sin_port= tserv->s_port; | 58 | sin.sin_port= tserv->s_port; |
68 | sin.sin_family = AF_INET; | 59 | sin.sin_family = AF_INET; |
69 | 60 | ||
70 | if (connect(fd, (struct sockaddr *)&sin, sizeof(sin)) < 0) { /* connect to time server */ | 61 | if (connect(fd, (struct sockaddr *)&sin, sizeof(sin)) < 0) /* connect to time server */ |
71 | perror_msg("%s", host); | 62 | perror_msg_and_die("%s", host); |
72 | close(fd); | 63 | |
73 | return(-1); | 64 | if (read(fd, (void *)&nett, 4) != 4) /* read time from server */ |
74 | } | 65 | error_msg_and_die("%s did not send the complete time", host); |
75 | if (read(fd, (void *)&nett, 4) != 4) { /* read time from server */ | 66 | |
76 | close(fd); | ||
77 | error_msg("%s did not send the complete time", host); | ||
78 | } | ||
79 | close(fd); | 67 | close(fd); |
80 | 68 | ||
81 | /* convert from network byte order to local byte order. | 69 | /* convert from network byte order to local byte order. |
@@ -93,6 +81,8 @@ int rdate_main(int argc, char **argv) | |||
93 | { | 81 | { |
94 | time_t time; | 82 | time_t time; |
95 | int opt; | 83 | int opt; |
84 | int setdate = 0; | ||
85 | int printdate= 0; | ||
96 | 86 | ||
97 | /* Interpret command line args */ | 87 | /* Interpret command line args */ |
98 | /* do special-case option parsing */ | 88 | /* do special-case option parsing */ |
@@ -118,20 +108,18 @@ int rdate_main(int argc, char **argv) | |||
118 | /* the default action is to set the date */ | 108 | /* the default action is to set the date */ |
119 | if (printdate==0 && setdate==0) setdate++; | 109 | if (printdate==0 && setdate==0) setdate++; |
120 | 110 | ||
121 | if (optind == argc) { | 111 | if (optind == argc) |
122 | show_usage(); | 112 | show_usage(); |
123 | } | ||
124 | 113 | ||
125 | if ((time= askremotedate(argv[optind])) == (time_t)-1) { | 114 | time = askremotedate(argv[optind]); |
126 | return EXIT_FAILURE; | 115 | |
127 | } | ||
128 | if (setdate) { | 116 | if (setdate) { |
129 | if (stime(&time) < 0) | 117 | if (stime(&time) < 0) |
130 | perror_msg_and_die("Could not set time of day"); | 118 | perror_msg_and_die("Could not set time of day"); |
131 | } | 119 | } |
132 | if (printdate) { | 120 | |
121 | if (printdate) | ||
133 | printf("%s", ctime(&time)); | 122 | printf("%s", ctime(&time)); |
134 | } | ||
135 | 123 | ||
136 | return EXIT_SUCCESS; | 124 | return EXIT_SUCCESS; |
137 | } | 125 | } |