calendar.c revision 13840
1/*
2 * Copyright (c) 1989, 1993, 1994
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by the University of
16 *	California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#ifndef lint
35static char copyright[] =
36"@(#) Copyright (c) 1989, 1993\n\
37	The Regents of the University of California.  All rights reserved.\n";
38#endif /* not lint */
39
40#ifndef lint
41static char sccsid[] = "@(#)calendar.c	8.3 (Berkeley) 3/25/94";
42#endif /* not lint */
43
44#include <pwd.h>
45#include <stdio.h>
46#include <unistd.h>
47#include <errno.h>
48#include <err.h>
49#include <stdlib.h>
50#include <time.h>
51
52#include "pathnames.h"
53#include "calendar.h"
54
55struct passwd *pw;
56int doall = 0;
57time_t f_time = 0;
58
59int f_dayAfter = 0; /* days after current date */
60int f_dayBefore = 0; /* days before current date */
61
62int
63main(argc, argv)
64	int argc;
65	char *argv[];
66{
67	extern int optind;
68	int ch;
69
70	while ((ch = getopt(argc, argv, "?-af:t:A:B:")) != EOF)
71		switch (ch) {
72		case '-':		/* backward contemptible */
73		case 'a':
74			if (getuid()) {
75				errno = EPERM;
76				err(1, NULL);
77			}
78			doall = 1;
79			break;
80
81
82		case 'f': /* other calendar file */
83		        calendarFile = optarg;
84			break;
85
86		case 't': /* other date, undocumented, for tests */
87			f_time = Mktime (optarg);
88			break;
89
90		case 'A': /* days after current date */
91			f_dayAfter = atoi(optarg);
92			break;
93
94		case 'B': /* days before current date */
95			f_dayBefore = atoi(optarg);
96			break;
97
98		case '?':
99		default:
100			usage();
101		}
102	argc -= optind;
103	argv += optind;
104
105	if (argc)
106		usage();
107
108	/* use current time */
109	if (f_time <= 0)
110	    (void)time(&f_time);
111
112	settime(f_time);
113
114	if (doall)
115		while ((pw = getpwent()) != NULL) {
116			(void)setegid(pw->pw_gid);
117			(void)seteuid(pw->pw_uid);
118			if (!chdir(pw->pw_dir))
119				cal();
120			(void)seteuid(0);
121		}
122	else
123		cal();
124	exit(0);
125}
126
127
128void
129usage()
130{
131	(void)fprintf(stderr,
132		      "usage: calendar [-a] [-A days] [-B days] [-f calendarfile]\n");
133	exit(1);
134}
135
136
137