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 47 #include "vary.h" 48 49 #ifndef TM_YEAR_BASE 50 #define TM_YEAR_BASE 1900 51 #endif 52 53 static time_t tval; 54 55 static void setthetime(const char *, const char *, int); 56 static void badformat(void); 57 static void usage(void); 58 59 static const char *rfc2822_format ="%a, %d %b %Y %T %z"; 60 61 int 62 main(int argc, char **argv) 63 { 64 int ch, rflag; 65 int jflag, Rflag; 66 const char *format; 67 char buf[1024]; 68 char *fmt; 69 char *tmp; 70 struct vary *v; 71 const struct vary *badv; 72 struct tm lt; 73 74 v = NULL; 75 fmt = NULL; 76 setlocale(LC_TIME, ""); 77 rflag = 0; 78 jflag = Rflag = 0; 79 while ((ch = getopt(argc, argv, "f:jnRr:uv:")) != -1) 80 switch(ch) { 81 case 'f': 82 fmt = optarg; 83 break; 84 case 'j': 85 jflag = 1; /* don't set time */ 86 break; 87 case 'n': /* don't set network */ 88 break; 89 case 'R': 90 Rflag = 1; /*RFC 2822 datetime format */ 91 break; 92 case 'r': /* user specified seconds */ 93 rflag = 1; 94 tval = strtoll(optarg, &tmp, 0); 95 if (*tmp != 0) 96 usage(); 97 break; 98 case 'u': /* do everything in UTC */ 99 if (setenv("TZ", "UTC0", 1) != 0) 100 err(1, "setenv: cannot set TZ=UTC0"); 101 break; 102 case 'v': 103 v = vary_append(v, optarg); 104 break; 105 default: 106 usage(); 107 } 108 argc -= optind; 109 argv += optind; 110 111 if (!rflag && time(&tval) == -1) 112 err(1, "time"); 113 114 format = "%+"; 115 116 if (Rflag) 117 format = rfc2822_format; 118 119 /* allow the operands in any order */ 120 if (*argv && **argv == '+') { 121 format = *argv + 1; 122 ++argv; 123 } 124 125 if (*argv) { 126 setthetime(fmt, *argv, jflag); 127 ++argv; 128 } else if (fmt != NULL) 129 usage(); 130 131 if (*argv && **argv == '+') 132 format = *argv + 1; 133 134 lt = *localtime(&tval); 135 badv = vary_apply(v, <); 136 if (badv) { 137 fprintf(stderr, "%s: Cannot apply date adjustment\n", 138 badv->arg); 139 vary_destroy(v); 140 usage(); 141 } 142 vary_destroy(v); 143 144 if (format == rfc2822_format) { 145 /* 146 * When using RFC 2822 datetime format, don't honor the 147 * locale. 148 */ 149 setlocale(LC_TIME, "C"); 150 } 151 152 strftime(buf, sizeof(buf), format, <); 153 printf("%s\n", buf); 154 if (fflush(stdout) != 0) 155 err(1, "stdout"); 156 exit(EXIT_SUCCESS); 157 } 158 159 #define ATOI2(s) ((s) += 2, ((s)[-2] - '0') * 10 + ((s)[-1] - '0')) 160 161 static void 162 setthetime(const char *fmt, const char *p, int jflag) 163 { 164 struct tm *lt; 165 struct timeval tv; 166 const char *dot, *t; 167 int century; 168 169 if (fmt != NULL) { 170 lt = localtime(&tval); 171 t = strptime(p, fmt, lt); 172 if (t == NULL) { 173 fprintf(stderr, "Failed conversion of ``%s''" 174 " using format ``%s''\n", p, fmt); 175 badformat(); 176 } else if (*t != '\0') 177 fprintf(stderr, "Warning: Ignoring %ld extraneous" 178 " characters in date string (%s)\n", 179 (long) strlen(t), t); 180 } else { 181 for (t = p, dot = NULL; *t; ++t) { 182 if (isdigit(*t)) 183 continue; 184 if (*t == '.' && dot == NULL) { 185 dot = t; 186 continue; 187 } 188 badformat(); 189 } 190 191 lt = localtime(&tval); 192 193 if (dot != NULL) { /* .ss */ 194 dot++; /* *dot++ = '\0'; */ 195 if (strlen(dot) != 2) 196 badformat(); 197 lt->tm_sec = ATOI2(dot); 198 if (lt->tm_sec > 61) 199 badformat(); 200 } else 201 lt->tm_sec = 0; 202 203 century = 0; 204 /* if p has a ".ss" field then let's pretend it's not there */ 205 switch (strlen(p) - ((dot != NULL) ? 3 : 0)) { 206 case 12: /* cc */ 207 lt->tm_year = ATOI2(p) * 100 - TM_YEAR_BASE; 208 century = 1; 209 /* FALLTHROUGH */ 210 case 10: /* yy */ 211 if (century) 212 lt->tm_year += ATOI2(p); 213 else { /* hack for 2000 ;-} */ 214 lt->tm_year = ATOI2(p); 215 if (lt->tm_year < 69) 216 lt->tm_year += 2000 - TM_YEAR_BASE; 217 else 218 lt->tm_year += 1900 - TM_YEAR_BASE; 219 } 220 /* FALLTHROUGH */ 221 case 8: /* mm */ 222 lt->tm_mon = ATOI2(p); 223 if (lt->tm_mon > 12) 224 badformat(); 225 --lt->tm_mon; /* time struct is 0 - 11 */ 226 /* FALLTHROUGH */ 227 case 6: /* dd */ 228 lt->tm_mday = ATOI2(p); 229 if (lt->tm_mday > 31) 230 badformat(); 231 /* FALLTHROUGH */ 232 case 4: /* HH */ 233 lt->tm_hour = ATOI2(p); 234 if (lt->tm_hour > 23) 235 badformat(); 236 /* FALLTHROUGH */ 237 case 2: /* MM */ 238 lt->tm_min = ATOI2(p); 239 if (lt->tm_min > 59) 240 badformat(); 241 break; 242 default: 243 badformat(); 244 } 245 } 246 247 /* Let mktime() decide whether summer time is in effect. */ 248 lt->tm_isdst = -1; 249 250 /* convert broken-down time to GMT clock time */ 251 if ((tval = mktime(lt)) == -1) 252 errx(1, "nonexistent time"); 253 254 if (!jflag) { 255 logwtmp("|", "date", ""); 256 tv.tv_sec = tval; 257 tv.tv_usec = 0; 258 if (settimeofday(&tv, NULL)) 259 err(1, "settimeofday (timeval)"); 260 logwtmp("{", "date", ""); 261 262 if ((p = getlogin()) == NULL) 263 p = "???"; 264 syslog(LOG_AUTH | LOG_NOTICE, "date set by %s", p); 265 } 266 } 267 268 static void 269 badformat(void) 270 { 271 warnx("illegal time format"); 272 usage(); 273 } 274 275 static void 276 usage(void) 277 { 278 fprintf(stderr, "%s\n%s\n", 279 "usage: date [-jnRu] [-r seconds] [-v[+|-]val[ymwdHMS]] ... ", 280 " " 281 "[-f fmt date | [[[[[cc]yy]mm]dd]HH]MM[.ss]] [+format]"); 282 exit(1); 283 } 284