calendar.c revision 59945
198944Sobrien/* 298944Sobrien * Copyright (c) 1989, 1993, 1994 398944Sobrien * The Regents of the University of California. All rights reserved. 498944Sobrien * 598944Sobrien * Redistribution and use in source and binary forms, with or without 698944Sobrien * modification, are permitted provided that the following conditions 798944Sobrien * are met: 898944Sobrien * 1. Redistributions of source code must retain the above copyright 998944Sobrien * notice, this list of conditions and the following disclaimer. 1098944Sobrien * 2. Redistributions in binary form must reproduce the above copyright 1198944Sobrien * notice, this list of conditions and the following disclaimer in the 1298944Sobrien * documentation and/or other materials provided with the distribution. 1398944Sobrien * 3. All advertising materials mentioning features or use of this software 1498944Sobrien * must display the following acknowledgement: 1598944Sobrien * This product includes software developed by the University of 1698944Sobrien * California, Berkeley and its contributors. 1798944Sobrien * 4. Neither the name of the University nor the names of its contributors 1898944Sobrien * may be used to endorse or promote products derived from this software 1998944Sobrien * without specific prior written permission. 2098944Sobrien * 2198944Sobrien * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2298944Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2398944Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2498944Sobrien * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2598944Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2698944Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2798944Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2898944Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2998944Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3098944Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3198944Sobrien * SUCH DAMAGE. 3298944Sobrien * 3398944Sobrien * $FreeBSD: head/usr.bin/calendar/calendar.c 59945 2000-05-04 11:28:55Z phantom $ 3498944Sobrien */ 3598944Sobrien 3698944Sobrien#ifndef lint 3798944Sobrienstatic const char copyright[] = 3898944Sobrien"@(#) Copyright (c) 1989, 1993\n\ 3998944Sobrien The Regents of the University of California. All rights reserved.\n"; 4098944Sobrien#endif /* not lint */ 4198944Sobrien 4298944Sobrien#ifndef lint 4398944Sobrienstatic const char sccsid[] = "@(#)calendar.c 8.3 (Berkeley) 3/25/94"; 4498944Sobrien#endif /* not lint */ 4598944Sobrien 4698944Sobrien#include <err.h> 4798944Sobrien#include <errno.h> 4898944Sobrien#include <locale.h> 4998944Sobrien#include <pwd.h> 5098944Sobrien#include <stdio.h> 5198944Sobrien#include <stdlib.h> 5298944Sobrien#include <time.h> 5398944Sobrien#include <unistd.h> 5498944Sobrien 5598944Sobrien#include "pathnames.h" 5698944Sobrien#include "calendar.h" 5798944Sobrien 5898944Sobrienstruct passwd *pw; 5998944Sobrienint doall = 0; 6098944Sobrientime_t f_time = 0; 6198944Sobrien 6298944Sobrienint f_dayAfter = 0; /* days after current date */ 6398944Sobrienint f_dayBefore = 0; /* days before current date */ 6498944Sobrien 6598944Sobrienint 6698944Sobrienmain(argc, argv) 6798944Sobrien int argc; 6898944Sobrien char *argv[]; 6998944Sobrien{ 7098944Sobrien extern int optind; 7198944Sobrien int ch; 7298944Sobrien 7398944Sobrien (void) setlocale(LC_ALL, ""); 7498944Sobrien 7598944Sobrien while ((ch = getopt(argc, argv, "-af:t:A:B:")) != -1) 7698944Sobrien switch (ch) { 7798944Sobrien case '-': /* backward contemptible */ 7898944Sobrien case 'a': 7998944Sobrien if (getuid()) { 8098944Sobrien errno = EPERM; 8198944Sobrien err(1, NULL); 8298944Sobrien } 8398944Sobrien doall = 1; 8498944Sobrien break; 8598944Sobrien 8698944Sobrien 8798944Sobrien case 'f': /* other calendar file */ 8898944Sobrien calendarFile = optarg; 8998944Sobrien break; 9098944Sobrien 9198944Sobrien case 't': /* other date, undocumented, for tests */ 9298944Sobrien f_time = Mktime (optarg); 9398944Sobrien break; 9498944Sobrien 9598944Sobrien case 'A': /* days after current date */ 9698944Sobrien f_dayAfter = atoi(optarg); 9798944Sobrien break; 9898944Sobrien 9998944Sobrien case 'B': /* days before current date */ 10098944Sobrien f_dayBefore = atoi(optarg); 101 break; 102 103 case '?': 104 default: 105 usage(); 106 } 107 argc -= optind; 108 argv += optind; 109 110 if (argc) 111 usage(); 112 113 /* use current time */ 114 if (f_time <= 0) 115 (void)time(&f_time); 116 117 settime(f_time); 118 119 if (doall) 120 while ((pw = getpwent()) != NULL) { 121 (void)setegid(pw->pw_gid); 122 (void)initgroups(pw->pw_name, pw->pw_gid); 123 (void)seteuid(pw->pw_uid); 124 if (!chdir(pw->pw_dir)) 125 cal(); 126 (void)seteuid(0); 127 } 128 else 129 cal(); 130 exit(0); 131} 132 133 134void 135usage() 136{ 137 (void)fprintf(stderr, 138 "usage: calendar [-a] [-A days] [-B days] [-f calendarfile]\n"); 139 exit(1); 140} 141 142 143