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 $ */
6 * Copyright (c) 1994 Christos Zoulas
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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>
58 static void usage(void);
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;
68 int family
= PF_UNSPEC
;
70 while ((c
= getopt(argc
, argv
, "46psancv")) != -1) {
118 ntp_client(hname
, family
, &new, &adjust
, corrleaps
);
120 rfc868time_client(hname
, family
, &new, &adjust
, corrleaps
);
124 logwtmp("|", "date", "");
125 if (settimeofday(&new, NULL
) == -1)
126 err(1, "Could not set time of day");
127 logwtmp("{", "date", "");
129 if (adjtime(&adjust
, NULL
) == -1)
130 err(1, "Could not adjust time of day");
137 time_t tim
= new.tv_sec
;
140 ltm
= localtime(&tim
);
141 strftime(buf
, sizeof buf
, "%a %b %e %H:%M:%S %Z %Y\n", ltm
);
144 adjsec
= adjust
.tv_sec
+ adjust
.tv_usec
/ 1.0e6
;
148 "%s: adjust local clock by %.6f seconds\n",
149 getprogname(), adjsec
);
152 "%s: adjust local clock by %ld seconds\n",
153 getprogname(), adjust
.tv_sec
);
157 return(EXIT_SUCCESS
);
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");