calendar.c revision 200462
1167857Simp/*
2167857Simp * Copyright (c) 1989, 1993, 1994
3167857Simp *	The Regents of the University of California.  All rights reserved.
4167857Simp *
5167857Simp * Redistribution and use in source and binary forms, with or without
6167857Simp * modification, are permitted provided that the following conditions
7167857Simp * are met:
8167857Simp * 1. Redistributions of source code must retain the above copyright
9167857Simp *    notice, this list of conditions and the following disclaimer.
10167857Simp * 2. Redistributions in binary form must reproduce the above copyright
11167857Simp *    notice, this list of conditions and the following disclaimer in the
12167857Simp *    documentation and/or other materials provided with the distribution.
13167857Simp * 3. All advertising materials mentioning features or use of this software
14167857Simp *    must display the following acknowledgement:
15167857Simp *	This product includes software developed by the University of
16167857Simp *	California, Berkeley and its contributors.
17167857Simp * 4. Neither the name of the University nor the names of its contributors
18167857Simp *    may be used to endorse or promote products derived from this software
19167857Simp *    without specific prior written permission.
20167857Simp *
21167857Simp * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22167857Simp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23167857Simp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24167857Simp * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25167857Simp * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26167857Simp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27167857Simp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28167857Simp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29167857Simp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30294674Sian * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31294674Sian * SUCH DAMAGE.
32294674Sian */
33167857Simp
34167857Simp#ifndef lint
35167857Simpstatic const char copyright[] =
36167857Simp"@(#) Copyright (c) 1989, 1993\n\
37167857Simp	The Regents of the University of California.  All rights reserved.\n";
38167857Simp#endif
39167857Simp
40181305Sjhb#if 0
41167857Simp#ifndef lint
42167857Simpstatic char sccsid[] = "@(#)calendar.c  8.3 (Berkeley) 3/25/94";
43294674Sian#endif
44294674Sian#endif
45294674Sian
46294674Sian#include <sys/cdefs.h>
47294674Sian__FBSDID("$FreeBSD: head/usr.bin/calendar/calendar.c 200462 2009-12-13 03:14:06Z delphij $");
48294674Sian
49167857Simp#include <err.h>
50167857Simp#include <errno.h>
51167857Simp#include <locale.h>
52167857Simp#include <pwd.h>
53167857Simp#include <stdio.h>
54294674Sian#include <stdlib.h>
55294674Sian#include <time.h>
56294674Sian#include <unistd.h>
57294674Sian
58294674Sian#include "pathnames.h"
59294674Sian#include "calendar.h"
60167857Simp
61294674Sianstruct passwd	*pw;
62167857Simpint		doall = 0;
63167857Simptime_t		f_time = 0;
64294674Sian
65167857Simpint	f_dayAfter = 0;		/* days after current date */
66294674Sianint	f_dayBefore = 0;	/* days before current date */
67167857Simpint	Friday = 5;		/* day before weekend */
68294674Sian
69167857Simpstatic void	usage(void) __dead2;
70167857Simp
71167857Simpint
72294674Sianmain(int argc, char *argv[])
73294674Sian{
74294674Sian	int ch;
75294674Sian
76294674Sian	(void)setlocale(LC_ALL, "");
77294674Sian
78294674Sian	while ((ch = getopt(argc, argv, "-A:aB:F:f:t:W:")) != -1)
79294674Sian		switch (ch) {
80294674Sian		case '-':		/* backward contemptible */
81294674Sian		case 'a':
82294674Sian			if (getuid()) {
83294674Sian				errno = EPERM;
84294674Sian				err(1, NULL);
85294674Sian			}
86294674Sian			doall = 1;
87294674Sian			break;
88294674Sian
89294674Sian		case 'f': /* other calendar file */
90294674Sian			calendarFile = optarg;
91294674Sian			break;
92294674Sian
93294674Sian		case 't': /* other date, undocumented, for tests */
94294674Sian			f_time = Mktime(optarg);
95294674Sian			break;
96294674Sian
97294674Sian		case 'W': /* we don't need no steenking Fridays */
98294674Sian			Friday = -1;
99294674Sian
100294674Sian			/* FALLTHROUGH */
101294674Sian		case 'A': /* days after current date */
102294674Sian			f_dayAfter = atoi(optarg);
103294674Sian			break;
104294674Sian
105294674Sian		case 'B': /* days before current date */
106294674Sian			f_dayBefore = atoi(optarg);
107294674Sian			break;
108294674Sian
109294674Sian		case 'F':
110167857Simp			Friday = atoi(optarg);
111167857Simp			break;
112167857Simp
113167857Simp		case '?':
114167857Simp		default:
115167857Simp			usage();
116167857Simp		}
117167857Simp
118167857Simp	argc -= optind;
119167857Simp	argv += optind;
120167857Simp
121181305Sjhb	if (argc)
122167857Simp		usage();
123167857Simp
124167857Simp	/* use current time */
125167857Simp	if (f_time <= 0)
126167857Simp		(void)time(&f_time);
127167857Simp
128294674Sian	settime(f_time);
129167857Simp
130167857Simp	if (doall)
131167857Simp		while ((pw = getpwent()) != NULL) {
132294674Sian			(void)setegid(pw->pw_gid);
133289666Sian			(void)initgroups(pw->pw_name, pw->pw_gid);
134294674Sian			(void)seteuid(pw->pw_uid);
135294674Sian			if (!chdir(pw->pw_dir))
136294674Sian				cal();
137294674Sian			(void)seteuid(0);
138294674Sian		}
139294674Sian	else
140294674Sian		cal();
141294674Sian	exit(0);
142294674Sian}
143294674Sian
144294674Sian
145294674Sianstatic void __dead2
146294674Sianusage(void)
147294674Sian{
148294674Sian
149294674Sian	fprintf(stderr, "%s\n%s\n",
150294674Sian	    "usage: calendar [-a] [-A days] [-B days] [-F friday] "
151294674Sian	    "[-f calendarfile]",
152294674Sian	    "                [-t dd[.mm[.year]]] [-W days]");
153294674Sian	exit(1);
154294674Sian}
155294674Sian