date.c revision 1.39
1/* $NetBSD: date.c,v 1.39 2005/05/15 18:39:39 dsl Exp $ */ 2 3/* 4 * Copyright (c) 1985, 1987, 1988, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32#include <sys/cdefs.h> 33#ifndef lint 34__COPYRIGHT( 35"@(#) Copyright (c) 1985, 1987, 1988, 1993\n\ 36 The Regents of the University of California. All rights reserved.\n"); 37#endif /* not lint */ 38 39#ifndef lint 40#if 0 41static char sccsid[] = "@(#)date.c 8.2 (Berkeley) 4/28/95"; 42#else 43__RCSID("$NetBSD: date.c,v 1.39 2005/05/15 18:39:39 dsl Exp $"); 44#endif 45#endif /* not lint */ 46 47#include <sys/param.h> 48#include <sys/time.h> 49 50#include <ctype.h> 51#include <err.h> 52#include <fcntl.h> 53#include <locale.h> 54#include <stdio.h> 55#include <stdlib.h> 56#include <string.h> 57#include <syslog.h> 58#include <time.h> 59#include <tzfile.h> 60#include <unistd.h> 61#include <util.h> 62 63#include "extern.h" 64 65static time_t tval; 66static int aflag, rflag, nflag; 67int retval; 68 69int main(int, char *[]); 70static void badformat(void); 71static void badtime(void); 72static void setthetime(const char *); 73static void usage(void); 74 75int 76main(int argc, char *argv[]) 77{ 78 char buf[1024], *format; 79 int ch; 80 81 setprogname(argv[0]); 82 (void)setlocale(LC_ALL, ""); 83 84 while ((ch = getopt(argc, argv, "anr:u")) != -1) { 85 switch((char)ch) { 86 case 'a': /* adjust time slowly */ 87 aflag = 1; 88 nflag = 1; 89 break; 90 case 'n': /* don't set network */ 91 nflag = 1; 92 break; 93 case 'r': /* user specified seconds */ 94 rflag = 1; 95 tval = strtol(optarg, NULL, 0); 96 break; 97 case 'u': /* do everything in UTC */ 98 (void)putenv("TZ=UTC0"); 99 break; 100 default: 101 usage(); 102 } 103 } 104 argc -= optind; 105 argv += optind; 106 107 if (!rflag && time(&tval) == -1) 108 err(1, "time"); 109 110 format = "%a %b %e %H:%M:%S %Z %Y"; 111 112 /* allow the operands in any order */ 113 if (*argv && **argv == '+') { 114 format = *argv + 1; 115 ++argv; 116 } 117 118 if (*argv) { 119 setthetime(*argv); 120 ++argv; 121 } 122 123 if (*argv && **argv == '+') 124 format = *argv + 1; 125 126 (void)strftime(buf, sizeof(buf), format, localtime(&tval)); 127 (void)printf("%s\n", buf); 128 exit(retval); 129 /* NOTREACHED */ 130} 131 132static void 133badformat(void) 134{ 135 warnx("illegal time format"); 136 usage(); 137} 138 139static void 140badtime(void) 141{ 142 errx(EXIT_FAILURE, "illegal time"); 143 /* NOTREACHED */ 144} 145 146#define ATOI2(s) ((s) += 2, ((s)[-2] - '0') * 10 + ((s)[-1] - '0')) 147 148static void 149setthetime(const char *p) 150{ 151 struct timeval tv; 152 time_t new_time; 153 struct tm *lt; 154 const char *dot, *t; 155 int len, yearset; 156 157 for (t = p, dot = NULL; *t; ++t) { 158 if (isdigit((unsigned char)*t)) 159 continue; 160 if (*t == '.' && dot == NULL) { 161 dot = t; 162 continue; 163 } 164 badformat(); 165 } 166 167 lt = localtime(&tval); 168 169 lt->tm_isdst = -1; /* Divine correct DST */ 170 171 if (dot != NULL) { /* .ss */ 172 len = strlen(dot); 173 if (len != 3) 174 badformat(); 175 ++dot; 176 lt->tm_sec = ATOI2(dot); 177 } else { 178 len = 0; 179 lt->tm_sec = 0; 180 } 181 182 yearset = 0; 183 switch (strlen(p) - len) { 184 case 12: /* cc */ 185 lt->tm_year = ATOI2(p) * 100 - TM_YEAR_BASE; 186 yearset = 1; 187 /* FALLTHROUGH */ 188 case 10: /* yy */ 189 if (yearset) { 190 lt->tm_year += ATOI2(p); 191 } else { 192 yearset = ATOI2(p); 193 if (yearset < 69) 194 lt->tm_year = yearset + 2000 - TM_YEAR_BASE; 195 else 196 lt->tm_year = yearset + 1900 - TM_YEAR_BASE; 197 } 198 /* FALLTHROUGH */ 199 case 8: /* mm */ 200 lt->tm_mon = ATOI2(p); 201 --lt->tm_mon; /* time struct is 0 - 11 */ 202 /* FALLTHROUGH */ 203 case 6: /* dd */ 204 lt->tm_mday = ATOI2(p); 205 /* FALLTHROUGH */ 206 case 4: /* hh */ 207 lt->tm_hour = ATOI2(p); 208 /* FALLTHROUGH */ 209 case 2: /* mm */ 210 lt->tm_min = ATOI2(p); 211 break; 212 case 0: /* was just .sss */ 213 if (len != 0) 214 break; 215 /* FALLTHROUGH */ 216 default: 217 badformat(); 218 } 219 220 /* convert broken-down time to UTC clock time */ 221 if ((new_time = mktime(lt)) == -1) 222 badtime(); 223 224 /* set the time */ 225 if (nflag || netsettime(new_time)) { 226 logwtmp("|", "date", ""); 227 if (aflag) { 228 tv.tv_sec = new_time - tval; 229 tv.tv_usec = 0; 230 if (adjtime(&tv, NULL)) { 231 perror("date: adjtime"); 232 exit(1); 233 } 234 } else { 235 tval = new_time; 236 tv.tv_sec = tval; 237 tv.tv_usec = 0; 238 if (settimeofday(&tv, NULL)) { 239 perror("date: settimeofday"); 240 exit(1); 241 } 242 } 243 logwtmp("{", "date", ""); 244 } 245 246 if ((p = getlogin()) == NULL) 247 p = "???"; 248 syslog(LOG_AUTH | LOG_NOTICE, "date set by %s", p); 249} 250 251static void 252usage(void) 253{ 254 (void)fprintf(stderr, 255 "usage: %s [-u] [-r seconds] [+format]\n", getprogname()); 256 (void)fprintf(stderr, " %s [-anu] [[[[[cc]yy]mm]dd]hh]mm[.ss]\n", getprogname()); 257 exit(1); 258 /* NOTREACHED */ 259} 260