date.c (c46940197cebd4c5cb0dc0f610ba16bc47721c55) date.c (7ca215a69037714e5f3cb65c394d3467621daa4c)
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

--- 15 unchanged lines hidden (view full) ---

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.
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

--- 15 unchanged lines hidden (view full) ---

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 * $Id: date.c,v 1.13 1997/06/06 06:34:37 charnier Exp $
32 */
33
34#ifndef lint
34 */
35
36#ifndef lint
35static char copyright[] =
37static char const copyright[] =
36"@(#) Copyright (c) 1985, 1987, 1988, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
38#endif /* not lint */
39
40#ifndef lint
38"@(#) Copyright (c) 1985, 1987, 1988, 1993\n\
39 The Regents of the University of California. All rights reserved.\n";
40#endif /* not lint */
41
42#ifndef lint
41static char sccsid[] = "@(#)date.c 8.2 (Berkeley) 4/28/95";
43static char const sccsid[] = "@(#)date.c 8.2 (Berkeley) 4/28/95";
42#endif /* not lint */
43
44#include <sys/param.h>
45#include <sys/time.h>
46
47#include <ctype.h>
48#include <err.h>
49#include <fcntl.h>
50#include <stdio.h>
51#include <stdlib.h>
52#include <string.h>
53#include <syslog.h>
54#include <unistd.h>
44#endif /* not lint */
45
46#include <sys/param.h>
47#include <sys/time.h>
48
49#include <ctype.h>
50#include <err.h>
51#include <fcntl.h>
52#include <stdio.h>
53#include <stdlib.h>
54#include <string.h>
55#include <syslog.h>
56#include <unistd.h>
57#include <locale.h>
55
56#include "extern.h"
58
59#include "extern.h"
60#include "vary.h"
57
58time_t tval;
59int retval, nflag;
60
61static void setthetime __P((char *));
62static void badformat __P((void));
63static void usage __P((void));
64

--- 4 unchanged lines hidden (view full) ---

69 int argc;
70 char **argv;
71{
72 extern int optind;
73 extern char *optarg;
74 struct timezone tz;
75 int ch, rflag;
76 char *format, buf[1024];
61
62time_t tval;
63int retval, nflag;
64
65static void setthetime __P((char *));
66static void badformat __P((void));
67static void usage __P((void));
68

--- 4 unchanged lines hidden (view full) ---

73 int argc;
74 char **argv;
75{
76 extern int optind;
77 extern char *optarg;
78 struct timezone tz;
79 int ch, rflag;
80 char *format, buf[1024];
81 char *endptr;
82 int set_timezone;
83 struct vary *v;
84 const struct vary *badv;
85 struct tm lt;
77
86
87 v = NULL;
88 (void) setlocale(LC_TIME, "");
78 tz.tz_dsttime = tz.tz_minuteswest = 0;
79 rflag = 0;
89 tz.tz_dsttime = tz.tz_minuteswest = 0;
90 rflag = 0;
80 while ((ch = getopt(argc, argv, "d:nr:ut:")) != -1)
91 set_timezone = 0;
92 while ((ch = getopt(argc, argv, "D:W:M:Y:d:nr:ut:")) != -1)
81 switch((char)ch) {
82 case 'd': /* daylight savings time */
93 switch((char)ch) {
94 case 'd': /* daylight savings time */
83 tz.tz_dsttime = atoi(optarg) ? 1 : 0;
95 tz.tz_dsttime = strtol(optarg, &endptr, 10) ? 1 : 0;
96 if (endptr == optarg || *endptr != '\0')
97 usage();
98 set_timezone = 1;
84 break;
85 case 'n': /* don't set network */
86 nflag = 1;
87 break;
88 case 'r': /* user specified seconds */
89 rflag = 1;
90 tval = atol(optarg);
91 break;
92 case 'u': /* do everything in GMT */
93 (void)setenv("TZ", "GMT0", 1);
94 break;
95 case 't': /* minutes west of GMT */
96 /* error check; don't allow "PST" */
99 break;
100 case 'n': /* don't set network */
101 nflag = 1;
102 break;
103 case 'r': /* user specified seconds */
104 rflag = 1;
105 tval = atol(optarg);
106 break;
107 case 'u': /* do everything in GMT */
108 (void)setenv("TZ", "GMT0", 1);
109 break;
110 case 't': /* minutes west of GMT */
111 /* error check; don't allow "PST" */
97 if (isdigit(*optarg)) {
98 tz.tz_minuteswest = atoi(optarg);
99 break;
100 }
101 /* FALLTHROUGH */
112 tz.tz_minuteswest = strtol(optarg, &endptr, 10);
113 if (endptr == optarg || *endptr != '\0')
114 usage();
115 set_timezone = 1;
116 break;
117 case 'D':
118 case 'W':
119 case 'M':
120 case 'Y':
121 v = vary_append(v, ch, optarg);
122 break;
102 default:
103 usage();
104 }
105 argc -= optind;
106 argv += optind;
107
108 /*
109 * If -d or -t, set the timezone or daylight savings time; this
123 default:
124 usage();
125 }
126 argc -= optind;
127 argv += optind;
128
129 /*
130 * If -d or -t, set the timezone or daylight savings time; this
110 * doesn't belong here, there kernel should not know about either.
131 * doesn't belong here; the kernel should not know about either.
111 */
132 */
112 if ((tz.tz_minuteswest || tz.tz_dsttime) &&
113 settimeofday(NULL, &tz))
114 err(1, "settimeofday");
133 if (set_timezone && settimeofday((struct timeval *)NULL, &tz))
134 err(1, "settimeofday (timezone)");
115
116 if (!rflag && time(&tval) == -1)
117 err(1, "time");
118
135
136 if (!rflag && time(&tval) == -1)
137 err(1, "time");
138
119 format = "%a %b %e %H:%M:%S %Z %Y";
139 format = "%+";
120
121 /* allow the operands in any order */
122 if (*argv && **argv == '+') {
123 format = *argv + 1;
124 ++argv;
125 }
126
127 if (*argv) {
128 setthetime(*argv);
129 ++argv;
130 }
131
132 if (*argv && **argv == '+')
133 format = *argv + 1;
134
140
141 /* allow the operands in any order */
142 if (*argv && **argv == '+') {
143 format = *argv + 1;
144 ++argv;
145 }
146
147 if (*argv) {
148 setthetime(*argv);
149 ++argv;
150 }
151
152 if (*argv && **argv == '+')
153 format = *argv + 1;
154
135 (void)strftime(buf, sizeof(buf), format, localtime(&tval));
155 lt = *localtime(&tval);
156 badv = vary_apply(v, &lt);
157 if (badv) {
158 fprintf(stderr, "-%c %s: Cannot apply date adjustment\n",
159 badv->flag, badv->arg);
160 vary_destroy(v);
161 usage();
162 }
163 vary_destroy(v);
164 (void)strftime(buf, sizeof(buf), format, &lt);
136 (void)printf("%s\n", buf);
137 exit(retval);
138}
139
140#define ATOI2(ar) ((ar)[0] - '0') * 10 + ((ar)[1] - '0'); (ar) += 2;
141void
142setthetime(p)
143 register char *p;

--- 52 unchanged lines hidden (view full) ---

196 badformat();
197 break;
198 default:
199 badformat();
200 }
201
202 /* convert broken-down time to GMT clock time */
203 if ((tval = mktime(lt)) == -1)
165 (void)printf("%s\n", buf);
166 exit(retval);
167}
168
169#define ATOI2(ar) ((ar)[0] - '0') * 10 + ((ar)[1] - '0'); (ar) += 2;
170void
171setthetime(p)
172 register char *p;

--- 52 unchanged lines hidden (view full) ---

225 badformat();
226 break;
227 default:
228 badformat();
229 }
230
231 /* convert broken-down time to GMT clock time */
232 if ((tval = mktime(lt)) == -1)
204 badformat();
233 errx(1, "nonexistent time");
205
206 /* set the time */
207 if (nflag || netsettime(tval)) {
208 logwtmp("|", "date", "");
209 tv.tv_sec = tval;
210 tv.tv_usec = 0;
234
235 /* set the time */
236 if (nflag || netsettime(tval)) {
237 logwtmp("|", "date", "");
238 tv.tv_sec = tval;
239 tv.tv_usec = 0;
211 if (settimeofday(&tv, NULL)) {
212 perror("date: settimeofday");
213 exit(1);
214 }
240 if (settimeofday(&tv, (struct timezone *)NULL))
241 err(1, "settimeofday (timeval)");
215 logwtmp("{", "date", "");
216 }
217
218 if ((p = getlogin()) == NULL)
219 p = "???";
220 syslog(LOG_AUTH | LOG_NOTICE, "date set by %s", p);
221}
222
223static void
224badformat()
225{
226 warnx("illegal time format");
227 usage();
228}
229
230static void
231usage()
232{
242 logwtmp("{", "date", "");
243 }
244
245 if ((p = getlogin()) == NULL)
246 p = "???";
247 syslog(LOG_AUTH | LOG_NOTICE, "date set by %s", p);
248}
249
250static void
251badformat()
252{
253 warnx("illegal time format");
254 usage();
255}
256
257static void
258usage()
259{
233 (void)fprintf(stderr,
234 "usage: date [-nu] [-d dst] [-r seconds] [-t west] [+format]\n");
235 (void)fprintf(stderr, " [yy[mm[dd[hh]]]]mm[.ss]]\n");
260 (void)fprintf(stderr, "%s\n%s\n",
261 "usage: date [-nu] [-d dst] [-r seconds] [-t west] [+format]",
262 " [-DWMY [+|-]val] [[[[yy]mm]dd]HH]MM[.ss]]");
236 exit(1);
237}
263 exit(1);
264}