minor fixes for safecopy & safemap tests
[minix.git] / commands / rdate / rdate.c
blobedb4b39f0eef63957f12f268a0c3f69abc79bbca
1 /* rdate 1.0 - Set time&date from remote host Author: Kees J. Bot
2 * 12 Oct 1995
3 */
4 #define nil 0
5 #include <sys/types.h>
6 #include <stdio.h>
7 #include <time.h>
8 #include <string.h>
9 #include <fcntl.h>
10 #include <errno.h>
11 #include <stdlib.h>
12 #include <unistd.h>
13 #include <sys/ioctl.h>
14 #include <net/hton.h>
15 #include <net/netlib.h>
16 #include <net/gen/in.h>
17 #include <net/gen/netdb.h>
18 #include <net/gen/tcp.h>
19 #include <net/gen/tcp_io.h>
21 void report(const char *label)
23 fprintf(stderr, "rdate: %s: %s\n", label, strerror(errno));
26 void fatal(const char *label)
28 report(label);
29 exit(1);
32 int main(int argc, char **argv)
34 char *tcp_device;
35 int fd;
36 int i;
37 struct servent *servent;
38 struct hostent *hostent;
39 u16_t time_port;
40 nwio_tcpconf_t tcpconf;
41 nwio_tcpcl_t tcpcl;
42 u32_t net_time;
43 time_t unix_time;
45 if (argc <= 1) {
46 fprintf(stderr, "Usage: rdate host ...\n");
47 exit(1);
50 /* Look up the port number of the TCP service "time". */
51 if ((servent= getservbyname("time", "tcp")) == nil) {
52 fprintf(stderr, "rdate: \"time\": unknown service\n");
53 exit(1);
55 time_port= servent->s_port;
57 if ((tcp_device= getenv("TCP_DEVICE")) == nil) tcp_device= TCP_DEVICE;
59 if ((fd= open(tcp_device, O_RDWR)) < 0) fatal(tcp_device);
61 /* Try each host on the command line. */
62 for (i= 1; i < argc; i++) {
63 if ((hostent= gethostbyname(argv[i])) == nil) {
64 fprintf(stderr, "rdate: %s: unknown host\n", argv[i]);
65 continue;
68 /* Configure a TCP channel and connect to the remote host. */
70 tcpconf.nwtc_flags= NWTC_LP_SEL | NWTC_SET_RA | NWTC_SET_RP;
71 memcpy(&tcpconf.nwtc_remaddr, hostent->h_addr, 4);
72 tcpconf.nwtc_remport= time_port;
73 if (ioctl(fd, NWIOSTCPCONF, &tcpconf) == -1) fatal(tcp_device);
75 tcpcl.nwtcl_flags= 0;
76 if (ioctl(fd, NWIOTCPCONN, &tcpcl) < 0) {
77 report(argv[i]);
78 continue;
81 /* Read four bytes to obtain the time. */
82 switch (read(fd, &net_time, sizeof(net_time))) {
83 case -1:
84 report(argv[i]);
85 continue;
86 default:
87 fprintf(stderr, "rdate: %s: short read\n", argv[i]);
88 continue;
89 case sizeof(net_time):
90 break;
92 break;
94 if (i == argc) exit(1);
96 /* Internet time is in seconds since 1900, UNIX time is in seconds
97 * since 1970.
99 unix_time= ntohl(net_time) - 2208988800;
101 /* Try to set the time and tell us about it. */
102 if (stime(&unix_time) < 0) {
103 printf("time on ");
104 } else {
105 printf("time set to ");
107 printf("%s: %s", argv[i], ctime(&unix_time));
108 exit(0);