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