2001-01-06 16:00:10 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2000, Hellmuth Michaelis. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
*---------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* isdnd - holiday file handling
|
|
|
|
* =============================
|
|
|
|
*
|
2003-10-06 13:43:27 +04:00
|
|
|
* $Id: holiday.c,v 1.3 2003/10/06 09:43:27 itojun Exp $
|
2001-01-06 16:00:10 +03:00
|
|
|
*
|
|
|
|
* $FreeBSD$
|
|
|
|
*
|
|
|
|
* last edit-date: [Mon Oct 9 11:32:28 2000]
|
|
|
|
*
|
|
|
|
* Format:
|
|
|
|
*
|
|
|
|
* day.month.year optional comment (different day every year) or
|
|
|
|
* day.month optional comment (same day every year)
|
|
|
|
*
|
|
|
|
* i.e.:
|
|
|
|
*
|
|
|
|
* 23.4.2000 Ostersonntag
|
|
|
|
* 3.10 Tag der deutschen Einheit
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
#include "isdnd.h"
|
|
|
|
|
|
|
|
struct holiday {
|
|
|
|
int day;
|
|
|
|
int month;
|
|
|
|
int year;
|
|
|
|
struct holiday *next;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct holiday *firsth = NULL;
|
|
|
|
|
|
|
|
#define MAXBUFSZ 256
|
|
|
|
|
|
|
|
static void free_holiday(struct holiday *ptr);
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------*
|
|
|
|
* read in and init holidayes
|
|
|
|
*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
init_holidays(char *filename)
|
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
unsigned char buffer[MAXBUFSZ + 1];
|
|
|
|
struct holiday *newh = NULL;
|
|
|
|
struct holiday *lasth = NULL;
|
|
|
|
int ret;
|
|
|
|
int day, month, year;
|
|
|
|
|
|
|
|
firsth = NULL;
|
|
|
|
|
2003-10-06 13:43:27 +04:00
|
|
|
if ((fp = fopen(filename, "r")) == NULL)
|
2001-01-06 16:00:10 +03:00
|
|
|
{
|
2002-12-06 18:00:14 +03:00
|
|
|
logit(LL_ERR, "init_holiday: error opening holidayfile %s: %s!", filename, strerror(errno));
|
2001-01-06 16:00:10 +03:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
while((fgets(buffer, MAXBUFSZ, fp)) != NULL)
|
|
|
|
{
|
2003-10-06 13:43:27 +04:00
|
|
|
if (buffer[0] == '#' || buffer[0] == ' ' ||
|
2001-01-06 16:00:10 +03:00
|
|
|
buffer[0] == '\t' || buffer[0] == '\n')
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = sscanf(buffer, "%d.%d.%d", &day, &month, &year);
|
|
|
|
|
2003-10-06 13:43:27 +04:00
|
|
|
if (ret != 3)
|
2001-01-06 16:00:10 +03:00
|
|
|
{
|
|
|
|
ret = sscanf(buffer, "%d.%d", &day, &month);
|
2003-10-06 13:43:27 +04:00
|
|
|
if (ret != 2)
|
2001-01-06 16:00:10 +03:00
|
|
|
{
|
2002-12-06 18:00:14 +03:00
|
|
|
logit(LL_ERR, "init_holiday: parse error for string [%s]!", buffer);
|
2001-01-06 16:00:10 +03:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
year = 0;
|
|
|
|
}
|
|
|
|
|
2003-10-06 13:43:27 +04:00
|
|
|
if ((newh = (struct holiday *) malloc(sizeof(struct holiday))) == NULL)
|
2001-01-06 16:00:10 +03:00
|
|
|
{
|
2002-12-06 18:00:14 +03:00
|
|
|
logit(LL_ERR, "init_holiday: malloc failed for struct holiday!\n");
|
2001-01-06 16:00:10 +03:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2003-10-06 13:43:27 +04:00
|
|
|
if (year)
|
2001-01-06 16:00:10 +03:00
|
|
|
{
|
2002-12-06 18:00:14 +03:00
|
|
|
DBGL(DL_MSG, (logit(LL_DBG, "init_holidays: add %d.%d.%d", day, month, year)));
|
2001-01-06 16:00:10 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2002-12-06 18:00:14 +03:00
|
|
|
DBGL(DL_MSG, (logit(LL_DBG, "init_holidays: add %d.%d", day, month)));
|
2001-01-06 16:00:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
newh->day = day;
|
|
|
|
newh->month = month;
|
|
|
|
newh->year = year;
|
|
|
|
newh->next = NULL;
|
|
|
|
|
2003-10-06 13:43:27 +04:00
|
|
|
if (firsth == NULL)
|
2001-01-06 16:00:10 +03:00
|
|
|
{
|
|
|
|
firsth = newh;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
lasth->next = newh;
|
|
|
|
}
|
|
|
|
lasth = newh;
|
|
|
|
}
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------*
|
|
|
|
* free all holidays
|
|
|
|
*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
free_holidays(void)
|
|
|
|
{
|
|
|
|
free_holiday(firsth);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------*
|
|
|
|
* free holidayes
|
|
|
|
*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
|
|
|
free_holiday(struct holiday *ptr)
|
|
|
|
{
|
|
|
|
|
2003-10-06 13:43:27 +04:00
|
|
|
if (ptr == NULL)
|
2001-01-06 16:00:10 +03:00
|
|
|
return;
|
|
|
|
|
2003-10-06 13:43:27 +04:00
|
|
|
if (ptr->next != NULL)
|
2001-01-06 16:00:10 +03:00
|
|
|
free_holiday(ptr->next);
|
|
|
|
|
|
|
|
free(ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------*
|
|
|
|
* check if date/month/year is a holiday
|
|
|
|
*---------------------------------------------------------------------------*/
|
|
|
|
int
|
|
|
|
isholiday(int d, int m, int y)
|
|
|
|
{
|
|
|
|
struct holiday *ch = NULL;
|
|
|
|
|
2003-10-06 13:43:27 +04:00
|
|
|
if (firsth == NULL)
|
2001-01-06 16:00:10 +03:00
|
|
|
return(0);
|
|
|
|
|
|
|
|
ch = firsth;
|
|
|
|
|
2003-10-06 13:43:27 +04:00
|
|
|
for (;;)
|
2001-01-06 16:00:10 +03:00
|
|
|
{
|
2003-10-06 13:43:27 +04:00
|
|
|
if (ch->day == d && ch->month == m)
|
2001-01-06 16:00:10 +03:00
|
|
|
{
|
2003-10-06 13:43:27 +04:00
|
|
|
if (ch->year == 0)
|
2001-01-06 16:00:10 +03:00
|
|
|
{
|
2002-12-06 18:00:14 +03:00
|
|
|
DBGL(DL_MSG, (logit(LL_DBG, "isholiday: %d.%d is a holiday!", d, m)));
|
2001-01-06 16:00:10 +03:00
|
|
|
return(1);
|
|
|
|
}
|
2003-10-06 13:43:27 +04:00
|
|
|
else if (ch->year == y)
|
2001-01-06 16:00:10 +03:00
|
|
|
{
|
2002-12-06 18:00:14 +03:00
|
|
|
DBGL(DL_MSG, (logit(LL_DBG, "isholiday: %d.%d.%d is a holiday!", d, m, y)));
|
2001-01-06 16:00:10 +03:00
|
|
|
return(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-10-06 13:43:27 +04:00
|
|
|
if (ch->next == NULL)
|
2001-01-06 16:00:10 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
ch = ch->next;
|
|
|
|
}
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* EOF */
|