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