+/*
+ * Copyright (c) 1989 The Regents of the University of California.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ * must display the following acknowledgement:
+ * This product includes software developed by the University of
+ * California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#ifndef lint
+char copyright[] =
+"@(#) Copyright (c) 1989 The Regents of the University of California.\n\
+ All rights reserved.\n";
+#endif /* not lint */
+
+#ifndef lint
+static char sccsid[] = "@(#)calendar.c 4.11 (Berkeley) 10/12/90";
+#endif /* not lint */
+
+#include <sys/param.h>
+#include <sys/time.h>
+#include <sys/stat.h>
+#include <sys/file.h>
+#include <sys/uio.h>
+#include <pwd.h>
+#include <errno.h>
+#include <tzfile.h>
+#include <stdio.h>
+#include <ctype.h>
+#include <unistd.h>
+#include <string.h>
+#include "pathnames.h"
+
+extern int errno;
+struct passwd *pw;
+int doall;
+
+main(argc, argv)
+ int argc;
+ char **argv;
+{
+ extern int optind;
+ int ch;
+
+ while ((ch = getopt(argc, argv, "-a")) != EOF)
+ switch(ch) {
+ case '-': /* backward contemptible */
+ case 'a':
+ if (getuid()) {
+ (void)fprintf(stderr,
+ "calendar: %s\n", strerror(EPERM));
+ exit(1);
+ }
+ doall = 1;
+ break;
+ case '?':
+ default:
+ usage();
+ }
+ argc -= optind;
+ argv += optind;
+
+ if (argc)
+ usage();
+
+ settime();
+ if (doall)
+ while (pw = getpwent()) {
+ (void)setegid(pw->pw_gid);
+ (void)seteuid(pw->pw_uid);
+ if (!chdir(pw->pw_dir))
+ cal();
+ (void)seteuid(0);
+ }
+ else
+ cal();
+ exit(0);
+}
+
+cal()
+{
+ register int printing;
+ register char *p;
+ FILE *fp, *opencal();
+ int ch;
+ char buf[2048 + 1];
+
+ if (!(fp = opencal()))
+ return;
+ for (printing = 0; fgets(buf, sizeof(buf), stdin);) {
+ if (p = index(buf, '\n'))
+ *p = '\0';
+ else
+ while ((ch = getchar()) != '\n' && ch != EOF);
+ if (buf[0] == '\0')
+ continue;
+ if (buf[0] != '\t')
+ printing = isnow(buf) ? 1 : 0;
+ if (printing)
+ (void)fprintf(fp, "%s\n", buf);
+ }
+ closecal(fp);
+}
+
+struct iovec header[] = {
+ "From: ", 6,
+ NULL, 0,
+ " (Reminder Service)\nTo: ", 24,
+ NULL, 0,
+ "\nSubject: ", 10,
+ NULL, 0,
+ "'s Calendar\nPrecedence: bulk\n\n", 30,
+};
+
+/* 1-based month, 0-based days, cumulative */
+int daytab[][14] = {
+ 0, 0, 30, 58, 89, 119, 150, 180, 211, 242, 272, 303, 333, 364,
+ 0, 0, 30, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365,