calendar.c revision 205872
10Sstevel@tonic-gate/*-
20Sstevel@tonic-gate * Copyright (c) 1989, 1993, 1994
30Sstevel@tonic-gate *	The Regents of the University of California.  All rights reserved.
40Sstevel@tonic-gate *
50Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
60Sstevel@tonic-gate * modification, are permitted provided that the following conditions
70Sstevel@tonic-gate * are met:
80Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright
90Sstevel@tonic-gate *    notice, this list of conditions and the following disclaimer.
100Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
110Sstevel@tonic-gate *    notice, this list of conditions and the following disclaimer in the
120Sstevel@tonic-gate *    documentation and/or other materials provided with the distribution.
130Sstevel@tonic-gate * 4. Neither the name of the University nor the names of its contributors
140Sstevel@tonic-gate *    may be used to endorse or promote products derived from this software
150Sstevel@tonic-gate *    without specific prior written permission.
160Sstevel@tonic-gate *
170Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
180Sstevel@tonic-gate * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
190Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
200Sstevel@tonic-gate * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
210Sstevel@tonic-gate * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
220Sstevel@tonic-gate * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
230Sstevel@tonic-gate * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
240Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
250Sstevel@tonic-gate * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
260Sstevel@tonic-gate * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
270Sstevel@tonic-gate * SUCH DAMAGE.
280Sstevel@tonic-gate */
290Sstevel@tonic-gate
300Sstevel@tonic-gate#ifndef lint
310Sstevel@tonic-gatestatic const char copyright[] =
320Sstevel@tonic-gate"@(#) Copyright (c) 1989, 1993\n\
330Sstevel@tonic-gate	The Regents of the University of California.  All rights reserved.\n";
340Sstevel@tonic-gate#endif
350Sstevel@tonic-gate
360Sstevel@tonic-gate#if 0
370Sstevel@tonic-gate#ifndef lint
380Sstevel@tonic-gatestatic char sccsid[] = "@(#)calendar.c  8.3 (Berkeley) 3/25/94";
390Sstevel@tonic-gate#endif
400Sstevel@tonic-gate#endif
410Sstevel@tonic-gate
420Sstevel@tonic-gate#include <sys/cdefs.h>
430Sstevel@tonic-gate__FBSDID("$FreeBSD: head/usr.bin/calendar/calendar.c 205872 2010-03-30 06:42:01Z edwin $");
440Sstevel@tonic-gate
450Sstevel@tonic-gate#include <err.h>
460Sstevel@tonic-gate#include <errno.h>
470Sstevel@tonic-gate#include <locale.h>
480Sstevel@tonic-gate#include <pwd.h>
490Sstevel@tonic-gate#include <stdio.h>
500Sstevel@tonic-gate#include <stdlib.h>
510Sstevel@tonic-gate#include <string.h>
520Sstevel@tonic-gate#include <time.h>
530Sstevel@tonic-gate#include <unistd.h>
540Sstevel@tonic-gate
550Sstevel@tonic-gate#include "calendar.h"
560Sstevel@tonic-gate
570Sstevel@tonic-gate#define	UTCOFFSET_NOTSET	100	/* Expected between -24 and +24 */
580Sstevel@tonic-gate#define	LONGITUDE_NOTSET	1000	/* Expected between -360 and +360 */
590Sstevel@tonic-gate
600Sstevel@tonic-gatestruct passwd	*pw;
610Sstevel@tonic-gateint		doall = 0;
620Sstevel@tonic-gateint		debug = 0;
630Sstevel@tonic-gatechar		*DEBUG = NULL;
640Sstevel@tonic-gatetime_t		f_time = 0;
650Sstevel@tonic-gatedouble		UTCOffset = UTCOFFSET_NOTSET;
660Sstevel@tonic-gateint		EastLongitude = LONGITUDE_NOTSET;
670Sstevel@tonic-gate
680Sstevel@tonic-gatestatic void	usage(void) __dead2;
690Sstevel@tonic-gate
700Sstevel@tonic-gateint
710Sstevel@tonic-gatemain(int argc, char *argv[])
720Sstevel@tonic-gate{
730Sstevel@tonic-gate	int	f_dayAfter = 0;		/* days after current date */
740Sstevel@tonic-gate	int	f_dayBefore = 0;	/* days before current date */
750Sstevel@tonic-gate	int	Friday = 5;		/* day before weekend */
760Sstevel@tonic-gate
770Sstevel@tonic-gate	int ch;
780Sstevel@tonic-gate	struct tm tp1, tp2;
790Sstevel@tonic-gate
800Sstevel@tonic-gate	(void)setlocale(LC_ALL, "");
810Sstevel@tonic-gate
820Sstevel@tonic-gate	while ((ch = getopt(argc, argv, "-A:aB:dD:F:f:l:t:U:W:")) != -1)
830Sstevel@tonic-gate		switch (ch) {
840Sstevel@tonic-gate		case '-':		/* backward contemptible */
850Sstevel@tonic-gate		case 'a':
860Sstevel@tonic-gate			if (getuid()) {
870Sstevel@tonic-gate				errno = EPERM;
880Sstevel@tonic-gate				err(1, NULL);
890Sstevel@tonic-gate			}
900Sstevel@tonic-gate			doall = 1;
910Sstevel@tonic-gate			break;
920Sstevel@tonic-gate
930Sstevel@tonic-gate		case 'f': /* other calendar file */
940Sstevel@tonic-gate			calendarFile = optarg;
950Sstevel@tonic-gate			break;
960Sstevel@tonic-gate
970Sstevel@tonic-gate		case 'W': /* we don't need no steenking Fridays */
98			Friday = -1;
99			/* FALLTHROUGH */
100
101		case 'A': /* days after current date */
102			f_dayAfter = atoi(optarg);
103			break;
104
105		case 'B': /* days before current date */
106			f_dayBefore = atoi(optarg);
107			break;
108
109		case 'F': /* Change the time: When does weekend start? */
110			Friday = atoi(optarg);
111			break;
112		case 'l': /* Change longitudal position */
113			EastLongitude = strtol(optarg, NULL, 10);
114			break;
115		case 'U': /* Change UTC offset */
116			UTCOffset = strtod(optarg, NULL);
117			break;
118
119		case 'd':
120			debug = 1;
121			break;
122		case 'D':
123			DEBUG = optarg;
124			break;
125		case 't': /* other date, undocumented, for tests */
126			f_time = Mktime(optarg);
127			break;
128
129		case '?':
130		default:
131			usage();
132		}
133
134	argc -= optind;
135	argv += optind;
136
137	if (argc)
138		usage();
139
140	/* use current time */
141	if (f_time <= 0)
142		(void)time(&f_time);
143
144	/* if not set, determine where I could be */
145	{
146		if (UTCOffset == UTCOFFSET_NOTSET &&
147		    EastLongitude == LONGITUDE_NOTSET) {
148			/* Calculate on difference between here and UTC */
149			time_t t;
150			struct tm tm;
151			long utcoffset, hh, mm, ss;
152			double uo;
153
154			time(&t);
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;
162			ss = utcoffset;
163
164			/* hh:mm:ss -> hh.mmss */
165			uo = mm + (100.0 * (ss / 60.0));
166			uo /=  60.0 / 100.0;
167			uo = hh + uo / 100;
168
169			UTCOffset = uo;
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;
177		}
178	}
179
180	settimes(f_time, f_dayBefore, f_dayAfter, Friday, &tp1, &tp2);
181	generatedates(&tp1, &tp2);
182
183	/*
184	 * FROM now on, we are working in UTC.
185	 * This will only affect moon and sun related events anyway.
186	 */
187	if (setenv("TZ", "UTC", 1) != 0)
188		errx(1, "setenv: %s", strerror(errno));
189	tzset();
190
191	if (debug)
192		dumpdates();
193
194	if (DEBUG != NULL) {
195		dodebug(DEBUG);
196		exit(0);
197	}
198
199	if (doall)
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))
205				cal();
206			(void)seteuid(0);
207		}
208	else
209		cal();
210	exit(0);
211}
212
213
214static void __dead2
215usage(void)
216{
217
218	fprintf(stderr, "%s\n%s\n%s\n",
219	    "usage: calendar [-a] [-A days] [-B days] [-F friday] "
220	    "[-f calendarfile]",
221	    "                [-d] [-t dd[.mm[.year]]] [-W days]",
222	    "                [-U utcoffset] [-l longitude]"
223	    );
224	exit(1);
225}
226