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