1 /* 2 * Copyright (c) 1985, 1987, 1988, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * @(#) Copyright (c) 1985, 1987, 1988, 1993 The Regents of the University of California. All rights reserved. 30 * @(#)date.c 8.2 (Berkeley) 4/28/95 31 * $FreeBSD: src/bin/date/date.c,v 1.47 2005/01/10 08:39:21 imp Exp $ 32 */ 33 34 #include <sys/param.h> 35 #include <sys/time.h> 36 37 #include <ctype.h> 38 #include <err.h> 39 #include <stdio.h> 40 #include <stdlib.h> 41 #include <string.h> 42 #include <syslog.h> 43 #include <unistd.h> 44 #include <locale.h> 45 #include <libutil.h> 46 #ifdef SUPPORT_UTMPX 47 #include <utmpx.h> 48 #endif 49 50 #include "vary.h" 51 52 #ifndef TM_YEAR_BASE 53 #define TM_YEAR_BASE 1900 54 #endif 55 56 static time_t tval; 57 58 static void setthetime(const char *, const char *, int); 59 static void badformat(void); 60 static void usage(void); 61 62 static const char *rfc2822_format ="%a, %d %b %Y %T %z"; 63 64 int 65 main(int argc, char **argv) 66 { 67 int ch, rflag; 68 int jflag, Rflag; 69 const char *format; 70 char buf[1024]; 71 char *fmt; 72 char *tmp; 73 struct vary *v; 74 const struct vary *badv; 75 struct tm lt; 76 77 v = NULL; 78 fmt = NULL; 79 setlocale(LC_TIME, ""); 80 rflag = 0; 81 jflag = Rflag = 0; 82 while ((ch = getopt(argc, argv, "f:jnRr:uv:")) != -1) 83 switch(ch) { 84 case 'f': 85 fmt = optarg; 86 break; 87 case 'j': 88 jflag = 1; /* don't set time */ 89 break; 90 case 'n': /* don't set network */ 91 break; 92 case 'R': 93 Rflag = 1; /*RFC 2822 datetime format */ 94 break; 95 case 'r': /* user specified seconds */ 96 rflag = 1; 97 tval = strtoll(optarg, &tmp, 0); 98 if (*tmp != 0) 99 usage(); 100 break; 101 case 'u': /* do everything in UTC */ 102 if (setenv("TZ", "UTC0", 1) != 0) 103 err(1, "setenv: cannot set TZ=UTC0"); 104 break; 105 case 'v': 106 v = vary_append(v, optarg); 107 break; 108 default: 109 usage(); 110 } 111 argc -= optind; 112 argv += optind; 113 114 if (!rflag && time(&tval) == -1) 115 err(1, "time"); 116 117 format = "%+"; 118 119 if (Rflag) 120 format = rfc2822_format; 121 122 /* allow the operands in any order */ 123 if (*argv && **argv == '+') { 124 format = *argv + 1; 125 ++argv; 126 } 127 128 if (*argv) { 129 setthetime(fmt, *argv, jflag); 130 ++argv; 131 } else if (fmt != NULL) 132 usage(); 133 134 if (*argv && **argv == '+') 135 format = *argv + 1; 136 137 lt = *localtime(&tval); 138 badv = vary_apply(v, <); 139 if (badv) { 140 fprintf(stderr, "%s: Cannot apply date adjustment\n", 141 badv->arg); 142 vary_destroy(v); 143 usage(); 144 } 145 vary_destroy(v); 146 147 if (format == rfc2822_format) { 148 /* 149 * When using RFC 2822 datetime format, don't honor the 150 * locale. 151 */ 152 setlocale(LC_TIME, "C"); 153 } 154 155 strftime(buf, sizeof(buf), format, <); 156 printf("%s\n", buf); 157 if (fflush(stdout) != 0) 158 err(1, "stdout"); 159 exit(EXIT_SUCCESS); 160 } 161 162 #define ATOI2(s) ((s) += 2, ((s)[-2] - '0') * 10 + ((s)[-1] - '0')) 163 164 static void 165 setthetime(const char *fmt, const char *p, int jflag) 166 { 167 struct tm *lt; 168 struct timeval tv; 169 const char *dot, *t; 170 int century; 171 172 if (fmt != NULL) { 173 lt = localtime(&tval); 174 t = strptime(p, fmt, lt); 175 if (t == NULL) { 176 fprintf(stderr, "Failed conversion of ``%s''" 177 " using format ``%s''\n", p, fmt); 178 badformat(); 179 } else if (*t != '\0') 180 fprintf(stderr, "Warning: Ignoring %ld extraneous" 181 " characters in date string (%s)\n", 182 (long) strlen(t), t); 183 } else { 184 for (t = p, dot = NULL; *t; ++t) { 185 if (isdigit(*t)) 186 continue; 187 if (*t == '.' && dot == NULL) { 188 dot = t; 189 continue; 190 } 191 badformat(); 192 } 193 194 lt = localtime(&tval); 195 196 if (dot != NULL) { /* .ss */ 197 dot++; /* *dot++ = '\0'; */ 198 if (strlen(dot) != 2) 199 badformat(); 200 lt->tm_sec = ATOI2(dot); 201 if (lt->tm_sec > 61) 202 badformat(); 203 } else 204 lt->tm_sec = 0; 205 206 century = 0; 207 /* if p has a ".ss" field then let's pretend it's not there */ 208 switch (strlen(p) - ((dot != NULL) ? 3 : 0)) { 209 case 12: /* cc */ 210 lt->tm_year = ATOI2(p) * 100 - TM_YEAR_BASE; 211 century = 1; 212 /* FALLTHROUGH */ 213 case 10: /* yy */ 214 if (century) 215 lt->tm_year += ATOI2(p); 216 else { /* hack for 2000 ;-} */ 217 lt->tm_year = ATOI2(p); 218 if (lt->tm_year < 69) 219 lt->tm_year += 2000 - TM_YEAR_BASE; 220 else 221 lt->tm_year += 1900 - TM_YEAR_BASE; 222 } 223 /* FALLTHROUGH */ 224 case 8: /* mm */ 225 lt->tm_mon = ATOI2(p); 226 if (lt->tm_mon > 12) 227 badformat(); 228 --lt->tm_mon; /* time struct is 0 - 11 */ 229 /* FALLTHROUGH */ 230 case 6: /* dd */ 231 lt->tm_mday = ATOI2(p); 232 if (lt->tm_mday > 31) 233 badformat(); 234 /* FALLTHROUGH */ 235 case 4: /* HH */ 236 lt->tm_hour = ATOI2(p); 237 if (lt->tm_hour > 23) 238 badformat(); 239 /* FALLTHROUGH */ 240 case 2: /* MM */ 241 lt->tm_min = ATOI2(p); 242 if (lt->tm_min > 59) 243 badformat(); 244 break; 245 default: 246 badformat(); 247 } 248 } 249 250 /* Let mktime() decide whether summer time is in effect. */ 251 lt->tm_isdst = -1; 252 253 /* convert broken-down time to GMT clock time */ 254 if ((tval = mktime(lt)) == -1) 255 errx(1, "nonexistent time"); 256 257 if (!jflag) { 258 #ifdef SUPPORT_UTMP 259 logwtmp("|", "date", ""); 260 #endif 261 #ifdef SUPPORT_UTMPX 262 logwtmpx("|", "date", "", 0, OLD_TIME); 263 #endif 264 tv.tv_sec = tval; 265 tv.tv_usec = 0; 266 if (settimeofday(&tv, NULL)) 267 err(1, "settimeofday (timeval)"); 268 #ifdef SUPPORT_UTMP 269 logwtmp("{", "date", ""); 270 #endif 271 #ifdef SUPPORT_UTMPX 272 logwtmpx("{", "date", "", 0, NEW_TIME); 273 #endif 274 275 if ((p = getlogin()) == NULL) 276 p = "???"; 277 syslog(LOG_AUTH | LOG_NOTICE, "date set by %s", p); 278 } 279 } 280 281 static void 282 badformat(void) 283 { 284 warnx("illegal time format"); 285 usage(); 286 } 287 288 static void 289 usage(void) 290 { 291 fprintf(stderr, "%s\n%s\n", 292 "usage: date [-jnRu] [-r seconds] [-v[+|-]val[ymwdHMS]] ... ", 293 " " 294 "[-f fmt date | [[[[[cc]yy]mm]dd]HH]MM[.ss]] [+format]"); 295 exit(1); 296 } 297