calendar.c revision 13840
150276Speter/*
2166124Srafan * Copyright (c) 1989, 1993, 1994
350276Speter *	The Regents of the University of California.  All rights reserved.
450276Speter *
550276Speter * Redistribution and use in source and binary forms, with or without
650276Speter * modification, are permitted provided that the following conditions
750276Speter * are met:
850276Speter * 1. Redistributions of source code must retain the above copyright
950276Speter *    notice, this list of conditions and the following disclaimer.
1050276Speter * 2. Redistributions in binary form must reproduce the above copyright
1150276Speter *    notice, this list of conditions and the following disclaimer in the
1250276Speter *    documentation and/or other materials provided with the distribution.
1350276Speter * 3. All advertising materials mentioning features or use of this software
1450276Speter *    must display the following acknowledgement:
1550276Speter *	This product includes software developed by the University of
1650276Speter *	California, Berkeley and its contributors.
1750276Speter * 4. Neither the name of the University nor the names of its contributors
1850276Speter *    may be used to endorse or promote products derived from this software
1950276Speter *    without specific prior written permission.
2050276Speter *
2150276Speter * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2250276Speter * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2350276Speter * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2450276Speter * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2550276Speter * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2650276Speter * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2750276Speter * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2850276Speter * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29166124Srafan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3050276Speter * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31166124Srafan * SUCH DAMAGE.
32166124Srafan */
3350276Speter
34166124Srafan#ifndef lint
35166124Srafanstatic char copyright[] =
36166124Srafan"@(#) Copyright (c) 1989, 1993\n\
37166124Srafan	The Regents of the University of California.  All rights reserved.\n";
38166124Srafan#endif /* not lint */
39166124Srafan
40166124Srafan#ifndef lint
41166124Srafanstatic char sccsid[] = "@(#)calendar.c	8.3 (Berkeley) 3/25/94";
42166124Srafan#endif /* not lint */
43166124Srafan
44166124Srafan#include <pwd.h>
45166124Srafan#include <stdio.h>
46166124Srafan#include <unistd.h>
47166124Srafan#include <errno.h>
48166124Srafan#include <err.h>
49166124Srafan#include <stdlib.h>
5050276Speter#include <time.h>
51166124Srafan
52166124Srafan#include "pathnames.h"
5350276Speter#include "calendar.h"
5450276Speter
55166124Srafanstruct passwd *pw;
5650276Speterint doall = 0;
5750276Spetertime_t f_time = 0;
5850276Speter
5950276Speterint f_dayAfter = 0; /* days after current date */
6050276Speterint f_dayBefore = 0; /* days before current date */
6150276Speter
6250276Speterint
6350276Spetermain(argc, argv)
6450276Speter	int argc;
6550276Speter	char *argv[];
6650276Speter{
6750276Speter	extern int optind;
6850276Speter	int ch;
6950276Speter
7050276Speter	while ((ch = getopt(argc, argv, "?-af:t:A:B:")) != EOF)
7150276Speter		switch (ch) {
7250276Speter		case '-':		/* backward contemptible */
7350276Speter		case 'a':
7450276Speter			if (getuid()) {
7550276Speter				errno = EPERM;
7650276Speter				err(1, NULL);
7750276Speter			}
7850276Speter			doall = 1;
7950276Speter			break;
8050276Speter
8150276Speter
8250276Speter		case 'f': /* other calendar file */
8350276Speter		        calendarFile = optarg;
8450276Speter			break;
8550276Speter
8650276Speter		case 't': /* other date, undocumented, for tests */
8750276Speter			f_time = Mktime (optarg);
8850276Speter			break;
8950276Speter
9050276Speter		case 'A': /* days after current date */
9150276Speter			f_dayAfter = atoi(optarg);
9250276Speter			break;
9350276Speter
9450276Speter		case 'B': /* days before current date */
9550276Speter			f_dayBefore = atoi(optarg);
9650276Speter			break;
97166124Srafan
9850276Speter		case '?':
9950276Speter		default:
10050276Speter			usage();
10150276Speter		}
10250276Speter	argc -= optind;
10350276Speter	argv += optind;
104166124Srafan
10550276Speter	if (argc)
10650276Speter		usage();
10750276Speter
10850276Speter	/* use current time */
10950276Speter	if (f_time <= 0)
11050276Speter	    (void)time(&f_time);
11150276Speter
11250276Speter	settime(f_time);
11350276Speter
114166124Srafan	if (doall)
11550276Speter		while ((pw = getpwent()) != NULL) {
11650276Speter			(void)setegid(pw->pw_gid);
11750276Speter			(void)seteuid(pw->pw_uid);
11850276Speter			if (!chdir(pw->pw_dir))
11950276Speter				cal();
12050276Speter			(void)seteuid(0);
121166124Srafan		}
12250276Speter	else
12350276Speter		cal();
12450276Speter	exit(0);
12550276Speter}
12650276Speter
12750276Speter
12850276Spetervoid
129166124Srafanusage()
13050276Speter{
13150276Speter	(void)fprintf(stderr,
13250276Speter		      "usage: calendar [-a] [-A days] [-B days] [-f calendarfile]\n");
13350276Speter	exit(1);
13450276Speter}
13550276Speter
13650276Speter
13750276Speter