calendar.c revision 169343
11590Srgrimes/* 21590Srgrimes * Copyright (c) 1989, 1993, 1994 31590Srgrimes * The Regents of the University of California. All rights reserved. 41590Srgrimes * 51590Srgrimes * Redistribution and use in source and binary forms, with or without 61590Srgrimes * modification, are permitted provided that the following conditions 71590Srgrimes * are met: 81590Srgrimes * 1. Redistributions of source code must retain the above copyright 91590Srgrimes * notice, this list of conditions and the following disclaimer. 101590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111590Srgrimes * notice, this list of conditions and the following disclaimer in the 121590Srgrimes * documentation and/or other materials provided with the distribution. 131590Srgrimes * 3. All advertising materials mentioning features or use of this software 141590Srgrimes * must display the following acknowledgement: 151590Srgrimes * This product includes software developed by the University of 161590Srgrimes * California, Berkeley and its contributors. 171590Srgrimes * 4. Neither the name of the University nor the names of its contributors 181590Srgrimes * may be used to endorse or promote products derived from this software 191590Srgrimes * without specific prior written permission. 201590Srgrimes * 211590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 221590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 231590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 241590Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 251590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 261590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 271590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 281590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 291590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 301590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 311590Srgrimes * SUCH DAMAGE. 321590Srgrimes */ 331590Srgrimes 341590Srgrimes#ifndef lint 3515714Sachestatic const char copyright[] = 361590Srgrimes"@(#) Copyright (c) 1989, 1993\n\ 371590Srgrimes The Regents of the University of California. All rights reserved.\n"; 3887235Smarkm#endif 391590Srgrimes 4087628Sdwmalone#if 0 411590Srgrimes#ifndef lint 4287628Sdwmalonestatic char sccsid[] = "@(#)calendar.c 8.3 (Berkeley) 3/25/94"; 4387235Smarkm#endif 4487628Sdwmalone#endif 451590Srgrimes 4687628Sdwmalone#include <sys/cdefs.h> 4787628Sdwmalone__FBSDID("$FreeBSD: head/usr.bin/calendar/calendar.c 169343 2007-05-07 11:18:30Z dwmalone $"); 4887628Sdwmalone 4915714Sache#include <err.h> 5015714Sache#include <errno.h> 5115714Sache#include <locale.h> 521590Srgrimes#include <pwd.h> 531590Srgrimes#include <stdio.h> 541590Srgrimes#include <stdlib.h> 5513840Swosch#include <time.h> 5615714Sache#include <unistd.h> 571590Srgrimes 581590Srgrimes#include "pathnames.h" 5913840Swosch#include "calendar.h" 601590Srgrimes 611590Srgrimesstruct passwd *pw; 6213840Swoschint doall = 0; 6313840Swoschtime_t f_time = 0; 641590Srgrimes 6513840Swoschint f_dayAfter = 0; /* days after current date */ 6613840Swoschint f_dayBefore = 0; /* days before current date */ 6798181Sgrogint Friday = 5; /* day before weekend */ 681590Srgrimes 691590Srgrimesint 70169343Sdwmalonemain(int argc, char *argv[]) 711590Srgrimes{ 721590Srgrimes int ch; 731590Srgrimes 7415714Sache (void) setlocale(LC_ALL, ""); 7515714Sache 7698181Sgrog while ((ch = getopt(argc, argv, "-af:t:A:B:F:W:")) != -1) 771590Srgrimes switch (ch) { 781590Srgrimes case '-': /* backward contemptible */ 791590Srgrimes case 'a': 801590Srgrimes if (getuid()) { 811590Srgrimes errno = EPERM; 821590Srgrimes err(1, NULL); 831590Srgrimes } 841590Srgrimes doall = 1; 851590Srgrimes break; 8613840Swosch 8713840Swosch 8813840Swosch case 'f': /* other calendar file */ 8913840Swosch calendarFile = optarg; 9013840Swosch break; 9113840Swosch 9213840Swosch case 't': /* other date, undocumented, for tests */ 9313840Swosch f_time = Mktime (optarg); 9413840Swosch break; 9513840Swosch 9698181Sgrog case 'W': /* we don't need no steenking Fridays */ 9798181Sgrog Friday = -1; 9898181Sgrog 9998181Sgrog /* FALLTHROUGH */ 10013840Swosch case 'A': /* days after current date */ 10113840Swosch f_dayAfter = atoi(optarg); 10213840Swosch break; 10313840Swosch 10413840Swosch case 'B': /* days before current date */ 10513840Swosch f_dayBefore = atoi(optarg); 10613840Swosch break; 10713840Swosch 10898181Sgrog case 'F': 10998181Sgrog Friday = atoi(optarg); 11098181Sgrog break; 11198181Sgrog 1121590Srgrimes case '?': 1131590Srgrimes default: 1141590Srgrimes usage(); 1151590Srgrimes } 1161590Srgrimes argc -= optind; 1171590Srgrimes argv += optind; 1181590Srgrimes 1191590Srgrimes if (argc) 1201590Srgrimes usage(); 1211590Srgrimes 12213840Swosch /* use current time */ 12313840Swosch if (f_time <= 0) 12413840Swosch (void)time(&f_time); 12513840Swosch 12613840Swosch settime(f_time); 12713840Swosch 1281590Srgrimes if (doall) 1291590Srgrimes while ((pw = getpwent()) != NULL) { 1301590Srgrimes (void)setegid(pw->pw_gid); 13122323Smpp (void)initgroups(pw->pw_name, pw->pw_gid); 1321590Srgrimes (void)seteuid(pw->pw_uid); 1331590Srgrimes if (!chdir(pw->pw_dir)) 1341590Srgrimes cal(); 1351590Srgrimes (void)seteuid(0); 1361590Srgrimes } 13711334Sache else 1381590Srgrimes cal(); 1391590Srgrimes exit(0); 1401590Srgrimes} 1411590Srgrimes 1421590Srgrimes 1431590Srgrimesvoid 144169343Sdwmaloneusage(void) 1451590Srgrimes{ 146101862Sru (void)fprintf(stderr, "%s\n%s\n", 147101862Sru "usage: calendar [-a] [-A days] [-B days] [-F friday] " 148101862Sru "[-f calendarfile]", 149101862Sru " [-t dd[.mm[.year]]] [-W days]"); 15013840Swosch exit(1); 1511590Srgrimes} 1521590Srgrimes 1531590Srgrimes 154