1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28*0Sstevel@tonic-gate /*	  All Rights Reserved	*/
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate 
31*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.7	*/
32*0Sstevel@tonic-gate 
33*0Sstevel@tonic-gate /*
34*0Sstevel@tonic-gate  * pnpsplit splits interval into prime & nonprime portions
35*0Sstevel@tonic-gate  * ONLY ROUTINE THAT KNOWS ABOUT HOLIDAYS AND DEFN OF PRIME/NONPRIME
36*0Sstevel@tonic-gate  */
37*0Sstevel@tonic-gate 
38*0Sstevel@tonic-gate #include <stdio.h>
39*0Sstevel@tonic-gate #include <sys/types.h>
40*0Sstevel@tonic-gate #include <sys/param.h>
41*0Sstevel@tonic-gate #include "acctdef.h"
42*0Sstevel@tonic-gate #include <time.h>
43*0Sstevel@tonic-gate #include <ctype.h>
44*0Sstevel@tonic-gate 
45*0Sstevel@tonic-gate /* validate that hours and minutes of prime/non-prime read in
46*0Sstevel@tonic-gate  * from holidays file fall within proper boundaries.
47*0Sstevel@tonic-gate  * Time is expected in the form and range of 0000-2359.
48*0Sstevel@tonic-gate  */
49*0Sstevel@tonic-gate 
50*0Sstevel@tonic-gate static int	thisyear = 1970;	/* this is changed by holidays file */
51*0Sstevel@tonic-gate static int	holidays[NHOLIDAYS];	/* holidays file day-of-year table */
52*0Sstevel@tonic-gate 
53*0Sstevel@tonic-gate static int day_tab[2][13] = {
54*0Sstevel@tonic-gate 	{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
55*0Sstevel@tonic-gate 	{0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
56*0Sstevel@tonic-gate };
57*0Sstevel@tonic-gate 
58*0Sstevel@tonic-gate /*
59*0Sstevel@tonic-gate  *	prime(0) and nonprime(1) times during a day
60*0Sstevel@tonic-gate  *	for BTL, prime time is 9AM to 5PM
61*0Sstevel@tonic-gate  */
62*0Sstevel@tonic-gate static struct hours {
63*0Sstevel@tonic-gate 	int	h_sec;		/* normally always zero */
64*0Sstevel@tonic-gate 	int	h_min;		/* initialized from holidays file (time%100) */
65*0Sstevel@tonic-gate 	int	h_hour;		/* initialized from holidays file (time/100) */
66*0Sstevel@tonic-gate 	long	h_type;		/* prime/nonprime of previous period */
67*0Sstevel@tonic-gate } h[4];
68*0Sstevel@tonic-gate int	daysend[] = {60, 59, 23};	/* the sec, min, hr of the day's end */
69*0Sstevel@tonic-gate 
70*0Sstevel@tonic-gate struct tm *localtime();
71*0Sstevel@tonic-gate long	tmsecs();
72*0Sstevel@tonic-gate 
73*0Sstevel@tonic-gate /*
74*0Sstevel@tonic-gate  * split interval of length etime, starting at start into prime/nonprime
75*0Sstevel@tonic-gate  * values, return as result
76*0Sstevel@tonic-gate  * input values in seconds
77*0Sstevel@tonic-gate  */
78*0Sstevel@tonic-gate pnpsplit(start, etime, result)
79*0Sstevel@tonic-gate long start, result[2];
80*0Sstevel@tonic-gate ulong_t etime;
81*0Sstevel@tonic-gate {
82*0Sstevel@tonic-gate 	struct tm cur, end;
83*0Sstevel@tonic-gate 	time_t tcur, tend;
84*0Sstevel@tonic-gate 	long tmp;
85*0Sstevel@tonic-gate 	register sameday;
86*0Sstevel@tonic-gate 	register struct hours *hp;
87*0Sstevel@tonic-gate 	char *memcpy();
88*0Sstevel@tonic-gate 
89*0Sstevel@tonic-gate 	/* once holidays file is read, this is zero */
90*0Sstevel@tonic-gate 	if (thisyear && (checkhol() == 0)) {
91*0Sstevel@tonic-gate 		return(0);
92*0Sstevel@tonic-gate 	}
93*0Sstevel@tonic-gate 	tcur = start;
94*0Sstevel@tonic-gate 	tend = start+etime;
95*0Sstevel@tonic-gate 	memcpy(&end, localtime(&tend), sizeof(end));
96*0Sstevel@tonic-gate 	result[PRIME] = 0;
97*0Sstevel@tonic-gate 	result[NONPRIME] = 0;
98*0Sstevel@tonic-gate 
99*0Sstevel@tonic-gate 	while (tcur < tend) {	/* one iteration per day or part thereof */
100*0Sstevel@tonic-gate 		memcpy(&cur, localtime(&tcur), sizeof(cur));
101*0Sstevel@tonic-gate 		sameday = cur.tm_yday == end.tm_yday;
102*0Sstevel@tonic-gate 		if (ssh(&cur)) {	/* ssh:only NONPRIME */
103*0Sstevel@tonic-gate 			if (sameday) {
104*0Sstevel@tonic-gate 				result[NONPRIME] += tend-tcur;
105*0Sstevel@tonic-gate 
106*0Sstevel@tonic-gate 				break;
107*0Sstevel@tonic-gate 			} else {
108*0Sstevel@tonic-gate 				tmp = tmsecs(&cur, daysend);
109*0Sstevel@tonic-gate 				result[NONPRIME] += tmp;
110*0Sstevel@tonic-gate 				tcur += tmp;
111*0Sstevel@tonic-gate 			}
112*0Sstevel@tonic-gate 		} else {	/* working day, PRIME or NONPRIME */
113*0Sstevel@tonic-gate 			for (hp = h; tmless(hp, &cur); hp++);
114*0Sstevel@tonic-gate 			for (; hp->h_sec >= 0; hp++) {
115*0Sstevel@tonic-gate 				if (sameday && tmless(&end, hp)) {
116*0Sstevel@tonic-gate 			/* WHCC mod, change from = to +=   3/6/86   Paul */
117*0Sstevel@tonic-gate 					result[hp->h_type] += tend-tcur;
118*0Sstevel@tonic-gate 					tcur = tend;
119*0Sstevel@tonic-gate 					break;	/* all done */
120*0Sstevel@tonic-gate 				} else {	/* time to next PRIME /NONPRIME change */
121*0Sstevel@tonic-gate 					tmp = tmsecs(&cur, hp);
122*0Sstevel@tonic-gate 					result[hp->h_type] += tmp;
123*0Sstevel@tonic-gate 					tcur += tmp;
124*0Sstevel@tonic-gate 					cur.tm_sec = hp->h_sec;
125*0Sstevel@tonic-gate 					cur.tm_min = hp->h_min;
126*0Sstevel@tonic-gate 					cur.tm_hour = hp->h_hour;
127*0Sstevel@tonic-gate 				}
128*0Sstevel@tonic-gate 			}
129*0Sstevel@tonic-gate 		}
130*0Sstevel@tonic-gate 	}
131*0Sstevel@tonic-gate 	return(1);
132*0Sstevel@tonic-gate }
133*0Sstevel@tonic-gate 
134*0Sstevel@tonic-gate /*
135*0Sstevel@tonic-gate  *	Starting day after Christmas, complain if holidays not yet updated.
136*0Sstevel@tonic-gate  *	This code is only executed once per program invocation.
137*0Sstevel@tonic-gate  */
138*0Sstevel@tonic-gate checkhol()
139*0Sstevel@tonic-gate {
140*0Sstevel@tonic-gate 	register struct tm *tp;
141*0Sstevel@tonic-gate 	time_t t;
142*0Sstevel@tonic-gate 
143*0Sstevel@tonic-gate 	if(inithol() == 0) {
144*0Sstevel@tonic-gate 		fprintf(stderr, "pnpsplit: holidays table setup failed\n");
145*0Sstevel@tonic-gate 		thisyear = 0;
146*0Sstevel@tonic-gate 		holidays[0] = -1;
147*0Sstevel@tonic-gate 		return(0);
148*0Sstevel@tonic-gate 	}
149*0Sstevel@tonic-gate 	time(&t);
150*0Sstevel@tonic-gate 	tp = localtime(&t);
151*0Sstevel@tonic-gate 	tp->tm_year += 1900;
152*0Sstevel@tonic-gate 	if ((tp->tm_year == thisyear && tp->tm_yday > 359)
153*0Sstevel@tonic-gate 		|| tp->tm_year > thisyear)
154*0Sstevel@tonic-gate 		fprintf(stderr,
155*0Sstevel@tonic-gate 			"***UPDATE %s WITH NEW HOLIDAYS***\n", HOLFILE);
156*0Sstevel@tonic-gate 	thisyear = 0;	/* checkhol() will not be called again */
157*0Sstevel@tonic-gate 	return(1);
158*0Sstevel@tonic-gate }
159*0Sstevel@tonic-gate 
160*0Sstevel@tonic-gate /*
161*0Sstevel@tonic-gate  * ssh returns 1 if Sat, Sun, or Holiday
162*0Sstevel@tonic-gate  */
163*0Sstevel@tonic-gate ssh(ltp)
164*0Sstevel@tonic-gate register struct tm *ltp;
165*0Sstevel@tonic-gate {
166*0Sstevel@tonic-gate 	register i;
167*0Sstevel@tonic-gate 
168*0Sstevel@tonic-gate 	if (ltp->tm_wday == 0 || ltp->tm_wday == 6)
169*0Sstevel@tonic-gate 		return(1);
170*0Sstevel@tonic-gate 	for (i = 0; holidays[i] >= 0; i++)
171*0Sstevel@tonic-gate 		if (ltp->tm_yday == holidays[i])
172*0Sstevel@tonic-gate 			return(1);
173*0Sstevel@tonic-gate 	return(0);
174*0Sstevel@tonic-gate }
175*0Sstevel@tonic-gate 
176*0Sstevel@tonic-gate /*
177*0Sstevel@tonic-gate  * inithol - read from an ascii file and initialize the "thisyear"
178*0Sstevel@tonic-gate  * variable, the times that prime and non-prime start, and the
179*0Sstevel@tonic-gate  * holidays array.
180*0Sstevel@tonic-gate  */
181*0Sstevel@tonic-gate inithol()
182*0Sstevel@tonic-gate {
183*0Sstevel@tonic-gate 	FILE		*fopen(), *holptr;
184*0Sstevel@tonic-gate 	char		*fgets(), holbuf[128];
185*0Sstevel@tonic-gate 	register int	line = 0,
186*0Sstevel@tonic-gate 			holindx = 0,
187*0Sstevel@tonic-gate 			errflag = 0;
188*0Sstevel@tonic-gate 	int		pstart, npstart;
189*0Sstevel@tonic-gate 	int		doy;	/* day of the year */
190*0Sstevel@tonic-gate 	int 		month, day;
191*0Sstevel@tonic-gate 	char		*c;
192*0Sstevel@tonic-gate 
193*0Sstevel@tonic-gate 	if((holptr=fopen(HOLFILE, "r")) == NULL) {
194*0Sstevel@tonic-gate 		perror(HOLFILE);
195*0Sstevel@tonic-gate 		fclose(holptr);
196*0Sstevel@tonic-gate 		return(0);
197*0Sstevel@tonic-gate 	}
198*0Sstevel@tonic-gate 	while(fgets(holbuf, sizeof(holbuf), holptr) != NULL) {
199*0Sstevel@tonic-gate 		/* skip over blank lines and comments */
200*0Sstevel@tonic-gate 		if (holbuf[0] == '*')
201*0Sstevel@tonic-gate 			continue;
202*0Sstevel@tonic-gate 
203*0Sstevel@tonic-gate 		for (c = holbuf; isspace(*c); c++)
204*0Sstevel@tonic-gate 			/* is a space */;
205*0Sstevel@tonic-gate 
206*0Sstevel@tonic-gate 		if (*c == '\0')
207*0Sstevel@tonic-gate 			continue;
208*0Sstevel@tonic-gate 
209*0Sstevel@tonic-gate 		else if(++line == 1) {	/* format: year p-start np-start */
210*0Sstevel@tonic-gate 			if(sscanf(holbuf, "%4d %4d %4d",
211*0Sstevel@tonic-gate 				&thisyear, &pstart, &npstart) != 3) {
212*0Sstevel@tonic-gate 				fprintf(stderr,
213*0Sstevel@tonic-gate 					"%s: bad {yr ptime nptime} conversion\n",
214*0Sstevel@tonic-gate 					HOLFILE);
215*0Sstevel@tonic-gate 				errflag++;
216*0Sstevel@tonic-gate 				break;
217*0Sstevel@tonic-gate 			}
218*0Sstevel@tonic-gate 
219*0Sstevel@tonic-gate 			/* validate year */
220*0Sstevel@tonic-gate 			if(thisyear < 1970 || thisyear > 2037) {
221*0Sstevel@tonic-gate 				fprintf(stderr, "pnpsplit: invalid year: %d\n",
222*0Sstevel@tonic-gate 					thisyear);
223*0Sstevel@tonic-gate 				errflag++;
224*0Sstevel@tonic-gate 				break;
225*0Sstevel@tonic-gate 			}
226*0Sstevel@tonic-gate 
227*0Sstevel@tonic-gate 			/* validate prime/nonprime hours */
228*0Sstevel@tonic-gate 			if((! okay(pstart)) || (! okay(npstart))) {
229*0Sstevel@tonic-gate 				fprintf(stderr,
230*0Sstevel@tonic-gate 					"pnpsplit: invalid p/np hours\n");
231*0Sstevel@tonic-gate 				errflag++;
232*0Sstevel@tonic-gate 				break;
233*0Sstevel@tonic-gate 			}
234*0Sstevel@tonic-gate 
235*0Sstevel@tonic-gate 			/* Set up start of prime time; 2400 == 0000 */
236*0Sstevel@tonic-gate 			h[0].h_sec = 0;
237*0Sstevel@tonic-gate 			h[0].h_min = pstart%100;
238*0Sstevel@tonic-gate 			h[0].h_hour = (pstart/100==24) ? 0 : pstart/100;
239*0Sstevel@tonic-gate 			h[0].h_type = NONPRIME;
240*0Sstevel@tonic-gate 
241*0Sstevel@tonic-gate 			/* Set up start of non-prime time; 2400 == 0000 */
242*0Sstevel@tonic-gate 			h[1].h_sec = 0;
243*0Sstevel@tonic-gate 			h[1].h_min = npstart%100;
244*0Sstevel@tonic-gate 			h[1].h_hour = (npstart/100==24) ? 0 : npstart/100;
245*0Sstevel@tonic-gate 			h[1].h_type = PRIME ;
246*0Sstevel@tonic-gate 
247*0Sstevel@tonic-gate 			/* This is the end of the day */
248*0Sstevel@tonic-gate 			h[2].h_sec = 60;
249*0Sstevel@tonic-gate 			h[2].h_min = 59;
250*0Sstevel@tonic-gate 			h[2].h_hour = 23;
251*0Sstevel@tonic-gate 			h[2].h_type = NONPRIME;
252*0Sstevel@tonic-gate 
253*0Sstevel@tonic-gate 			/* The end of the array */
254*0Sstevel@tonic-gate 			h[3].h_sec = -1;
255*0Sstevel@tonic-gate 
256*0Sstevel@tonic-gate 			continue;
257*0Sstevel@tonic-gate 		}
258*0Sstevel@tonic-gate 		else if(holindx >= NHOLIDAYS) {
259*0Sstevel@tonic-gate 			fprintf(stderr, "pnpsplit: too many holidays, ");
260*0Sstevel@tonic-gate 			fprintf(stderr, "recompile with larger NHOLIDAYS\n");
261*0Sstevel@tonic-gate 			errflag++;
262*0Sstevel@tonic-gate 			break;
263*0Sstevel@tonic-gate 		}
264*0Sstevel@tonic-gate 
265*0Sstevel@tonic-gate 		/* Fill up holidays array from holidays file */
266*0Sstevel@tonic-gate 		sscanf(holbuf, "%d/%d	%*s %*s	%*[^\n]\n", &month, &day);
267*0Sstevel@tonic-gate 		if (month < 0 || month > 12) {
268*0Sstevel@tonic-gate 			fprintf(stderr, "pnpsplit: invalid month %d\n", month);
269*0Sstevel@tonic-gate 			errflag++;
270*0Sstevel@tonic-gate 			break;
271*0Sstevel@tonic-gate 		}
272*0Sstevel@tonic-gate 		if (day < 0 || day > 31) {
273*0Sstevel@tonic-gate 			fprintf(stderr, "pnpsplit: invalid day %d\n", day);
274*0Sstevel@tonic-gate 			errflag++;
275*0Sstevel@tonic-gate 			break;
276*0Sstevel@tonic-gate 		}
277*0Sstevel@tonic-gate 		doy = day_of_year(thisyear, month, day);
278*0Sstevel@tonic-gate 		holidays[holindx++] = (doy - 1);
279*0Sstevel@tonic-gate 	}
280*0Sstevel@tonic-gate 	fclose(holptr);
281*0Sstevel@tonic-gate 	if(!errflag && holindx < NHOLIDAYS) {
282*0Sstevel@tonic-gate 		holidays[holindx] = -1;
283*0Sstevel@tonic-gate 		return(1);
284*0Sstevel@tonic-gate 	}
285*0Sstevel@tonic-gate 	else
286*0Sstevel@tonic-gate 		return(0);
287*0Sstevel@tonic-gate }
288*0Sstevel@tonic-gate 
289*0Sstevel@tonic-gate /*
290*0Sstevel@tonic-gate  *	tmsecs returns number of seconds from t1 to t2,
291*0Sstevel@tonic-gate  *	times expressed in localtime format.
292*0Sstevel@tonic-gate  *	assumed that t1 <= t2, and are in same day.
293*0Sstevel@tonic-gate  */
294*0Sstevel@tonic-gate 
295*0Sstevel@tonic-gate long
296*0Sstevel@tonic-gate tmsecs(t1, t2)
297*0Sstevel@tonic-gate register struct tm *t1, *t2;
298*0Sstevel@tonic-gate {
299*0Sstevel@tonic-gate 	return((t2->tm_sec - t1->tm_sec) +
300*0Sstevel@tonic-gate 		60*(t2->tm_min - t1->tm_min) +
301*0Sstevel@tonic-gate 		3600L*(t2->tm_hour - t1->tm_hour));
302*0Sstevel@tonic-gate }
303*0Sstevel@tonic-gate 
304*0Sstevel@tonic-gate /*
305*0Sstevel@tonic-gate  *	return 1 if t1 earlier than t2 (times in localtime format)
306*0Sstevel@tonic-gate  *	assumed that t1 and t2 are in same day
307*0Sstevel@tonic-gate  */
308*0Sstevel@tonic-gate 
309*0Sstevel@tonic-gate tmless(t1, t2)
310*0Sstevel@tonic-gate register struct tm *t1, *t2;
311*0Sstevel@tonic-gate {
312*0Sstevel@tonic-gate 	if (t1->tm_hour != t2->tm_hour)
313*0Sstevel@tonic-gate 		return(t1->tm_hour < t2->tm_hour);
314*0Sstevel@tonic-gate 	if (t1->tm_min != t2->tm_min)
315*0Sstevel@tonic-gate 		return(t1->tm_min < t2->tm_min);
316*0Sstevel@tonic-gate 	return(t1->tm_sec < t2->tm_sec);
317*0Sstevel@tonic-gate }
318*0Sstevel@tonic-gate 
319*0Sstevel@tonic-gate /* set day of year from month and day */
320*0Sstevel@tonic-gate 
321*0Sstevel@tonic-gate day_of_year(year, month, day)
322*0Sstevel@tonic-gate {
323*0Sstevel@tonic-gate 	int i, leap;
324*0Sstevel@tonic-gate 
325*0Sstevel@tonic-gate 	leap = year%4 == 0 && year%100 || year%400 == 0;
326*0Sstevel@tonic-gate 	for (i = 1; i < month; i++)
327*0Sstevel@tonic-gate 		day += day_tab[leap][i];
328*0Sstevel@tonic-gate 	return(day);
329*0Sstevel@tonic-gate }
330