calendar.c revision 22323
193383Smdodd/*
293383Smdodd * Copyright (c) 1989, 1993, 1994
321826Sjoerg *	The Regents of the University of California.  All rights reserved.
421826Sjoerg *
521826Sjoerg * Redistribution and use in source and binary forms, with or without
621826Sjoerg * modification, are permitted provided that the following conditions
721826Sjoerg * are met:
821826Sjoerg * 1. Redistributions of source code must retain the above copyright
921826Sjoerg *    notice, this list of conditions and the following disclaimer.
1021826Sjoerg * 2. Redistributions in binary form must reproduce the above copyright
1121826Sjoerg *    notice, this list of conditions and the following disclaimer in the
1221826Sjoerg *    documentation and/or other materials provided with the distribution.
1393383Smdodd * 3. All advertising materials mentioning features or use of this software
1421826Sjoerg *    must display the following acknowledgement:
1521826Sjoerg *	This product includes software developed by the University of
1621826Sjoerg *	California, Berkeley and its contributors.
1721826Sjoerg * 4. Neither the name of the University nor the names of its contributors
1821826Sjoerg *    may be used to endorse or promote products derived from this software
1921826Sjoerg *    without specific prior written permission.
2021826Sjoerg *
2121826Sjoerg * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2221826Sjoerg * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2321826Sjoerg * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2421826Sjoerg * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2521826Sjoerg * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2693383Smdodd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2721826Sjoerg * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2821826Sjoerg * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29119418Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30119418Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31119418Sobrien * SUCH DAMAGE.
3221826Sjoerg */
3321826Sjoerg
3421826Sjoerg#ifndef lint
3521826Sjoergstatic const char copyright[] =
3621826Sjoerg"@(#) Copyright (c) 1989, 1993\n\
3721826Sjoerg	The Regents of the University of California.  All rights reserved.\n";
3821826Sjoerg#endif /* not lint */
3921826Sjoerg
4093383Smdodd#ifndef lint
4132350Seivindstatic const char sccsid[] = "@(#)calendar.c  8.3 (Berkeley) 3/25/94";
4221826Sjoerg#endif /* not lint */
4393383Smdodd
4493383Smdodd#include <err.h>
4593383Smdodd#include <errno.h>
4693383Smdodd#include <locale.h>
4793383Smdodd#include <pwd.h>
4821826Sjoerg#include <stdio.h>
4924204Sbde#include <stdlib.h>
5021826Sjoerg#include <time.h>
5193383Smdodd#include <unistd.h>
5293383Smdodd
5393383Smdodd#include "pathnames.h"
5493383Smdodd#include "calendar.h"
5593383Smdodd
5693383Smdoddstruct passwd *pw;
5793383Smdoddint doall = 0;
5821826Sjoergtime_t f_time = 0;
5993383Smdodd
6021826Sjoergint f_dayAfter = 0; /* days after current date */
6193383Smdoddint f_dayBefore = 0; /* days before current date */
62147256Sbrooks
6393383Smdoddint
6421826Sjoergmain(argc, argv)
6521826Sjoerg	int argc;
6621826Sjoerg	char *argv[];
6793383Smdodd{
6893383Smdodd	extern int optind;
6921826Sjoerg	int ch;
7093383Smdodd
7121826Sjoerg	(void) setlocale(LC_ALL, "");
7221826Sjoerg
7321826Sjoerg	while ((ch = getopt(argc, argv, "?-af:t:A:B:")) != EOF)
7421826Sjoerg		switch (ch) {
7521826Sjoerg		case '-':		/* backward contemptible */
76147256Sbrooks		case 'a':
77147256Sbrooks			if (getuid()) {
78147256Sbrooks				errno = EPERM;
7921826Sjoerg				err(1, NULL);
8021826Sjoerg			}
8121826Sjoerg			doall = 1;
8221826Sjoerg			break;
83147256Sbrooks
8421826Sjoerg
8521826Sjoerg		case 'f': /* other calendar file */
8621826Sjoerg		        calendarFile = optarg;
8721826Sjoerg			break;
8821826Sjoerg
8921826Sjoerg		case 't': /* other date, undocumented, for tests */
9021826Sjoerg			f_time = Mktime (optarg);
91147256Sbrooks			break;
9221826Sjoerg
9321826Sjoerg		case 'A': /* days after current date */
9421826Sjoerg			f_dayAfter = atoi(optarg);
9521826Sjoerg			break;
9621826Sjoerg
9793383Smdodd		case 'B': /* days before current date */
9821826Sjoerg			f_dayBefore = atoi(optarg);
9921826Sjoerg			break;
10021826Sjoerg
10121826Sjoerg		case '?':
10221826Sjoerg		default:
10321826Sjoerg			usage();
10421826Sjoerg		}
10521826Sjoerg	argc -= optind;
10621826Sjoerg	argv += optind;
10721826Sjoerg
10893383Smdodd	if (argc)
10993383Smdodd		usage();
11093383Smdodd
11193383Smdodd	/* use current time */
11293383Smdodd	if (f_time <= 0)
11393383Smdodd	    (void)time(&f_time);
11493383Smdodd
11521826Sjoerg	settime(f_time);
11621826Sjoerg
11793383Smdodd	if (doall)
11821826Sjoerg		while ((pw = getpwent()) != NULL) {
11921826Sjoerg			(void)setegid(pw->pw_gid);
12021826Sjoerg			(void)initgroups(pw->pw_name, pw->pw_gid);
12193383Smdodd			(void)seteuid(pw->pw_uid);
12221826Sjoerg			if (!chdir(pw->pw_dir))
12321826Sjoerg				cal();
12421826Sjoerg			(void)seteuid(0);
12521826Sjoerg		}
12621826Sjoerg	else
12721826Sjoerg		cal();
128147256Sbrooks	exit(0);
129147256Sbrooks}
13021826Sjoerg
13121826Sjoerg
132147256Sbrooksvoid
13321826Sjoergusage()
13421826Sjoerg{
13593383Smdodd	(void)fprintf(stderr,
13621826Sjoerg		      "usage: calendar [-a] [-A days] [-B days] [-f calendarfile]\n");
13793383Smdodd	exit(1);
13821826Sjoerg}
13921826Sjoerg
14093383Smdodd
14193383Smdodd