Unconstify members of the lconv structure to make it conform to the C89 and
[dragonfly/port-amd64.git] / usr.sbin / rdate / rdate.c
blobd83822e3083125d3274949e507e586bf7eec37f9
1 /* $OpenBSD: src/usr.sbin/rdate/rdate.c,v 1.22 2004/02/18 20:10:53 jmc Exp $ */
2 /* $NetBSD: rdate.c,v 1.4 1996/03/16 12:37:45 pk Exp $ */
3 /* $DragonFly: src/usr.sbin/rdate/rdate.c,v 1.2 2005/03/28 02:39:57 dillon Exp $ */
5 /*
6 * Copyright (c) 1994 Christos Zoulas
7 * All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by Christos Zoulas.
20 * 4. The name of the author may not be used to endorse or promote products
21 * derived from this software without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 * rdate.c: Set the date from the specified host
38 * Uses the rfc868 time protocol at socket 37.
39 * Time is returned as the number of seconds since
40 * midnight January 1st 1900.
43 #include <sys/param.h>
44 #include <sys/socket.h>
45 #include <sys/time.h>
47 #include <ctype.h>
48 #include <err.h>
49 #include <libutil.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <time.h>
54 #include <unistd.h>
56 #include "rdate.h"
58 static void usage(void);
60 int
61 main(int argc, char **argv)
63 struct timeval new, adjust;
64 int pr = 0, silent = 0, ntp = 0, verbose = 0;
65 int slidetime = 0, corrleaps = 0;
66 const char *hname;
67 int c;
68 int family = PF_UNSPEC;
70 while ((c = getopt(argc, argv, "46psancv")) != -1) {
71 switch (c) {
72 case '4':
73 family = PF_INET;
74 break;
76 case '6':
77 family = PF_INET6;
78 break;
80 case 'p':
81 pr++;
82 break;
84 case 's':
85 silent++;
86 break;
88 case 'a':
89 slidetime++;
90 break;
92 case 'n':
93 ntp++;
94 break;
96 case 'c':
97 corrleaps = 1;
98 break;
100 case 'v':
101 verbose++;
102 break;
104 default:
105 usage();
106 return 1;
109 argc -= optind;
110 argv += optind;
112 if (argc != 1)
113 usage();
115 hname = *argv;
117 if (ntp)
118 ntp_client(hname, family, &new, &adjust, corrleaps);
119 else
120 rfc868time_client(hname, family, &new, &adjust, corrleaps);
122 if (!pr) {
123 if (!slidetime) {
124 logwtmp("|", "date", "");
125 if (settimeofday(&new, NULL) == -1)
126 err(1, "Could not set time of day");
127 logwtmp("{", "date", "");
128 } else {
129 if (adjtime(&adjust, NULL) == -1)
130 err(1, "Could not adjust time of day");
134 if (!silent) {
135 struct tm *ltm;
136 char buf[80];
137 time_t tim = new.tv_sec;
138 double adjsec;
140 ltm = localtime(&tim);
141 strftime(buf, sizeof buf, "%a %b %e %H:%M:%S %Z %Y\n", ltm);
142 fputs(buf, stdout);
144 adjsec = adjust.tv_sec + adjust.tv_usec / 1.0e6;
146 if (ntp) {
147 fprintf(stdout,
148 "%s: adjust local clock by %.6f seconds\n",
149 getprogname(), adjsec);
150 } else {
151 fprintf(stdout,
152 "%s: adjust local clock by %ld seconds\n",
153 getprogname(), adjust.tv_sec);
157 return(EXIT_SUCCESS);
160 static void
161 usage(void)
163 fprintf(stderr, "Usage: %s [-46acnpsv] host\n", getprogname());
164 fprintf(stderr, " -4: use IPv4 only\n");
165 fprintf(stderr, " -6: use IPv6 only\n");
166 fprintf(stderr, " -a: use adjtime instead of instant change\n");
167 fprintf(stderr, " -c: correct leap second count\n");
168 fprintf(stderr, " -n: use SNTP instead of RFC868 time protocol\n");
169 fprintf(stderr, " -p: just print, don't set\n");
170 fprintf(stderr, " -s: just set, don't print\n");
171 fprintf(stderr, " -v: verbose output\n");
172 exit(EXIT_FAILURE);