date.c revision 48214
1275970Scy/* 2275970Scy * Copyright (c) 1985, 1987, 1988, 1993 3275970Scy * The Regents of the University of California. All rights reserved. 4275970Scy * 5275970Scy * Redistribution and use in source and binary forms, with or without 6285612Sdelphij * modification, are permitted provided that the following conditions 7275970Scy * are met: 8275970Scy * 1. Redistributions of source code must retain the above copyright 9275970Scy * notice, this list of conditions and the following disclaimer. 10275970Scy * 2. Redistributions in binary form must reproduce the above copyright 11275970Scy * notice, this list of conditions and the following disclaimer in the 12275970Scy * documentation and/or other materials provided with the distribution. 13275970Scy * 3. All advertising materials mentioning features or use of this software 14275970Scy * must display the following acknowledgement: 15275970Scy * This product includes software developed by the University of 16275970Scy * California, Berkeley and its contributors. 17275970Scy * 4. Neither the name of the University nor the names of its contributors 18275970Scy * may be used to endorse or promote products derived from this software 19275970Scy * without specific prior written permission. 20275970Scy * 21275970Scy * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22275970Scy * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23275970Scy * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24275970Scy * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25275970Scy * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26275970Scy * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27275970Scy * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28275970Scy * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29275970Scy * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30275970Scy * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31275970Scy * SUCH DAMAGE. 32275970Scy */ 33275970Scy 34275970Scy#ifndef lint 35275970Scystatic char const copyright[] = 36275970Scy"@(#) Copyright (c) 1985, 1987, 1988, 1993\n\ 37275970Scy The Regents of the University of California. All rights reserved.\n"; 38275970Scy#endif /* not lint */ 39275970Scy 40275970Scy#ifndef lint 41275970Scy#if 0 42275970Scystatic char sccsid[] = "@(#)date.c 8.2 (Berkeley) 4/28/95"; 43275970Scy#endif 44275970Scystatic const char rcsid[] = 45275970Scy "$Id: date.c,v 1.28 1999/05/14 00:28:41 jmg Exp $"; 46275970Scy#endif /* not lint */ 47275970Scy 48275970Scy#include <sys/param.h> 49275970Scy#include <sys/time.h> 50275970Scy 51275970Scy#include <ctype.h> 52275970Scy#include <err.h> 53275970Scy#include <stdio.h> 54285612Sdelphij#include <stdlib.h> 55285612Sdelphij#include <string.h> 56275970Scy#include <syslog.h> 57275970Scy#include <unistd.h> 58275970Scy#include <locale.h> 59275970Scy 60275970Scy#include "extern.h" 61275970Scy#include "vary.h" 62275970Scy 63275970Scytime_t tval; 64275970Scyint retval; 65275970Scy 66275970Scystatic void setthetime __P((const char *, const char *, int, int)); 67275970Scystatic void badformat __P((void)); 68275970Scystatic void usage __P((void)); 69275970Scy 70275970Scyint logwtmp __P((char *, char *, char *)); 71275970Scy 72275970Scyint 73275970Scymain(argc, argv) 74275970Scy int argc; 75275970Scy char **argv; 76275970Scy{ 77275970Scy extern int optind; 78275970Scy extern char *optarg; 79275970Scy struct timezone tz; 80275970Scy int ch, rflag; 81275970Scy int jflag, nflag; 82275970Scy char *format, buf[1024]; 83275970Scy char *endptr, *fmt; 84275970Scy int set_timezone; 85275970Scy struct vary *v; 86275970Scy const struct vary *badv; 87275970Scy struct tm lt; 88275970Scy 89275970Scy v = NULL; 90275970Scy fmt = NULL; 91285612Sdelphij (void) setlocale(LC_TIME, ""); 92275970Scy tz.tz_dsttime = tz.tz_minuteswest = 0; 93275970Scy rflag = 0; 94275970Scy jflag = nflag = 0; 95275970Scy set_timezone = 0; 96275970Scy while ((ch = getopt(argc, argv, "d:f:jnr:t:uv:")) != -1) 97275970Scy switch((char)ch) { 98275970Scy case 'd': /* daylight savings time */ 99275970Scy tz.tz_dsttime = strtol(optarg, &endptr, 10) ? 1 : 0; 100275970Scy if (endptr == optarg || *endptr != '\0') 101275970Scy usage(); 102275970Scy set_timezone = 1; 103275970Scy break; 104275970Scy case 'f': 105275970Scy fmt = optarg; 106275970Scy break; 107275970Scy case 'j': 108275970Scy jflag = 1; /* don't set time */ 109275970Scy break; 110275970Scy case 'n': /* don't set network */ 111275970Scy nflag = 1; 112275970Scy break; 113275970Scy case 'r': /* user specified seconds */ 114275970Scy rflag = 1; 115275970Scy tval = atol(optarg); 116275970Scy break; 117275970Scy case 't': /* minutes west of GMT */ 118275970Scy /* error check; don't allow "PST" */ 119275970Scy tz.tz_minuteswest = strtol(optarg, &endptr, 10); 120275970Scy if (endptr == optarg || *endptr != '\0') 121275970Scy usage(); 122275970Scy set_timezone = 1; 123275970Scy break; 124275970Scy case 'u': /* do everything in GMT */ 125275970Scy (void)setenv("TZ", "GMT0", 1); 126275970Scy break; 127275970Scy case 'v': 128275970Scy v = vary_append(v, optarg); 129275970Scy break; 130275970Scy default: 131275970Scy usage(); 132275970Scy } 133275970Scy argc -= optind; 134275970Scy argv += optind; 135275970Scy 136285612Sdelphij /* 137275970Scy * If -d or -t, set the timezone or daylight savings time; this 138275970Scy * doesn't belong here; the kernel should not know about either. 139275970Scy */ 140275970Scy if (set_timezone && settimeofday((struct timeval *)NULL, &tz)) 141275970Scy err(1, "settimeofday (timezone)"); 142275970Scy 143275970Scy if (!rflag && time(&tval) == -1) 144275970Scy err(1, "time"); 145275970Scy 146275970Scy format = "%+"; 147275970Scy 148275970Scy /* allow the operands in any order */ 149275970Scy if (*argv && **argv == '+') { 150275970Scy format = *argv + 1; 151275970Scy ++argv; 152275970Scy } 153275970Scy 154275970Scy if (*argv) { 155275970Scy setthetime(fmt, *argv, jflag, nflag); 156275970Scy ++argv; 157275970Scy } else if (fmt != NULL) 158275970Scy usage(); 159275970Scy 160275970Scy if (*argv && **argv == '+') 161275970Scy format = *argv + 1; 162275970Scy 163275970Scy lt = *localtime(&tval); 164275970Scy badv = vary_apply(v, <); 165275970Scy if (badv) { 166275970Scy fprintf(stderr, "%s: Cannot apply date adjustment\n", 167275970Scy badv->arg); 168275970Scy vary_destroy(v); 169275970Scy usage(); 170275970Scy } 171275970Scy vary_destroy(v); 172275970Scy (void)strftime(buf, sizeof(buf), format, <); 173 (void)printf("%s\n", buf); 174 exit(retval); 175} 176 177#define ATOI2(ar) ((ar)[0] - '0') * 10 + ((ar)[1] - '0'); (ar) += 2; 178void 179setthetime(fmt, p, jflag, nflag) 180 const char *fmt; 181 register const char *p; 182 int jflag, nflag; 183{ 184 register struct tm *lt; 185 struct timeval tv; 186 const char *dot, *t; 187 188 if (fmt != NULL) { 189 lt = localtime(&tval); 190 t = strptime(p, fmt, lt); 191 if (t == NULL) { 192 fprintf(stderr, "Failed conversion of ``%s''" 193 " using format ``%s''\n", p, fmt); 194 badformat(); 195 } else if (*t != '\0') 196 fprintf(stderr, "Warning: Ignoring %ld extraneous" 197 " characters in date string (%s)\n", 198 (long) strlen(t), t); 199 } else { 200 for (t = p, dot = NULL; *t; ++t) { 201 if (isdigit(*t)) 202 continue; 203 if (*t == '.' && dot == NULL) { 204 dot = t; 205 continue; 206 } 207 badformat(); 208 } 209 210 lt = localtime(&tval); 211 212 if (dot != NULL) { /* .ss */ 213 dot++; /* *dot++ = '\0'; */ 214 if (strlen(dot) != 2) 215 badformat(); 216 lt->tm_sec = ATOI2(dot); 217 if (lt->tm_sec > 61) 218 badformat(); 219 } else 220 lt->tm_sec = 0; 221 222 /* if p has a ".ss" field then let's pretend it's not there */ 223 switch (strlen(p) - ((dot != NULL) ? 3 : 0)) { 224 case 10: /* yy */ 225 lt->tm_year = ATOI2(p); 226 if (lt->tm_year < 69) /* hack for 2000 ;-} */ 227 lt->tm_year += 100; 228 /* FALLTHROUGH */ 229 case 8: /* mm */ 230 lt->tm_mon = ATOI2(p); 231 if (lt->tm_mon > 12) 232 badformat(); 233 --lt->tm_mon; /* time struct is 0 - 11 */ 234 /* FALLTHROUGH */ 235 case 6: /* dd */ 236 lt->tm_mday = ATOI2(p); 237 if (lt->tm_mday > 31) 238 badformat(); 239 /* FALLTHROUGH */ 240 case 4: /* HH */ 241 lt->tm_hour = ATOI2(p); 242 if (lt->tm_hour > 23) 243 badformat(); 244 /* FALLTHROUGH */ 245 case 2: /* MM */ 246 lt->tm_min = ATOI2(p); 247 if (lt->tm_min > 59) 248 badformat(); 249 break; 250 default: 251 badformat(); 252 } 253 } 254 255 /* convert broken-down time to GMT clock time */ 256 if ((tval = mktime(lt)) == -1) 257 errx(1, "nonexistent time"); 258 259 if (!jflag) { 260 /* set the time */ 261 if (nflag || netsettime(tval)) { 262 logwtmp("|", "date", ""); 263 tv.tv_sec = tval; 264 tv.tv_usec = 0; 265 if (settimeofday(&tv, (struct timezone *)NULL)) 266 err(1, "settimeofday (timeval)"); 267 logwtmp("{", "date", ""); 268 } 269 270 if ((p = getlogin()) == NULL) 271 p = "???"; 272 syslog(LOG_AUTH | LOG_NOTICE, "date set by %s", p); 273 } 274} 275 276static void 277badformat() 278{ 279 warnx("illegal time format"); 280 usage(); 281} 282 283static void 284usage() 285{ 286 (void)fprintf(stderr, "%s\n%s\n", 287 "usage: date [-nu] [-d dst] [-r seconds] [-t west] " 288 "[-v[+|-]val[ymwdHMS]] ... ", 289 " [-f fmt date | [[[[yy]mm]dd]HH]MM[.ss]] [+format]"); 290 exit(1); 291} 292