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