Initial import from FreeBSD RELENG_4:
[dragonfly.git] / usr.sbin / i4b / isdnd / holiday.c
1 /*
2  * Copyright (c) 2000, 2001 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  * $FreeBSD: src/usr.sbin/i4b/isdnd/holiday.c,v 1.3.2.1 2001/08/01 17:45:03 obrien Exp $
31  *
32  *      last edit-date: [Wed May  2 09:42:56 2001]
33  *
34  *      Format:
35  *
36  *      day.month.year  optional comment (different day every year)     or
37  *      day.month       optional comment (same day every year)
38  *
39  *      i.e.:
40  *
41  *      23.4.2000       Ostersonntag
42  *      3.10            Tag der deutschen Einheit
43  *
44  *----------------------------------------------------------------------------*/
45
46 #include "isdnd.h"
47
48 struct holiday {
49         int     day;
50         int     month;
51         int     year;
52         struct holiday *next;
53 };
54
55 static struct holiday *firsth = NULL;
56
57 #define MAXBUFSZ        256
58
59 static void free_holiday(struct holiday *ptr);
60
61 /*---------------------------------------------------------------------------*
62  *      read in and init holidayes
63  *---------------------------------------------------------------------------*/
64 void 
65 init_holidays(char *filename)
66 {
67         FILE *fp;
68         unsigned char buffer[MAXBUFSZ + 1];
69         struct holiday *newh = NULL;
70         struct holiday *lasth = NULL;
71         int ret;
72         int day, month, year;
73         
74         firsth = NULL;
75         
76         if((fp = fopen(filename, "r")) == NULL)
77         {
78                 DBGL(DL_VALID, (log(LL_DBG, "init_holiday: error opening holidayfile %s: %s!", filename, strerror(errno))));
79                 return;
80         }
81
82         while((fgets(buffer, MAXBUFSZ, fp)) != NULL)
83         {
84                 if(buffer[0] == '#'  || buffer[0] == ' ' ||
85                    buffer[0] == '\t' || buffer[0] == '\n')
86                 {
87                         continue;
88                 }
89
90                 ret = sscanf(buffer, "%d.%d.%d", &day, &month, &year);
91
92                 if(ret != 3)
93                 {
94                         ret = sscanf(buffer, "%d.%d", &day, &month);
95                         if(ret != 2)
96                         {
97                                 log(LL_ERR, "init_holiday: parse error for string [%s]!", buffer);
98                                 exit(1);
99                         }
100                         year = 0;
101                 }
102
103                 if((newh = (struct holiday *) malloc(sizeof(struct holiday))) == NULL)
104                 {
105                         log(LL_ERR, "init_holiday: malloc failed for struct holiday!\n");
106                         exit(1);
107                 }
108
109                 if(year)
110                 {
111                         DBGL(DL_VALID, (log(LL_DBG, "init_holidays: add %d.%d.%d", day, month, year)));
112                 }
113                 else
114                 {
115                         DBGL(DL_VALID, (log(LL_DBG, "init_holidays: add %d.%d", day, month)));
116                 }
117                 
118                 newh->day = day;
119                 newh->month = month;
120                 newh->year = year;
121                 newh->next = NULL;
122                 
123                 if(firsth == NULL)
124                 {
125                         firsth = newh;
126                 }
127                 else
128                 {
129                         lasth->next = newh;
130                 }
131                 lasth = newh;                   
132         }
133         fclose(fp);
134 }
135
136 /*---------------------------------------------------------------------------*
137  *      free all holidays
138  *---------------------------------------------------------------------------*/
139 void
140 free_holidays(void)
141 {
142         free_holiday(firsth);
143 }
144
145 /*---------------------------------------------------------------------------*
146  *      free holidayes
147  *---------------------------------------------------------------------------*/
148 static void
149 free_holiday(struct holiday *ptr)
150 {
151
152         if(ptr == NULL)
153                 return;
154
155         if(ptr->next != NULL)
156                 free_holiday(ptr->next);
157
158         free(ptr);
159 }
160         
161 /*---------------------------------------------------------------------------*
162  *      check if date/month/year is a holiday
163  *---------------------------------------------------------------------------*/
164 int
165 isholiday(int d, int m, int y)
166 {
167         struct holiday *ch = NULL;
168
169         if(firsth == NULL)
170                 return(0);
171
172         ch = firsth;
173
174         for(;;)
175         {
176                 if(ch->day == d && ch->month == m)
177                 {
178                         if(ch->year == 0)
179                         {
180                                 DBGL(DL_VALID, (log(LL_DBG, "isholiday: %d.%d is a holiday!", d, m)));
181                                 return(1);
182                         }
183                         else if(ch->year == y)
184                         {
185                                 DBGL(DL_VALID, (log(LL_DBG, "isholiday: %d.%d.%d is a holiday!", d, m, y)));
186                                 return(1);
187                         }
188                 }
189
190                 if(ch->next == NULL)
191                         break;
192
193                 ch = ch->next;
194         }
195         return(0);
196 }
197                         
198 /* EOF */