xref: /freebsd/usr.bin/calendar/calendar.c (revision 1843dfb05ed80149f5a412180af882e3cb8f451b)
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 #include <sys/types.h>
46 #include <err.h>
47 #include <errno.h>
48 #include <locale.h>
49 #include <login_cap.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 = NULL;
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 
88 	while ((ch = getopt(argc, argv, "-A:aB:D:dF:f:l:t:U:W:?")) != -1)
89 		switch (ch) {
90 		case '-':		/* backward contemptible */
91 		case 'a':
92 			if (getuid()) {
93 				errno = EPERM;
94 				err(1, NULL);
95 			}
96 			doall = 1;
97 			break;
98 
99 		case 'W': /* we don't need no steenking Fridays */
100 			Friday = -1;
101 			/* FALLTHROUGH */
102 
103 		case 'A': /* days after current date */
104 			f_dayAfter = atoi(optarg);
105 			if (f_dayAfter < 0)
106 				errx(1, "number of days must be positive");
107 			break;
108 
109 		case 'B': /* days before current date */
110 			f_dayBefore = atoi(optarg);
111 			if (f_dayBefore < 0)
112 				errx(1, "number of days must be positive");
113 			break;
114 
115 		case 'D': /* debug output of sun and moon info */
116 			DEBUG = optarg;
117 			break;
118 
119 		case 'd': /* debug output of current date */
120 			debug = 1;
121 			break;
122 
123 		case 'F': /* Change the time: When does weekend start? */
124 			Friday = atoi(optarg);
125 			break;
126 
127 		case 'f': /* other calendar file */
128 			calendarFile = optarg;
129 			break;
130 
131 		case 'l': /* Change longitudal position */
132 			EastLongitude = strtol(optarg, NULL, 10);
133 			break;
134 
135 		case 't': /* other date, for tests */
136 			f_time = Mktime(optarg);
137 			break;
138 
139 		case 'U': /* Change UTC offset */
140 			UTCOffset = strtod(optarg, NULL);
141 			break;
142 
143 		case '?':
144 		default:
145 			usage();
146 		}
147 
148 	argc -= optind;
149 	argv += optind;
150 
151 	if (argc)
152 		usage();
153 
154 	/* use current time */
155 	if (f_time <= 0)
156 		(void)time(&f_time);
157 
158 	/* if not set, determine where I could be */
159 	{
160 		if (UTCOffset == UTCOFFSET_NOTSET &&
161 		    EastLongitude == LONGITUDE_NOTSET) {
162 			/* Calculate on difference between here and UTC */
163 			time_t t;
164 			struct tm tm;
165 			long utcoffset, hh, mm, ss;
166 			double uo;
167 
168 			time(&t);
169 			localtime_r(&t, &tm);
170 			utcoffset = tm.tm_gmtoff;
171 			/* seconds -> hh:mm:ss */
172 			hh = utcoffset / SECSPERHOUR;
173 			utcoffset %= SECSPERHOUR;
174 			mm = utcoffset / SECSPERMINUTE;
175 			utcoffset %= SECSPERMINUTE;
176 			ss = utcoffset;
177 
178 			/* hh:mm:ss -> hh.mmss */
179 			uo = mm + (100.0 * (ss / 60.0));
180 			uo /=  60.0 / 100.0;
181 			uo = hh + uo / 100;
182 
183 			UTCOffset = uo;
184 			EastLongitude = UTCOffset * 15;
185 		} else if (UTCOffset == UTCOFFSET_NOTSET) {
186 			/* Base on information given */
187 			UTCOffset = EastLongitude / 15;
188 		} else if (EastLongitude == LONGITUDE_NOTSET) {
189 			/* Base on information given */
190 			EastLongitude = UTCOffset * 15;
191 		}
192 	}
193 
194 	settimes(f_time, f_dayBefore, f_dayAfter, Friday, &tp1, &tp2);
195 	generatedates(&tp1, &tp2);
196 
197 	/*
198 	 * FROM now on, we are working in UTC.
199 	 * This will only affect moon and sun related events anyway.
200 	 */
201 	if (setenv("TZ", "UTC", 1) != 0)
202 		errx(1, "setenv: %s", strerror(errno));
203 	tzset();
204 
205 	if (debug)
206 		dumpdates();
207 
208 	if (DEBUG != NULL) {
209 		dodebug(DEBUG);
210 		exit(0);
211 	}
212 
213 	if (doall)
214 		while ((pw = getpwent()) != NULL) {
215 			pid_t pid;
216 
217 			if (chdir(pw->pw_dir) == -1)
218 				continue;
219 			pid = fork();
220 			if (pid < 0)
221 				err(1, "fork");
222 			if (pid == 0) {
223 				login_cap_t *lc;
224 
225 				lc = login_getpwclass(pw);
226 				if (setusercontext(lc, pw, pw->pw_uid,
227 				    LOGIN_SETALL) != 0)
228 					errx(1, "setusercontext");
229 				setenv("HOME", pw->pw_dir, 1);
230 				cal();
231 				exit(0);
232 			}
233 		}
234 	else {
235 #ifdef WITH_ICONV
236 		/* Save the information about the encoding used in the terminal. */
237 		outputEncoding = strdup(nl_langinfo(CODESET));
238 		if (outputEncoding == NULL)
239 			errx(1, "cannot allocate memory");
240 #endif
241 		cal();
242 	}
243 	exit(0);
244 }
245 
246 
247 static void __dead2
248 usage(void)
249 {
250 
251 	fprintf(stderr, "%s\n%s\n%s\n",
252 	    "usage: calendar [-A days] [-a] [-B days] [-D sun|moon] [-d]",
253 	    "		     [-F friday] [-f calendarfile] [-l longitude]",
254 	    "		     [-t dd[.mm[.year]]] [-U utcoffset] [-W days]"
255 	    );
256 	exit(1);
257 }
258