date.c revision 110390
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 35static 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#if 0 41#ifndef lint 42static char sccsid[] = "@(#)date.c 8.2 (Berkeley) 4/28/95"; 43#endif /* not lint */ 44#endif 45 46#include <sys/cdefs.h> 47__FBSDID("$FreeBSD: head/bin/date/date.c 110390 2003-02-05 12:56:40Z charnier $"); 48 49#include <sys/param.h> 50#include <sys/time.h> 51 52#include <ctype.h> 53#include <err.h> 54#include <locale.h> 55#include <libutil.h> 56#include <stdio.h> 57#include <stdlib.h> 58#include <string.h> 59#include <syslog.h> 60#include <unistd.h> 61 62#include "extern.h" 63#include "vary.h" 64 65#ifndef TM_YEAR_BASE 66#define TM_YEAR_BASE 1900 67#endif 68 69static time_t tval; 70int retval; 71 72static void setthetime(const char *, const char *, int, int); 73static void badformat(void); 74static void usage(void); 75 76int 77main(int argc, char *argv[]) 78{ 79 struct timezone tz; 80 int ch, rflag; 81 int jflag, nflag; 82 const char *format; 83 char buf[1024]; 84 char *endptr, *fmt; 85 char *tmp; 86 int set_timezone; 87 struct vary *v; 88 const struct vary *badv; 89 struct tm lt; 90 91 v = NULL; 92 fmt = NULL; 93 (void) setlocale(LC_TIME, ""); 94 tz.tz_dsttime = tz.tz_minuteswest = 0; 95 rflag = 0; 96 jflag = nflag = 0; 97 set_timezone = 0; 98 while ((ch = getopt(argc, argv, "d:f:jnr:t:uv:")) != -1) 99 switch((char)ch) { 100 case 'd': /* daylight savings time */ 101 tz.tz_dsttime = strtol(optarg, &endptr, 10) ? 1 : 0; 102 if (endptr == optarg || *endptr != '\0') 103 usage(); 104 set_timezone = 1; 105 break; 106 case 'f': 107 fmt = optarg; 108 break; 109 case 'j': 110 jflag = 1; /* don't set time */ 111 break; 112 case 'n': /* don't set network */ 113 nflag = 1; 114 break; 115 case 'r': /* user specified seconds */ 116 rflag = 1; 117 tval = strtoq(optarg, &tmp, 0); 118 if (*tmp != 0) 119 usage(); 120 break; 121 case 't': /* minutes west of UTC */ 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 UTC */ 129 (void)setenv("TZ", "UTC0", 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 183static void 184setthetime(const char *fmt, const char *p, int jflag, int nflag) 185{ 186 struct tm *lt; 187 struct timeval tv; 188 const char *dot, *t; 189 int century; 190 191 if (fmt != NULL) { 192 lt = localtime(&tval); 193 t = strptime(p, fmt, lt); 194 if (t == NULL) { 195 fprintf(stderr, "Failed conversion of ``%s''" 196 " using format ``%s''\n", p, fmt); 197 badformat(); 198 } else if (*t != '\0') 199 fprintf(stderr, "Warning: Ignoring %ld extraneous" 200 " characters in date string (%s)\n", 201 (long) strlen(t), t); 202 } else { 203 for (t = p, dot = NULL; *t; ++t) { 204 if (isdigit(*t)) 205 continue; 206 if (*t == '.' && dot == NULL) { 207 dot = t; 208 continue; 209 } 210 badformat(); 211 } 212 213 lt = localtime(&tval); 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 { /* hack for 2000 ;-} */ 236 lt->tm_year = ATOI2(p); 237 if (lt->tm_year < 69) 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 /* Let mktime() decide whether summer time is in effect. */ 270 lt->tm_isdst = -1; 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 293static void 294badformat(void) 295{ 296 warnx("illegal time format"); 297 usage(); 298} 299 300static void 301usage(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