1 /* $OpenBSD: rdate.c,v 1.30 2013/11/12 22:27:13 deraadt 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 * Time is returned as the number of seconds since 38 * midnight January 1st 1900. 39 */ 40 41 #include <sys/param.h> 42 #include <sys/socket.h> 43 #include <sys/time.h> 44 45 #include <stdio.h> 46 #include <stdlib.h> 47 #include <ctype.h> 48 #include <err.h> 49 #include <string.h> 50 #include <unistd.h> 51 #include <time.h> 52 53 /* there are systems without libutil; for portability */ 54 #ifndef NO_UTIL 55 #include <util.h> 56 #else 57 #define logwtmp(a,b,c) 58 #endif 59 60 void rfc868time_client(const char *, int, struct timeval *, struct timeval *, int); 61 void ntp_client(const char *, int, struct timeval *, struct timeval *, int); 62 63 extern char *__progname; 64 __dead void usage(void); 65 66 __dead void 67 usage(void) 68 { 69 (void) fprintf(stderr, "usage: %s [-46acnopsv] host\n", __progname); 70 exit(1); 71 } 72 73 int 74 main(int argc, char **argv) 75 { 76 int pr = 0, silent = 0, ntp = 1, verbose = 0; 77 int slidetime = 0, corrleaps = 0; 78 char *hname; 79 extern int optind; 80 int c; 81 int family = PF_UNSPEC; 82 83 struct timeval new, adjust; 84 85 while ((c = getopt(argc, argv, "46psanocv")) != -1) { 86 switch (c) { 87 case '4': 88 family = PF_INET; 89 break; 90 91 case '6': 92 family = PF_INET6; 93 break; 94 95 case 'p': 96 pr++; 97 break; 98 99 case 's': 100 silent++; 101 break; 102 103 case 'a': 104 slidetime++; 105 break; 106 107 case 'n': 108 ntp++; 109 break; 110 111 case 'o': 112 ntp = 0; 113 break; 114 115 case 'c': 116 corrleaps = 1; 117 break; 118 119 case 'v': 120 verbose++; 121 break; 122 123 default: 124 usage(); 125 } 126 } 127 if (argc - 1 != optind) 128 usage(); 129 hname = argv[optind]; 130 131 if (ntp) 132 ntp_client(hname, family, &new, &adjust, corrleaps); 133 else 134 rfc868time_client(hname, family, &new, &adjust, corrleaps); 135 136 if (!pr) { 137 if (!slidetime) { 138 logwtmp("|", "date", ""); 139 if (settimeofday(&new, NULL) == -1) 140 err(1, "Could not set time of day"); 141 logwtmp("{", "date", ""); 142 } else { 143 if (adjtime(&adjust, NULL) == -1) 144 err(1, "Could not adjust time of day"); 145 } 146 } 147 148 if (!silent) { 149 struct tm *ltm; 150 char buf[80]; 151 time_t tim = new.tv_sec; 152 double adjsec; 153 154 ltm = localtime(&tim); 155 (void) strftime(buf, sizeof buf, "%a %b %e %H:%M:%S %Z %Y\n", ltm); 156 (void) fputs(buf, stdout); 157 158 adjsec = adjust.tv_sec + adjust.tv_usec / 1.0e6; 159 160 if (slidetime || verbose) { 161 if (ntp) 162 (void) fprintf(stdout, 163 "%s: adjust local clock by %.6f seconds\n", 164 __progname, adjsec); 165 else 166 (void) fprintf(stdout, 167 "%s: adjust local clock by %lld seconds\n", 168 __progname, (long long)adjust.tv_sec); 169 } 170 } 171 172 return 0; 173 } 174