2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1989, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 static const char copyright[] =
34 "@(#) Copyright (c) 1989, 1993\n\
35 The Regents of the University of California. All rights reserved.\n";
40 static char sccsid[] = "@(#)calendar.c 8.3 (Berkeley) 3/25/94";
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
59 #define UTCOFFSET_NOTSET 100 /* Expected between -24 and +24 */
60 #define LONGITUDE_NOTSET 1000 /* Expected between -360 and +360 */
65 static char *DEBUG = NULL;
66 static time_t f_time = 0;
67 double UTCOffset = UTCOFFSET_NOTSET;
68 int EastLongitude = LONGITUDE_NOTSET;
70 static void usage(void) __dead2;
73 main(int argc, char *argv[])
75 int f_dayAfter = 0; /* days after current date */
76 int f_dayBefore = 0; /* days before current date */
77 int Friday = 5; /* day before weekend */
82 (void)setlocale(LC_ALL, "");
84 while ((ch = getopt(argc, argv, "-A:aB:D:dF:f:l:t:U:W:?")) != -1)
86 case '-': /* backward contemptible */
95 case 'W': /* we don't need no steenking Fridays */
99 case 'A': /* days after current date */
100 f_dayAfter = atoi(optarg);
102 errx(1, "number of days must be positive");
105 case 'B': /* days before current date */
106 f_dayBefore = atoi(optarg);
108 errx(1, "number of days must be positive");
111 case 'D': /* debug output of sun and moon info */
115 case 'd': /* debug output of current date */
119 case 'F': /* Change the time: When does weekend start? */
120 Friday = atoi(optarg);
123 case 'f': /* other calendar file */
124 calendarFile = optarg;
127 case 'l': /* Change longitudal position */
128 EastLongitude = strtol(optarg, NULL, 10);
131 case 't': /* other date, for tests */
132 f_time = Mktime(optarg);
135 case 'U': /* Change UTC offset */
136 UTCOffset = strtod(optarg, NULL);
150 /* use current time */
154 /* if not set, determine where I could be */
156 if (UTCOffset == UTCOFFSET_NOTSET &&
157 EastLongitude == LONGITUDE_NOTSET) {
158 /* Calculate on difference between here and UTC */
161 long utcoffset, hh, mm, ss;
165 localtime_r(&t, &tm);
166 utcoffset = tm.tm_gmtoff;
167 /* seconds -> hh:mm:ss */
168 hh = utcoffset / SECSPERHOUR;
169 utcoffset %= SECSPERHOUR;
170 mm = utcoffset / SECSPERMINUTE;
171 utcoffset %= SECSPERMINUTE;
174 /* hh:mm:ss -> hh.mmss */
175 uo = mm + (100.0 * (ss / 60.0));
180 EastLongitude = UTCOffset * 15;
181 } else if (UTCOffset == UTCOFFSET_NOTSET) {
182 /* Base on information given */
183 UTCOffset = EastLongitude / 15;
184 } else if (EastLongitude == LONGITUDE_NOTSET) {
185 /* Base on information given */
186 EastLongitude = UTCOffset * 15;
190 settimes(f_time, f_dayBefore, f_dayAfter, Friday, &tp1, &tp2);
191 generatedates(&tp1, &tp2);
194 * FROM now on, we are working in UTC.
195 * This will only affect moon and sun related events anyway.
197 if (setenv("TZ", "UTC", 1) != 0)
198 errx(1, "setenv: %s", strerror(errno));
210 while ((pw = getpwent()) != NULL) {
211 (void)setegid(pw->pw_gid);
212 (void)initgroups(pw->pw_name, pw->pw_gid);
213 (void)seteuid(pw->pw_uid);
214 if (!chdir(pw->pw_dir))
228 fprintf(stderr, "%s\n%s\n%s\n",
229 "usage: calendar [-A days] [-a] [-B days] [-D sun|moon] [-d]",
230 " [-F friday] [-f calendarfile] [-l longitude]",
231 " [-t dd[.mm[.year]]] [-U utcoffset] [-W days]"