1 /* $OpenBSD: rdate.c,v 1.19 2002/09/08 12:33:42 jakob 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.19 2002/09/08 12:33:42 jakob Exp $"; 46 #endif 47 #endif /* lint */ 48 49 #include <sys/param.h> 50 #include <sys/time.h> 51 52 #include <stdio.h> 53 #include <ctype.h> 54 #include <err.h> 55 #include <string.h> 56 #include <unistd.h> 57 #include <time.h> 58 59 /* there are systems without libutil; for portability */ 60 #ifndef NO_UTIL 61 #include <util.h> 62 #else 63 #define logwtmp(a,b,c) 64 #endif 65 66 void rfc868time_client (const char *, struct timeval *, struct timeval *, int); 67 void ntp_client (const char *, struct timeval *, struct timeval *, int); 68 69 extern char *__progname; 70 71 void 72 usage() 73 { 74 (void) fprintf(stderr, "Usage: %s [-ncpsa] host\n", __progname); 75 (void) fprintf(stderr, " -n: use SNTP instead of RFC868 time protocol\n"); 76 (void) fprintf(stderr, " -c: correct leap second count\n"); 77 (void) fprintf(stderr, " -p: just print, don't set\n"); 78 (void) fprintf(stderr, " -s: just set, don't print\n"); 79 (void) fprintf(stderr, " -a: use adjtime instead of instant change\n"); 80 (void) fprintf(stderr, " -v: verbose output\n"); 81 } 82 83 int 84 main(int argc, char **argv) 85 { 86 int pr = 0, silent = 0, ntp = 0, verbose = 0; 87 int slidetime = 0, corrleaps = 0; 88 char *hname; 89 extern int optind; 90 int c; 91 92 struct timeval new, adjust; 93 94 while ((c = getopt(argc, argv, "psancv")) != -1) 95 switch (c) { 96 case 'p': 97 pr++; 98 break; 99 100 case 's': 101 silent++; 102 break; 103 104 case 'a': 105 slidetime++; 106 break; 107 108 case 'n': 109 ntp++; 110 break; 111 112 case 'c': 113 corrleaps = 1; 114 break; 115 116 case 'v': 117 verbose++; 118 break; 119 120 default: 121 usage(); 122 return 1; 123 } 124 125 if (argc - 1 != optind) { 126 usage(); 127 return 1; 128 } 129 hname = argv[optind]; 130 131 if (ntp) 132 ntp_client(hname, &new, &adjust, corrleaps); 133 else 134 rfc868time_client(hname, &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 %ld seconds\n", 168 __progname, adjust.tv_sec); 169 } 170 } 171 172 return 0; 173 } 174