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