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