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