xref: /freebsd/usr.bin/calendar/calendar.c (revision f6a3b357e9be4c6423c85eff9a847163a0d307c8)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1989, 1993, 1994
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
18  *
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
29  * SUCH DAMAGE.
30  */
31 
32 #ifndef lint
33 static const char copyright[] =
34 "@(#) Copyright (c) 1989, 1993\n\
35 	The Regents of the University of California.  All rights reserved.\n";
36 #endif
37 
38 #if 0
39 #ifndef lint
40 static char sccsid[] = "@(#)calendar.c	8.3 (Berkeley) 3/25/94";
41 #endif
42 #endif
43 
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
46 
47 #include <err.h>
48 #include <errno.h>
49 #include <locale.h>
50 #include <langinfo.h>
51 #include <pwd.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <time.h>
56 #include <unistd.h>
57 
58 #include "calendar.h"
59 
60 #define	UTCOFFSET_NOTSET	100	/* Expected between -24 and +24 */
61 #define	LONGITUDE_NOTSET	1000	/* Expected between -360 and +360 */
62 
63 struct passwd	*pw;
64 int		doall = 0;
65 int		debug = 0;
66 static char	*DEBUG = NULL;
67 static time_t	f_time = 0;
68 double		UTCOffset = UTCOFFSET_NOTSET;
69 int		EastLongitude = LONGITUDE_NOTSET;
70 #ifdef WITH_ICONV
71 const char	*outputEncoding;
72 #endif
73 
74 static void	usage(void) __dead2;
75 
76 int
77 main(int argc, char *argv[])
78 {
79 	int	f_dayAfter = 0;		/* days after current date */
80 	int	f_dayBefore = 0;	/* days before current date */
81 	int	Friday = 5;		/* day before weekend */
82 
83 	int ch;
84 	struct tm tp1, tp2;
85 
86 	(void)setlocale(LC_ALL, "");
87 #ifdef WITH_ICONV
88 	/* save the information about the encoding used in the terminal */
89 	outputEncoding = strdup(nl_langinfo(CODESET));
90 	if (outputEncoding == NULL)
91 		errx(1, "cannot allocate memory");
92 #endif
93 
94 	while ((ch = getopt(argc, argv, "-A:aB:D:dF:f:l:t:U:W:?")) != -1)
95 		switch (ch) {
96 		case '-':		/* backward contemptible */
97 		case 'a':
98 			if (getuid()) {
99 				errno = EPERM;
100 				err(1, NULL);
101 			}
102 			doall = 1;
103 			break;
104 
105 		case 'W': /* we don't need no steenking Fridays */
106 			Friday = -1;
107 			/* FALLTHROUGH */
108 
109 		case 'A': /* days after current date */
110 			f_dayAfter = atoi(optarg);
111 			if (f_dayAfter < 0)
112 				errx(1, "number of days must be positive");
113 			break;
114 
115 		case 'B': /* days before current date */
116 			f_dayBefore = atoi(optarg);
117 			if (f_dayBefore < 0)
118 				errx(1, "number of days must be positive");
119 			break;
120 
121 		case 'D': /* debug output of sun and moon info */
122 			DEBUG = optarg;
123 			break;
124 
125 		case 'd': /* debug output of current date */
126 			debug = 1;
127 			break;
128 
129 		case 'F': /* Change the time: When does weekend start? */
130 			Friday = atoi(optarg);
131 			break;
132 
133 		case 'f': /* other calendar file */
134 			calendarFile = optarg;
135 			break;
136 
137 		case 'l': /* Change longitudal position */
138 			EastLongitude = strtol(optarg, NULL, 10);
139 			break;
140 
141 		case 't': /* other date, for tests */
142 			f_time = Mktime(optarg);
143 			break;
144 
145 		case 'U': /* Change UTC offset */
146 			UTCOffset = strtod(optarg, NULL);
147 			break;
148 
149 		case '?':
150 		default:
151 			usage();
152 		}
153 
154 	argc -= optind;
155 	argv += optind;
156 
157 	if (argc)
158 		usage();
159 
160 	/* use current time */
161 	if (f_time <= 0)
162 		(void)time(&f_time);
163 
164 	/* if not set, determine where I could be */
165 	{
166 		if (UTCOffset == UTCOFFSET_NOTSET &&
167 		    EastLongitude == LONGITUDE_NOTSET) {
168 			/* Calculate on difference between here and UTC */
169 			time_t t;
170 			struct tm tm;
171 			long utcoffset, hh, mm, ss;
172 			double uo;
173 
174 			time(&t);
175 			localtime_r(&t, &tm);
176 			utcoffset = tm.tm_gmtoff;
177 			/* seconds -> hh:mm:ss */
178 			hh = utcoffset / SECSPERHOUR;
179 			utcoffset %= SECSPERHOUR;
180 			mm = utcoffset / SECSPERMINUTE;
181 			utcoffset %= SECSPERMINUTE;
182 			ss = utcoffset;
183 
184 			/* hh:mm:ss -> hh.mmss */
185 			uo = mm + (100.0 * (ss / 60.0));
186 			uo /=  60.0 / 100.0;
187 			uo = hh + uo / 100;
188 
189 			UTCOffset = uo;
190 			EastLongitude = UTCOffset * 15;
191 		} else if (UTCOffset == UTCOFFSET_NOTSET) {
192 			/* Base on information given */
193 			UTCOffset = EastLongitude / 15;
194 		} else if (EastLongitude == LONGITUDE_NOTSET) {
195 			/* Base on information given */
196 			EastLongitude = UTCOffset * 15;
197 		}
198 	}
199 
200 	settimes(f_time, f_dayBefore, f_dayAfter, Friday, &tp1, &tp2);
201 	generatedates(&tp1, &tp2);
202 
203 	/*
204 	 * FROM now on, we are working in UTC.
205 	 * This will only affect moon and sun related events anyway.
206 	 */
207 	if (setenv("TZ", "UTC", 1) != 0)
208 		errx(1, "setenv: %s", strerror(errno));
209 	tzset();
210 
211 	if (debug)
212 		dumpdates();
213 
214 	if (DEBUG != NULL) {
215 		dodebug(DEBUG);
216 		exit(0);
217 	}
218 
219 	if (doall)
220 		while ((pw = getpwent()) != NULL) {
221 			(void)setegid(pw->pw_gid);
222 			(void)initgroups(pw->pw_name, pw->pw_gid);
223 			(void)seteuid(pw->pw_uid);
224 			if (!chdir(pw->pw_dir))
225 				cal();
226 			(void)seteuid(0);
227 		}
228 	else
229 		cal();
230 	exit(0);
231 }
232 
233 
234 static void __dead2
235 usage(void)
236 {
237 
238 	fprintf(stderr, "%s\n%s\n%s\n",
239 	    "usage: calendar [-A days] [-a] [-B days] [-D sun|moon] [-d]",
240 	    "		     [-F friday] [-f calendarfile] [-l longitude]",
241 	    "		     [-t dd[.mm[.year]]] [-U utcoffset] [-W days]"
242 	    );
243 	exit(1);
244 }
245