2 * Copyright (c) 1989, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
31 static const char copyright[] =
32 "@(#) Copyright (c) 1989, 1993\n\
33 The Regents of the University of California. All rights reserved.\n";
38 static char sccsid[] = "@(#)calendar.c 8.3 (Berkeley) 3/25/94";
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD$");
57 #define UTCOFFSET_NOTSET 100 /* Expected between -24 and +24 */
58 #define LONGITUDE_NOTSET 1000 /* Expected between -360 and +360 */
65 double UTCOffset = UTCOFFSET_NOTSET;
66 int EastLongitude = LONGITUDE_NOTSET;
68 static void usage(void) __dead2;
71 main(int argc, char *argv[])
73 int f_dayAfter = 0; /* days after current date */
74 int f_dayBefore = 0; /* days before current date */
75 int Friday = 5; /* day before weekend */
80 (void)setlocale(LC_ALL, "");
82 while ((ch = getopt(argc, argv, "-A:aB:dD:F:f:l:t:U:W:")) != -1)
84 case '-': /* backward contemptible */
93 case 'f': /* other calendar file */
94 calendarFile = optarg;
97 case 'W': /* we don't need no steenking Fridays */
101 case 'A': /* days after current date */
102 f_dayAfter = atoi(optarg);
105 case 'B': /* days before current date */
106 f_dayBefore = atoi(optarg);
109 case 'F': /* Change the time: When does weekend start? */
110 Friday = atoi(optarg);
112 case 'l': /* Change longitudal position */
113 EastLongitude = strtol(optarg, NULL, 10);
115 case 'U': /* Change UTC offset */
116 UTCOffset = strtod(optarg, NULL);
125 case 't': /* other date, undocumented, for tests */
126 f_time = Mktime(optarg);
140 /* use current time */
144 /* if not set, determine where I could be */
146 if (UTCOffset == UTCOFFSET_NOTSET &&
147 EastLongitude == LONGITUDE_NOTSET) {
148 /* Calculate on difference between here and UTC */
151 long utcoffset, hh, mm, ss;
155 localtime_r(&t, &tm);
156 utcoffset = tm.tm_gmtoff;
157 /* seconds -> hh:mm:ss */
158 hh = utcoffset / SECSPERHOUR;
159 utcoffset %= SECSPERHOUR;
160 mm = utcoffset / SECSPERMINUTE;
161 utcoffset %= SECSPERMINUTE;
164 /* hh:mm:ss -> hh.mmss */
165 uo = mm + (100.0 * (ss / 60.0));
170 EastLongitude = UTCOffset * 15;
171 } else if (UTCOffset == UTCOFFSET_NOTSET) {
172 /* Base on information given */
173 UTCOffset = EastLongitude / 15;
174 } else if (EastLongitude == LONGITUDE_NOTSET) {
175 /* Base on information given */
176 EastLongitude = UTCOffset * 15;
180 settimes(f_time, f_dayBefore, f_dayAfter, Friday, &tp1, &tp2);
181 generatedates(&tp1, &tp2);
184 * FROM now on, we are working in UTC.
185 * This will only affect moon and sun related events anyway.
187 if (setenv("TZ", "UTC", 1) != 0)
188 errx(1, "setenv: %s", strerror(errno));
200 while ((pw = getpwent()) != NULL) {
201 (void)setegid(pw->pw_gid);
202 (void)initgroups(pw->pw_name, pw->pw_gid);
203 (void)seteuid(pw->pw_uid);
204 if (!chdir(pw->pw_dir))
218 fprintf(stderr, "%s\n%s\n%s\n",
219 "usage: calendar [-a] [-A days] [-B days] [-F friday] "
221 " [-d] [-t dd[.mm[.year]]] [-W days]",
222 " [-U utcoffset] [-l longitude]"