| 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 | * @(#) Copyright (c) 1985, 1987, 1988, 1993 The Regents of the University of California. All rights reserved. |
| 34 | * @(#)date.c 8.2 (Berkeley) 4/28/95 |
| 35 | * $FreeBSD: src/bin/date/date.c,v 1.32.2.6 2001/10/31 17:31:51 dillon Exp $ |
| 36 | * $DragonFly: src/bin/date/date.c,v 1.8 2004/11/07 20:54:51 eirikn Exp $ |
| 37 | */ |
| 38 | |
| 39 | #include <sys/param.h> |
| 40 | #include <sys/time.h> |
| 41 | |
| 42 | #include <ctype.h> |
| 43 | #include <err.h> |
| 44 | #include <stdio.h> |
| 45 | #include <stdlib.h> |
| 46 | #include <string.h> |
| 47 | #include <syslog.h> |
| 48 | #include <unistd.h> |
| 49 | #include <locale.h> |
| 50 | #include <libutil.h> |
| 51 | |
| 52 | #include "extern.h" |
| 53 | #include "vary.h" |
| 54 | |
| 55 | #ifndef TM_YEAR_BASE |
| 56 | #define TM_YEAR_BASE 1900 |
| 57 | #endif |
| 58 | |
| 59 | time_t tval; |
| 60 | int retval; |
| 61 | |
| 62 | static void setthetime (const char *, const char *, int, int); |
| 63 | static void badformat (void); |
| 64 | static void usage (void); |
| 65 | |
| 66 | int |
| 67 | main(int argc, char **argv) |
| 68 | { |
| 69 | struct timezone tz; |
| 70 | int ch, rflag; |
| 71 | int jflag, nflag; |
| 72 | const char *format; |
| 73 | char buf[1024]; |
| 74 | char *endptr, *fmt; |
| 75 | char *tmp; |
| 76 | int set_timezone; |
| 77 | struct vary *v; |
| 78 | const struct vary *badv; |
| 79 | struct tm lt; |
| 80 | |
| 81 | v = NULL; |
| 82 | fmt = NULL; |
| 83 | setlocale(LC_TIME, ""); |
| 84 | tz.tz_dsttime = tz.tz_minuteswest = 0; |
| 85 | rflag = 0; |
| 86 | jflag = nflag = 0; |
| 87 | set_timezone = 0; |
| 88 | while ((ch = getopt(argc, argv, "d:f:jnr:t:uv:")) != -1) |
| 89 | switch((char)ch) { |
| 90 | case 'd': /* daylight savings time */ |
| 91 | tz.tz_dsttime = strtol(optarg, &endptr, 10) ? 1 : 0; |
| 92 | if (endptr == optarg || *endptr != '\0') |
| 93 | usage(); |
| 94 | set_timezone = 1; |
| 95 | break; |
| 96 | case 'f': |
| 97 | fmt = optarg; |
| 98 | break; |
| 99 | case 'j': |
| 100 | jflag = 1; /* don't set time */ |
| 101 | break; |
| 102 | case 'n': /* don't set network */ |
| 103 | nflag = 1; |
| 104 | break; |
| 105 | case 'r': /* user specified seconds */ |
| 106 | rflag = 1; |
| 107 | tval = strtoll(optarg, &tmp, 0); |
| 108 | if (*tmp != 0) |
| 109 | usage(); |
| 110 | break; |
| 111 | case 't': /* minutes west of UTC */ |
| 112 | /* error check; don't allow "PST" */ |
| 113 | tz.tz_minuteswest = strtol(optarg, &endptr, 10); |
| 114 | if (endptr == optarg || *endptr != '\0') |
| 115 | usage(); |
| 116 | set_timezone = 1; |
| 117 | break; |
| 118 | case 'u': /* do everything in UTC */ |
| 119 | setenv("TZ", "UTC0", 1); |
| 120 | break; |
| 121 | case 'v': |
| 122 | v = vary_append(v, optarg); |
| 123 | break; |
| 124 | default: |
| 125 | usage(); |
| 126 | } |
| 127 | argc -= optind; |
| 128 | argv += optind; |
| 129 | |
| 130 | /* |
| 131 | * If -d or -t, set the timezone or daylight savings time; this |
| 132 | * doesn't belong here; the kernel should not know about either. |
| 133 | */ |
| 134 | if (set_timezone && settimeofday((struct timeval *)NULL, &tz)) |
| 135 | err(1, "settimeofday (timezone)"); |
| 136 | |
| 137 | if (!rflag && time(&tval) == -1) |
| 138 | err(1, "time"); |
| 139 | |
| 140 | format = "%+"; |
| 141 | |
| 142 | /* allow the operands in any order */ |
| 143 | if (*argv && **argv == '+') { |
| 144 | format = *argv + 1; |
| 145 | ++argv; |
| 146 | } |
| 147 | |
| 148 | if (*argv) { |
| 149 | setthetime(fmt, *argv, jflag, nflag); |
| 150 | ++argv; |
| 151 | } else if (fmt != NULL) |
| 152 | usage(); |
| 153 | |
| 154 | if (*argv && **argv == '+') |
| 155 | format = *argv + 1; |
| 156 | |
| 157 | lt = *localtime(&tval); |
| 158 | badv = vary_apply(v, <); |
| 159 | if (badv) { |
| 160 | fprintf(stderr, "%s: Cannot apply date adjustment\n", |
| 161 | badv->arg); |
| 162 | vary_destroy(v); |
| 163 | usage(); |
| 164 | } |
| 165 | vary_destroy(v); |
| 166 | strftime(buf, sizeof(buf), format, <); |
| 167 | printf("%s\n", buf); |
| 168 | exit(retval); |
| 169 | } |
| 170 | |
| 171 | #define ATOI2(s) ((s) += 2, ((s)[-2] - '0') * 10 + ((s)[-1] - '0')) |
| 172 | |
| 173 | static void |
| 174 | setthetime(const char *fmt, const char *p, int jflag, int nflag) |
| 175 | { |
| 176 | struct tm *lt; |
| 177 | struct timeval tv; |
| 178 | const char *dot, *t; |
| 179 | int century; |
| 180 | |
| 181 | if (fmt != NULL) { |
| 182 | lt = localtime(&tval); |
| 183 | t = strptime(p, fmt, lt); |
| 184 | if (t == NULL) { |
| 185 | fprintf(stderr, "Failed conversion of ``%s''" |
| 186 | " using format ``%s''\n", p, fmt); |
| 187 | badformat(); |
| 188 | } else if (*t != '\0') |
| 189 | fprintf(stderr, "Warning: Ignoring %ld extraneous" |
| 190 | " characters in date string (%s)\n", |
| 191 | (long) strlen(t), t); |
| 192 | } else { |
| 193 | for (t = p, dot = NULL; *t; ++t) { |
| 194 | if (isdigit(*t)) |
| 195 | continue; |
| 196 | if (*t == '.' && dot == NULL) { |
| 197 | dot = t; |
| 198 | continue; |
| 199 | } |
| 200 | badformat(); |
| 201 | } |
| 202 | |
| 203 | lt = localtime(&tval); |
| 204 | |
| 205 | if (dot != NULL) { /* .ss */ |
| 206 | dot++; /* *dot++ = '\0'; */ |
| 207 | if (strlen(dot) != 2) |
| 208 | badformat(); |
| 209 | lt->tm_sec = ATOI2(dot); |
| 210 | if (lt->tm_sec > 61) |
| 211 | badformat(); |
| 212 | } else |
| 213 | lt->tm_sec = 0; |
| 214 | |
| 215 | century = 0; |
| 216 | /* if p has a ".ss" field then let's pretend it's not there */ |
| 217 | switch (strlen(p) - ((dot != NULL) ? 3 : 0)) { |
| 218 | case 12: /* cc */ |
| 219 | lt->tm_year = ATOI2(p) * 100 - TM_YEAR_BASE; |
| 220 | century = 1; |
| 221 | /* FALLTHROUGH */ |
| 222 | case 10: /* yy */ |
| 223 | if (century) |
| 224 | lt->tm_year += ATOI2(p); |
| 225 | else { /* hack for 2000 ;-} */ |
| 226 | lt->tm_year = ATOI2(p); |
| 227 | if (lt->tm_year < 69) |
| 228 | lt->tm_year += 2000 - TM_YEAR_BASE; |
| 229 | else |
| 230 | lt->tm_year += 1900 - TM_YEAR_BASE; |
| 231 | } |
| 232 | /* FALLTHROUGH */ |
| 233 | case 8: /* mm */ |
| 234 | lt->tm_mon = ATOI2(p); |
| 235 | if (lt->tm_mon > 12) |
| 236 | badformat(); |
| 237 | --lt->tm_mon; /* time struct is 0 - 11 */ |
| 238 | /* FALLTHROUGH */ |
| 239 | case 6: /* dd */ |
| 240 | lt->tm_mday = ATOI2(p); |
| 241 | if (lt->tm_mday > 31) |
| 242 | badformat(); |
| 243 | /* FALLTHROUGH */ |
| 244 | case 4: /* HH */ |
| 245 | lt->tm_hour = ATOI2(p); |
| 246 | if (lt->tm_hour > 23) |
| 247 | badformat(); |
| 248 | /* FALLTHROUGH */ |
| 249 | case 2: /* MM */ |
| 250 | lt->tm_min = ATOI2(p); |
| 251 | if (lt->tm_min > 59) |
| 252 | badformat(); |
| 253 | break; |
| 254 | default: |
| 255 | badformat(); |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | /* Let mktime() decide whether summer time is in effect. */ |
| 260 | lt->tm_isdst = -1; |
| 261 | |
| 262 | /* convert broken-down time to GMT clock time */ |
| 263 | if ((tval = mktime(lt)) == -1) |
| 264 | errx(1, "nonexistent time"); |
| 265 | |
| 266 | if (!jflag) { |
| 267 | /* set the time */ |
| 268 | if (nflag || netsettime(tval)) { |
| 269 | logwtmp("|", "date", ""); |
| 270 | tv.tv_sec = tval; |
| 271 | tv.tv_usec = 0; |
| 272 | if (settimeofday(&tv, (struct timezone *)NULL)) |
| 273 | err(1, "settimeofday (timeval)"); |
| 274 | logwtmp("{", "date", ""); |
| 275 | } |
| 276 | |
| 277 | if ((p = getlogin()) == NULL) |
| 278 | p = "???"; |
| 279 | syslog(LOG_AUTH | LOG_NOTICE, "date set by %s", p); |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | static void |
| 284 | badformat(void) |
| 285 | { |
| 286 | warnx("illegal time format"); |
| 287 | usage(); |
| 288 | } |
| 289 | |
| 290 | static void |
| 291 | usage(void) |
| 292 | { |
| 293 | fprintf(stderr, "%s\n%s\n", |
| 294 | "usage: date [-jnu] [-d dst] [-r seconds] [-t west] " |
| 295 | "[-v[+|-]val[ymwdHMS]] ... ", |
| 296 | " " |
| 297 | "[-f fmt date | [[[[[cc]yy]mm]dd]HH]MM[.ss]] [+format]"); |
| 298 | exit(1); |
| 299 | } |