version 1.20, 2005/11/16 16:45:11 |
version 1.34, 2016/09/14 15:09:46 |
|
|
* SUCH DAMAGE. |
* SUCH DAMAGE. |
*/ |
*/ |
|
|
#ifndef lint |
|
static const char copyright[] = |
|
"@(#) Copyright (c) 1989, 1993\n\ |
|
The Regents of the University of California. All rights reserved.\n"; |
|
#endif /* not lint */ |
|
|
|
#ifndef lint |
|
#if 0 |
|
static const char sccsid[] = "@(#)calendar.c 8.3 (Berkeley) 3/25/94"; |
|
#else |
|
static const char rcsid[] = "$OpenBSD$"; |
|
#endif |
|
#endif /* not lint */ |
|
|
|
#include <sys/types.h> |
#include <sys/types.h> |
#include <sys/uio.h> |
#include <sys/uio.h> |
|
|
|
|
#include <stdlib.h> |
#include <stdlib.h> |
#include <string.h> |
#include <string.h> |
#include <time.h> |
#include <time.h> |
#include <tzfile.h> |
|
|
|
#include "pathnames.h" |
#include "pathnames.h" |
#include "calendar.h" |
#include "calendar.h" |
|
|
|
extern struct iovec header[]; |
|
|
#define WEEKLY 1 |
#define WEEKLY 1 |
#define MONTHLY 2 |
#define MONTHLY 2 |
#define YEARLY 3 |
#define YEARLY 3 |
|
|
static struct fixs fnmonths[13]; /* full national months names */ |
static struct fixs fnmonths[13]; /* full national months names */ |
static struct fixs nmonths[13]; /* short national month names */ |
static struct fixs nmonths[13]; /* short national month names */ |
|
|
|
void |
|
fill_print_date(struct match *m, struct tm *tm) |
|
{ |
|
if (strftime(m->print_date, sizeof(m->print_date), |
|
daynames ? "%a %b %d" : "%b %d", tm) == 0) |
|
m->print_date[sizeof(m->print_date) - 1] = '\0'; |
|
} |
|
|
void |
void |
setnnames(void) |
setnnames(void) |
|
|
for (i = 0; i < 7; i++) { |
for (i = 0; i < 7; i++) { |
tm.tm_wday = i; |
tm.tm_wday = i; |
l = strftime(buf, sizeof(buf), "%a", &tm); |
l = strftime(buf, sizeof(buf), "%a", &tm); |
for (; l > 0 && isspace((int)buf[l - 1]); l--) |
for (; l > 0 && isspace((unsigned char)buf[l - 1]); l--) |
; |
; |
buf[l] = '\0'; |
buf[l] = '\0'; |
if (ndays[i].name != NULL) |
free(ndays[i].name); |
free(ndays[i].name); |
|
if ((ndays[i].name = strdup(buf)) == NULL) |
if ((ndays[i].name = strdup(buf)) == NULL) |
err(1, NULL); |
err(1, NULL); |
ndays[i].len = strlen(buf); |
ndays[i].len = strlen(buf); |
|
|
l = strftime(buf, sizeof(buf), "%A", &tm); |
l = strftime(buf, sizeof(buf), "%A", &tm); |
for (; l > 0 && isspace((int)buf[l - 1]); l--) |
for (; l > 0 && isspace((unsigned char)buf[l - 1]); l--) |
; |
; |
buf[l] = '\0'; |
buf[l] = '\0'; |
if (fndays[i].name != NULL) |
free(fndays[i].name); |
free(fndays[i].name); |
|
if ((fndays[i].name = strdup(buf)) == NULL) |
if ((fndays[i].name = strdup(buf)) == NULL) |
err(1, NULL); |
err(1, NULL); |
fndays[i].len = strlen(buf); |
fndays[i].len = strlen(buf); |
|
|
for (i = 0; i < 12; i++) { |
for (i = 0; i < 12; i++) { |
tm.tm_mon = i; |
tm.tm_mon = i; |
l = strftime(buf, sizeof(buf), "%b", &tm); |
l = strftime(buf, sizeof(buf), "%b", &tm); |
for (; l > 0 && isspace((int)buf[l - 1]); l--) |
for (; l > 0 && isspace((unsigned char)buf[l - 1]); l--) |
; |
; |
buf[l] = '\0'; |
buf[l] = '\0'; |
if (nmonths[i].name != NULL) |
free(nmonths[i].name); |
free(nmonths[i].name); |
|
if ((nmonths[i].name = strdup(buf)) == NULL) |
if ((nmonths[i].name = strdup(buf)) == NULL) |
err(1, NULL); |
err(1, NULL); |
nmonths[i].len = strlen(buf); |
nmonths[i].len = strlen(buf); |
|
|
l = strftime(buf, sizeof(buf), "%B", &tm); |
l = strftime(buf, sizeof(buf), "%B", &tm); |
for (; l > 0 && isspace((int)buf[l - 1]); l--) |
for (; l > 0 && isspace((unsigned char)buf[l - 1]); l--) |
; |
; |
buf[l] = '\0'; |
buf[l] = '\0'; |
if (fnmonths[i].name != NULL) |
free(fnmonths[i].name); |
free(fnmonths[i].name); |
|
if ((fnmonths[i].name = strdup(buf)) == NULL) |
if ((fnmonths[i].name = strdup(buf)) == NULL) |
err(1, NULL); |
err(1, NULL); |
fnmonths[i].len = strlen(buf); |
fnmonths[i].len = strlen(buf); |
|
|
tp->tm_isdst = 0; |
tp->tm_isdst = 0; |
tp->tm_hour = 12; |
tp->tm_hour = 12; |
*now = mktime(tp); |
*now = mktime(tp); |
if (isleap(tp->tm_year + TM_YEAR_BASE)) |
if (isleap(tp->tm_year + 1900)) |
cumdays = daytab[1]; |
cumdays = daytab[1]; |
else |
else |
cumdays = daytab[0]; |
cumdays = daytab[0]; |
|
|
len = strlen(date); |
len = strlen(date); |
if (len < 2) |
if (len < 2) |
return((time_t)-1); |
return((time_t)-1); |
|
bzero(&tm, sizeof tm); |
tm.tm_sec = 0; |
tm.tm_sec = 0; |
tm.tm_min = 0; |
tm.tm_min = 0; |
/* Avoid getting caught by a timezone shift; set time to noon */ |
/* Avoid getting caught by a timezone shift; set time to noon */ |
|
|
*(date + len - 4) = '\0'; |
*(date + len - 4) = '\0'; |
tm.tm_year = atoi(date); |
tm.tm_year = atoi(date); |
|
|
/* tm_year up TM_YEAR_BASE ... */ |
|
if (tm.tm_year < 69) /* Y2K */ |
if (tm.tm_year < 69) /* Y2K */ |
tm.tm_year += 2000 - TM_YEAR_BASE; |
tm.tm_year += 100; |
else if (tm.tm_year < 100) |
else if (tm.tm_year > 1900) |
tm.tm_year += 1900 - TM_YEAR_BASE; |
tm.tm_year -= 1900; |
else if (tm.tm_year > TM_YEAR_BASE) |
|
tm.tm_year -= TM_YEAR_BASE; |
|
} |
} |
|
|
#if DEBUG |
#if DEBUG |
printf("Mktime: %d %d %d %s\n", (int)mktime(&tm), (int)t, len, |
printf("Mktime: %d %lld %d %s\n", (int)mktime(&tm), (long long)t, len, |
asctime(&tm)); |
asctime(&tm)); |
#endif |
#endif |
return(mktime(&tm)); |
return(mktime(&tm)); |
} |
} |
|
|
void |
static void |
adjust_calendar(int *day, int *month) |
adjust_calendar(int *day, int *month) |
{ |
{ |
switch (calendar) { |
switch (calendar) { |
|
|
|
|
/* adjust bodun rate */ |
/* adjust bodun rate */ |
if (bodun && !bodun_always) |
if (bodun && !bodun_always) |
bodun = !(arc4random() % 3); |
bodun = !arc4random_uniform(3); |
|
|
/* Easter or Easter depending days */ |
/* Easter or Easter depending days */ |
if (flags & F_SPECIAL) |
if (flags & F_SPECIAL) |
vwd = v1; |
vwd = v1; |
|
|
*/ |
*/ |
if (tp->tm_yday > 300 && tmtmp.tm_mon <= 1) |
if (tp->tm_yday > 300 && tmtmp.tm_mon <= 1) |
variable_weekday(&vwd, tmtmp.tm_mon + 1, |
variable_weekday(&vwd, tmtmp.tm_mon + 1, |
tmtmp.tm_year + TM_YEAR_BASE + 1); |
tmtmp.tm_year + 1900 + 1); |
else |
else |
variable_weekday(&vwd, tmtmp.tm_mon + 1, |
variable_weekday(&vwd, tmtmp.tm_mon + 1, |
tmtmp.tm_year + TM_YEAR_BASE); |
tmtmp.tm_year + 1900); |
day = cumdays[tmtmp.tm_mon + 1] + vwd; |
day = cumdays[tmtmp.tm_mon + 1] + vwd; |
tmtmp.tm_mday = vwd; |
tmtmp.tm_mday = vwd; |
} |
} |
v2 = day - tp->tm_yday; |
v2 = day - tp->tm_yday; |
if ((v2 > v1) || (v2 < 0)) { |
if ((v2 > v1) || (v2 < 0)) { |
if ((v2 += isleap(tp->tm_year + TM_YEAR_BASE) ? 366 : 365) |
if ((v2 += isleap(tp->tm_year + 1900) ? 366 : 365) |
<= v1) |
<= v1) |
tmtmp.tm_year++; |
tmtmp.tm_year++; |
else if(!bodun || (day - tp->tm_yday) != -1) |
else if(!bodun || (day - tp->tm_yday) != -1) |
|
|
} |
} |
|
|
(void)mktime(&tmtmp); |
(void)mktime(&tmtmp); |
if (strftime(tmp->print_date, |
fill_print_date(tmp, &tmtmp); |
sizeof(tmp->print_date), |
|
/* "%a %b %d", &tm); Skip weekdays */ |
|
"%b %d", &tmtmp) == 0) |
|
tmp->print_date[sizeof(tmp->print_date) - 1] = '\0'; |
|
|
|
tmp->var = varp; |
tmp->var = varp; |
tmp->next = NULL; |
tmp->next = NULL; |
return(tmp); |
return(tmp); |
|
|
if (vwd) { |
if (vwd) { |
v1 = vwd; |
v1 = vwd; |
variable_weekday(&v1, tmtmp.tm_mon + 1, |
variable_weekday(&v1, tmtmp.tm_mon + 1, |
tmtmp.tm_year + TM_YEAR_BASE); |
tmtmp.tm_year + 1900); |
tmtmp.tm_mday = v1; |
tmtmp.tm_mday = v1; |
} else |
} else |
tmtmp.tm_mday = dayp; |
tmtmp.tm_mday = dayp; |
|
|
if (flags & F_SPECIAL) { |
if (flags & F_SPECIAL) { |
tmtmp.tm_mon = 0; /* Gee, mktime() is nice */ |
tmtmp.tm_mon = 0; /* Gee, mktime() is nice */ |
tmtmp.tm_mday = spev[v1].getev(tmtmp.tm_year + |
tmtmp.tm_mday = spev[v1].getev(tmtmp.tm_year + |
TM_YEAR_BASE) + vwd; |
1900) + vwd; |
} else if (vwd) { |
} else if (vwd) { |
v1 = vwd; |
v1 = vwd; |
variable_weekday(&v1, tmtmp.tm_mon + 1, |
variable_weekday(&v1, tmtmp.tm_mon + 1, |
tmtmp.tm_year + TM_YEAR_BASE); |
tmtmp.tm_year + 1900); |
tmtmp.tm_mday = v1; |
tmtmp.tm_mday = v1; |
} else { |
} else { |
/* Need the following to keep Feb 29 from |
/* Need the following to keep Feb 29 from |
|
|
tdiff = difftime(ttmp, f_time)/ SECSPERDAY; |
tdiff = difftime(ttmp, f_time)/ SECSPERDAY; |
if (tdiff <= offset + f_dayAfter || |
if (tdiff <= offset + f_dayAfter || |
(bodun && tdiff == -1)) { |
(bodun && tdiff == -1)) { |
if (tdiff >= 0 || |
if (((tmtmp.tm_mon == month) || |
(bodun && tdiff == -1)) { |
(flags & F_SPECIAL) || |
|
(interval == WEEKLY)) && |
|
(tdiff >= 0 || |
|
(bodun && tdiff == -1))) { |
if ((tmp = malloc(sizeof(struct match))) == NULL) |
if ((tmp = malloc(sizeof(struct match))) == NULL) |
err(1, NULL); |
err(1, NULL); |
tmp->when = ttmp; |
tmp->when = ttmp; |
if (strftime(tmp->print_date, |
fill_print_date(tmp, &tmtmp); |
sizeof(tmp->print_date), |
|
/* "%a %b %d", &tm); Skip weekdays */ |
|
"%b %d", &tmtmp) == 0) |
|
tmp->print_date[sizeof(tmp->print_date) - 1] = '\0'; |
|
tmp->bodun = bodun && tdiff == -1; |
tmp->bodun = bodun && tdiff == -1; |
tmp->var = varp; |
tmp->var = varp; |
tmp->next = NULL; |
tmp->next = NULL; |