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