xref: /dragonfly/usr.bin/calendar/calendar.c (revision 9348a738)
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. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * @(#) Copyright (c) 1989, 1993 The Regents of the University of California.  All rights reserved.
30  * @(#)calendar.c  8.3 (Berkeley) 3/25/94
31  * $FreeBSD: src/usr.bin/calendar/calendar.c,v 1.11.2.5 2003/04/06 20:04:56 dwmalone Exp $
32  * $DragonFly: src/usr.bin/calendar/calendar.c,v 1.5 2007/09/24 20:49:09 pavalos Exp $
33  */
34 
35 #include <err.h>
36 #include <errno.h>
37 #include <locale.h>
38 #include <pwd.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <sysexits.h>
42 #include <time.h>
43 #include <unistd.h>
44 
45 #include "pathnames.h"
46 #include "calendar.h"
47 
48 struct passwd *pw;
49 int doall = 0;
50 int f_dayAfter = 0; /* days after current date */
51 int f_dayBefore = 0; /* days before current date */
52 int Friday = 5;	     /* day before weekend */
53 
54 static void	usage(void);
55 
56 int
57 main(int argc, char **argv)
58 {
59 	int ch;
60 	time_t f_time = 0;
61 
62 	setlocale(LC_ALL, "");
63 
64 	while ((ch = getopt(argc, argv, "-af:t:A:B:F:W:")) != -1) {
65 		switch (ch) {
66 		case '-':		/* backward compatible */
67 		case 'a':
68 			if (getuid()) {
69 				errno = EPERM;
70 				err(EXIT_FAILURE, NULL);
71 			}
72 			doall = 1;
73 			break;
74 
75 		case 'f': /* other calendar file */
76 		        calendarFile = optarg;
77 			break;
78 
79 		case 't': /* other date, undocumented, for tests */
80 			f_time = Mktime(optarg);
81 			break;
82 
83 		case 'W': /* we don't need no steenking Fridays */
84 			Friday = -1;
85 
86 			/* FALLTHROUGH */
87 		case 'A': /* days after current date */
88 			f_dayAfter = atoi(optarg);
89 			break;
90 
91 		case 'B': /* days before current date */
92 			f_dayBefore = atoi(optarg);
93 			break;
94 
95 		case 'F':
96 			Friday = atoi(optarg);
97 			break;
98 
99 		case '?':
100 		default:
101 			usage();
102 		}
103 	}
104 	argc -= optind;
105 	argv += optind;
106 
107 	if (argc)
108 		usage();
109 
110 	/* use current time */
111 	if (f_time <= 0)
112 	    time(&f_time);
113 
114 	settime(f_time);
115 
116 	if (doall)
117 		while ((pw = getpwent()) != NULL) {
118 			setegid(pw->pw_gid);
119 			initgroups(pw->pw_name, pw->pw_gid);
120 			seteuid(pw->pw_uid);
121 			if (!chdir(pw->pw_dir))
122 				cal();
123 			seteuid(0);
124 		}
125 	else
126 		cal();
127 	exit(EX_OK);
128 }
129 
130 
131 static void
132 usage(void)
133 {
134 	fprintf(stderr, "%s\n%s\n",
135 	    "usage: calendar [-a] [-A days] [-B days] [-F friday] "
136 	    "[-f calendarfile]",
137 	    "                [-t dd[.mm[.year]]] [-W days]");
138 	exit(EX_USAGE);
139 }
140