Cleanup some warnings and a really obvious bug.
[oweals/busybox.git] / rdate.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * The Rdate command will ask a time server for the RFC 868 time
4  *  and optionally set the system time.
5  *
6  * by Sterling Huxley <sterling@europa.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
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 */
23
24 #include <sys/time.h>
25 #include <sys/types.h>
26 #include <sys/socket.h>
27 #include <netinet/in.h>
28 #include <netdb.h>
29 #include <stdio.h>
30 #include <getopt.h>
31 #include <string.h>
32 #include <time.h>
33 #include <stdlib.h>
34 #include <unistd.h>
35 #include "busybox.h"
36
37
38 static const int RFC_868_BIAS = 2208988800UL;
39
40 static time_t askremotedate(const char *host)
41 {
42         struct hostent *h;
43         struct sockaddr_in s_in;
44         struct servent *tserv;
45         unsigned long int nett, localt;
46         int fd;
47
48         h = xgethostbyname(host);         /* get the IP addr */
49
50         if ((tserv = getservbyname("time", "tcp")) == NULL)   /* find port # */
51                 perror_msg_and_die("%s", "time");
52
53         if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)    /* get net connection */
54                 perror_msg_and_die("%s", "socket");
55
56         memcpy(&s_in.sin_addr, h->h_addr, sizeof(s_in.sin_addr));
57         s_in.sin_port= tserv->s_port;
58         s_in.sin_family = AF_INET;
59
60         if (connect(fd, (struct sockaddr *)&s_in, sizeof(s_in)) < 0)      /* connect to time server */
61                 perror_msg_and_die("%s", host);
62
63         if (read(fd, (void *)&nett, 4) != 4)    /* read time from server */
64                 error_msg_and_die("%s did not send the complete time", host);
65
66         close(fd);
67
68         /* convert from network byte order to local byte order.
69          * RFC 868 time is the number of seconds
70          *  since 00:00 (midnight) 1 January 1900 GMT
71          *  the RFC 868 time 2,208,988,800 corresponds to 00:00  1 Jan 1970 GMT
72          * Subtract the RFC 868 time  to get Linux epoch
73          */
74         localt= ntohl(nett) - RFC_868_BIAS;
75
76         return(localt);
77 }
78
79 int rdate_main(int argc, char **argv)
80 {
81         time_t remote_time;
82         int opt;
83         int setdate = 0;
84         int printdate= 0;
85
86         /* Interpret command line args */
87         /* do special-case option parsing */
88         if (argv[1] && (strcmp(argv[1], "--help") == 0))
89                 show_usage();
90
91         /* do normal option parsing */
92         while ((opt = getopt(argc, argv, "Hsp")) > 0) {
93                 switch (opt) {
94                         default:
95                         case 'H':
96                                 show_usage();
97                                 break;
98                         case 's':
99                                 setdate++;
100                                 break;
101                         case 'p':
102                                 printdate++;
103                                 break;
104                 }
105         }
106
107         /* the default action is to set the date */
108         if (printdate==0 && setdate==0) setdate++;
109
110         if (optind == argc)
111                 show_usage();
112
113         remote_time = askremotedate(argv[optind]);
114
115         if (setdate) {
116                 if (stime(&remote_time) < 0)
117                         perror_msg_and_die("Could not set time of day");
118         }
119
120         if (printdate)
121                 printf("%s", ctime(&remote_time));
122
123         return EXIT_SUCCESS;
124 }