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); 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, 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 = 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': 136 break; 137 case 'R': /* RFC 2822 datetime format */ 138 if (Iflag) 139 multipleformats(); 140 Rflag = 1; 141 break; 142 case 'r': /* user specified seconds */ 143 rflag = 1; 144 tval = strtoq(optarg, &tmp, 0); 145 if (*tmp != 0) { 146 if (stat(optarg, &sb) == 0) 147 tval = sb.st_mtim.tv_sec; 148 else 149 usage(); 150 } 151 break; 152 case 'u': /* do everything in UTC */ 153 (void)setenv("TZ", "UTC0", 1); 154 break; 155 case 'v': 156 v = vary_append(v, optarg); 157 break; 158 default: 159 usage(); 160 } 161 argc -= optind; 162 argv += optind; 163 164 if (!rflag && time(&tval) == -1) 165 err(1, "time"); 166 167 format = "%+"; 168 169 if (Rflag) 170 format = rfc2822_format; 171 172 /* allow the operands in any order */ 173 if (*argv && **argv == '+') { 174 if (Iflag) 175 multipleformats(); 176 format = *argv + 1; 177 ++argv; 178 } 179 180 if (*argv) { 181 setthetime(fmt, *argv, jflag); 182 ++argv; 183 } else if (fmt != NULL) 184 usage(); 185 186 if (*argv && **argv == '+') { 187 if (Iflag) 188 multipleformats(); 189 format = *argv + 1; 190 } 191 192 lt = localtime(&tval); 193 if (lt == NULL) 194 errx(1, "invalid time"); 195 badv = vary_apply(v, lt); 196 if (badv) { 197 fprintf(stderr, "%s: Cannot apply date adjustment\n", 198 badv->arg); 199 vary_destroy(v); 200 usage(); 201 } 202 vary_destroy(v); 203 204 if (Iflag) 205 printisodate(lt); 206 207 if (format == rfc2822_format) 208 /* 209 * When using RFC 2822 datetime format, don't honor the 210 * locale. 211 */ 212 setlocale(LC_TIME, "C"); 213 214 (void)strftime(buf, sizeof(buf), format, lt); 215 printdate(buf); 216 } 217 218 static void 219 printdate(const char *buf) 220 { 221 (void)printf("%s\n", buf); 222 if (fflush(stdout)) 223 err(1, "stdout"); 224 exit(EXIT_SUCCESS); 225 } 226 227 static void 228 printisodate(struct tm *lt) 229 { 230 const struct iso8601_fmt *it; 231 char fmtbuf[32], buf[32], tzbuf[8]; 232 233 fmtbuf[0] = 0; 234 for (it = iso8601_fmts; it <= iso8601_selected; it++) 235 strlcat(fmtbuf, it->format_string, sizeof(fmtbuf)); 236 237 (void)strftime(buf, sizeof(buf), fmtbuf, lt); 238 239 if (iso8601_selected > iso8601_fmts) { 240 (void)strftime(tzbuf, sizeof(tzbuf), "%z", lt); 241 memmove(&tzbuf[4], &tzbuf[3], 3); 242 tzbuf[3] = ':'; 243 strlcat(buf, tzbuf, sizeof(buf)); 244 } 245 246 printdate(buf); 247 } 248 249 #define ATOI2(s) ((s) += 2, ((s)[-2] - '0') * 10 + ((s)[-1] - '0')) 250 251 static void 252 setthetime(const char *fmt, const char *p, int jflag) 253 { 254 struct utmpx utx; 255 struct tm *lt; 256 struct timeval tv; 257 const char *dot, *t; 258 int century; 259 260 lt = localtime(&tval); 261 if (lt == NULL) 262 errx(1, "invalid time"); 263 lt->tm_isdst = -1; /* divine correct DST */ 264 265 if (fmt != NULL) { 266 t = strptime(p, fmt, lt); 267 if (t == NULL) { 268 fprintf(stderr, "Failed conversion of ``%s''" 269 " using format ``%s''\n", p, fmt); 270 badformat(); 271 } else if (*t != '\0') 272 fprintf(stderr, "Warning: Ignoring %ld extraneous" 273 " characters in date string (%s)\n", 274 (long) strlen(t), t); 275 } else { 276 for (t = p, dot = NULL; *t; ++t) { 277 if (isdigit(*t)) 278 continue; 279 if (*t == '.' && dot == NULL) { 280 dot = t; 281 continue; 282 } 283 badformat(); 284 } 285 286 if (dot != NULL) { /* .ss */ 287 dot++; /* *dot++ = '\0'; */ 288 if (strlen(dot) != 2) 289 badformat(); 290 lt->tm_sec = ATOI2(dot); 291 if (lt->tm_sec > 61) 292 badformat(); 293 } else 294 lt->tm_sec = 0; 295 296 century = 0; 297 /* if p has a ".ss" field then let's pretend it's not there */ 298 switch (strlen(p) - ((dot != NULL) ? 3 : 0)) { 299 case 12: /* cc */ 300 lt->tm_year = ATOI2(p) * 100 - TM_YEAR_BASE; 301 century = 1; 302 /* FALLTHROUGH */ 303 case 10: /* yy */ 304 if (century) 305 lt->tm_year += ATOI2(p); 306 else { 307 lt->tm_year = ATOI2(p); 308 if (lt->tm_year < 69) /* hack for 2000 ;-} */ 309 lt->tm_year += 2000 - TM_YEAR_BASE; 310 else 311 lt->tm_year += 1900 - TM_YEAR_BASE; 312 } 313 /* FALLTHROUGH */ 314 case 8: /* mm */ 315 lt->tm_mon = ATOI2(p); 316 if (lt->tm_mon > 12) 317 badformat(); 318 --lt->tm_mon; /* time struct is 0 - 11 */ 319 /* FALLTHROUGH */ 320 case 6: /* dd */ 321 lt->tm_mday = ATOI2(p); 322 if (lt->tm_mday > 31) 323 badformat(); 324 /* FALLTHROUGH */ 325 case 4: /* HH */ 326 lt->tm_hour = ATOI2(p); 327 if (lt->tm_hour > 23) 328 badformat(); 329 /* FALLTHROUGH */ 330 case 2: /* MM */ 331 lt->tm_min = ATOI2(p); 332 if (lt->tm_min > 59) 333 badformat(); 334 break; 335 default: 336 badformat(); 337 } 338 } 339 340 /* convert broken-down time to GMT clock time */ 341 if ((tval = mktime(lt)) == -1) 342 errx(1, "nonexistent time"); 343 344 if (!jflag) { 345 utx.ut_type = OLD_TIME; 346 memset(utx.ut_id, 0, sizeof(utx.ut_id)); 347 (void)gettimeofday(&utx.ut_tv, NULL); 348 pututxline(&utx); 349 tv.tv_sec = tval; 350 tv.tv_usec = 0; 351 if (settimeofday(&tv, NULL) != 0) 352 err(1, "settimeofday (timeval)"); 353 utx.ut_type = NEW_TIME; 354 (void)gettimeofday(&utx.ut_tv, NULL); 355 pututxline(&utx); 356 357 if ((p = getlogin()) == NULL) 358 p = "???"; 359 syslog(LOG_AUTH | LOG_NOTICE, "date set by %s", p); 360 } 361 } 362 363 static void 364 badformat(void) 365 { 366 warnx("illegal time format"); 367 usage(); 368 } 369 370 static void 371 iso8601_usage(const char *badarg) 372 { 373 errx(1, "invalid argument '%s' for -I", badarg); 374 } 375 376 static void 377 multipleformats(void) 378 { 379 errx(1, "multiple output formats specified"); 380 } 381 382 static void 383 usage(void) 384 { 385 (void)fprintf(stderr, "%s\n%s\n%s\n", 386 "usage: date [-jnRu] [-r seconds|file] [-v[+|-]val[ymwdHMS]]", 387 " " 388 "[-I[date | hours | minutes | seconds]]", 389 " " 390 "[-f fmt date | [[[[[cc]yy]mm]dd]HH]MM[.ss]] [+format]" 391 ); 392 exit(1); 393 } 394