1/* 2 * Copyright (c) 2000, Hellmuth Michaelis. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23 * SUCH DAMAGE. 24 * 25 *--------------------------------------------------------------------------- 26 * 27 * isdnd - holiday file handling 28 * ============================= 29 * 30 * $Id: holiday.c,v 1.4 2006/05/24 23:43:11 christos Exp $ 31 * 32 * $FreeBSD$ 33 * 34 * last edit-date: [Mon Oct 9 11:32:28 2000] 35 * 36 * Format: 37 * 38 * day.month.year optional comment (different day every year) or 39 * day.month optional comment (same day every year) 40 * 41 * i.e.: 42 * 43 * 23.4.2000 Ostersonntag 44 * 3.10 Tag der deutschen Einheit 45 * 46 * 47 * 48 *----------------------------------------------------------------------------*/ 49 50#include "isdnd.h" 51 52struct holiday { 53 int day; 54 int month; 55 int year; 56 struct holiday *next; 57}; 58 59static struct holiday *firsth = NULL; 60 61#define MAXBUFSZ 256 62 63static void free_holiday(struct holiday *ptr); 64 65/*---------------------------------------------------------------------------* 66 * read in and init holidayes 67 *---------------------------------------------------------------------------*/ 68void 69init_holidays(char *filename) 70{ 71 FILE *fp; 72 unsigned char buffer[MAXBUFSZ + 1]; 73 struct holiday *newh = NULL; 74 struct holiday *lasth = NULL; 75 int ret; 76 int day, month, year; 77 78 firsth = NULL; 79 80 if ((fp = fopen(filename, "r")) == NULL) 81 { 82 logit(LL_ERR, "init_holiday: error opening holidayfile %s: %s!", filename, strerror(errno)); 83 exit(1); 84 } 85 86 while((fgets(buffer, MAXBUFSZ, fp)) != NULL) 87 { 88 if (buffer[0] == '#' || buffer[0] == ' ' || 89 buffer[0] == '\t' || buffer[0] == '\n') 90 { 91 continue; 92 } 93 94 ret = sscanf(buffer, "%d.%d.%d", &day, &month, &year); 95 96 if (ret != 3) 97 { 98 ret = sscanf(buffer, "%d.%d", &day, &month); 99 if (ret != 2) 100 { 101 logit(LL_ERR, "init_holiday: parse error for string [%s]!", buffer); 102 exit(1); 103 } 104 year = 0; 105 } 106 107 if ((newh = (struct holiday *) malloc(sizeof(struct holiday))) == NULL) 108 { 109 logit(LL_ERR, "init_holiday: malloc failed for struct holiday!\n"); 110 exit(1); 111 } 112 113 if (year) 114 { 115 DBGL(DL_MSG, (logit(LL_DBG, "init_holidays: add %d.%d.%d", day, month, year))); 116 } 117 else 118 { 119 DBGL(DL_MSG, (logit(LL_DBG, "init_holidays: add %d.%d", day, month))); 120 } 121 122 newh->day = day; 123 newh->month = month; 124 newh->year = year; 125 newh->next = NULL; 126 127 if (firsth == NULL || lasth == NULL) 128 { 129 firsth = newh; 130 } 131 else 132 { 133 lasth->next = newh; 134 } 135 lasth = newh; 136 } 137 fclose(fp); 138} 139 140/*---------------------------------------------------------------------------* 141 * free all holidays 142 *---------------------------------------------------------------------------*/ 143void 144free_holidays(void) 145{ 146 free_holiday(firsth); 147} 148 149/*---------------------------------------------------------------------------* 150 * free holidayes 151 *---------------------------------------------------------------------------*/ 152static void 153free_holiday(struct holiday *ptr) 154{ 155 156 if (ptr == NULL) 157 return; 158 159 if (ptr->next != NULL) 160 free_holiday(ptr->next); 161 162 free(ptr); 163} 164 165/*---------------------------------------------------------------------------* 166 * check if date/month/year is a holiday 167 *---------------------------------------------------------------------------*/ 168int 169isholiday(int d, int m, int y) 170{ 171 struct holiday *ch = NULL; 172 173 if (firsth == NULL) 174 return(0); 175 176 ch = firsth; 177 178 for (;;) 179 { 180 if (ch->day == d && ch->month == m) 181 { 182 if (ch->year == 0) 183 { 184 DBGL(DL_MSG, (logit(LL_DBG, "isholiday: %d.%d is a holiday!", d, m))); 185 return(1); 186 } 187 else if (ch->year == y) 188 { 189 DBGL(DL_MSG, (logit(LL_DBG, "isholiday: %d.%d.%d is a holiday!", d, m, y))); 190 return(1); 191 } 192 } 193 194 if (ch->next == NULL) 195 break; 196 197 ch = ch->next; 198 } 199 return(0); 200} 201 202/* EOF */ 203