Deleted Added
full compact
calendar.c (200470) calendar.c (205821)
1/*
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.

--- 29 unchanged lines hidden (view full) ---

39
40#if 0
41#ifndef lint
42static char sccsid[] = "@(#)calendar.c 8.3 (Berkeley) 3/25/94";
43#endif
44#endif
45
46#include <sys/cdefs.h>
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.

--- 29 unchanged lines hidden (view full) ---

39
40#if 0
41#ifndef lint
42static char sccsid[] = "@(#)calendar.c 8.3 (Berkeley) 3/25/94";
43#endif
44#endif
45
46#include <sys/cdefs.h>
47__FBSDID("$FreeBSD: head/usr.bin/calendar/calendar.c 200470 2009-12-13 04:50:11Z delphij $");
47__FBSDID("$FreeBSD: head/usr.bin/calendar/calendar.c 205821 2010-03-29 06:49:20Z edwin $");
48
49#include <err.h>
50#include <errno.h>
51#include <locale.h>
52#include <pwd.h>
53#include <stdio.h>
54#include <stdlib.h>
48
49#include <err.h>
50#include <errno.h>
51#include <locale.h>
52#include <pwd.h>
53#include <stdio.h>
54#include <stdlib.h>
55#include <string.h>
55#include <time.h>
56#include <unistd.h>
57
58#include "calendar.h"
59
56#include <time.h>
57#include <unistd.h>
58
59#include "calendar.h"
60
61#define UTCOFFSET_NOTSET 100 /* Expected between -24 and +24 */
62#define LONGITUDE_NOTSET 1000 /* Expected between -360 and +360 */
63
60struct passwd *pw;
61int doall = 0;
64struct passwd *pw;
65int doall = 0;
66int debug = 0;
67char *DEBUG = NULL;
62time_t f_time = 0;
68time_t f_time = 0;
69double UTCOffset = UTCOFFSET_NOTSET;
70int EastLongitude = LONGITUDE_NOTSET;
63
71
64int f_dayAfter = 0; /* days after current date */
65int f_dayBefore = 0; /* days before current date */
66int Friday = 5; /* day before weekend */
67
68static void usage(void) __dead2;
69
70int
71main(int argc, char *argv[])
72{
72static void usage(void) __dead2;
73
74int
75main(int argc, char *argv[])
76{
77 int f_dayAfter = 0; /* days after current date */
78 int f_dayBefore = 0; /* days before current date */
79 int Friday = 5; /* day before weekend */
80
73 int ch;
81 int ch;
82 struct tm tp1, tp2;
74
75 (void)setlocale(LC_ALL, "");
76
83
84 (void)setlocale(LC_ALL, "");
85
77 while ((ch = getopt(argc, argv, "-A:aB:F:f:t:W:")) != -1)
86 while ((ch = getopt(argc, argv, "-A:aB:dD:F:f:l:t:U:W:")) != -1)
78 switch (ch) {
79 case '-': /* backward contemptible */
80 case 'a':
81 if (getuid()) {
82 errno = EPERM;
83 err(1, NULL);
84 }
85 doall = 1;
86 break;
87
88 case 'f': /* other calendar file */
89 calendarFile = optarg;
90 break;
91
87 switch (ch) {
88 case '-': /* backward contemptible */
89 case 'a':
90 if (getuid()) {
91 errno = EPERM;
92 err(1, NULL);
93 }
94 doall = 1;
95 break;
96
97 case 'f': /* other calendar file */
98 calendarFile = optarg;
99 break;
100
92 case 't': /* other date, undocumented, for tests */
93 f_time = Mktime(optarg);
94 break;
95
96 case 'W': /* we don't need no steenking Fridays */
97 Friday = -1;
101 case 'W': /* we don't need no steenking Fridays */
102 Friday = -1;
98
99 /* FALLTHROUGH */
103 /* FALLTHROUGH */
104
100 case 'A': /* days after current date */
101 f_dayAfter = atoi(optarg);
102 break;
103
104 case 'B': /* days before current date */
105 f_dayBefore = atoi(optarg);
106 break;
107
105 case 'A': /* days after current date */
106 f_dayAfter = atoi(optarg);
107 break;
108
109 case 'B': /* days before current date */
110 f_dayBefore = atoi(optarg);
111 break;
112
108 case 'F':
113 case 'F': /* Change the time: When does weekend start? */
109 Friday = atoi(optarg);
110 break;
114 Friday = atoi(optarg);
115 break;
116 case 'l': /* Change longitudal position */
117 EastLongitude = strtol(optarg, NULL, 10);
118 break;
119 case 'U': /* Change UTC offset */
120 UTCOffset = strtod(optarg, NULL);
121 break;
111
122
123 case 'd':
124 debug = 1;
125 break;
126 case 'D':
127 DEBUG = optarg;
128 break;
129 case 't': /* other date, undocumented, for tests */
130 f_time = Mktime(optarg);
131 break;
132
112 case '?':
113 default:
114 usage();
115 }
116
117 argc -= optind;
118 argv += optind;
119
120 if (argc)
121 usage();
122
123 /* use current time */
124 if (f_time <= 0)
125 (void)time(&f_time);
126
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
127 settime(f_time);
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;
128
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
129 if (doall)
130 while ((pw = getpwent()) != NULL) {
131 (void)setegid(pw->pw_gid);
132 (void)initgroups(pw->pw_name, pw->pw_gid);
133 (void)seteuid(pw->pw_uid);
134 if (!chdir(pw->pw_dir))
135 cal();
136 (void)seteuid(0);
137 }
138 else
139 cal();
140 exit(0);
141}
142
143
144static void __dead2
145usage(void)
146{
147
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
218static void __dead2
219usage(void)
220{
221
148 fprintf(stderr, "%s\n%s\n",
222 fprintf(stderr, "%s\n%s\n%s\n",
149 "usage: calendar [-a] [-A days] [-B days] [-F friday] "
150 "[-f calendarfile]",
223 "usage: calendar [-a] [-A days] [-B days] [-F friday] "
224 "[-f calendarfile]",
151 " [-t dd[.mm[.year]]] [-W days]");
225 " [-d] [-t dd[.mm[.year]]] [-W days]",
226 " [-U utcoffset] [-l longitude]"
227 );
152 exit(1);
153}
228 exit(1);
229}