date.c revision 1.57
1/* $NetBSD: date.c,v 1.57 2010/12/12 17:30:23 christos 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\ 36 The Regents of the University of California. All rights reserved."); 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.57 2010/12/12 17:30:23 christos 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 <errno.h> 54#include <locale.h> 55#include <stdio.h> 56#include <stdlib.h> 57#include <string.h> 58#include <syslog.h> 59#include <time.h> 60#include <tzfile.h> 61#include <unistd.h> 62#include <util.h> 63 64#include "extern.h" 65 66static time_t tval; 67static int aflag, jflag, rflag, nflag; 68 69static void badformat(void); 70static void badtime(void); 71static void badvalue(const char *); 72static void setthetime(const char *); 73static void usage(void) __attribute__((__noreturn__)); 74 75int 76main(int argc, char *argv[]) 77{ 78 char *buf; 79 size_t bufsiz; 80 const char *format; 81 int ch; 82 long long val; 83 struct tm *tm; 84 85 setprogname(argv[0]); 86 (void)setlocale(LC_ALL, ""); 87 88 while ((ch = getopt(argc, argv, "ad:jnr:u")) != -1) { 89 switch (ch) { 90 case 'a': /* adjust time slowly */ 91 aflag = 1; 92 nflag = 1; 93 break; 94 case 'd': 95 rflag = 1; 96 tval = parsedate(optarg, NULL, NULL); 97 if (tval == -1) 98badarg: errx(EXIT_FAILURE, 99 "Cannot parse `%s'", optarg); 100 break; 101 case 'j': /* don't set time */ 102 jflag = 1; 103 break; 104 case 'n': /* don't set network */ 105 nflag = 1; 106 break; 107 case 'r': /* user specified seconds */ 108 errno = 0; 109 val = strtoll(optarg, &buf, 0); 110 if (optarg[0] == '\0' || *buf != '\0') 111 goto badarg; 112 if (errno == ERANGE && (val == LLONG_MAX || 113 val == LLONG_MIN)) 114 err(EXIT_FAILURE, "Bad number `%s'", optarg); 115 rflag = 1; 116 tval = (time_t)val; 117 break; 118 case 'u': /* do everything in UTC */ 119 (void)setenv("TZ", "UTC0", 1); 120 break; 121 default: 122 usage(); 123 } 124 } 125 argc -= optind; 126 argv += optind; 127 128 if (!rflag && time(&tval) == -1) 129 err(EXIT_FAILURE, "time"); 130 131 132 /* allow the operands in any order */ 133 if (*argv && **argv == '+') { 134 format = *argv; 135 ++argv; 136 } else 137 format = "+%a %b %e %H:%M:%S %Z %Y"; 138 139 if (*argv) { 140 setthetime(*argv); 141 ++argv; 142 } 143 144 if (*argv && **argv == '+') 145 format = *argv; 146 147 if ((buf = malloc(bufsiz = 1024)) == NULL) 148 goto bad; 149 150 if ((tm = localtime(&tval)) == NULL) 151 err(EXIT_FAILURE, "localtime %lld failed", (long long)tval); 152 153 while (strftime(buf, bufsiz, format, tm) == 0) 154 if ((buf = realloc(buf, bufsiz <<= 1)) == NULL) 155 goto bad; 156 157 (void)printf("%s\n", buf + 1); 158 free(buf); 159 return 0; 160bad: 161 err(EXIT_FAILURE, "Cannot allocate format buffer"); 162} 163 164static void 165badformat(void) 166{ 167 warnx("illegal time format"); 168 usage(); 169} 170 171static void 172badtime(void) 173{ 174 errx(EXIT_FAILURE, "illegal time"); 175 /* NOTREACHED */ 176} 177 178static void 179badvalue(const char *param) 180{ 181 warnx("invalid %s supplied", param); 182 usage(); 183} 184 185#define ATOI2(s) ((s) += 2, ((s)[-2] - '0') * 10 + ((s)[-1] - '0')) 186 187static void 188setthetime(const char *p) 189{ 190 struct timeval tv; 191 time_t new_time; 192 struct tm *lt; 193 const char *dot, *t; 194 size_t len; 195 int yearset; 196 197 for (t = p, dot = NULL; *t; ++t) { 198 if (isdigit((unsigned char)*t)) 199 continue; 200 if (*t == '.' && dot == NULL) { 201 dot = t; 202 continue; 203 } 204 badformat(); 205 } 206 207 if ((lt = localtime(&tval)) == NULL) 208 err(EXIT_FAILURE, "localtime %lld failed", (long long)tval); 209 210 lt->tm_isdst = -1; /* Divine correct DST */ 211 212 if (dot != NULL) { /* .ss */ 213 len = strlen(dot); 214 if (len != 3) 215 badformat(); 216 ++dot; 217 lt->tm_sec = ATOI2(dot); 218 if (lt->tm_sec > 61) 219 badvalue("seconds"); 220 } else { 221 len = 0; 222 lt->tm_sec = 0; 223 } 224 225 yearset = 0; 226 switch (strlen(p) - len) { 227 case 12: /* cc */ 228 lt->tm_year = ATOI2(p) * 100 - TM_YEAR_BASE; 229 if (lt->tm_year < 0) 230 badtime(); 231 yearset = 1; 232 /* FALLTHROUGH */ 233 case 10: /* yy */ 234 if (yearset) { 235 lt->tm_year += ATOI2(p); 236 } else { 237 yearset = ATOI2(p); 238 if (yearset < 69) 239 lt->tm_year = yearset + 2000 - TM_YEAR_BASE; 240 else 241 lt->tm_year = yearset + 1900 - TM_YEAR_BASE; 242 } 243 /* FALLTHROUGH */ 244 case 8: /* mm */ 245 lt->tm_mon = ATOI2(p); 246 if (lt->tm_mon > 12 || lt->tm_mon == 0) 247 badvalue("month"); 248 --lt->tm_mon; /* time struct is 0 - 11 */ 249 /* FALLTHROUGH */ 250 case 6: /* dd */ 251 lt->tm_mday = ATOI2(p); 252 switch (lt->tm_mon) { 253 case 0: 254 case 2: 255 case 4: 256 case 6: 257 case 7: 258 case 9: 259 case 11: 260 if (lt->tm_mday > 31 || lt->tm_mday == 0) 261 badvalue("day of month"); 262 break; 263 case 3: 264 case 5: 265 case 8: 266 case 10: 267 if (lt->tm_mday > 30 || lt->tm_mday == 0) 268 badvalue("day of month"); 269 break; 270 case 1: 271 if (lt->tm_mday > 29 || lt->tm_mday == 0 || 272 (lt->tm_mday == 29 && 273 !isleap(lt->tm_year + TM_YEAR_BASE))) 274 badvalue("day of month"); 275 break; 276 default: 277 badvalue("month"); 278 break; 279 } 280 /* FALLTHROUGH */ 281 case 4: /* hh */ 282 lt->tm_hour = ATOI2(p); 283 if (lt->tm_hour > 23) 284 badvalue("hour"); 285 /* FALLTHROUGH */ 286 case 2: /* mm */ 287 lt->tm_min = ATOI2(p); 288 if (lt->tm_min > 59) 289 badvalue("minute"); 290 break; 291 case 0: /* was just .sss */ 292 if (len != 0) 293 break; 294 /* FALLTHROUGH */ 295 default: 296 badformat(); 297 } 298 299 /* convert broken-down time to UTC clock time */ 300 if ((new_time = mktime(lt)) == -1) 301 badtime(); 302 303 /* if jflag is set, don't actually change the time, just return */ 304 if (jflag) { 305 tval = new_time; 306 return; 307 } 308 309 /* set the time */ 310 if (nflag || netsettime(new_time)) { 311 logwtmp("|", "date", ""); 312 if (aflag) { 313 tv.tv_sec = new_time - tval; 314 tv.tv_usec = 0; 315 if (adjtime(&tv, NULL)) 316 err(EXIT_FAILURE, "adjtime"); 317 } else { 318 tval = new_time; 319 tv.tv_sec = tval; 320 tv.tv_usec = 0; 321 if (settimeofday(&tv, NULL)) 322 err(EXIT_FAILURE, "settimeofday"); 323 } 324 logwtmp("{", "date", ""); 325 } 326 327 if ((p = getlogin()) == NULL) 328 p = "???"; 329 syslog(LOG_AUTH | LOG_NOTICE, "date set by %s", p); 330} 331 332static void 333usage(void) 334{ 335 (void)fprintf(stderr, 336 "Usage: %s [-ajnu] [-d date] [-r seconds] [+format]", 337 getprogname()); 338 (void)fprintf(stderr, " [[[[[[CC]yy]mm]dd]HH]MM[.SS]]\n"); 339 exit(EXIT_FAILURE); 340 /* NOTREACHED */ 341} 342