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 * $Id: date.c,v 1.2 1994/09/24 02:54:36 davidg Exp $ 34 */ 35 36 #ifndef lint 37 static char copyright[] = 38 "@(#) Copyright (c) 1985, 1987, 1988, 1993\n\ 39 The Regents of the University of California. All rights reserved.\n"; 40 #endif /* not lint */ 41 42 #ifndef lint 43 static char sccsid[] = "@(#)date.c 8.1 (Berkeley) 5/31/93"; 44 #endif /* not lint */ 45 46 #include <sys/param.h> 47 #include <sys/time.h> 48 49 #include <ctype.h> 50 #include <err.h> 51 #include <fcntl.h> 52 #include <stdio.h> 53 #include <stdlib.h> 54 #include <string.h> 55 #include <syslog.h> 56 #include <unistd.h> 57 58 #include "extern.h" 59 60 time_t tval; 61 int retval, nflag; 62 63 static void setthetime __P((char *)); 64 static void badformat __P((void)); 65 static void usage __P((void)); 66 67 int logwtmp __P((char *, char *, char *)); 68 69 int 70 main(argc, argv) 71 int argc; 72 char **argv; 73 { 74 extern int optind; 75 extern char *optarg; 76 struct timezone tz; 77 int ch, rflag; 78 char *format, buf[1024]; 79 char *endptr; 80 int set_timezone; 81 82 tz.tz_dsttime = tz.tz_minuteswest = 0; 83 rflag = 0; 84 set_timezone = 0; 85 while ((ch = getopt(argc, argv, "d:nr:ut:")) != EOF) 86 switch((char)ch) { 87 case 'd': /* daylight savings time */ 88 tz.tz_dsttime = strtol(optarg, &endptr, 10) ? 1 : 0; 89 if (endptr == optarg || *endptr != '\0') 90 usage(); 91 set_timezone = 1; 92 break; 93 case 'n': /* don't set network */ 94 nflag = 1; 95 break; 96 case 'r': /* user specified seconds */ 97 rflag = 1; 98 tval = atol(optarg); 99 break; 100 case 'u': /* do everything in GMT */ 101 (void)setenv("TZ", "GMT0", 1); 102 break; 103 case 't': /* minutes west of GMT */ 104 /* error check; don't allow "PST" */ 105 tz.tz_minuteswest = strtol(optarg, &endptr, 10); 106 if (endptr == optarg || *endptr != '\0') 107 usage(); 108 set_timezone = 1; 109 break; 110 default: 111 usage(); 112 } 113 argc -= optind; 114 argv += optind; 115 116 /* 117 * If -d or -t, set the timezone or daylight savings time; this 118 * doesn't belong here, there kernel should not know about either. 119 */ 120 if (set_timezone && settimeofday((struct timeval *)NULL, &tz)) 121 err(1, "settimeofday (timezone)"); 122 123 if (!rflag && time(&tval) == -1) 124 err(1, "time"); 125 126 format = "%a %b %e %H:%M:%S %Z %Y\n"; 127 128 /* allow the operands in any order */ 129 if (*argv && **argv == '+') { 130 format = *argv + 1; 131 ++argv; 132 } 133 134 if (*argv) { 135 setthetime(*argv); 136 ++argv; 137 } 138 139 if (*argv && **argv == '+') 140 format = *argv + 1; 141 142 (void)strftime(buf, sizeof(buf), format, localtime(&tval)); 143 (void)printf("%s", buf); 144 exit(retval); 145 } 146 147 #define ATOI2(ar) ((ar)[0] - '0') * 10 + ((ar)[1] - '0'); (ar) += 2; 148 void 149 setthetime(p) 150 register char *p; 151 { 152 register struct tm *lt; 153 struct timeval tv; 154 char *dot, *t; 155 156 for (t = p, dot = NULL; *t; ++t) { 157 if (isdigit(*t)) 158 continue; 159 if (*t == '.' && dot == NULL) { 160 dot = t; 161 continue; 162 } 163 badformat(); 164 } 165 166 lt = localtime(&tval); 167 168 if (dot != NULL) { /* .ss */ 169 *dot++ = '\0'; 170 if (strlen(dot) != 2) 171 badformat(); 172 lt->tm_sec = ATOI2(dot); 173 if (lt->tm_sec > 61) 174 badformat(); 175 } else 176 lt->tm_sec = 0; 177 178 switch (strlen(p)) { 179 case 10: /* yy */ 180 lt->tm_year = ATOI2(p); 181 if (lt->tm_year < 69) /* hack for 2000 ;-} */ 182 lt->tm_year += 100; 183 /* FALLTHROUGH */ 184 case 8: /* mm */ 185 lt->tm_mon = ATOI2(p); 186 if (lt->tm_mon > 12) 187 badformat(); 188 --lt->tm_mon; /* time struct is 0 - 11 */ 189 /* FALLTHROUGH */ 190 case 6: /* dd */ 191 lt->tm_mday = ATOI2(p); 192 if (lt->tm_mday > 31) 193 badformat(); 194 /* FALLTHROUGH */ 195 case 4: /* hh */ 196 lt->tm_hour = ATOI2(p); 197 if (lt->tm_hour > 23) 198 badformat(); 199 /* FALLTHROUGH */ 200 case 2: /* mm */ 201 lt->tm_min = ATOI2(p); 202 if (lt->tm_min > 59) 203 badformat(); 204 break; 205 default: 206 badformat(); 207 } 208 209 /* convert broken-down time to GMT clock time */ 210 if ((tval = mktime(lt)) == -1) 211 badformat(); 212 213 /* set the time */ 214 if (nflag || netsettime(tval)) { 215 logwtmp("|", "date", ""); 216 tv.tv_sec = tval; 217 tv.tv_usec = 0; 218 if (settimeofday(&tv, (struct timezone *)NULL)) 219 err(1, "settimeofday (timeval)"); 220 logwtmp("{", "date", ""); 221 } 222 223 if ((p = getlogin()) == NULL) 224 p = "???"; 225 syslog(LOG_AUTH | LOG_NOTICE, "date set by %s", p); 226 } 227 228 static void 229 badformat() 230 { 231 warnx("illegal time format"); 232 usage(); 233 } 234 235 static void 236 usage() 237 { 238 (void)fprintf(stderr, 239 "usage: date [-nu] [-d dst] [-r seconds] [-t west] [+format]\n"); 240 (void)fprintf(stderr, " [yy[mm[dd[hh]]]]mm[.ss]]\n"); 241 exit(1); 242 } 243