source: trunk/minix/commands/simple/rdate.c@ 10

Last change on this file since 10 was 9, checked in by Mattia Monga, 13 years ago

Minix 3.1.2a

File size: 2.4 KB
Line 
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>
20
21void report(const char *label)
22{
23 fprintf(stderr, "rdate: %s: %s\n", label, strerror(errno));
24}
25
26void fatal(const char *label)
27{
28 report(label);
29 exit(1);
30}
31
32int main(int argc, char **argv)
33{
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;
44
45 if (argc <= 1) {
46 fprintf(stderr, "Usage: rdate host ...\n");
47 exit(1);
48 }
49
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);
54 }
55 time_port= servent->s_port;
56
57 if ((tcp_device= getenv("TCP_DEVICE")) == nil) tcp_device= TCP_DEVICE;
58
59 if ((fd= open(tcp_device, O_RDWR)) < 0) fatal(tcp_device);
60
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;
66 }
67
68 /* Configure a TCP channel and connect to the remote host. */
69
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);
74
75 tcpcl.nwtcl_flags= 0;
76 if (ioctl(fd, NWIOTCPCONN, &tcpcl) < 0) {
77 report(argv[i]);
78 continue;
79 }
80
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;
91 }
92 break;
93 }
94 if (i == argc) exit(1);
95
96 /* Internet time is in seconds since 1900, UNIX time is in seconds
97 * since 1970.
98 */
99 unix_time= ntohl(net_time) - 2208988800;
100
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 ");
106 }
107 printf("%s: %s", argv[i], ctime(&unix_time));
108 exit(0);
109}
Note: See TracBrowser for help on using the repository browser.