rename menu and touchup whitespace
[oweals/busybox.git] / util-linux / 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 <signal.h>
36
37 #include "busybox.h"
38
39
40 static const int RFC_868_BIAS = 2208988800UL;
41
42 static void socket_timeout(int sig)
43 {
44         bb_error_msg_and_die("timeout connecting to time server");
45 }
46
47 static time_t askremotedate(const char *host)
48 {
49         unsigned long int nett, localt;
50         struct sockaddr_in s_in;
51         int fd;
52
53         bb_lookup_host(&s_in, host);
54         s_in.sin_port = bb_lookup_port("time", "tcp", 37);
55
56         /* Add a timeout for dead or non accessable servers */
57         alarm(10);
58         signal(SIGALRM, socket_timeout);
59
60         fd = xconnect(&s_in);
61
62         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);
64
65         close(fd);
66
67         /* convert from network byte order to local byte order.
68          * RFC 868 time is the number of seconds
69          *  since 00:00 (midnight) 1 January 1900 GMT
70          *  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
72          */
73         localt= ntohl(nett) - RFC_868_BIAS;
74
75         return(localt);
76 }
77
78 int rdate_main(int argc, char **argv)
79 {
80         time_t remote_time;
81         int opt;
82         int setdate = 1;
83         int printdate = 1;
84
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();
103
104         remote_time = askremotedate(argv[optind]);
105
106         if (setdate) {
107                 time_t current_time;
108
109                 time(&current_time);
110                 if (current_time == remote_time)
111                         bb_error_msg("Current time matches remote time.");
112                 else
113                         if (stime(&remote_time) < 0)
114                                 bb_perror_msg_and_die("Could not set time of day");
115         }
116
117         if (printdate)
118                 printf("%s", ctime(&remote_time));
119
120         return EXIT_SUCCESS;
121 }