[BACK]Return to day.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / calendar

Diff for /src/usr.bin/calendar/day.c between version 1.7 and 1.33

version 1.7, 1998/12/13 07:31:07 version 1.33, 2016/07/13 21:32:01
Line 12 
Line 12 
  * 2. Redistributions in binary form must reproduce the above copyright   * 2. Redistributions in binary form must reproduce the above copyright
  *    notice, this list of conditions and the following disclaimer in the   *    notice, this list of conditions and the following disclaimer in the
  *    documentation and/or other materials provided with the distribution.   *    documentation and/or other materials provided with the distribution.
  * 3. All advertising materials mentioning features or use of this software   * 3. Neither the name of the University nor the names of its contributors
  *    must display the following acknowledgement:  
  *      This product includes software developed by the University of  
  *      California, Berkeley and its contributors.  
  * 4. Neither the name of the University nor the names of its contributors  
  *    may be used to endorse or promote products derived from this software   *    may be used to endorse or promote products derived from this software
  *    without specific prior written permission.   *    without specific prior written permission.
  *   *
Line 33 
Line 29 
  * 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 char rcsid[] = "$OpenBSD$";  
 #endif  
 #endif /* not lint */  
   
 #include <sys/types.h>  #include <sys/types.h>
 #include <sys/uio.h>  #include <sys/uio.h>
   
Line 57 
Line 39 
 #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
Line 69 
Line 52 
 struct tm *tp;  struct tm *tp;
 int *cumdays, offset;  int *cumdays, offset;
 char dayname[10];  char dayname[10];
   enum calendars calendar;
   u_long julian;
   
   
 /* 1-based month, 0-based days, cumulative */  /* 1-based month, 0-based days, cumulative */
Line 92 
Line 77 
 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 setnnames(void)  void
   setnnames(void)
 {  {
         char buf[80];          char buf[80];
         int i, l;          int i, l;
Line 102 
Line 95 
         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)
                         errx(1, "cannot allocate memory");                          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)
                         errx(1, "cannot allocate memory");                          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)
                         errx(1, "cannot allocate memory");                          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)
                         errx(1, "cannot allocate memory");                          err(1, NULL);
                 fnmonths[i].len = strlen(buf);                  fnmonths[i].len = strlen(buf);
         }          }
         /* Hardwired special events */          /* Hardwired special events */
         spev[0].name = strdup(EASTER);          spev[0].name = strdup(PESACH);
         spev[0].nlen = EASTERNAMELEN;          spev[0].nlen = PESACHLEN;
         spev[0].getev = easter;          spev[0].getev = pesach;
         spev[1].name = strdup(PASKHA);          spev[1].name = strdup(EASTER);
         spev[1].nlen = PASKHALEN;          spev[1].nlen = EASTERNAMELEN;
         spev[1].getev = paskha;          spev[1].getev = easter;
           spev[2].name = strdup(PASKHA);
           spev[2].nlen = PASKHALEN;
           spev[2].getev = paskha;
         for (i = 0; i < NUMEV; i++) {          for (i = 0; i < NUMEV; i++) {
                 if (spev[i].name == NULL)                  if (spev[i].name == NULL)
                         errx(1, "cannot allocate memory");                          err(1, NULL);
                 spev[i].uname = NULL;                  spev[i].uname = NULL;
         }          }
 }  }
   
 void  void
 settime(now)  settime(time_t *now)
         time_t *now;  
 {  {
         tp = localtime(now);          tp = localtime(now);
         tp->tm_sec = 0;          tp->tm_sec = 0;
Line 169 
Line 160 
         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];
         /* Friday displays Monday's events */          /* Friday displays Monday's events */
         offset = tp->tm_wday == 5 ? 3 : 1;          offset = tp->tm_wday == 5 ? 3 : 1;
         if (f_dayAfter)          if (f_SetdayAfter)
                 offset = 0;     /* Except not when range is set explicitly */                  offset = 0;     /* Except not when range is set explicitly */
         header[5].iov_base = dayname;          header[5].iov_base = dayname;
   
Line 189 
Line 180 
 /* convert [Year][Month]Day into unix time (since 1970)  /* convert [Year][Month]Day into unix time (since 1970)
  * Year: two or four digits, Month: two digits, Day: two digits   * Year: two or four digits, Month: two digits, Day: two digits
  */   */
 time_t Mktime (date)  time_t
     char *date;  Mktime(char *date)
 {  {
     time_t t;          time_t t;
     int len;          int len;
     struct tm tm;          struct tm tm;
   
     (void)time(&t);          (void)time(&t);
     tp = localtime(&t);          tp = localtime(&t);
   
     len = strlen(date);          len = strlen(date);
     if (len < 2)          if (len < 2)
         return((time_t)-1);                  return((time_t)-1);
     tm.tm_sec = 0;          bzero(&tm, sizeof tm);
     tm.tm_min = 0;          tm.tm_sec = 0;
     /* Avoid getting caught by a timezone shift; set time to noon */          tm.tm_min = 0;
     tm.tm_isdst = 0;          /* Avoid getting caught by a timezone shift; set time to noon */
     tm.tm_hour = 12;          tm.tm_isdst = 0;
     tm.tm_wday = 0;          tm.tm_hour = 12;
     tm.tm_mday = tp->tm_mday;          tm.tm_wday = 0;
     tm.tm_mon = tp->tm_mon;          tm.tm_mday = tp->tm_mday;
     tm.tm_year = tp->tm_year;          tm.tm_mon = tp->tm_mon;
           tm.tm_year = tp->tm_year;
   
     /* Day */          /* Day */
     tm.tm_mday = atoi(date + len - 2);          tm.tm_mday = atoi(date + len - 2);
   
     /* Month */          /* Month */
     if (len >= 4) {          if (len >= 4) {
         *(date + len - 2) = '\0';                  *(date + len - 2) = '\0';
         tm.tm_mon = atoi(date + len - 4) - 1;                  tm.tm_mon = atoi(date + len - 4) - 1;
     }          }
   
     /* Year */          /* Year */
     if (len >= 6) {          if (len >= 6) {
                 *(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 += 100;
                 tm.tm_year += 2000 - TM_YEAR_BASE;                  else if (tm.tm_year > 1900)
         else if (tm.tm_year < 100)                          tm.tm_year -= 1900;
                 tm.tm_year += 1900 - TM_YEAR_BASE;          }
         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));
 }  }
   
   static void
   adjust_calendar(int *day, int *month)
   {
           switch (calendar) {
           case GREGORIAN:
                   break;
   
           case JULIAN:
                   *day += julian;
                   if (*day > (cumdays[*month + 1] - cumdays[*month])) {
                           *day -= (cumdays[*month + 1] - cumdays[*month]);
                           if (++*month > 12)
                                   *month = 1;
                   }
                   break;
           case LUNAR:
                   break;
           }
   }
   
 /*  /*
  * Possible date formats include any combination of:   * Possible date formats include any combination of:
  *      3-charmonth                     (January, Jan, Jan)   *      3-charmonth                     (January, Jan, Jan)
  *      3-charweekday                   (Friday, Monday, mon.)   *      3-charweekday                   (Friday, Monday, mon.)
  *      numeric month or day            (1, 2, 04)   *      numeric month or day            (1, 2, 04)
  *   *
  * Any character may separate them, or they may not be separated.  Any line,   * Any character except \t or '*' may separate them, or they may not be
  * following a line that is matched, that starts with "whitespace", is shown   * separated.  Any line following a line that is matched, that starts
  * along with the matched line.   * with \t, is shown along with the matched line.
  */   */
 struct match *  struct match *
 isnow(endp)  isnow(char *endp, int bodun)
         char    *endp;  
 {  {
         int day = 0, flags = 0, month = 0, v1, v2, i;          int day = 0, flags = 0, month = 0, v1, v2, i;
         int monthp, dayp, varp = 0;          int monthp, dayp, varp = 0;
Line 279 
Line 287 
         if (!(v1 = getfield(endp, &endp, &flags)))          if (!(v1 = getfield(endp, &endp, &flags)))
                 return (NULL);                  return (NULL);
   
           /* adjust bodun rate */
           if (bodun && !bodun_always)
                   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;
Line 311 
Line 323 
                 if (month == -1) {                  if (month == -1) {
                         month = tp->tm_mon + 1;                          month = tp->tm_mon + 1;
                         interval = MONTHLY;                          interval = MONTHLY;
                 }                  } else if (calendar)
                           adjust_calendar(&day, &month);
                 if ((month > 12) || (month < 1))                  if ((month > 12) || (month < 1))
                         return (NULL);                          return (NULL);
         }          }
Line 329 
Line 342 
                         day = 1;                          day = 1;
                 /* If a weekday was spelled out without an ordering,                  /* If a weekday was spelled out without an ordering,
                  * assume the first of that day in the month */                   * assume the first of that day in the month */
                 if ((flags & F_ISDAY) && (day >= 1) && (day <=7))                  if ((flags & F_ISDAY)) {
                         day += 10;                          if ((day >= 1) && (day <=7))
                                   day += 10;
                   } else if (calendar)
                           adjust_calendar(&day, &month);
         }          }
   
         /* Hm ... */          /* Hm ... */
Line 347 
Line 363 
                         if (month == -1) {                          if (month == -1) {
                                 month = tp->tm_mon + 1;                                  month = tp->tm_mon + 1;
                                 interval = MONTHLY;                                  interval = MONTHLY;
                         }                          } else if (calendar)
                                   adjust_calendar(&day, &month);
                 }                  }
   
                 /* {Month} {Weekday,Day} ...  */                  /* {Month} {Weekday,Day} ...  */
Line 356 
Line 373 
                         month = v1;                          month = v1;
                         /* if no recognizable day, assume the first */                          /* if no recognizable day, assume the first */
                         day = v2 ? v2 : 1;                          day = v2 ? v2 : 1;
                         if ((flags & F_ISDAY) && (day >= 1) && (day <= 7))                          if ((flags & F_ISDAY)) {
                                 day += 10;                                  if ((day >= 1) && (day <= 7))
                                           day += 10;
                           } else
                                   adjust_calendar(&day, &month);
                 }                  }
         }          }
   
Line 404 
Line 424 
                         memcpy(&tmtmp, tp, sizeof(struct tm));                          memcpy(&tmtmp, tp, sizeof(struct tm));
                         tmtmp.tm_mday = dayp;                          tmtmp.tm_mday = dayp;
                         tmtmp.tm_mon = monthp - 1;                          tmtmp.tm_mon = monthp - 1;
                           if (vwd) {
                           /* We want the event next year if it's late now
                            * this year.  The 50-day limit means we don't have to
                            * worry if next year is or isn't a leap year.
                            */
                                   if (tp->tm_yday > 300 && tmtmp.tm_mon <= 1)
                                           variable_weekday(&vwd, tmtmp.tm_mon + 1,
                                               tmtmp.tm_year + 1900 + 1);
                                   else
                                           variable_weekday(&vwd, tmtmp.tm_mon + 1,
                                               tmtmp.tm_year + 1900);
                                   day = cumdays[tmtmp.tm_mon + 1] + 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                                  else if(!bodun || (day - tp->tm_yday) != -1)
                                         return(NULL);                                          return(NULL);
                         }                          }
                         if ((tmp = malloc(sizeof(struct match))) == NULL)                          if ((tmp = malloc(sizeof(struct match))) == NULL)
                                 errx(1, "cannot allocate memory");                                  err(1, NULL);
                         tmp->when = f_time + v2 * SECSPERDAY;  
                           if (bodun && (day - tp->tm_yday) == -1) {
                                   tmp->when = f_time - 1 * SECSPERDAY;
                                   tmtmp.tm_mday++;
                                   tmp->bodun = 1;
                           } else {
                                   tmp->when = f_time + v2 * SECSPERDAY;
                                   tmp->bodun = 0;
                           }
   
                         (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);
                 }                  }
         }          } else {
         else {  
                 varp = 1;                  varp = 1;
                 /* Set up v1 to the event number and ... */                  /* Set up v1 to the event number and ... */
                 v1 = vwd % (NUMEV + 1) - 1;                  v1 = vwd % (NUMEV + 1) - 1;
Line 473 
Line 511 
                                 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;
Line 484 
Line 522 
                                 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 +
                                             vwd + TM_YEAR_BASE);                                              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
Line 503 
Line 541 
                                 warnx("time out of range: %s", endp);                                  warnx("time out of range: %s", endp);
                         else {                          else {
                                 tdiff = difftime(ttmp, f_time)/ SECSPERDAY;                                  tdiff = difftime(ttmp, f_time)/ SECSPERDAY;
                                 if (tdiff <= offset + f_dayAfter) {                                  if (tdiff <= offset + f_dayAfter ||
                                         if (tdiff >=  0) {                                      (bodun && tdiff == -1)) {
                                           if ((tmtmp.tm_mon == month) &&
                                               (tdiff >=  0 ||
                                               (bodun && tdiff == -1))) {
                                         if ((tmp = malloc(sizeof(struct match))) == NULL)                                          if ((tmp = malloc(sizeof(struct match))) == NULL)
                                                 errx(1, "cannot allocate memory");                                                  err(1, NULL);
                                         tmp->when = ttmp;                                          tmp->when = ttmp;
                                         if (strftime(tmp->print_date,                                          fill_print_date(tmp, &tmtmp);
                                             sizeof(tmp->print_date),                                          tmp->bodun = bodun && tdiff == -1;
                                         /*    "%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;
                                         if (tmp2)                                          if (tmp2)
Line 532 
Line 570 
   
   
 int  int
 getmonth(s)  getmonth(char *s)
         register char *s;  
 {  {
         register char **p;          char **p;
         struct fixs *n;          struct fixs *n;
   
         for (n = fnmonths; n->name; ++n)          for (n = fnmonths; n->name; ++n)
Line 552 
Line 589 
   
   
 int  int
 getday(s)  getday(char *s)
         register char *s;  
 {  {
         register char **p;          char **p;
         struct fixs *n;          struct fixs *n;
   
         for (n = fndays; n->name; ++n)          for (n = fndays; n->name; ++n)
Line 576 
Line 612 
  * ... etc ...   * ... etc ...
  */   */
 int  int
 getdayvar(s)  getdayvar(char *s)
         register char *s;  
 {  {
         register int offset;          int offset;
   
   
         offset = strlen(s);          offset = strlen(s);
Line 617 
Line 652 
   
   
 int  int
 foy(year)  foy(int year)
         int year;  
 {  {
         /* 0-6; what weekday Jan 1 is */          /* 0-6; what weekday Jan 1 is */
         year--;          year--;
Line 628 
Line 662 
   
   
 void  void
 variable_weekday(day, month, year)  variable_weekday(int *day, int month, int year)
         int *day, month, year;  
 {  {
         int v1, v2;          int v1, v2;
         int *cumdays;          int *cumdays;

Legend:
Removed from v.1.7  
changed lines
  Added in v.1.33