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 * 4. 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 30 #ifndef lint 31 static const char copyright[] = 32 "@(#) Copyright (c) 1989, 1993\n\ 33 The Regents of the University of California. All rights reserved.\n"; 34 #endif 35 36 #if 0 37 #ifndef lint 38 static char sccsid[] = "@(#)calendar.c 8.3 (Berkeley) 3/25/94"; 39 #endif 40 #endif 41 42 #include <sys/cdefs.h> 43 __FBSDID("$FreeBSD$"); 44 45 #include <err.h> 46 #include <errno.h> 47 #include <locale.h> 48 #include <pwd.h> 49 #include <stdio.h> 50 #include <stdlib.h> 51 #include <string.h> 52 #include <time.h> 53 #include <unistd.h> 54 55 #include "calendar.h" 56 57 #define UTCOFFSET_NOTSET 100 /* Expected between -24 and +24 */ 58 #define LONGITUDE_NOTSET 1000 /* Expected between -360 and +360 */ 59 60 struct passwd *pw; 61 int doall = 0; 62 int debug = 0; 63 static char *DEBUG = NULL; 64 static time_t f_time = 0; 65 double UTCOffset = UTCOFFSET_NOTSET; 66 int EastLongitude = LONGITUDE_NOTSET; 67 68 static void usage(void) __dead2; 69 70 int 71 main(int argc, char *argv[]) 72 { 73 int f_dayAfter = 0; /* days after current date */ 74 int f_dayBefore = 0; /* days before current date */ 75 int Friday = 5; /* day before weekend */ 76 77 int ch; 78 struct tm tp1, tp2; 79 80 (void)setlocale(LC_ALL, ""); 81 82 while ((ch = getopt(argc, argv, "-A:aB:D:dF:f:l:t:U:W:?")) != -1) 83 switch (ch) { 84 case '-': /* backward contemptible */ 85 case 'a': 86 if (getuid()) { 87 errno = EPERM; 88 err(1, NULL); 89 } 90 doall = 1; 91 break; 92 93 case 'W': /* we don't need no steenking Fridays */ 94 Friday = -1; 95 /* FALLTHROUGH */ 96 97 case 'A': /* days after current date */ 98 f_dayAfter = atoi(optarg); 99 if (f_dayAfter < 0) 100 errx(1, "number of days must be positive"); 101 break; 102 103 case 'B': /* days before current date */ 104 f_dayBefore = atoi(optarg); 105 if (f_dayBefore < 0) 106 errx(1, "number of days must be positive"); 107 break; 108 109 case 'D': /* debug output of sun and moon info */ 110 DEBUG = optarg; 111 break; 112 113 case 'd': /* debug output of current date */ 114 debug = 1; 115 break; 116 117 case 'F': /* Change the time: When does weekend start? */ 118 Friday = atoi(optarg); 119 break; 120 121 case 'f': /* other calendar file */ 122 calendarFile = optarg; 123 break; 124 125 case 'l': /* Change longitudal position */ 126 EastLongitude = strtol(optarg, NULL, 10); 127 break; 128 129 case 't': /* other date, for tests */ 130 f_time = Mktime(optarg); 131 break; 132 133 case 'U': /* Change UTC offset */ 134 UTCOffset = strtod(optarg, NULL); 135 break; 136 137 case '?': 138 default: 139 usage(); 140 } 141 142 argc -= optind; 143 argv += optind; 144 145 if (argc) 146 usage(); 147 148 /* use current time */ 149 if (f_time <= 0) 150 (void)time(&f_time); 151 152 /* if not set, determine where I could be */ 153 { 154 if (UTCOffset == UTCOFFSET_NOTSET && 155 EastLongitude == LONGITUDE_NOTSET) { 156 /* Calculate on difference between here and UTC */ 157 time_t t; 158 struct tm tm; 159 long utcoffset, hh, mm, ss; 160 double uo; 161 162 time(&t); 163 localtime_r(&t, &tm); 164 utcoffset = tm.tm_gmtoff; 165 /* seconds -> hh:mm:ss */ 166 hh = utcoffset / SECSPERHOUR; 167 utcoffset %= SECSPERHOUR; 168 mm = utcoffset / SECSPERMINUTE; 169 utcoffset %= SECSPERMINUTE; 170 ss = utcoffset; 171 172 /* hh:mm:ss -> hh.mmss */ 173 uo = mm + (100.0 * (ss / 60.0)); 174 uo /= 60.0 / 100.0; 175 uo = hh + uo / 100; 176 177 UTCOffset = uo; 178 EastLongitude = UTCOffset * 15; 179 } else if (UTCOffset == UTCOFFSET_NOTSET) { 180 /* Base on information given */ 181 UTCOffset = EastLongitude / 15; 182 } else if (EastLongitude == LONGITUDE_NOTSET) { 183 /* Base on information given */ 184 EastLongitude = UTCOffset * 15; 185 } 186 } 187 188 settimes(f_time, f_dayBefore, f_dayAfter, Friday, &tp1, &tp2); 189 generatedates(&tp1, &tp2); 190 191 /* 192 * FROM now on, we are working in UTC. 193 * This will only affect moon and sun related events anyway. 194 */ 195 if (setenv("TZ", "UTC", 1) != 0) 196 errx(1, "setenv: %s", strerror(errno)); 197 tzset(); 198 199 if (debug) 200 dumpdates(); 201 202 if (DEBUG != NULL) { 203 dodebug(DEBUG); 204 exit(0); 205 } 206 207 if (doall) 208 while ((pw = getpwent()) != NULL) { 209 (void)setegid(pw->pw_gid); 210 (void)initgroups(pw->pw_name, pw->pw_gid); 211 (void)seteuid(pw->pw_uid); 212 if (!chdir(pw->pw_dir)) 213 cal(); 214 (void)seteuid(0); 215 } 216 else 217 cal(); 218 exit(0); 219 } 220 221 222 static void __dead2 223 usage(void) 224 { 225 226 fprintf(stderr, "%s\n%s\n%s\n", 227 "usage: calendar [-A days] [-a] [-B days] [-D sun|moon] [-d]", 228 " [-F friday] [-f calendarfile] [-l longitude]", 229 " [-t dd[.mm[.year]]] [-U utcoffset] [-W days]" 230 ); 231 exit(1); 232 } 233