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