1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 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 #ifndef lint 33 static char const copyright[] = 34 "@(#) Copyright (c) 1985, 1987, 1988, 1993\n\ 35 The Regents of the University of California. All rights reserved.\n"; 36 #endif /* not lint */ 37 38 #if 0 39 #ifndef lint 40 static char sccsid[] = "@(#)date.c 8.2 (Berkeley) 4/28/95"; 41 #endif /* not lint */ 42 #endif 43 44 #include <sys/cdefs.h> 45 __FBSDID("$FreeBSD$"); 46 47 #include <sys/param.h> 48 #include <sys/time.h> 49 #include <sys/stat.h> 50 51 #include <ctype.h> 52 #include <err.h> 53 #include <locale.h> 54 #include <stdbool.h> 55 #include <stdio.h> 56 #include <stdlib.h> 57 #include <string.h> 58 #include <syslog.h> 59 #include <unistd.h> 60 #include <utmpx.h> 61 62 #include "vary.h" 63 64 #ifndef TM_YEAR_BASE 65 #define TM_YEAR_BASE 1900 66 #endif 67 68 static time_t tval; 69 70 static void badformat(void); 71 static void iso8601_usage(const char *); 72 static void multipleformats(void); 73 static void printdate(const char *); 74 static void printisodate(struct tm *); 75 static void setthetime(const char *, const char *, int, int); 76 static void usage(void); 77 78 static const struct iso8601_fmt { 79 const char *refname; 80 const char *format_string; 81 } iso8601_fmts[] = { 82 { "date", "%Y-%m-%d" }, 83 { "hours", "T%H" }, 84 { "minutes", ":%M" }, 85 { "seconds", ":%S" }, 86 }; 87 static const struct iso8601_fmt *iso8601_selected; 88 89 static const char *rfc2822_format = "%a, %d %b %Y %T %z"; 90 91 int 92 main(int argc, char *argv[]) 93 { 94 int ch, rflag; 95 bool Iflag, jflag, nflag, Rflag; 96 const char *format; 97 char buf[1024]; 98 char *fmt; 99 char *tmp; 100 struct vary *v; 101 const struct vary *badv; 102 struct tm *lt; 103 struct stat sb; 104 size_t i; 105 106 v = NULL; 107 fmt = NULL; 108 (void) setlocale(LC_TIME, ""); 109 rflag = 0; 110 Iflag = jflag = nflag = Rflag = 0; 111 while ((ch = getopt(argc, argv, "f:I::jnRr:uv:")) != -1) 112 switch((char)ch) { 113 case 'f': 114 fmt = optarg; 115 break; 116 case 'I': 117 if (Rflag) 118 multipleformats(); 119 Iflag = 1; 120 if (optarg == NULL) { 121 iso8601_selected = iso8601_fmts; 122 break; 123 } 124 for (i = 0; i < nitems(iso8601_fmts); i++) 125 if (strcmp(optarg, iso8601_fmts[i].refname) == 0) 126 break; 127 if (i == nitems(iso8601_fmts)) 128 iso8601_usage(optarg); 129 130 iso8601_selected = &iso8601_fmts[i]; 131 break; 132 case 'j': 133 jflag = 1; /* don't set time */ 134 break; 135 case 'n': /* don't set network */ 136 nflag = 1; 137 break; 138 case 'R': /* RFC 2822 datetime format */ 139 if (Iflag) 140 multipleformats(); 141 Rflag = 1; 142 break; 143 case 'r': /* user specified seconds */ 144 rflag = 1; 145 tval = strtoq(optarg, &tmp, 0); 146 if (*tmp != 0) { 147 if (stat(optarg, &sb) == 0) 148 tval = sb.st_mtim.tv_sec; 149 else 150 usage(); 151 } 152 break; 153 case 'u': /* do everything in UTC */ 154 (void)setenv("TZ", "UTC0", 1); 155 break; 156 case 'v': 157 v = vary_append(v, optarg); 158 break; 159 default: 160 usage(); 161 } 162 argc -= optind; 163 argv += optind; 164 165 if (!rflag && time(&tval) == -1) 166 err(1, "time"); 167 168 format = "%+"; 169 170 if (Rflag) 171 format = rfc2822_format; 172 173 /* allow the operands in any order */ 174 if (*argv && **argv == '+') { 175 if (Iflag) 176 multipleformats(); 177 format = *argv + 1; 178 ++argv; 179 } 180 181 if (*argv) { 182 setthetime(fmt, *argv, jflag, nflag); 183 ++argv; 184 } else if (fmt != NULL) 185 usage(); 186 187 if (*argv && **argv == '+') { 188 if (Iflag) 189 multipleformats(); 190 format = *argv + 1; 191 } 192 193 lt = localtime(&tval); 194 if (lt == NULL) 195 errx(1, "invalid time"); 196 badv = vary_apply(v, lt); 197 if (badv) { 198 fprintf(stderr, "%s: Cannot apply date adjustment\n", 199 badv->arg); 200 vary_destroy(v); 201 usage(); 202 } 203 vary_destroy(v); 204 205 if (Iflag) 206 printisodate(lt); 207 208 if (format == rfc2822_format) 209 /* 210 * When using RFC 2822 datetime format, don't honor the 211 * locale. 212 */ 213 setlocale(LC_TIME, "C"); 214 215 (void)strftime(buf, sizeof(buf), format, lt); 216 printdate(buf); 217 } 218 219 static void 220 printdate(const char *buf) 221 { 222 (void)printf("%s\n", buf); 223 if (fflush(stdout)) 224 err(1, "stdout"); 225 exit(EXIT_SUCCESS); 226 } 227 228 static void 229 printisodate(struct tm *lt) 230 { 231 const struct iso8601_fmt *it; 232 char fmtbuf[32], buf[32], tzbuf[8]; 233 234 fmtbuf[0] = 0; 235 for (it = iso8601_fmts; it <= iso8601_selected; it++) 236 strlcat(fmtbuf, it->format_string, sizeof(fmtbuf)); 237 238 (void)strftime(buf, sizeof(buf), fmtbuf, lt); 239 240 if (iso8601_selected > iso8601_fmts) { 241 (void)strftime(tzbuf, sizeof(tzbuf), "%z", lt); 242 memmove(&tzbuf[4], &tzbuf[3], 3); 243 tzbuf[3] = ':'; 244 strlcat(buf, tzbuf, sizeof(buf)); 245 } 246 247 printdate(buf); 248 } 249 250 #define ATOI2(s) ((s) += 2, ((s)[-2] - '0') * 10 + ((s)[-1] - '0')) 251 252 static void 253 setthetime(const char *fmt, const char *p, int jflag, int nflag) 254 { 255 struct utmpx utx; 256 struct tm *lt; 257 struct timeval tv; 258 const char *dot, *t; 259 int century; 260 261 lt = localtime(&tval); 262 if (lt == NULL) 263 errx(1, "invalid time"); 264 lt->tm_isdst = -1; /* divine correct DST */ 265 266 if (fmt != NULL) { 267 t = strptime(p, fmt, lt); 268 if (t == NULL) { 269 fprintf(stderr, "Failed conversion of ``%s''" 270 " using format ``%s''\n", p, fmt); 271 badformat(); 272 } else if (*t != '\0') 273 fprintf(stderr, "Warning: Ignoring %ld extraneous" 274 " characters in date string (%s)\n", 275 (long) strlen(t), t); 276 } else { 277 for (t = p, dot = NULL; *t; ++t) { 278 if (isdigit(*t)) 279 continue; 280 if (*t == '.' && dot == NULL) { 281 dot = t; 282 continue; 283 } 284 badformat(); 285 } 286 287 if (dot != NULL) { /* .ss */ 288 dot++; /* *dot++ = '\0'; */ 289 if (strlen(dot) != 2) 290 badformat(); 291 lt->tm_sec = ATOI2(dot); 292 if (lt->tm_sec > 61) 293 badformat(); 294 } else 295 lt->tm_sec = 0; 296 297 century = 0; 298 /* if p has a ".ss" field then let's pretend it's not there */ 299 switch (strlen(p) - ((dot != NULL) ? 3 : 0)) { 300 case 12: /* cc */ 301 lt->tm_year = ATOI2(p) * 100 - TM_YEAR_BASE; 302 century = 1; 303 /* FALLTHROUGH */ 304 case 10: /* yy */ 305 if (century) 306 lt->tm_year += ATOI2(p); 307 else { 308 lt->tm_year = ATOI2(p); 309 if (lt->tm_year < 69) /* hack for 2000 ;-} */ 310 lt->tm_year += 2000 - TM_YEAR_BASE; 311 else 312 lt->tm_year += 1900 - TM_YEAR_BASE; 313 } 314 /* FALLTHROUGH */ 315 case 8: /* mm */ 316 lt->tm_mon = ATOI2(p); 317 if (lt->tm_mon > 12) 318 badformat(); 319 --lt->tm_mon; /* time struct is 0 - 11 */ 320 /* FALLTHROUGH */ 321 case 6: /* dd */ 322 lt->tm_mday = ATOI2(p); 323 if (lt->tm_mday > 31) 324 badformat(); 325 /* FALLTHROUGH */ 326 case 4: /* HH */ 327 lt->tm_hour = ATOI2(p); 328 if (lt->tm_hour > 23) 329 badformat(); 330 /* FALLTHROUGH */ 331 case 2: /* MM */ 332 lt->tm_min = ATOI2(p); 333 if (lt->tm_min > 59) 334 badformat(); 335 break; 336 default: 337 badformat(); 338 } 339 } 340 341 /* convert broken-down time to GMT clock time */ 342 if ((tval = mktime(lt)) == -1) 343 errx(1, "nonexistent time"); 344 345 if (!jflag) { 346 /* set the time */ 347 if (nflag) { 348 utx.ut_type = OLD_TIME; 349 memset(utx.ut_id, 0, sizeof(utx.ut_id)); 350 (void)gettimeofday(&utx.ut_tv, NULL); 351 pututxline(&utx); 352 tv.tv_sec = tval; 353 tv.tv_usec = 0; 354 if (settimeofday(&tv, NULL) != 0) 355 err(1, "settimeofday (timeval)"); 356 utx.ut_type = NEW_TIME; 357 (void)gettimeofday(&utx.ut_tv, NULL); 358 pututxline(&utx); 359 } 360 361 if ((p = getlogin()) == NULL) 362 p = "???"; 363 syslog(LOG_AUTH | LOG_NOTICE, "date set by %s", p); 364 } 365 } 366 367 static void 368 badformat(void) 369 { 370 warnx("illegal time format"); 371 usage(); 372 } 373 374 static void 375 iso8601_usage(const char *badarg) 376 { 377 errx(1, "invalid argument '%s' for -I", badarg); 378 } 379 380 static void 381 multipleformats(void) 382 { 383 errx(1, "multiple output formats specified"); 384 } 385 386 static void 387 usage(void) 388 { 389 (void)fprintf(stderr, "%s\n%s\n%s\n", 390 "usage: date [-jnRu] [-r seconds|file] [-v[+|-]val[ymwdHMS]]", 391 " " 392 "[-I[date | hours | minutes | seconds]]", 393 " " 394 "[-f fmt date | [[[[[cc]yy]mm]dd]HH]MM[.ss]] [+format]" 395 ); 396 exit(1); 397 } 398