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 30 #ifndef lint 31 static char const copyright[] = 32 "@(#) Copyright (c) 1985, 1987, 1988, 1993\n\ 33 The Regents of the University of California. All rights reserved.\n"; 34 #endif /* not lint */ 35 36 #if 0 37 #ifndef lint 38 static char sccsid[] = "@(#)date.c 8.2 (Berkeley) 4/28/95"; 39 #endif /* not lint */ 40 #endif 41 42 #include <sys/cdefs.h> 43 __FBSDID("$FreeBSD$"); 44 45 #include <sys/param.h> 46 #include <sys/time.h> 47 #include <sys/stat.h> 48 49 #include <ctype.h> 50 #include <err.h> 51 #include <locale.h> 52 #include <stdio.h> 53 #include <stdlib.h> 54 #include <string.h> 55 #include <syslog.h> 56 #include <unistd.h> 57 #include <utmpx.h> 58 59 #include "extern.h" 60 #include "vary.h" 61 62 #ifndef TM_YEAR_BASE 63 #define TM_YEAR_BASE 1900 64 #endif 65 66 static time_t tval; 67 int retval; 68 69 static void setthetime(const char *, const char *, int, int); 70 static void badformat(void); 71 static void usage(void); 72 73 static const char *rfc2822_format = "%a, %d %b %Y %T %z"; 74 75 int 76 main(int argc, char *argv[]) 77 { 78 struct timezone tz; 79 int ch, rflag; 80 int jflag, nflag, Rflag; 81 const char *format; 82 char buf[1024]; 83 char *endptr, *fmt; 84 char *tmp; 85 int set_timezone; 86 struct vary *v; 87 const struct vary *badv; 88 struct tm lt; 89 struct stat sb; 90 91 v = NULL; 92 fmt = NULL; 93 (void) setlocale(LC_TIME, ""); 94 tz.tz_dsttime = tz.tz_minuteswest = 0; 95 rflag = 0; 96 jflag = nflag = Rflag = 0; 97 set_timezone = 0; 98 while ((ch = getopt(argc, argv, "d:f:jnRr:t:uv:")) != -1) 99 switch((char)ch) { 100 case 'd': /* daylight savings time */ 101 tz.tz_dsttime = strtol(optarg, &endptr, 10) ? 1 : 0; 102 if (endptr == optarg || *endptr != '\0') 103 usage(); 104 set_timezone = 1; 105 break; 106 case 'f': 107 fmt = optarg; 108 break; 109 case 'j': 110 jflag = 1; /* don't set time */ 111 break; 112 case 'n': /* don't set network */ 113 nflag = 1; 114 break; 115 case 'R': /* RFC 2822 datetime format */ 116 Rflag = 1; 117 break; 118 case 'r': /* user specified seconds */ 119 rflag = 1; 120 tval = strtoq(optarg, &tmp, 0); 121 if (*tmp != 0) { 122 if (stat(optarg, &sb) == 0) 123 tval = sb.st_mtim.tv_sec; 124 else 125 usage(); 126 } 127 break; 128 case 't': /* minutes west of UTC */ 129 /* error check; don't allow "PST" */ 130 tz.tz_minuteswest = strtol(optarg, &endptr, 10); 131 if (endptr == optarg || *endptr != '\0') 132 usage(); 133 set_timezone = 1; 134 break; 135 case 'u': /* do everything in UTC */ 136 (void)setenv("TZ", "UTC0", 1); 137 break; 138 case 'v': 139 v = vary_append(v, optarg); 140 break; 141 default: 142 usage(); 143 } 144 argc -= optind; 145 argv += optind; 146 147 /* 148 * If -d or -t, set the timezone or daylight savings time; this 149 * doesn't belong here; the kernel should not know about either. 150 */ 151 if (set_timezone && settimeofday(NULL, &tz) != 0) 152 err(1, "settimeofday (timezone)"); 153 154 if (!rflag && time(&tval) == -1) 155 err(1, "time"); 156 157 format = "%+"; 158 159 if (Rflag) 160 format = rfc2822_format; 161 162 /* allow the operands in any order */ 163 if (*argv && **argv == '+') { 164 format = *argv + 1; 165 ++argv; 166 } 167 168 if (*argv) { 169 setthetime(fmt, *argv, jflag, nflag); 170 ++argv; 171 } else if (fmt != NULL) 172 usage(); 173 174 if (*argv && **argv == '+') 175 format = *argv + 1; 176 177 lt = *localtime(&tval); 178 badv = vary_apply(v, <); 179 if (badv) { 180 fprintf(stderr, "%s: Cannot apply date adjustment\n", 181 badv->arg); 182 vary_destroy(v); 183 usage(); 184 } 185 vary_destroy(v); 186 187 if (format == rfc2822_format) 188 /* 189 * When using RFC 2822 datetime format, don't honor the 190 * locale. 191 */ 192 setlocale(LC_TIME, "C"); 193 194 (void)strftime(buf, sizeof(buf), format, <); 195 (void)printf("%s\n", buf); 196 if (fflush(stdout)) 197 err(1, "stdout"); 198 exit(retval); 199 } 200 201 #define ATOI2(s) ((s) += 2, ((s)[-2] - '0') * 10 + ((s)[-1] - '0')) 202 203 static void 204 setthetime(const char *fmt, const char *p, int jflag, int nflag) 205 { 206 struct utmpx utx; 207 struct tm *lt; 208 struct timeval tv; 209 const char *dot, *t; 210 int century; 211 212 lt = localtime(&tval); 213 lt->tm_isdst = -1; /* divine correct DST */ 214 215 if (fmt != NULL) { 216 t = strptime(p, fmt, lt); 217 if (t == NULL) { 218 fprintf(stderr, "Failed conversion of ``%s''" 219 " using format ``%s''\n", p, fmt); 220 badformat(); 221 } else if (*t != '\0') 222 fprintf(stderr, "Warning: Ignoring %ld extraneous" 223 " characters in date string (%s)\n", 224 (long) strlen(t), t); 225 } else { 226 for (t = p, dot = NULL; *t; ++t) { 227 if (isdigit(*t)) 228 continue; 229 if (*t == '.' && dot == NULL) { 230 dot = t; 231 continue; 232 } 233 badformat(); 234 } 235 236 if (dot != NULL) { /* .ss */ 237 dot++; /* *dot++ = '\0'; */ 238 if (strlen(dot) != 2) 239 badformat(); 240 lt->tm_sec = ATOI2(dot); 241 if (lt->tm_sec > 61) 242 badformat(); 243 } else 244 lt->tm_sec = 0; 245 246 century = 0; 247 /* if p has a ".ss" field then let's pretend it's not there */ 248 switch (strlen(p) - ((dot != NULL) ? 3 : 0)) { 249 case 12: /* cc */ 250 lt->tm_year = ATOI2(p) * 100 - TM_YEAR_BASE; 251 century = 1; 252 /* FALLTHROUGH */ 253 case 10: /* yy */ 254 if (century) 255 lt->tm_year += ATOI2(p); 256 else { 257 lt->tm_year = ATOI2(p); 258 if (lt->tm_year < 69) /* hack for 2000 ;-} */ 259 lt->tm_year += 2000 - TM_YEAR_BASE; 260 else 261 lt->tm_year += 1900 - TM_YEAR_BASE; 262 } 263 /* FALLTHROUGH */ 264 case 8: /* mm */ 265 lt->tm_mon = ATOI2(p); 266 if (lt->tm_mon > 12) 267 badformat(); 268 --lt->tm_mon; /* time struct is 0 - 11 */ 269 /* FALLTHROUGH */ 270 case 6: /* dd */ 271 lt->tm_mday = ATOI2(p); 272 if (lt->tm_mday > 31) 273 badformat(); 274 /* FALLTHROUGH */ 275 case 4: /* HH */ 276 lt->tm_hour = ATOI2(p); 277 if (lt->tm_hour > 23) 278 badformat(); 279 /* FALLTHROUGH */ 280 case 2: /* MM */ 281 lt->tm_min = ATOI2(p); 282 if (lt->tm_min > 59) 283 badformat(); 284 break; 285 default: 286 badformat(); 287 } 288 } 289 290 /* convert broken-down time to GMT clock time */ 291 if ((tval = mktime(lt)) == -1) 292 errx(1, "nonexistent time"); 293 294 if (!jflag) { 295 /* set the time */ 296 if (nflag || netsettime(tval)) { 297 utx.ut_type = OLD_TIME; 298 (void)gettimeofday(&utx.ut_tv, NULL); 299 pututxline(&utx); 300 tv.tv_sec = tval; 301 tv.tv_usec = 0; 302 if (settimeofday(&tv, NULL) != 0) 303 err(1, "settimeofday (timeval)"); 304 utx.ut_type = NEW_TIME; 305 (void)gettimeofday(&utx.ut_tv, NULL); 306 pututxline(&utx); 307 } 308 309 if ((p = getlogin()) == NULL) 310 p = "???"; 311 syslog(LOG_AUTH | LOG_NOTICE, "date set by %s", p); 312 } 313 } 314 315 static void 316 badformat(void) 317 { 318 warnx("illegal time format"); 319 usage(); 320 } 321 322 static void 323 usage(void) 324 { 325 (void)fprintf(stderr, "%s\n%s\n", 326 "usage: date [-jnRu] [-d dst] [-r seconds] [-t west] " 327 "[-v[+|-]val[ymwdHMS]] ... ", 328 " " 329 "[-f fmt date | [[[[[cc]yy]mm]dd]HH]MM[.ss]] [+format]"); 330 exit(1); 331 } 332