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