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