calendar.c revision 98181
11590Srgrimes/*
21590Srgrimes * Copyright (c) 1989, 1993, 1994
31590Srgrimes *	The Regents of the University of California.  All rights reserved.
41590Srgrimes *
51590Srgrimes * Redistribution and use in source and binary forms, with or without
61590Srgrimes * modification, are permitted provided that the following conditions
71590Srgrimes * are met:
81590Srgrimes * 1. Redistributions of source code must retain the above copyright
91590Srgrimes *    notice, this list of conditions and the following disclaimer.
101590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111590Srgrimes *    notice, this list of conditions and the following disclaimer in the
121590Srgrimes *    documentation and/or other materials provided with the distribution.
131590Srgrimes * 3. All advertising materials mentioning features or use of this software
141590Srgrimes *    must display the following acknowledgement:
151590Srgrimes *	This product includes software developed by the University of
161590Srgrimes *	California, Berkeley and its contributors.
171590Srgrimes * 4. Neither the name of the University nor the names of its contributors
181590Srgrimes *    may be used to endorse or promote products derived from this software
191590Srgrimes *    without specific prior written permission.
201590Srgrimes *
211590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241590Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311590Srgrimes * SUCH DAMAGE.
321590Srgrimes */
331590Srgrimes
341590Srgrimes#ifndef lint
3515714Sachestatic const char copyright[] =
361590Srgrimes"@(#) Copyright (c) 1989, 1993\n\
371590Srgrimes	The Regents of the University of California.  All rights reserved.\n";
3887235Smarkm#endif
391590Srgrimes
4087628Sdwmalone#if 0
411590Srgrimes#ifndef lint
4287628Sdwmalonestatic char sccsid[] = "@(#)calendar.c  8.3 (Berkeley) 3/25/94";
4387235Smarkm#endif
4487628Sdwmalone#endif
451590Srgrimes
4687628Sdwmalone#include <sys/cdefs.h>
4787628Sdwmalone__FBSDID("$FreeBSD: head/usr.bin/calendar/calendar.c 98181 2002-06-13 21:20:56Z grog $");
4887628Sdwmalone
4915714Sache#include <err.h>
5015714Sache#include <errno.h>
5115714Sache#include <locale.h>
521590Srgrimes#include <pwd.h>
531590Srgrimes#include <stdio.h>
541590Srgrimes#include <stdlib.h>
5513840Swosch#include <time.h>
5615714Sache#include <unistd.h>
571590Srgrimes
581590Srgrimes#include "pathnames.h"
5913840Swosch#include "calendar.h"
601590Srgrimes
611590Srgrimesstruct passwd *pw;
6213840Swoschint doall = 0;
6313840Swoschtime_t f_time = 0;
641590Srgrimes
6513840Swoschint f_dayAfter = 0; /* days after current date */
6613840Swoschint f_dayBefore = 0; /* days before current date */
6798181Sgrogint Friday = 5;	     /* day before weekend */
681590Srgrimes
691590Srgrimesint
701590Srgrimesmain(argc, argv)
711590Srgrimes	int argc;
721590Srgrimes	char *argv[];
731590Srgrimes{
741590Srgrimes	int ch;
751590Srgrimes
7615714Sache	(void) setlocale(LC_ALL, "");
7715714Sache
7898181Sgrog	while ((ch = getopt(argc, argv, "-af:t:A:B:F:W:")) != -1)
791590Srgrimes		switch (ch) {
801590Srgrimes		case '-':		/* backward contemptible */
811590Srgrimes		case 'a':
821590Srgrimes			if (getuid()) {
831590Srgrimes				errno = EPERM;
841590Srgrimes				err(1, NULL);
851590Srgrimes			}
861590Srgrimes			doall = 1;
871590Srgrimes			break;
8813840Swosch
8913840Swosch
9013840Swosch		case 'f': /* other calendar file */
9113840Swosch		        calendarFile = optarg;
9213840Swosch			break;
9313840Swosch
9413840Swosch		case 't': /* other date, undocumented, for tests */
9513840Swosch			f_time = Mktime (optarg);
9613840Swosch			break;
9713840Swosch
9898181Sgrog		case 'W': /* we don't need no steenking Fridays */
9998181Sgrog			Friday = -1;
10098181Sgrog
10198181Sgrog			/* FALLTHROUGH */
10213840Swosch		case 'A': /* days after current date */
10313840Swosch			f_dayAfter = atoi(optarg);
10413840Swosch			break;
10513840Swosch
10613840Swosch		case 'B': /* days before current date */
10713840Swosch			f_dayBefore = atoi(optarg);
10813840Swosch			break;
10913840Swosch
11098181Sgrog		case 'F':
11198181Sgrog			Friday = atoi(optarg);
11298181Sgrog			break;
11398181Sgrog
1141590Srgrimes		case '?':
1151590Srgrimes		default:
1161590Srgrimes			usage();
1171590Srgrimes		}
1181590Srgrimes	argc -= optind;
1191590Srgrimes	argv += optind;
1201590Srgrimes
1211590Srgrimes	if (argc)
1221590Srgrimes		usage();
1231590Srgrimes
12413840Swosch	/* use current time */
12513840Swosch	if (f_time <= 0)
12613840Swosch	    (void)time(&f_time);
12713840Swosch
12813840Swosch	settime(f_time);
12913840Swosch
1301590Srgrimes	if (doall)
1311590Srgrimes		while ((pw = getpwent()) != NULL) {
1321590Srgrimes			(void)setegid(pw->pw_gid);
13322323Smpp			(void)initgroups(pw->pw_name, pw->pw_gid);
1341590Srgrimes			(void)seteuid(pw->pw_uid);
1351590Srgrimes			if (!chdir(pw->pw_dir))
1361590Srgrimes				cal();
1371590Srgrimes			(void)seteuid(0);
1381590Srgrimes		}
13911334Sache	else
1401590Srgrimes		cal();
1411590Srgrimes	exit(0);
1421590Srgrimes}
1431590Srgrimes
1441590Srgrimes
1451590Srgrimesvoid
14613840Swoschusage()
1471590Srgrimes{
14813840Swosch	(void)fprintf(stderr,
14998181Sgrog		      "usage: calendar [-a] [-A days] [-W days] [-F friday] [-B days]\n"
15098181Sgrog		      "\t[-f calendarfile] [-t dd[.mm[.year]]]\n");
15113840Swosch	exit(1);
1521590Srgrimes}
1531590Srgrimes
1541590Srgrimes
155