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