1139969Simp/*- 21556Srgrimes * Copyright (c) 1985, 1987, 1988, 1993 31556Srgrimes * The Regents of the University of California. All rights reserved. 41556Srgrimes * 51556Srgrimes * Redistribution and use in source and binary forms, with or without 61556Srgrimes * modification, are permitted provided that the following conditions 71556Srgrimes * are met: 81556Srgrimes * 1. Redistributions of source code must retain the above copyright 91556Srgrimes * notice, this list of conditions and the following disclaimer. 101556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111556Srgrimes * notice, this list of conditions and the following disclaimer in the 121556Srgrimes * documentation and/or other materials provided with the distribution. 131556Srgrimes * 4. Neither the name of the University nor the names of its contributors 141556Srgrimes * may be used to endorse or promote products derived from this software 151556Srgrimes * without specific prior written permission. 161556Srgrimes * 171556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 181556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 191556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 201556Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 211556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 221556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 231556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 241556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 251556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 261556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 271556Srgrimes * SUCH DAMAGE. 281556Srgrimes */ 291556Srgrimes 301556Srgrimes#ifndef lint 3120413Sstevestatic char const copyright[] = 321556Srgrimes"@(#) Copyright (c) 1985, 1987, 1988, 1993\n\ 331556Srgrimes The Regents of the University of California. All rights reserved.\n"; 341556Srgrimes#endif /* not lint */ 351556Srgrimes 36110390Scharnier#if 0 371556Srgrimes#ifndef lint 3836006Scharnierstatic char sccsid[] = "@(#)date.c 8.2 (Berkeley) 4/28/95"; 39110390Scharnier#endif /* not lint */ 4035773Scharnier#endif 41110390Scharnier 4299109Sobrien#include <sys/cdefs.h> 4399109Sobrien__FBSDID("$FreeBSD$"); 441556Srgrimes 4536006Scharnier#include <sys/param.h> 461556Srgrimes#include <sys/time.h> 471556Srgrimes 481556Srgrimes#include <ctype.h> 491556Srgrimes#include <err.h> 5091079Smarkm#include <locale.h> 511556Srgrimes#include <stdio.h> 521556Srgrimes#include <stdlib.h> 5339925Salex#include <string.h> 541556Srgrimes#include <syslog.h> 551556Srgrimes#include <unistd.h> 56202193Sed#include <utmpx.h> 571556Srgrimes 581556Srgrimes#include "extern.h" 5927874Sbrian#include "vary.h" 601556Srgrimes 6155225Ssheldonh#ifndef TM_YEAR_BASE 6255225Ssheldonh#define TM_YEAR_BASE 1900 6355225Ssheldonh#endif 6455225Ssheldonh 65105396Smarkmstatic time_t tval; 6647129Sjmgint retval; 671556Srgrimes 6890108Simpstatic void setthetime(const char *, const char *, int, int); 6990108Simpstatic void badformat(void); 7090108Simpstatic void usage(void); 711556Srgrimes 72265265Sdumbbellstatic const char *rfc2822_format = "%a, %d %b %Y %T %z"; 73265265Sdumbbell 741556Srgrimesint 7590108Simpmain(int argc, char *argv[]) 761556Srgrimes{ 771556Srgrimes struct timezone tz; 7830073Sdanny int ch, rflag; 79265265Sdumbbell int jflag, nflag, Rflag; 8091079Smarkm const char *format; 8191079Smarkm char buf[1024]; 8228037Sbrian char *endptr, *fmt; 8385615Sdillon char *tmp; 845233Sbde int set_timezone; 8527874Sbrian struct vary *v; 8627874Sbrian const struct vary *badv; 8727874Sbrian struct tm lt; 881556Srgrimes 8927874Sbrian v = NULL; 9028037Sbrian fmt = NULL; 9111738Sache (void) setlocale(LC_TIME, ""); 921556Srgrimes tz.tz_dsttime = tz.tz_minuteswest = 0; 9330073Sdanny rflag = 0; 94265265Sdumbbell jflag = nflag = Rflag = 0; 955233Sbde set_timezone = 0; 96265265Sdumbbell while ((ch = getopt(argc, argv, "d:f:jnRr:t:uv:")) != -1) 971556Srgrimes switch((char)ch) { 981556Srgrimes case 'd': /* daylight savings time */ 995233Sbde tz.tz_dsttime = strtol(optarg, &endptr, 10) ? 1 : 0; 1005233Sbde if (endptr == optarg || *endptr != '\0') 1015233Sbde usage(); 1025233Sbde set_timezone = 1; 1031556Srgrimes break; 10428037Sbrian case 'f': 10528037Sbrian fmt = optarg; 10628037Sbrian break; 10747129Sjmg case 'j': 10847129Sjmg jflag = 1; /* don't set time */ 10947129Sjmg break; 1101556Srgrimes case 'n': /* don't set network */ 1111556Srgrimes nflag = 1; 1121556Srgrimes break; 113265265Sdumbbell case 'R': /* RFC 2822 datetime format */ 114265265Sdumbbell Rflag = 1; 115265265Sdumbbell break; 1161556Srgrimes case 'r': /* user specified seconds */ 1171556Srgrimes rflag = 1; 11885615Sdillon tval = strtoq(optarg, &tmp, 0); 11985615Sdillon if (*tmp != 0) 12063761Sjwd usage(); 1211556Srgrimes break; 12260718Sdbaker case 't': /* minutes west of UTC */ 1231556Srgrimes /* error check; don't allow "PST" */ 1245233Sbde tz.tz_minuteswest = strtol(optarg, &endptr, 10); 1255233Sbde if (endptr == optarg || *endptr != '\0') 1265233Sbde usage(); 1275233Sbde set_timezone = 1; 1285233Sbde break; 12960718Sdbaker case 'u': /* do everything in UTC */ 13060718Sdbaker (void)setenv("TZ", "UTC0", 1); 13128037Sbrian break; 13228025Sbrian case 'v': 13328025Sbrian v = vary_append(v, optarg); 13427874Sbrian break; 1351556Srgrimes default: 1361556Srgrimes usage(); 1371556Srgrimes } 1381556Srgrimes argc -= optind; 1391556Srgrimes argv += optind; 1401556Srgrimes 1411556Srgrimes /* 1421556Srgrimes * If -d or -t, set the timezone or daylight savings time; this 14324976Sdanny * doesn't belong here; the kernel should not know about either. 1441556Srgrimes */ 145239991Sed if (set_timezone && settimeofday(NULL, &tz) != 0) 1465233Sbde err(1, "settimeofday (timezone)"); 1471556Srgrimes 1481556Srgrimes if (!rflag && time(&tval) == -1) 1491556Srgrimes err(1, "time"); 1501556Srgrimes 1519944Sache format = "%+"; 1521556Srgrimes 153265265Sdumbbell if (Rflag) 154265265Sdumbbell format = rfc2822_format; 155265265Sdumbbell 1561556Srgrimes /* allow the operands in any order */ 1571556Srgrimes if (*argv && **argv == '+') { 1581556Srgrimes format = *argv + 1; 1591556Srgrimes ++argv; 1601556Srgrimes } 1611556Srgrimes 1621556Srgrimes if (*argv) { 16347129Sjmg setthetime(fmt, *argv, jflag, nflag); 1641556Srgrimes ++argv; 16528037Sbrian } else if (fmt != NULL) 16628037Sbrian usage(); 1671556Srgrimes 1681556Srgrimes if (*argv && **argv == '+') 1691556Srgrimes format = *argv + 1; 1701556Srgrimes 17127874Sbrian lt = *localtime(&tval); 17227874Sbrian badv = vary_apply(v, <); 17327874Sbrian if (badv) { 17428025Sbrian fprintf(stderr, "%s: Cannot apply date adjustment\n", 17528025Sbrian badv->arg); 17627874Sbrian vary_destroy(v); 17727874Sbrian usage(); 17827874Sbrian } 17927874Sbrian vary_destroy(v); 180265265Sdumbbell 181265265Sdumbbell if (format == rfc2822_format) 182265265Sdumbbell /* 183265265Sdumbbell * When using RFC 2822 datetime format, don't honor the 184265265Sdumbbell * locale. 185265265Sdumbbell */ 186265265Sdumbbell setlocale(LC_TIME, "C"); 187265265Sdumbbell 18827874Sbrian (void)strftime(buf, sizeof(buf), format, <); 18930073Sdanny (void)printf("%s\n", buf); 190120729Sdds if (fflush(stdout)) 191120729Sdds err(1, "stdout"); 1921556Srgrimes exit(retval); 1931556Srgrimes} 1941556Srgrimes 19555225Ssheldonh#define ATOI2(s) ((s) += 2, ((s)[-2] - '0') * 10 + ((s)[-1] - '0')) 19655225Ssheldonh 197105396Smarkmstatic void 19890108Simpsetthetime(const char *fmt, const char *p, int jflag, int nflag) 1991556Srgrimes{ 200200157Sed struct utmpx utx; 20190108Simp struct tm *lt; 2021556Srgrimes struct timeval tv; 20328037Sbrian const char *dot, *t; 20455225Ssheldonh int century; 2051556Srgrimes 206176094Sru lt = localtime(&tval); 207176094Sru lt->tm_isdst = -1; /* divine correct DST */ 208176094Sru 20928037Sbrian if (fmt != NULL) { 21028037Sbrian t = strptime(p, fmt, lt); 21128037Sbrian if (t == NULL) { 21228037Sbrian fprintf(stderr, "Failed conversion of ``%s''" 21328037Sbrian " using format ``%s''\n", p, fmt); 21448214Scracauer badformat(); 21528037Sbrian } else if (*t != '\0') 21632756Sjb fprintf(stderr, "Warning: Ignoring %ld extraneous" 21728037Sbrian " characters in date string (%s)\n", 21832756Sjb (long) strlen(t), t); 21928037Sbrian } else { 22028037Sbrian for (t = p, dot = NULL; *t; ++t) { 22128037Sbrian if (isdigit(*t)) 22228037Sbrian continue; 22328037Sbrian if (*t == '.' && dot == NULL) { 22428037Sbrian dot = t; 22528037Sbrian continue; 22628037Sbrian } 22728037Sbrian badformat(); 2281556Srgrimes } 2291556Srgrimes 23028037Sbrian if (dot != NULL) { /* .ss */ 23128037Sbrian dot++; /* *dot++ = '\0'; */ 23228037Sbrian if (strlen(dot) != 2) 23328037Sbrian badformat(); 23428037Sbrian lt->tm_sec = ATOI2(dot); 23528037Sbrian if (lt->tm_sec > 61) 23628037Sbrian badformat(); 23728037Sbrian } else 23828037Sbrian lt->tm_sec = 0; 2391556Srgrimes 24055225Ssheldonh century = 0; 24130013Sjoerg /* if p has a ".ss" field then let's pretend it's not there */ 24230013Sjoerg switch (strlen(p) - ((dot != NULL) ? 3 : 0)) { 24353082Ssheldonh case 12: /* cc */ 24455225Ssheldonh lt->tm_year = ATOI2(p) * 100 - TM_YEAR_BASE; 24555225Ssheldonh century = 1; 24655225Ssheldonh /* FALLTHROUGH */ 24728037Sbrian case 10: /* yy */ 24855225Ssheldonh if (century) 24955225Ssheldonh lt->tm_year += ATOI2(p); 250126605Smtm else { 25155225Ssheldonh lt->tm_year = ATOI2(p); 252126605Smtm if (lt->tm_year < 69) /* hack for 2000 ;-} */ 25355225Ssheldonh lt->tm_year += 2000 - TM_YEAR_BASE; 25455225Ssheldonh else 25555225Ssheldonh lt->tm_year += 1900 - TM_YEAR_BASE; 25655225Ssheldonh } 25755225Ssheldonh /* FALLTHROUGH */ 25828037Sbrian case 8: /* mm */ 25928037Sbrian lt->tm_mon = ATOI2(p); 26028037Sbrian if (lt->tm_mon > 12) 26128037Sbrian badformat(); 26228037Sbrian --lt->tm_mon; /* time struct is 0 - 11 */ 26328037Sbrian /* FALLTHROUGH */ 26428037Sbrian case 6: /* dd */ 26528037Sbrian lt->tm_mday = ATOI2(p); 26628037Sbrian if (lt->tm_mday > 31) 26728037Sbrian badformat(); 26828037Sbrian /* FALLTHROUGH */ 26928037Sbrian case 4: /* HH */ 27028037Sbrian lt->tm_hour = ATOI2(p); 27128037Sbrian if (lt->tm_hour > 23) 27228037Sbrian badformat(); 27328037Sbrian /* FALLTHROUGH */ 27428037Sbrian case 2: /* MM */ 27528037Sbrian lt->tm_min = ATOI2(p); 27628037Sbrian if (lt->tm_min > 59) 27728037Sbrian badformat(); 27828037Sbrian break; 27928037Sbrian default: 2801556Srgrimes badformat(); 28128037Sbrian } 2821556Srgrimes } 2831556Srgrimes 2841556Srgrimes /* convert broken-down time to GMT clock time */ 2851556Srgrimes if ((tval = mktime(lt)) == -1) 28615068Sache errx(1, "nonexistent time"); 2871556Srgrimes 28847129Sjmg if (!jflag) { 28947129Sjmg /* set the time */ 29047129Sjmg if (nflag || netsettime(tval)) { 291200157Sed utx.ut_type = OLD_TIME; 292239991Sed (void)gettimeofday(&utx.ut_tv, NULL); 293200157Sed pututxline(&utx); 29447129Sjmg tv.tv_sec = tval; 29547129Sjmg tv.tv_usec = 0; 296239991Sed if (settimeofday(&tv, NULL) != 0) 29747129Sjmg err(1, "settimeofday (timeval)"); 298200157Sed utx.ut_type = NEW_TIME; 299239991Sed (void)gettimeofday(&utx.ut_tv, NULL); 300200157Sed pututxline(&utx); 30147129Sjmg } 30247129Sjmg 30347129Sjmg if ((p = getlogin()) == NULL) 30447129Sjmg p = "???"; 30547129Sjmg syslog(LOG_AUTH | LOG_NOTICE, "date set by %s", p); 3061556Srgrimes } 3071556Srgrimes} 3081556Srgrimes 3091556Srgrimesstatic void 31090108Simpbadformat(void) 3111556Srgrimes{ 3121556Srgrimes warnx("illegal time format"); 3131556Srgrimes usage(); 3141556Srgrimes} 3151556Srgrimes 3161556Srgrimesstatic void 31790108Simpusage(void) 3181556Srgrimes{ 31926465Scharnier (void)fprintf(stderr, "%s\n%s\n", 320265265Sdumbbell "usage: date [-jnRu] [-d dst] [-r seconds] [-t west] " 32144598Sbrian "[-v[+|-]val[ymwdHMS]] ... ", 32253082Ssheldonh " " 32353082Ssheldonh "[-f fmt date | [[[[[cc]yy]mm]dd]HH]MM[.ss]] [+format]"); 3241556Srgrimes exit(1); 3251556Srgrimes} 326