1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 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. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #ifndef lint 33 static const char copyright[] = 34 "@(#) Copyright (c) 1989, 1993\n\ 35 The Regents of the University of California. All rights reserved.\n"; 36 #endif 37 38 #if 0 39 #ifndef lint 40 static char sccsid[] = "@(#)calendar.c 8.3 (Berkeley) 3/25/94"; 41 #endif 42 #endif 43 44 #include <sys/cdefs.h> 45 __FBSDID("$FreeBSD$"); 46 47 #include <sys/types.h> 48 #include <err.h> 49 #include <errno.h> 50 #include <locale.h> 51 #include <login_cap.h> 52 #include <langinfo.h> 53 #include <pwd.h> 54 #include <stdio.h> 55 #include <stdlib.h> 56 #include <string.h> 57 #include <time.h> 58 #include <unistd.h> 59 60 #include "calendar.h" 61 62 #define UTCOFFSET_NOTSET 100 /* Expected between -24 and +24 */ 63 #define LONGITUDE_NOTSET 1000 /* Expected between -360 and +360 */ 64 65 struct passwd *pw; 66 int doall = 0; 67 int debug = 0; 68 static char *DEBUG = NULL; 69 static time_t f_time = 0; 70 double UTCOffset = UTCOFFSET_NOTSET; 71 int EastLongitude = LONGITUDE_NOTSET; 72 #ifdef WITH_ICONV 73 const char *outputEncoding = NULL; 74 #endif 75 76 static void usage(void) __dead2; 77 78 int 79 main(int argc, char *argv[]) 80 { 81 int f_dayAfter = 0; /* days after current date */ 82 int f_dayBefore = 0; /* days before current date */ 83 int Friday = 5; /* day before weekend */ 84 85 int ch; 86 struct tm tp1, tp2; 87 88 (void)setlocale(LC_ALL, ""); 89 90 while ((ch = getopt(argc, argv, "-A:aB:D:dF:f:l:t:U:W:?")) != -1) 91 switch (ch) { 92 case '-': /* backward contemptible */ 93 case 'a': 94 if (getuid()) { 95 errno = EPERM; 96 err(1, NULL); 97 } 98 doall = 1; 99 break; 100 101 case 'W': /* we don't need no steenking Fridays */ 102 Friday = -1; 103 /* FALLTHROUGH */ 104 105 case 'A': /* days after current date */ 106 f_dayAfter = atoi(optarg); 107 if (f_dayAfter < 0) 108 errx(1, "number of days must be positive"); 109 break; 110 111 case 'B': /* days before current date */ 112 f_dayBefore = atoi(optarg); 113 if (f_dayBefore < 0) 114 errx(1, "number of days must be positive"); 115 break; 116 117 case 'D': /* debug output of sun and moon info */ 118 DEBUG = optarg; 119 break; 120 121 case 'd': /* debug output of current date */ 122 debug = 1; 123 break; 124 125 case 'F': /* Change the time: When does weekend start? */ 126 Friday = atoi(optarg); 127 break; 128 129 case 'f': /* other calendar file */ 130 calendarFile = optarg; 131 break; 132 133 case 'l': /* Change longitudal position */ 134 EastLongitude = strtol(optarg, NULL, 10); 135 break; 136 137 case 't': /* other date, for tests */ 138 f_time = Mktime(optarg); 139 break; 140 141 case 'U': /* Change UTC offset */ 142 UTCOffset = strtod(optarg, NULL); 143 break; 144 145 case '?': 146 default: 147 usage(); 148 } 149 150 argc -= optind; 151 argv += optind; 152 153 if (argc) 154 usage(); 155 156 /* use current time */ 157 if (f_time <= 0) 158 (void)time(&f_time); 159 160 /* if not set, determine where I could be */ 161 { 162 if (UTCOffset == UTCOFFSET_NOTSET && 163 EastLongitude == LONGITUDE_NOTSET) { 164 /* Calculate on difference between here and UTC */ 165 time_t t; 166 struct tm tm; 167 long utcoffset, hh, mm, ss; 168 double uo; 169 170 time(&t); 171 localtime_r(&t, &tm); 172 utcoffset = tm.tm_gmtoff; 173 /* seconds -> hh:mm:ss */ 174 hh = utcoffset / SECSPERHOUR; 175 utcoffset %= SECSPERHOUR; 176 mm = utcoffset / SECSPERMINUTE; 177 utcoffset %= SECSPERMINUTE; 178 ss = utcoffset; 179 180 /* hh:mm:ss -> hh.mmss */ 181 uo = mm + (100.0 * (ss / 60.0)); 182 uo /= 60.0 / 100.0; 183 uo = hh + uo / 100; 184 185 UTCOffset = uo; 186 EastLongitude = UTCOffset * 15; 187 } else if (UTCOffset == UTCOFFSET_NOTSET) { 188 /* Base on information given */ 189 UTCOffset = EastLongitude / 15; 190 } else if (EastLongitude == LONGITUDE_NOTSET) { 191 /* Base on information given */ 192 EastLongitude = UTCOffset * 15; 193 } 194 } 195 196 settimes(f_time, f_dayBefore, f_dayAfter, Friday, &tp1, &tp2); 197 generatedates(&tp1, &tp2); 198 199 /* 200 * FROM now on, we are working in UTC. 201 * This will only affect moon and sun related events anyway. 202 */ 203 if (setenv("TZ", "UTC", 1) != 0) 204 errx(1, "setenv: %s", strerror(errno)); 205 tzset(); 206 207 if (debug) 208 dumpdates(); 209 210 if (DEBUG != NULL) { 211 dodebug(DEBUG); 212 exit(0); 213 } 214 215 if (doall) 216 while ((pw = getpwent()) != NULL) { 217 pid_t pid; 218 219 if (chdir(pw->pw_dir) == -1) 220 continue; 221 pid = fork(); 222 if (pid < 0) 223 err(1, "fork"); 224 if (pid == 0) { 225 login_cap_t *lc; 226 227 lc = login_getpwclass(pw); 228 if (setusercontext(lc, pw, pw->pw_uid, 229 LOGIN_SETALL) != 0) 230 errx(1, "setusercontext"); 231 cal(); 232 exit(0); 233 } 234 } 235 else { 236 #ifdef WITH_ICONV 237 /* Save the information about the encoding used in the terminal. */ 238 outputEncoding = strdup(nl_langinfo(CODESET)); 239 if (outputEncoding == NULL) 240 errx(1, "cannot allocate memory"); 241 #endif 242 cal(); 243 } 244 exit(0); 245 } 246 247 248 static void __dead2 249 usage(void) 250 { 251 252 fprintf(stderr, "%s\n%s\n%s\n", 253 "usage: calendar [-A days] [-a] [-B days] [-D sun|moon] [-d]", 254 " [-F friday] [-f calendarfile] [-l longitude]", 255 " [-t dd[.mm[.year]]] [-U utcoffset] [-W days]" 256 ); 257 exit(1); 258 } 259