10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 70Sstevel@tonic-gate * with the License. 80Sstevel@tonic-gate * 90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 110Sstevel@tonic-gate * See the License for the specific language governing permissions 120Sstevel@tonic-gate * and limitations under the License. 130Sstevel@tonic-gate * 140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 190Sstevel@tonic-gate * 200Sstevel@tonic-gate * CDDL HEADER END 210Sstevel@tonic-gate */ 220Sstevel@tonic-gate /* 23*428Ssl108498 * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 280Sstevel@tonic-gate /* All Rights Reserved */ 290Sstevel@tonic-gate 300Sstevel@tonic-gate 310Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" /* SVr4.0 1.7 */ 320Sstevel@tonic-gate 330Sstevel@tonic-gate /* 340Sstevel@tonic-gate * pnpsplit splits interval into prime & nonprime portions 350Sstevel@tonic-gate * ONLY ROUTINE THAT KNOWS ABOUT HOLIDAYS AND DEFN OF PRIME/NONPRIME 360Sstevel@tonic-gate */ 370Sstevel@tonic-gate 380Sstevel@tonic-gate #include <stdio.h> 390Sstevel@tonic-gate #include <sys/types.h> 400Sstevel@tonic-gate #include <sys/param.h> 410Sstevel@tonic-gate #include "acctdef.h" 420Sstevel@tonic-gate #include <time.h> 430Sstevel@tonic-gate #include <ctype.h> 440Sstevel@tonic-gate 450Sstevel@tonic-gate /* validate that hours and minutes of prime/non-prime read in 460Sstevel@tonic-gate * from holidays file fall within proper boundaries. 470Sstevel@tonic-gate * Time is expected in the form and range of 0000-2359. 480Sstevel@tonic-gate */ 490Sstevel@tonic-gate 500Sstevel@tonic-gate static int thisyear = 1970; /* this is changed by holidays file */ 510Sstevel@tonic-gate static int holidays[NHOLIDAYS]; /* holidays file day-of-year table */ 520Sstevel@tonic-gate 530Sstevel@tonic-gate static int day_tab[2][13] = { 540Sstevel@tonic-gate {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, 550Sstevel@tonic-gate {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} 560Sstevel@tonic-gate }; 570Sstevel@tonic-gate 580Sstevel@tonic-gate /* 590Sstevel@tonic-gate * prime(0) and nonprime(1) times during a day 600Sstevel@tonic-gate * for BTL, prime time is 9AM to 5PM 610Sstevel@tonic-gate */ 620Sstevel@tonic-gate static struct hours { 630Sstevel@tonic-gate int h_sec; /* normally always zero */ 640Sstevel@tonic-gate int h_min; /* initialized from holidays file (time%100) */ 650Sstevel@tonic-gate int h_hour; /* initialized from holidays file (time/100) */ 660Sstevel@tonic-gate long h_type; /* prime/nonprime of previous period */ 670Sstevel@tonic-gate } h[4]; 680Sstevel@tonic-gate int daysend[] = {60, 59, 23}; /* the sec, min, hr of the day's end */ 690Sstevel@tonic-gate 700Sstevel@tonic-gate struct tm *localtime(); 710Sstevel@tonic-gate long tmsecs(); 720Sstevel@tonic-gate 730Sstevel@tonic-gate /* 740Sstevel@tonic-gate * split interval of length etime, starting at start into prime/nonprime 750Sstevel@tonic-gate * values, return as result 760Sstevel@tonic-gate * input values in seconds 770Sstevel@tonic-gate */ 78*428Ssl108498 int 790Sstevel@tonic-gate pnpsplit(start, etime, result) 800Sstevel@tonic-gate long start, result[2]; 810Sstevel@tonic-gate ulong_t etime; 820Sstevel@tonic-gate { 830Sstevel@tonic-gate struct tm cur, end; 840Sstevel@tonic-gate time_t tcur, tend; 850Sstevel@tonic-gate long tmp; 86*428Ssl108498 int sameday; 870Sstevel@tonic-gate register struct hours *hp; 880Sstevel@tonic-gate char *memcpy(); 890Sstevel@tonic-gate 900Sstevel@tonic-gate /* once holidays file is read, this is zero */ 910Sstevel@tonic-gate if (thisyear && (checkhol() == 0)) { 920Sstevel@tonic-gate return(0); 930Sstevel@tonic-gate } 940Sstevel@tonic-gate tcur = start; 950Sstevel@tonic-gate tend = start+etime; 960Sstevel@tonic-gate memcpy(&end, localtime(&tend), sizeof(end)); 970Sstevel@tonic-gate result[PRIME] = 0; 980Sstevel@tonic-gate result[NONPRIME] = 0; 990Sstevel@tonic-gate 1000Sstevel@tonic-gate while (tcur < tend) { /* one iteration per day or part thereof */ 1010Sstevel@tonic-gate memcpy(&cur, localtime(&tcur), sizeof(cur)); 1020Sstevel@tonic-gate sameday = cur.tm_yday == end.tm_yday; 1030Sstevel@tonic-gate if (ssh(&cur)) { /* ssh:only NONPRIME */ 1040Sstevel@tonic-gate if (sameday) { 1050Sstevel@tonic-gate result[NONPRIME] += tend-tcur; 1060Sstevel@tonic-gate 1070Sstevel@tonic-gate break; 1080Sstevel@tonic-gate } else { 1090Sstevel@tonic-gate tmp = tmsecs(&cur, daysend); 1100Sstevel@tonic-gate result[NONPRIME] += tmp; 1110Sstevel@tonic-gate tcur += tmp; 1120Sstevel@tonic-gate } 1130Sstevel@tonic-gate } else { /* working day, PRIME or NONPRIME */ 1140Sstevel@tonic-gate for (hp = h; tmless(hp, &cur); hp++); 1150Sstevel@tonic-gate for (; hp->h_sec >= 0; hp++) { 1160Sstevel@tonic-gate if (sameday && tmless(&end, hp)) { 1170Sstevel@tonic-gate /* WHCC mod, change from = to += 3/6/86 Paul */ 1180Sstevel@tonic-gate result[hp->h_type] += tend-tcur; 1190Sstevel@tonic-gate tcur = tend; 1200Sstevel@tonic-gate break; /* all done */ 1210Sstevel@tonic-gate } else { /* time to next PRIME /NONPRIME change */ 1220Sstevel@tonic-gate tmp = tmsecs(&cur, hp); 1230Sstevel@tonic-gate result[hp->h_type] += tmp; 1240Sstevel@tonic-gate tcur += tmp; 1250Sstevel@tonic-gate cur.tm_sec = hp->h_sec; 1260Sstevel@tonic-gate cur.tm_min = hp->h_min; 1270Sstevel@tonic-gate cur.tm_hour = hp->h_hour; 1280Sstevel@tonic-gate } 1290Sstevel@tonic-gate } 1300Sstevel@tonic-gate } 1310Sstevel@tonic-gate } 1320Sstevel@tonic-gate return(1); 1330Sstevel@tonic-gate } 1340Sstevel@tonic-gate 1350Sstevel@tonic-gate /* 1360Sstevel@tonic-gate * Starting day after Christmas, complain if holidays not yet updated. 1370Sstevel@tonic-gate * This code is only executed once per program invocation. 1380Sstevel@tonic-gate */ 139*428Ssl108498 int 1400Sstevel@tonic-gate checkhol() 1410Sstevel@tonic-gate { 1420Sstevel@tonic-gate register struct tm *tp; 1430Sstevel@tonic-gate time_t t; 1440Sstevel@tonic-gate 1450Sstevel@tonic-gate if(inithol() == 0) { 1460Sstevel@tonic-gate fprintf(stderr, "pnpsplit: holidays table setup failed\n"); 1470Sstevel@tonic-gate thisyear = 0; 1480Sstevel@tonic-gate holidays[0] = -1; 1490Sstevel@tonic-gate return(0); 1500Sstevel@tonic-gate } 1510Sstevel@tonic-gate time(&t); 1520Sstevel@tonic-gate tp = localtime(&t); 1530Sstevel@tonic-gate tp->tm_year += 1900; 1540Sstevel@tonic-gate if ((tp->tm_year == thisyear && tp->tm_yday > 359) 1550Sstevel@tonic-gate || tp->tm_year > thisyear) 1560Sstevel@tonic-gate fprintf(stderr, 1570Sstevel@tonic-gate "***UPDATE %s WITH NEW HOLIDAYS***\n", HOLFILE); 1580Sstevel@tonic-gate thisyear = 0; /* checkhol() will not be called again */ 1590Sstevel@tonic-gate return(1); 1600Sstevel@tonic-gate } 1610Sstevel@tonic-gate 1620Sstevel@tonic-gate /* 1630Sstevel@tonic-gate * ssh returns 1 if Sat, Sun, or Holiday 1640Sstevel@tonic-gate */ 165*428Ssl108498 int 1660Sstevel@tonic-gate ssh(ltp) 1670Sstevel@tonic-gate register struct tm *ltp; 1680Sstevel@tonic-gate { 169*428Ssl108498 int i; 1700Sstevel@tonic-gate 1710Sstevel@tonic-gate if (ltp->tm_wday == 0 || ltp->tm_wday == 6) 1720Sstevel@tonic-gate return(1); 1730Sstevel@tonic-gate for (i = 0; holidays[i] >= 0; i++) 1740Sstevel@tonic-gate if (ltp->tm_yday == holidays[i]) 1750Sstevel@tonic-gate return(1); 1760Sstevel@tonic-gate return(0); 1770Sstevel@tonic-gate } 1780Sstevel@tonic-gate 1790Sstevel@tonic-gate /* 1800Sstevel@tonic-gate * inithol - read from an ascii file and initialize the "thisyear" 1810Sstevel@tonic-gate * variable, the times that prime and non-prime start, and the 1820Sstevel@tonic-gate * holidays array. 1830Sstevel@tonic-gate */ 184*428Ssl108498 int 1850Sstevel@tonic-gate inithol() 1860Sstevel@tonic-gate { 1870Sstevel@tonic-gate FILE *fopen(), *holptr; 1880Sstevel@tonic-gate char *fgets(), holbuf[128]; 1890Sstevel@tonic-gate register int line = 0, 1900Sstevel@tonic-gate holindx = 0, 1910Sstevel@tonic-gate errflag = 0; 1920Sstevel@tonic-gate int pstart, npstart; 1930Sstevel@tonic-gate int doy; /* day of the year */ 1940Sstevel@tonic-gate int month, day; 1950Sstevel@tonic-gate char *c; 1960Sstevel@tonic-gate 1970Sstevel@tonic-gate if((holptr=fopen(HOLFILE, "r")) == NULL) { 1980Sstevel@tonic-gate perror(HOLFILE); 1990Sstevel@tonic-gate fclose(holptr); 2000Sstevel@tonic-gate return(0); 2010Sstevel@tonic-gate } 2020Sstevel@tonic-gate while(fgets(holbuf, sizeof(holbuf), holptr) != NULL) { 2030Sstevel@tonic-gate /* skip over blank lines and comments */ 2040Sstevel@tonic-gate if (holbuf[0] == '*') 2050Sstevel@tonic-gate continue; 2060Sstevel@tonic-gate 2070Sstevel@tonic-gate for (c = holbuf; isspace(*c); c++) 2080Sstevel@tonic-gate /* is a space */; 2090Sstevel@tonic-gate 2100Sstevel@tonic-gate if (*c == '\0') 2110Sstevel@tonic-gate continue; 2120Sstevel@tonic-gate 2130Sstevel@tonic-gate else if(++line == 1) { /* format: year p-start np-start */ 2140Sstevel@tonic-gate if(sscanf(holbuf, "%4d %4d %4d", 2150Sstevel@tonic-gate &thisyear, &pstart, &npstart) != 3) { 2160Sstevel@tonic-gate fprintf(stderr, 2170Sstevel@tonic-gate "%s: bad {yr ptime nptime} conversion\n", 2180Sstevel@tonic-gate HOLFILE); 2190Sstevel@tonic-gate errflag++; 2200Sstevel@tonic-gate break; 2210Sstevel@tonic-gate } 2220Sstevel@tonic-gate 2230Sstevel@tonic-gate /* validate year */ 2240Sstevel@tonic-gate if(thisyear < 1970 || thisyear > 2037) { 2250Sstevel@tonic-gate fprintf(stderr, "pnpsplit: invalid year: %d\n", 2260Sstevel@tonic-gate thisyear); 2270Sstevel@tonic-gate errflag++; 2280Sstevel@tonic-gate break; 2290Sstevel@tonic-gate } 2300Sstevel@tonic-gate 2310Sstevel@tonic-gate /* validate prime/nonprime hours */ 2320Sstevel@tonic-gate if((! okay(pstart)) || (! okay(npstart))) { 2330Sstevel@tonic-gate fprintf(stderr, 2340Sstevel@tonic-gate "pnpsplit: invalid p/np hours\n"); 2350Sstevel@tonic-gate errflag++; 2360Sstevel@tonic-gate break; 2370Sstevel@tonic-gate } 2380Sstevel@tonic-gate 2390Sstevel@tonic-gate /* Set up start of prime time; 2400 == 0000 */ 2400Sstevel@tonic-gate h[0].h_sec = 0; 2410Sstevel@tonic-gate h[0].h_min = pstart%100; 2420Sstevel@tonic-gate h[0].h_hour = (pstart/100==24) ? 0 : pstart/100; 2430Sstevel@tonic-gate h[0].h_type = NONPRIME; 2440Sstevel@tonic-gate 2450Sstevel@tonic-gate /* Set up start of non-prime time; 2400 == 0000 */ 2460Sstevel@tonic-gate h[1].h_sec = 0; 2470Sstevel@tonic-gate h[1].h_min = npstart%100; 2480Sstevel@tonic-gate h[1].h_hour = (npstart/100==24) ? 0 : npstart/100; 2490Sstevel@tonic-gate h[1].h_type = PRIME ; 2500Sstevel@tonic-gate 2510Sstevel@tonic-gate /* This is the end of the day */ 2520Sstevel@tonic-gate h[2].h_sec = 60; 2530Sstevel@tonic-gate h[2].h_min = 59; 2540Sstevel@tonic-gate h[2].h_hour = 23; 2550Sstevel@tonic-gate h[2].h_type = NONPRIME; 2560Sstevel@tonic-gate 2570Sstevel@tonic-gate /* The end of the array */ 2580Sstevel@tonic-gate h[3].h_sec = -1; 2590Sstevel@tonic-gate 2600Sstevel@tonic-gate continue; 2610Sstevel@tonic-gate } 2620Sstevel@tonic-gate else if(holindx >= NHOLIDAYS) { 2630Sstevel@tonic-gate fprintf(stderr, "pnpsplit: too many holidays, "); 2640Sstevel@tonic-gate fprintf(stderr, "recompile with larger NHOLIDAYS\n"); 2650Sstevel@tonic-gate errflag++; 2660Sstevel@tonic-gate break; 2670Sstevel@tonic-gate } 2680Sstevel@tonic-gate 2690Sstevel@tonic-gate /* Fill up holidays array from holidays file */ 2700Sstevel@tonic-gate sscanf(holbuf, "%d/%d %*s %*s %*[^\n]\n", &month, &day); 2710Sstevel@tonic-gate if (month < 0 || month > 12) { 2720Sstevel@tonic-gate fprintf(stderr, "pnpsplit: invalid month %d\n", month); 2730Sstevel@tonic-gate errflag++; 2740Sstevel@tonic-gate break; 2750Sstevel@tonic-gate } 2760Sstevel@tonic-gate if (day < 0 || day > 31) { 2770Sstevel@tonic-gate fprintf(stderr, "pnpsplit: invalid day %d\n", day); 2780Sstevel@tonic-gate errflag++; 2790Sstevel@tonic-gate break; 2800Sstevel@tonic-gate } 2810Sstevel@tonic-gate doy = day_of_year(thisyear, month, day); 2820Sstevel@tonic-gate holidays[holindx++] = (doy - 1); 2830Sstevel@tonic-gate } 2840Sstevel@tonic-gate fclose(holptr); 2850Sstevel@tonic-gate if(!errflag && holindx < NHOLIDAYS) { 2860Sstevel@tonic-gate holidays[holindx] = -1; 2870Sstevel@tonic-gate return(1); 2880Sstevel@tonic-gate } 2890Sstevel@tonic-gate else 2900Sstevel@tonic-gate return(0); 2910Sstevel@tonic-gate } 2920Sstevel@tonic-gate 2930Sstevel@tonic-gate /* 2940Sstevel@tonic-gate * tmsecs returns number of seconds from t1 to t2, 2950Sstevel@tonic-gate * times expressed in localtime format. 2960Sstevel@tonic-gate * assumed that t1 <= t2, and are in same day. 2970Sstevel@tonic-gate */ 2980Sstevel@tonic-gate 2990Sstevel@tonic-gate long 3000Sstevel@tonic-gate tmsecs(t1, t2) 3010Sstevel@tonic-gate register struct tm *t1, *t2; 3020Sstevel@tonic-gate { 3030Sstevel@tonic-gate return((t2->tm_sec - t1->tm_sec) + 3040Sstevel@tonic-gate 60*(t2->tm_min - t1->tm_min) + 3050Sstevel@tonic-gate 3600L*(t2->tm_hour - t1->tm_hour)); 3060Sstevel@tonic-gate } 3070Sstevel@tonic-gate 3080Sstevel@tonic-gate /* 3090Sstevel@tonic-gate * return 1 if t1 earlier than t2 (times in localtime format) 3100Sstevel@tonic-gate * assumed that t1 and t2 are in same day 3110Sstevel@tonic-gate */ 3120Sstevel@tonic-gate 313*428Ssl108498 int 3140Sstevel@tonic-gate tmless(t1, t2) 3150Sstevel@tonic-gate register struct tm *t1, *t2; 3160Sstevel@tonic-gate { 3170Sstevel@tonic-gate if (t1->tm_hour != t2->tm_hour) 3180Sstevel@tonic-gate return(t1->tm_hour < t2->tm_hour); 3190Sstevel@tonic-gate if (t1->tm_min != t2->tm_min) 3200Sstevel@tonic-gate return(t1->tm_min < t2->tm_min); 3210Sstevel@tonic-gate return(t1->tm_sec < t2->tm_sec); 3220Sstevel@tonic-gate } 3230Sstevel@tonic-gate 3240Sstevel@tonic-gate /* set day of year from month and day */ 3250Sstevel@tonic-gate 326*428Ssl108498 int 3270Sstevel@tonic-gate day_of_year(year, month, day) 3280Sstevel@tonic-gate { 3290Sstevel@tonic-gate int i, leap; 3300Sstevel@tonic-gate 3310Sstevel@tonic-gate leap = year%4 == 0 && year%100 || year%400 == 0; 3320Sstevel@tonic-gate for (i = 1; i < month; i++) 3330Sstevel@tonic-gate day += day_tab[leap][i]; 3340Sstevel@tonic-gate return(day); 3350Sstevel@tonic-gate } 336