Merge branch 'vendor/LESS'
[dragonfly.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  * @(#) Copyright (c) 1985, 1987, 1988, 1993 The Regents of the University of California.  All rights reserved.
30  * @(#)date.c   8.2 (Berkeley) 4/28/95
31  * $FreeBSD: src/bin/date/date.c,v 1.47 2005/01/10 08:39:21 imp Exp $
32  * $DragonFly: src/bin/date/date.c,v 1.14 2006/01/12 13:43:10 corecode Exp $
33  */
34
35 #include <sys/param.h>
36 #include <sys/time.h>
37
38 #include <ctype.h>
39 #include <err.h>
40 #include <langinfo.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <syslog.h>
45 #include <unistd.h>
46 #include <locale.h>
47 #include <libutil.h>
48
49 #include "extern.h"
50 #include "vary.h"
51
52 #ifndef TM_YEAR_BASE
53 #define TM_YEAR_BASE    1900
54 #endif
55
56 static time_t tval;
57 int retval;
58
59 static void setthetime(const char *, const char *, int, int);
60 static void badformat(void);
61 static void usage(void);
62
63 int
64 main(int argc, char **argv)
65 {
66         struct timezone tz;
67         int ch, rflag;
68         int jflag, nflag;
69         const char *format;
70         char buf[1024];
71         char *endptr, *fmt;
72         char *tmp;
73         int set_timezone;
74         struct vary *v;
75         const struct vary *badv;
76         struct tm lt;
77
78         v = NULL;
79         fmt = NULL;
80         setlocale(LC_TIME, "");
81         tz.tz_dsttime = tz.tz_minuteswest = 0;
82         rflag = 0;
83         jflag = nflag = 0;
84         set_timezone = 0;
85         while ((ch = getopt(argc, argv, "d:f:jnr:t:uv:")) != -1)
86                 switch(ch) {
87                 case 'd':               /* daylight savings time */
88                         tz.tz_dsttime = strtol(optarg, &endptr, 10) ? 1 : 0;
89                         if (endptr == optarg || *endptr != '\0')
90                                 usage();
91                         set_timezone = 1;
92                         break;
93                 case 'f':
94                         fmt = optarg;
95                         break;
96                 case 'j':
97                         jflag = 1;      /* don't set time */
98                         break;
99                 case 'n':               /* don't set network */
100                         nflag = 1;
101                         break;
102                 case 'r':               /* user specified seconds */
103                         rflag = 1;
104                         tval = strtoll(optarg, &tmp, 0);
105                         if (*tmp != 0)
106                                 usage();
107                         break;
108                 case 't':               /* minutes west of UTC */
109                                         /* error check; don't allow "PST" */
110                         tz.tz_minuteswest = strtol(optarg, &endptr, 10);
111                         if (endptr == optarg || *endptr != '\0')
112                                 usage();
113                         set_timezone = 1;
114                         break;
115                 case 'u':               /* do everything in UTC */
116                         if (setenv("TZ", "UTC0", 1) != 0)
117                                 err(1, "setenv: cannot set TZ=UTC0");
118                         break;
119                 case 'v':
120                         v = vary_append(v, optarg);
121                         break;
122                 default:
123                         usage();
124                 }
125         argc -= optind;
126         argv += optind;
127
128         /*
129          * If -d or -t, set the timezone or daylight savings time; this
130          * doesn't belong here; the kernel should not know about either.
131          */
132         if (set_timezone && settimeofday(NULL, &tz))
133                 err(1, "settimeofday (timezone)");
134
135         if (!rflag && time(&tval) == -1)
136                 err(1, "time");
137
138         format = nl_langinfo(_DATE_FMT);
139
140         /* allow the operands in any order */
141         if (*argv && **argv == '+') {
142                 format = *argv + 1;
143                 ++argv;
144         }
145
146         if (*argv) {
147                 setthetime(fmt, *argv, jflag, nflag);
148                 ++argv;
149         } else if (fmt != NULL)
150                 usage();
151
152         if (*argv && **argv == '+')
153                 format = *argv + 1;
154
155         lt = *localtime(&tval);
156         badv = vary_apply(v, &lt);
157         if (badv) {
158                 fprintf(stderr, "%s: Cannot apply date adjustment\n",
159                         badv->arg);
160                 vary_destroy(v);
161                 usage();
162         }
163         vary_destroy(v);
164         strftime(buf, sizeof(buf), format, &lt);
165         printf("%s\n", buf);
166         if (fflush(stdout) != 0)
167                 err(1, "stdout");
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, 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 }