calendar.c revision 181323
1169689Skan/*
2169689Skan * Copyright (c) 1989, 1993, 1994
3169689Skan *	The Regents of the University of California.  All rights reserved.
4169689Skan *
5169689Skan * Redistribution and use in source and binary forms, with or without
6169689Skan * modification, are permitted provided that the following conditions
7169689Skan * are met:
8169689Skan * 1. Redistributions of source code must retain the above copyright
9169689Skan *    notice, this list of conditions and the following disclaimer.
10169689Skan * 2. Redistributions in binary form must reproduce the above copyright
11169689Skan *    notice, this list of conditions and the following disclaimer in the
12169689Skan *    documentation and/or other materials provided with the distribution.
13169689Skan * 3. All advertising materials mentioning features or use of this software
14169689Skan *    must display the following acknowledgement:
15169689Skan *	This product includes software developed by the University of
16169689Skan *	California, Berkeley and its contributors.
17169689Skan * 4. Neither the name of the University nor the names of its contributors
18169689Skan *    may be used to endorse or promote products derived from this software
19169689Skan *    without specific prior written permission.
20169689Skan *
21169689Skan * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22169689Skan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23169689Skan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24169689Skan * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25169689Skan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26169689Skan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27169689Skan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28169689Skan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29169689Skan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30169689Skan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31169689Skan * SUCH DAMAGE.
32169689Skan */
33169689Skan
34169689Skan#ifndef lint
35169689Skanstatic const char copyright[] =
36169689Skan"@(#) Copyright (c) 1989, 1993\n\
37169689Skan	The Regents of the University of California.  All rights reserved.\n";
38169689Skan#endif
39169689Skan
40169689Skan#if 0
41169689Skan#ifndef lint
42169689Skanstatic char sccsid[] = "@(#)calendar.c  8.3 (Berkeley) 3/25/94";
43169689Skan#endif
44169689Skan#endif
45169689Skan
46169689Skan#include <sys/cdefs.h>
47169689Skan__FBSDID("$FreeBSD: head/usr.bin/calendar/calendar.c 181323 2008-08-05 08:16:37Z edwin $");
48169689Skan
49169689Skan#include <err.h>
50169689Skan#include <errno.h>
51169689Skan#include <locale.h>
52169689Skan#include <pwd.h>
53169689Skan#include <stdio.h>
54169689Skan#include <stdlib.h>
55169689Skan#include <time.h>
56169689Skan#include <unistd.h>
57169689Skan
58169689Skan#include "pathnames.h"
59169689Skan#include "calendar.h"
60169689Skan
61169689Skanstruct passwd	*pw;
62169689Skanint		doall = 0;
63169689Skantime_t		f_time = 0;
64169689Skan
65169689Skanint	f_dayAfter = 0;		/* days after current date */
66169689Skanint	f_dayBefore = 0;	/* days before current date */
67169689Skanint	Friday = 5;		/* day before weekend */
68169689Skan
69169689Skanstatic void	usage(void) __dead2;
70169689Skan
71169689Skanint
72169689Skanmain(int argc, char *argv[])
73169689Skan{
74169689Skan	int ch;
75169689Skan
76169689Skan	(void)setlocale(LC_ALL, "");
77169689Skan
78169689Skan	while ((ch = getopt(argc, argv, "-A:aB:F:f:t:W:")) != -1)
79169689Skan		switch (ch) {
80169689Skan		case '-':		/* backward contemptible */
81169689Skan		case 'a':
82169689Skan			if (getuid()) {
83169689Skan				errno = EPERM;
84169689Skan				err(1, NULL);
85169689Skan			}
86169689Skan			doall = 1;
87169689Skan			break;
88169689Skan
89169689Skan		case 'f': /* other calendar file */
90169689Skan			calendarFile = optarg;
91169689Skan			break;
92169689Skan
93169689Skan		case 't': /* other date, undocumented, for tests */
94169689Skan			f_time = Mktime(optarg);
95169689Skan			break;
96169689Skan
97169689Skan		case 'W': /* we don't need no steenking Fridays */
98169689Skan			Friday = -1;
99169689Skan
100169689Skan			/* FALLTHROUGH */
101169689Skan		case 'A': /* days after current date */
102169689Skan			f_dayAfter = atoi(optarg);
103169689Skan			break;
104169689Skan
105169689Skan		case 'B': /* days before current date */
106169689Skan			f_dayBefore = atoi(optarg);
107169689Skan			break;
108169689Skan
109169689Skan		case 'F':
110169689Skan			Friday = atoi(optarg);
111169689Skan			break;
112169689Skan
113169689Skan		case '?':
114169689Skan		default:
115169689Skan			usage();
116169689Skan		}
117169689Skan
118169689Skan	argc -= optind;
119169689Skan	argv += optind;
120169689Skan
121169689Skan	if (argc)
122169689Skan		usage();
123169689Skan
124169689Skan	/* use current time */
125169689Skan	if (f_time <= 0)
126169689Skan		(void)time(&f_time);
127169689Skan
128169689Skan	settime(f_time);
129169689Skan
130169689Skan	if (doall)
131169689Skan		while ((pw = getpwent()) != NULL) {
132169689Skan			(void)setegid(pw->pw_gid);
133169689Skan			(void)initgroups(pw->pw_name, pw->pw_gid);
134169689Skan			(void)seteuid(pw->pw_uid);
135169689Skan			if (!chdir(pw->pw_dir))
136169689Skan				cal();
137169689Skan			(void)seteuid(0);
138169689Skan		}
139169689Skan	else
140169689Skan		cal();
141169689Skan	exit(0);
142169689Skan}
143169689Skan
144169689Skan
145169689Skanstatic void __dead2
146169689Skanusage(void)
147169689Skan{
148169689Skan
149169689Skan	fprintf(stderr, "%s\n%s\n",
150169689Skan	    "usage: calendar [-a] [-A days] [-B days] [-F friday] "
151169689Skan	    "[-f calendarfile]",
152169689Skan	    "                [-t dd[.mm[.year]]] [-W days]");
153169689Skan	exit(1);
154169689Skan}
155169689Skan