date.c revision 264968
11573Srgrimes/*- 21573Srgrimes * Copyright (c) 1985, 1987, 1988, 1993 31573Srgrimes * The Regents of the University of California. All rights reserved. 41573Srgrimes * 51573Srgrimes * Redistribution and use in source and binary forms, with or without 61573Srgrimes * modification, are permitted provided that the following conditions 71573Srgrimes * are met: 81573Srgrimes * 1. Redistributions of source code must retain the above copyright 91573Srgrimes * notice, this list of conditions and the following disclaimer. 101573Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111573Srgrimes * notice, this list of conditions and the following disclaimer in the 121573Srgrimes * documentation and/or other materials provided with the distribution. 131573Srgrimes * 4. Neither the name of the University nor the names of its contributors 141573Srgrimes * may be used to endorse or promote products derived from this software 151573Srgrimes * without specific prior written permission. 161573Srgrimes * 171573Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 181573Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 191573Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 201573Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 211573Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 221573Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 231573Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 241573Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 251573Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 261573Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 271573Srgrimes * SUCH DAMAGE. 281573Srgrimes */ 291573Srgrimes 301573Srgrimes#ifndef lint 311573Srgrimesstatic char const copyright[] = 321573Srgrimes"@(#) Copyright (c) 1985, 1987, 1988, 1993\n\ 331573Srgrimes The Regents of the University of California. All rights reserved.\n"; 341573Srgrimes#endif /* not lint */ 3516586Sjraynard 361573Srgrimes#if 0 3716586Sjraynard#ifndef lint 3816586Sjraynardstatic char sccsid[] = "@(#)date.c 8.2 (Berkeley) 4/28/95"; 3950476Speter#endif /* not lint */ 401573Srgrimes#endif 411573Srgrimes 421573Srgrimes#include <sys/cdefs.h> 431573Srgrimes__FBSDID("$FreeBSD: head/bin/date/date.c 264968 2014-04-26 13:05:56Z dumbbell $"); 441573Srgrimes 451573Srgrimes#include <sys/param.h> 4616586Sjraynard#include <sys/time.h> 471573Srgrimes 481573Srgrimes#include <ctype.h> 491573Srgrimes#include <err.h> 5050121Simp#include <locale.h> 5150121Simp#include <stdio.h> 5250121Simp#include <stdlib.h> 5392905Sobrien#include <string.h> 5450121Simp#include <syslog.h> 551573Srgrimes#include <unistd.h> 561573Srgrimes#include <utmpx.h> 571573Srgrimes 581573Srgrimes#include "extern.h" 591573Srgrimes#include "vary.h" 601573Srgrimes 611573Srgrimes#ifndef TM_YEAR_BASE 621573Srgrimes#define TM_YEAR_BASE 1900 631573Srgrimes#endif 641573Srgrimes 651573Srgrimesstatic time_t tval; 661573Srgrimesint retval; 671573Srgrimes 6850121Simpstatic void setthetime(const char *, const char *, int, int); 691573Srgrimesstatic void badformat(void); 701573Srgrimesstatic void usage(void); 7150121Simp 721573Srgrimesstatic const char *rfc2822_format = "%a, %d %b %Y %T %z"; 731573Srgrimes 741573Srgrimesint 7516337Sjraynardmain(int argc, char *argv[]) 761573Srgrimes{ 771573Srgrimes struct timezone tz; 7852466Sache int ch, rflag; 791573Srgrimes int jflag, nflag, Rflag; 801573Srgrimes const char *format; 811573Srgrimes char buf[1024]; 821573Srgrimes char *endptr, *fmt; 831573Srgrimes char *tmp; 8452466Sache int set_timezone; 851573Srgrimes struct vary *v; 861573Srgrimes const struct vary *badv; 871573Srgrimes struct tm lt; 881573Srgrimes 8952466Sache v = NULL; 901573Srgrimes fmt = NULL; 911573Srgrimes (void) setlocale(LC_TIME, ""); 921573Srgrimes tz.tz_dsttime = tz.tz_minuteswest = 0; 931573Srgrimes rflag = 0; 941573Srgrimes jflag = nflag = Rflag = 0; 951573Srgrimes set_timezone = 0; 961573Srgrimes while ((ch = getopt(argc, argv, "d:f:jnRr:t:uv:")) != -1) 97 switch((char)ch) { 98 case 'd': /* daylight savings time */ 99 tz.tz_dsttime = strtol(optarg, &endptr, 10) ? 1 : 0; 100 if (endptr == optarg || *endptr != '\0') 101 usage(); 102 set_timezone = 1; 103 break; 104 case 'f': 105 fmt = optarg; 106 break; 107 case 'j': 108 jflag = 1; /* don't set time */ 109 break; 110 case 'n': /* don't set network */ 111 nflag = 1; 112 break; 113 case 'R': /* RFC 2822 datetime format */ 114 Rflag = 1; 115 break; 116 case 'r': /* user specified seconds */ 117 rflag = 1; 118 tval = strtoq(optarg, &tmp, 0); 119 if (*tmp != 0) 120 usage(); 121 break; 122 case 't': /* minutes west of UTC */ 123 /* error check; don't allow "PST" */ 124 tz.tz_minuteswest = strtol(optarg, &endptr, 10); 125 if (endptr == optarg || *endptr != '\0') 126 usage(); 127 set_timezone = 1; 128 break; 129 case 'u': /* do everything in UTC */ 130 (void)setenv("TZ", "UTC0", 1); 131 break; 132 case 'v': 133 v = vary_append(v, optarg); 134 break; 135 default: 136 usage(); 137 } 138 argc -= optind; 139 argv += optind; 140 141 /* 142 * If -d or -t, set the timezone or daylight savings time; this 143 * doesn't belong here; the kernel should not know about either. 144 */ 145 if (set_timezone && settimeofday(NULL, &tz) != 0) 146 err(1, "settimeofday (timezone)"); 147 148 if (!rflag && time(&tval) == -1) 149 err(1, "time"); 150 151 format = "%+"; 152 153 if (Rflag) 154 format = rfc2822_format; 155 156 /* allow the operands in any order */ 157 if (*argv && **argv == '+') { 158 format = *argv + 1; 159 ++argv; 160 } 161 162 if (*argv) { 163 setthetime(fmt, *argv, jflag, nflag); 164 ++argv; 165 } else if (fmt != NULL) 166 usage(); 167 168 if (*argv && **argv == '+') 169 format = *argv + 1; 170 171 lt = *localtime(&tval); 172 badv = vary_apply(v, <); 173 if (badv) { 174 fprintf(stderr, "%s: Cannot apply date adjustment\n", 175 badv->arg); 176 vary_destroy(v); 177 usage(); 178 } 179 vary_destroy(v); 180 181 if (format == rfc2822_format) 182 /* 183 * When using RFC 2822 datetime format, don't honor the 184 * locale. 185 */ 186 setlocale(LC_TIME, "C"); 187 188 (void)strftime(buf, sizeof(buf), format, <); 189 (void)printf("%s\n", buf); 190 if (fflush(stdout)) 191 err(1, "stdout"); 192 exit(retval); 193} 194 195#define ATOI2(s) ((s) += 2, ((s)[-2] - '0') * 10 + ((s)[-1] - '0')) 196 197static void 198setthetime(const char *fmt, const char *p, int jflag, int nflag) 199{ 200 struct utmpx utx; 201 struct tm *lt; 202 struct timeval tv; 203 const char *dot, *t; 204 int century; 205 206 lt = localtime(&tval); 207 lt->tm_isdst = -1; /* divine correct DST */ 208 209 if (fmt != NULL) { 210 t = strptime(p, fmt, lt); 211 if (t == NULL) { 212 fprintf(stderr, "Failed conversion of ``%s''" 213 " using format ``%s''\n", p, fmt); 214 badformat(); 215 } else if (*t != '\0') 216 fprintf(stderr, "Warning: Ignoring %ld extraneous" 217 " characters in date string (%s)\n", 218 (long) strlen(t), t); 219 } else { 220 for (t = p, dot = NULL; *t; ++t) { 221 if (isdigit(*t)) 222 continue; 223 if (*t == '.' && dot == NULL) { 224 dot = t; 225 continue; 226 } 227 badformat(); 228 } 229 230 if (dot != NULL) { /* .ss */ 231 dot++; /* *dot++ = '\0'; */ 232 if (strlen(dot) != 2) 233 badformat(); 234 lt->tm_sec = ATOI2(dot); 235 if (lt->tm_sec > 61) 236 badformat(); 237 } else 238 lt->tm_sec = 0; 239 240 century = 0; 241 /* if p has a ".ss" field then let's pretend it's not there */ 242 switch (strlen(p) - ((dot != NULL) ? 3 : 0)) { 243 case 12: /* cc */ 244 lt->tm_year = ATOI2(p) * 100 - TM_YEAR_BASE; 245 century = 1; 246 /* FALLTHROUGH */ 247 case 10: /* yy */ 248 if (century) 249 lt->tm_year += ATOI2(p); 250 else { 251 lt->tm_year = ATOI2(p); 252 if (lt->tm_year < 69) /* hack for 2000 ;-} */ 253 lt->tm_year += 2000 - TM_YEAR_BASE; 254 else 255 lt->tm_year += 1900 - TM_YEAR_BASE; 256 } 257 /* FALLTHROUGH */ 258 case 8: /* mm */ 259 lt->tm_mon = ATOI2(p); 260 if (lt->tm_mon > 12) 261 badformat(); 262 --lt->tm_mon; /* time struct is 0 - 11 */ 263 /* FALLTHROUGH */ 264 case 6: /* dd */ 265 lt->tm_mday = ATOI2(p); 266 if (lt->tm_mday > 31) 267 badformat(); 268 /* FALLTHROUGH */ 269 case 4: /* HH */ 270 lt->tm_hour = ATOI2(p); 271 if (lt->tm_hour > 23) 272 badformat(); 273 /* FALLTHROUGH */ 274 case 2: /* MM */ 275 lt->tm_min = ATOI2(p); 276 if (lt->tm_min > 59) 277 badformat(); 278 break; 279 default: 280 badformat(); 281 } 282 } 283 284 /* convert broken-down time to GMT clock time */ 285 if ((tval = mktime(lt)) == -1) 286 errx(1, "nonexistent time"); 287 288 if (!jflag) { 289 /* set the time */ 290 if (nflag || netsettime(tval)) { 291 utx.ut_type = OLD_TIME; 292 (void)gettimeofday(&utx.ut_tv, NULL); 293 pututxline(&utx); 294 tv.tv_sec = tval; 295 tv.tv_usec = 0; 296 if (settimeofday(&tv, NULL) != 0) 297 err(1, "settimeofday (timeval)"); 298 utx.ut_type = NEW_TIME; 299 (void)gettimeofday(&utx.ut_tv, NULL); 300 pututxline(&utx); 301 } 302 303 if ((p = getlogin()) == NULL) 304 p = "???"; 305 syslog(LOG_AUTH | LOG_NOTICE, "date set by %s", p); 306 } 307} 308 309static void 310badformat(void) 311{ 312 warnx("illegal time format"); 313 usage(); 314} 315 316static void 317usage(void) 318{ 319 (void)fprintf(stderr, "%s\n%s\n", 320 "usage: date [-jnRu] [-d dst] [-r seconds] [-t west] " 321 "[-v[+|-]val[ymwdHMS]] ... ", 322 " " 323 "[-f fmt date | [[[[[cc]yy]mm]dd]HH]MM[.ss]] [+format]"); 324 exit(1); 325} 326