calendar.c revision 98181
14887Schin/*
24887Schin * Copyright (c) 1989, 1993, 1994
34887Schin *	The Regents of the University of California.  All rights reserved.
412068SRoger.Faulkner@Oracle.COM *
54887Schin * Redistribution and use in source and binary forms, with or without
64887Schin * modification, are permitted provided that the following conditions
78462SApril.Chin@Sun.COM * are met:
84887Schin * 1. Redistributions of source code must retain the above copyright
94887Schin *    notice, this list of conditions and the following disclaimer.
104887Schin * 2. Redistributions in binary form must reproduce the above copyright
114887Schin *    notice, this list of conditions and the following disclaimer in the
124887Schin *    documentation and/or other materials provided with the distribution.
134887Schin * 3. All advertising materials mentioning features or use of this software
144887Schin *    must display the following acknowledgement:
154887Schin *	This product includes software developed by the University of
164887Schin *	California, Berkeley and its contributors.
174887Schin * 4. Neither the name of the University nor the names of its contributors
184887Schin *    may be used to endorse or promote products derived from this software
194887Schin *    without specific prior written permission.
204887Schin *
214887Schin * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
224887Schin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
234887Schin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
244887Schin * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
254887Schin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
264887Schin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
274887Schin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
284887Schin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
294887Schin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
304887Schin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
314887Schin * SUCH DAMAGE.
324887Schin */
334887Schin
344887Schin#ifndef lint
354887Schinstatic const char copyright[] =
364887Schin"@(#) Copyright (c) 1989, 1993\n\
374887Schin	The Regents of the University of California.  All rights reserved.\n";
384887Schin#endif
394887Schin
404887Schin#if 0
414887Schin#ifndef lint
424887Schinstatic char sccsid[] = "@(#)calendar.c  8.3 (Berkeley) 3/25/94";
434887Schin#endif
444887Schin#endif
454887Schin
464887Schin#include <sys/cdefs.h>
474887Schin__FBSDID("$FreeBSD: head/usr.bin/calendar/calendar.c 98181 2002-06-13 21:20:56Z grog $");
484887Schin
494887Schin#include <err.h>
504887Schin#include <errno.h>
514887Schin#include <locale.h>
524887Schin#include <pwd.h>
534887Schin#include <stdio.h>
544887Schin#include <stdlib.h>
554887Schin#include <time.h>
564887Schin#include <unistd.h>
574887Schin
584887Schin#include "pathnames.h"
594887Schin#include "calendar.h"
604887Schin
614887Schinstruct passwd *pw;
624887Schinint doall = 0;
634887Schintime_t f_time = 0;
644887Schin
654887Schinint f_dayAfter = 0; /* days after current date */
664887Schinint f_dayBefore = 0; /* days before current date */
674887Schinint Friday = 5;	     /* day before weekend */
684887Schin
694887Schinint
704887Schinmain(argc, argv)
714887Schin	int argc;
724887Schin	char *argv[];
734887Schin{
744887Schin	int ch;
754887Schin
764887Schin	(void) setlocale(LC_ALL, "");
774887Schin
784887Schin	while ((ch = getopt(argc, argv, "-af:t:A:B:F:W:")) != -1)
794887Schin		switch (ch) {
804887Schin		case '-':		/* backward contemptible */
814887Schin		case 'a':
824887Schin			if (getuid()) {
834887Schin				errno = EPERM;
844887Schin				err(1, NULL);
854887Schin			}
864887Schin			doall = 1;
874887Schin			break;
884887Schin
894887Schin
904887Schin		case 'f': /* other calendar file */
914887Schin		        calendarFile = optarg;
924887Schin			break;
934887Schin
944887Schin		case 't': /* other date, undocumented, for tests */
954887Schin			f_time = Mktime (optarg);
964887Schin			break;
974887Schin
984887Schin		case 'W': /* we don't need no steenking Fridays */
994887Schin			Friday = -1;
1004887Schin
1014887Schin			/* FALLTHROUGH */
1024887Schin		case 'A': /* days after current date */
1034887Schin			f_dayAfter = atoi(optarg);
1044887Schin			break;
1054887Schin
1064887Schin		case 'B': /* days before current date */
1074887Schin			f_dayBefore = atoi(optarg);
1084887Schin			break;
1094887Schin
1104887Schin		case 'F':
1114887Schin			Friday = atoi(optarg);
1124887Schin			break;
1134887Schin
1144887Schin		case '?':
1154887Schin		default:
1164887Schin			usage();
1174887Schin		}
1184887Schin	argc -= optind;
1194887Schin	argv += optind;
1204887Schin
1214887Schin	if (argc)
1224887Schin		usage();
1234887Schin
1244887Schin	/* use current time */
1254887Schin	if (f_time <= 0)
1264887Schin	    (void)time(&f_time);
1274887Schin
1284887Schin	settime(f_time);
1294887Schin
1304887Schin	if (doall)
1314887Schin		while ((pw = getpwent()) != NULL) {
1324887Schin			(void)setegid(pw->pw_gid);
1334887Schin			(void)initgroups(pw->pw_name, pw->pw_gid);
1344887Schin			(void)seteuid(pw->pw_uid);
1354887Schin			if (!chdir(pw->pw_dir))
1364887Schin				cal();
1374887Schin			(void)seteuid(0);
1384887Schin		}
1394887Schin	else
1404887Schin		cal();
1414887Schin	exit(0);
1424887Schin}
1434887Schin
1444887Schin
1454887Schinvoid
1464887Schinusage()
1474887Schin{
1484887Schin	(void)fprintf(stderr,
1494887Schin		      "usage: calendar [-a] [-A days] [-W days] [-F friday] [-B days]\n"
1504887Schin		      "\t[-f calendarfile] [-t dd[.mm[.year]]]\n");
1514887Schin	exit(1);
1524887Schin}
1534887Schin
1544887Schin
1554887Schin