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