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 <locale.h> 54 #include <libutil.h> 55 #include <stdio.h> 56 #include <stdlib.h> 57 #include <string.h> 58 #include <syslog.h> 59 #include <unistd.h> 60 61 #include "extern.h" 62 #include "vary.h" 63 64 #ifndef TM_YEAR_BASE 65 #define TM_YEAR_BASE 1900 66 #endif 67 68 time_t tval; 69 int retval; 70 71 static void setthetime(const char *, const char *, int, int); 72 static void badformat(void); 73 static void usage(void); 74 75 int 76 main(int argc, char *argv[]) 77 { 78 struct timezone tz; 79 int ch, rflag; 80 int jflag, nflag; 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 90 v = NULL; 91 fmt = NULL; 92 (void) setlocale(LC_TIME, ""); 93 tz.tz_dsttime = tz.tz_minuteswest = 0; 94 rflag = 0; 95 jflag = nflag = 0; 96 set_timezone = 0; 97 while ((ch = getopt(argc, argv, "d:f:jnr:t:uv:")) != -1) 98 switch((char)ch) { 99 case 'd': /* daylight savings time */ 100 tz.tz_dsttime = strtol(optarg, &endptr, 10) ? 1 : 0; 101 if (endptr == optarg || *endptr != '\0') 102 usage(); 103 set_timezone = 1; 104 break; 105 case 'f': 106 fmt = optarg; 107 break; 108 case 'j': 109 jflag = 1; /* don't set time */ 110 break; 111 case 'n': /* don't set network */ 112 nflag = 1; 113 break; 114 case 'r': /* user specified seconds */ 115 rflag = 1; 116 tval = strtoq(optarg, &tmp, 0); 117 if (*tmp != 0) 118 usage(); 119 break; 120 case 't': /* minutes west of UTC */ 121 /* error check; don't allow "PST" */ 122 tz.tz_minuteswest = strtol(optarg, &endptr, 10); 123 if (endptr == optarg || *endptr != '\0') 124 usage(); 125 set_timezone = 1; 126 break; 127 case 'u': /* do everything in UTC */ 128 (void)setenv("TZ", "UTC0", 1); 129 break; 130 case 'v': 131 v = vary_append(v, optarg); 132 break; 133 default: 134 usage(); 135 } 136 argc -= optind; 137 argv += optind; 138 139 /* 140 * If -d or -t, set the timezone or daylight savings time; this 141 * doesn't belong here; the kernel should not know about either. 142 */ 143 if (set_timezone && settimeofday((struct timeval *)NULL, &tz)) 144 err(1, "settimeofday (timezone)"); 145 146 if (!rflag && time(&tval) == -1) 147 err(1, "time"); 148 149 format = "%+"; 150 151 /* allow the operands in any order */ 152 if (*argv && **argv == '+') { 153 format = *argv + 1; 154 ++argv; 155 } 156 157 if (*argv) { 158 setthetime(fmt, *argv, jflag, nflag); 159 ++argv; 160 } else if (fmt != NULL) 161 usage(); 162 163 if (*argv && **argv == '+') 164 format = *argv + 1; 165 166 lt = *localtime(&tval); 167 badv = vary_apply(v, <); 168 if (badv) { 169 fprintf(stderr, "%s: Cannot apply date adjustment\n", 170 badv->arg); 171 vary_destroy(v); 172 usage(); 173 } 174 vary_destroy(v); 175 (void)strftime(buf, sizeof(buf), format, <); 176 (void)printf("%s\n", buf); 177 exit(retval); 178 } 179 180 #define ATOI2(s) ((s) += 2, ((s)[-2] - '0') * 10 + ((s)[-1] - '0')) 181 182 void 183 setthetime(const char *fmt, const char *p, int jflag, int nflag) 184 { 185 struct tm *lt; 186 struct timeval tv; 187 const char *dot, *t; 188 int century; 189 190 if (fmt != NULL) { 191 lt = localtime(&tval); 192 t = strptime(p, fmt, lt); 193 if (t == NULL) { 194 fprintf(stderr, "Failed conversion of ``%s''" 195 " using format ``%s''\n", p, fmt); 196 badformat(); 197 } else if (*t != '\0') 198 fprintf(stderr, "Warning: Ignoring %ld extraneous" 199 " characters in date string (%s)\n", 200 (long) strlen(t), t); 201 } else { 202 for (t = p, dot = NULL; *t; ++t) { 203 if (isdigit(*t)) 204 continue; 205 if (*t == '.' && dot == NULL) { 206 dot = t; 207 continue; 208 } 209 badformat(); 210 } 211 212 lt = localtime(&tval); 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 { /* hack for 2000 ;-} */ 235 lt->tm_year = ATOI2(p); 236 if (lt->tm_year < 69) 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 /* Let mktime() decide whether summer time is in effect. */ 269 lt->tm_isdst = -1; 270 271 /* convert broken-down time to GMT clock time */ 272 if ((tval = mktime(lt)) == -1) 273 errx(1, "nonexistent time"); 274 275 if (!jflag) { 276 /* set the time */ 277 if (nflag || netsettime(tval)) { 278 logwtmp("|", "date", ""); 279 tv.tv_sec = tval; 280 tv.tv_usec = 0; 281 if (settimeofday(&tv, (struct timezone *)NULL)) 282 err(1, "settimeofday (timeval)"); 283 logwtmp("{", "date", ""); 284 } 285 286 if ((p = getlogin()) == NULL) 287 p = "???"; 288 syslog(LOG_AUTH | LOG_NOTICE, "date set by %s", p); 289 } 290 } 291 292 static void 293 badformat(void) 294 { 295 warnx("illegal time format"); 296 usage(); 297 } 298 299 static void 300 usage(void) 301 { 302 (void)fprintf(stderr, "%s\n%s\n", 303 "usage: date [-jnu] [-d dst] [-r seconds] [-t west] " 304 "[-v[+|-]val[ymwdHMS]] ... ", 305 " " 306 "[-f fmt date | [[[[[cc]yy]mm]dd]HH]MM[.ss]] [+format]"); 307 exit(1); 308 } 309