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