]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - bin/date/date.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / bin / date / date.c
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  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29
30 #ifndef lint
31 static char const copyright[] =
32 "@(#) Copyright (c) 1985, 1987, 1988, 1993\n\
33         The Regents of the University of California.  All rights reserved.\n";
34 #endif /* not lint */
35
36 #if 0
37 #ifndef lint
38 static char sccsid[] = "@(#)date.c      8.2 (Berkeley) 4/28/95";
39 #endif /* not lint */
40 #endif
41
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD$");
44
45 #include <sys/param.h>
46 #include <sys/time.h>
47
48 #include <ctype.h>
49 #include <err.h>
50 #include <locale.h>
51 #include <libutil.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 #include "vary.h"
60
61 #ifndef TM_YEAR_BASE
62 #define TM_YEAR_BASE    1900
63 #endif
64
65 static time_t tval;
66 int retval;
67
68 static void setthetime(const char *, const char *, int, int);
69 static void badformat(void);
70 static void usage(void);
71
72 int
73 main(int argc, char *argv[])
74 {
75         struct timezone tz;
76         int ch, rflag;
77         int jflag, nflag;
78         const char *format;
79         char buf[1024];
80         char *endptr, *fmt;
81         char *tmp;
82         int set_timezone;
83         struct vary *v;
84         const struct vary *badv;
85         struct tm lt;
86
87         v = NULL;
88         fmt = NULL;
89         (void) setlocale(LC_TIME, "");
90         tz.tz_dsttime = tz.tz_minuteswest = 0;
91         rflag = 0;
92         jflag = nflag = 0;
93         set_timezone = 0;
94         while ((ch = getopt(argc, argv, "d:f:jnr:t:uv:")) != -1)
95                 switch((char)ch) {
96                 case 'd':               /* daylight savings time */
97                         tz.tz_dsttime = strtol(optarg, &endptr, 10) ? 1 : 0;
98                         if (endptr == optarg || *endptr != '\0')
99                                 usage();
100                         set_timezone = 1;
101                         break;
102                 case 'f':
103                         fmt = optarg;
104                         break;
105                 case 'j':
106                         jflag = 1;      /* don't set time */
107                         break;
108                 case 'n':               /* don't set network */
109                         nflag = 1;
110                         break;
111                 case 'r':               /* user specified seconds */
112                         rflag = 1;
113                         tval = strtoq(optarg, &tmp, 0);
114                         if (*tmp != 0)
115                                 usage();
116                         break;
117                 case 't':               /* minutes west of UTC */
118                                         /* error check; don't allow "PST" */
119                         tz.tz_minuteswest = strtol(optarg, &endptr, 10);
120                         if (endptr == optarg || *endptr != '\0')
121                                 usage();
122                         set_timezone = 1;
123                         break;
124                 case 'u':               /* do everything in UTC */
125                         (void)setenv("TZ", "UTC0", 1);
126                         break;
127                 case 'v':
128                         v = vary_append(v, optarg);
129                         break;
130                 default:
131                         usage();
132                 }
133         argc -= optind;
134         argv += optind;
135
136         /*
137          * If -d or -t, set the timezone or daylight savings time; this
138          * doesn't belong here; the kernel should not know about either.
139          */
140         if (set_timezone && settimeofday((struct timeval *)NULL, &tz))
141                 err(1, "settimeofday (timezone)");
142
143         if (!rflag && time(&tval) == -1)
144                 err(1, "time");
145
146         format = "%+";
147
148         /* allow the operands in any order */
149         if (*argv && **argv == '+') {
150                 format = *argv + 1;
151                 ++argv;
152         }
153
154         if (*argv) {
155                 setthetime(fmt, *argv, jflag, nflag);
156                 ++argv;
157         } else if (fmt != NULL)
158                 usage();
159
160         if (*argv && **argv == '+')
161                 format = *argv + 1;
162
163         lt = *localtime(&tval);
164         badv = vary_apply(v, &lt);
165         if (badv) {
166                 fprintf(stderr, "%s: Cannot apply date adjustment\n",
167                         badv->arg);
168                 vary_destroy(v);
169                 usage();
170         }
171         vary_destroy(v);
172         (void)strftime(buf, sizeof(buf), format, &lt);
173         (void)printf("%s\n", buf);
174         if (fflush(stdout))
175                 err(1, "stdout");
176         exit(retval);
177 }
178
179 #define ATOI2(s)        ((s) += 2, ((s)[-2] - '0') * 10 + ((s)[-1] - '0'))
180
181 static void
182 setthetime(const char *fmt, const char *p, int jflag, int nflag)
183 {
184         struct tm *lt;
185         struct timeval tv;
186         const char *dot, *t;
187         int century;
188
189         lt = localtime(&tval);
190         lt->tm_isdst = -1;              /* divine correct DST */
191
192         if (fmt != NULL) {
193                 t = strptime(p, fmt, lt);
194                 if (t == NULL) {
195                         fprintf(stderr, "Failed conversion of ``%s''"
196                                 " using format ``%s''\n", p, fmt);
197                         badformat();
198                 } else if (*t != '\0')
199                         fprintf(stderr, "Warning: Ignoring %ld extraneous"
200                                 " characters in date string (%s)\n",
201                                 (long) strlen(t), t);
202         } else {
203                 for (t = p, dot = NULL; *t; ++t) {
204                         if (isdigit(*t))
205                                 continue;
206                         if (*t == '.' && dot == NULL) {
207                                 dot = t;
208                                 continue;
209                         }
210                         badformat();
211                 }
212
213                 if (dot != NULL) {                      /* .ss */
214                         dot++; /* *dot++ = '\0'; */
215                         if (strlen(dot) != 2)
216                                 badformat();
217                         lt->tm_sec = ATOI2(dot);
218                         if (lt->tm_sec > 61)
219                                 badformat();
220                 } else
221                         lt->tm_sec = 0;
222
223                 century = 0;
224                 /* if p has a ".ss" field then let's pretend it's not there */
225                 switch (strlen(p) - ((dot != NULL) ? 3 : 0)) {
226                 case 12:                                /* cc */
227                         lt->tm_year = ATOI2(p) * 100 - TM_YEAR_BASE;
228                         century = 1;
229                         /* FALLTHROUGH */
230                 case 10:                                /* yy */
231                         if (century)
232                                 lt->tm_year += ATOI2(p);
233                         else {
234                                 lt->tm_year = ATOI2(p);
235                                 if (lt->tm_year < 69)   /* hack for 2000 ;-} */
236                                         lt->tm_year += 2000 - TM_YEAR_BASE;
237                                 else
238                                         lt->tm_year += 1900 - TM_YEAR_BASE;
239                         }
240                         /* FALLTHROUGH */
241                 case 8:                                 /* mm */
242                         lt->tm_mon = ATOI2(p);
243                         if (lt->tm_mon > 12)
244                                 badformat();
245                         --lt->tm_mon;           /* time struct is 0 - 11 */
246                         /* FALLTHROUGH */
247                 case 6:                                 /* dd */
248                         lt->tm_mday = ATOI2(p);
249                         if (lt->tm_mday > 31)
250                                 badformat();
251                         /* FALLTHROUGH */
252                 case 4:                                 /* HH */
253                         lt->tm_hour = ATOI2(p);
254                         if (lt->tm_hour > 23)
255                                 badformat();
256                         /* FALLTHROUGH */
257                 case 2:                                 /* MM */
258                         lt->tm_min = ATOI2(p);
259                         if (lt->tm_min > 59)
260                                 badformat();
261                         break;
262                 default:
263                         badformat();
264                 }
265         }
266
267         /* convert broken-down time to GMT clock time */
268         if ((tval = mktime(lt)) == -1)
269                 errx(1, "nonexistent time");
270
271         if (!jflag) {
272                 /* set the time */
273                 if (nflag || netsettime(tval)) {
274                         logwtmp("|", "date", "");
275                         tv.tv_sec = tval;
276                         tv.tv_usec = 0;
277                         if (settimeofday(&tv, (struct timezone *)NULL))
278                                 err(1, "settimeofday (timeval)");
279                         logwtmp("{", "date", "");
280                 }
281
282                 if ((p = getlogin()) == NULL)
283                         p = "???";
284                 syslog(LOG_AUTH | LOG_NOTICE, "date set by %s", p);
285         }
286 }
287
288 static void
289 badformat(void)
290 {
291         warnx("illegal time format");
292         usage();
293 }
294
295 static void
296 usage(void)
297 {
298         (void)fprintf(stderr, "%s\n%s\n",
299             "usage: date [-jnu] [-d dst] [-r seconds] [-t west] "
300             "[-v[+|-]val[ymwdHMS]] ... ",
301             "            "
302             "[-f fmt date | [[[[[cc]yy]mm]dd]HH]MM[.ss]] [+format]");
303         exit(1);
304 }