[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.9 and 1.16

version 1.9, 1999/04/25 01:16:04 version 1.16, 2004/12/10 15:00:27
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 69 
Line 65 
 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 108 
Line 106 
                 if (ndays[i].name != NULL)                  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);
Line 118 
Line 116 
                 if (fndays[i].name != NULL)                  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);
         }          }
   
Line 131 
Line 129 
                 if (nmonths[i].name != NULL)                  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);
Line 141 
Line 139 
                 if (fnmonths[i].name != NULL)                  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 */
Line 153 
Line 151 
         spev[1].getev = paskha;          spev[1].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;
         }          }
 }  }
Line 242 
Line 240 
     return(mktime(&tm));      return(mktime(&tm));
 }  }
   
   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(endp, bodun)
         char    *endp;          char    *endp;
           int     bodun;
 {  {
         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 298 
         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() % 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 334 
                 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 353 
                         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 374 
                         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 384 
                         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 423 
Line 454 
                                 if ((v2 += isleap(tp->tm_year + TM_YEAR_BASE) ? 366 : 365)                                  if ((v2 += isleap(tp->tm_year + TM_YEAR_BASE) ? 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,                          if (strftime(tmp->print_date,
                             sizeof(tmp->print_date),                              sizeof(tmp->print_date),
                         /*    "%a %b %d", &tm);  Skip weekdays */                          /*    "%a %b %d", &tm);  Skip weekdays */
                             "%b %d", &tmtmp) == 0)                              "%b %d", &tmtmp) == 0)
                                 tmp->print_date[sizeof(tmp->print_date) - 1] = '\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 517 
Line 557 
                                 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 (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,                                          if (strftime(tmp->print_date,
                                             sizeof(tmp->print_date),                                              sizeof(tmp->print_date),
                                         /*    "%a %b %d", &tm);  Skip weekdays */                                          /*    "%a %b %d", &tm);  Skip weekdays */
                                             "%b %d", &tmtmp) == 0)                                              "%b %d", &tmtmp) == 0)
                                                 tmp->print_date[sizeof(tmp->print_date) - 1] = '\0';                                                  tmp->print_date[sizeof(tmp->print_date) - 1] = '\0';
                                           tmp->bodun = bodun && tdiff == -1;
                                         tmp->var   = varp;                                          tmp->var   = varp;
                                         tmp->next  = NULL;                                          tmp->next  = NULL;
                                         if (tmp2)                                          if (tmp2)
Line 547 
Line 590 
   
 int  int
 getmonth(s)  getmonth(s)
         register char *s;          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 567 
Line 610 
   
 int  int
 getday(s)  getday(s)
         register char *s;          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 591 
Line 634 
  */   */
 int  int
 getdayvar(s)  getdayvar(s)
         register char *s;          char *s;
 {  {
         register int offset;          int offset;
   
   
         offset = strlen(s);          offset = strlen(s);

Legend:
Removed from v.1.9  
changed lines
  Added in v.1.16