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