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

Diff for /src/usr.bin/calendar/io.c between version 1.5 and 1.10

version 1.5, 1998/11/08 04:31:13 version 1.10, 2000/12/07 19:36:37
Line 53 
Line 53 
 #include <sys/types.h>  #include <sys/types.h>
 #include <sys/uio.h>  #include <sys/uio.h>
 #include <sys/wait.h>  #include <sys/wait.h>
   #include <sys/file.h>
   
 #include <ctype.h>  #include <ctype.h>
 #include <err.h>  #include <err.h>
Line 73 
Line 74 
 char *calendarHome = ".calendar"; /* HOME */  char *calendarHome = ".calendar"; /* HOME */
 char *calendarNoMail = "nomail";  /* don't sent mail if this file exist */  char *calendarNoMail = "nomail";  /* don't sent mail if this file exist */
   
 struct fixs neaster, npaskha;  
   
 struct iovec header[] = {  struct iovec header[] = {
         {"From: ", 6},          {"From: ", 6},
         {NULL, 0},          {NULL, 0},
Line 86 
Line 85 
 };  };
   
   
   int     openf(char *path);
   
 void  void
 cal()  cal()
 {  {
         register int printing;          register int printing;
         register char *p;          register char *p;
         FILE *fp;          FILE *fp;
         int ch, l;          int ch, l, i;
         int var;          int var;
         char buf[2048 + 1];          char buf[2048 + 1];
         struct event *events, *cur_evt, *tmp;          struct event *events, *cur_evt, *ev1, *tmp;
         struct match *m;          struct match *m;
   
         events = NULL;          events = NULL;
Line 107 
Line 108 
                         *p = '\0';                          *p = '\0';
                 else                  else
                         while ((ch = getchar()) != '\n' && ch != EOF);                          while ((ch = getchar()) != '\n' && ch != EOF);
                 for (l = strlen(buf);                  for (l = strlen(buf); l > 0 && isspace(buf[l - 1]); l--)
                      l > 0 && isspace(buf[l - 1]);  
                      l--)  
                         ;                          ;
                 buf[l] = '\0';                  buf[l] = '\0';
                 if (buf[0] == '\0')                  if (buf[0] == '\0')
Line 119 
Line 118 
                         setnnames();                          setnnames();
                         continue;                          continue;
                 }                  }
                 if (strncasecmp(buf, "Easter=", 7) == 0 && buf[7]) {                  /* User defined names for special events */
                         if (neaster.name != NULL)                  if ((p = strchr(buf, '='))) {
                                 free(neaster.name);                          for (i = 0; i < NUMEV; i++) {
                         if ((neaster.name = strdup(buf + 7)) == NULL)                          if (strncasecmp(buf, spev[i].name, spev[i].nlen) == 0 &&
                                 errx(1, "cannot allocate memory");                              (p - buf == spev[i].nlen) && buf[spev[i].nlen + 1]) {
                         neaster.len = strlen(buf + 7);                                  p++;
                                   if (spev[i].uname != NULL)
                                           free(spev[i].uname);
                                   if ((spev[i].uname = strdup(p)) == NULL)
                                           errx(1, "cannot allocate memory");
                                   spev[i].ulen = strlen(p);
                                   i = NUMEV + 1;
                           }
                           }
                   if (i > NUMEV)
                         continue;                          continue;
                 }                  }
                 if (strncasecmp(buf, "Paskha=", 7) == 0 && buf[7]) {  
                         if (npaskha.name != NULL)  
                                 free(npaskha.name);  
                         if ((npaskha.name = strdup(buf + 7)) == NULL)  
                                 errx(1, "cannot allocate memory");  
                         npaskha.len = strlen(buf + 7);  
                         continue;  
                 }  
                 if (buf[0] != '\t') {                  if (buf[0] != '\t') {
                         printing = (m = isnow(buf)) ? 1 : 0;                          printing = (m = isnow(buf)) ? 1 : 0;
                         if ((p = strchr(buf, '\t')) == NULL)                          if ((p = strchr(buf, '\t')) == NULL) {
                                   printing = 0;
                                 continue;                                  continue;
                           }
                         /* Need the following to catch hardwired "variable"                          /* Need the following to catch hardwired "variable"
                          * dates */                           * dates */
                         if (p > buf && p[-1] == '*')                          if (p > buf && p[-1] == '*')
Line 146 
Line 148 
                         else                          else
                                 var = 0;                                  var = 0;
                         if (printing) {                          if (printing) {
                                 struct tm tm;  
                                 struct match *foo;                                  struct match *foo;
                                 char *dsc;  
   
                                 dsc = NULL;                                  ev1 = NULL;
                                 while (m) {                                  while (m) {
                                 cur_evt = (struct event *) malloc(sizeof(struct event));                                  cur_evt = (struct event *) malloc(sizeof(struct event));
                                 if (cur_evt == NULL)                                  if (cur_evt == NULL)
                                         errx(1, "cannot allocate memory");                                          errx(1, "cannot allocate memory");
   
                                 tm.tm_sec = 0;  /* unused */                                  cur_evt->when = m->when;
                                 tm.tm_min = 0;  /* unused */                                  snprintf(cur_evt->print_date,
                                 tm.tm_hour = 12; /* unused */                                      sizeof(cur_evt->print_date), "%s%c",
                                 tm.tm_wday = 0; /* unused */                                      m->print_date, (var + m->var) ? '*' : ' ');
                                 tm.tm_mon = m->month - 1;                                  if (ev1) {
                                 tm.tm_mday = m->day;                                          cur_evt->desc = ev1->desc;
                                 tm.tm_year = m->year;                                          cur_evt->ldesc = NULL;
                                 tm.tm_isdst = tp->tm_isdst; /* unused */                                  } else {
                                 tm.tm_gmtoff = tp->tm_gmtoff; /* unused */                                          if ((cur_evt->ldesc = strdup(p)) == NULL)
                                 tm.tm_zone = tp->tm_zone; /* unused */  
                                 (void)strftime(cur_evt->print_date,  
                                     sizeof(cur_evt->print_date) - 1,  
                                 /*    "%a %b %d", &tm);  Skip weekdays */  
                                     "%b %d", &tm);  
                                 strcat(cur_evt->print_date,  
                                     (var + m->var) ? "*" : " ");  
                                 cur_evt->when = mktime(&tm);  
                                 if (dsc)  
                                         cur_evt->desc = dsc;  
                                 else {  
                                         if ((cur_evt->desc = strdup(p)) == NULL)  
                                                 errx(1, "cannot allocate memory");                                                  errx(1, "cannot allocate memory");
                                         dsc = cur_evt->desc;                                          cur_evt->desc = &(cur_evt->ldesc);
                                           ev1 = cur_evt;
                                 }                                  }
                                 insert(&events, cur_evt);                                  insert(&events, cur_evt);
                                 foo = m;                                  foo = m;
Line 188 
Line 177 
                         }                          }
                 }                  }
                 else if (printing) {                  else if (printing) {
                         if ((cur_evt->desc = realloc(cur_evt->desc,                          if ((ev1->ldesc = realloc(ev1->ldesc,
                             (2 + strlen(cur_evt->desc) + strlen(buf)))) == NULL)                              (2 + strlen(ev1->ldesc) + strlen(buf)))) == NULL)
                                 errx(1, "cannot allocate memory");                                  errx(1, "cannot allocate memory");
                         strcat(cur_evt->desc, "\n");                          strcat(ev1->ldesc, "\n");
                         strcat(cur_evt->desc, buf);                          strcat(ev1->ldesc, buf);
                 }                  }
         }          }
         tmp = events;          tmp = events;
         while (tmp) {          while (tmp) {
                 (void)fprintf(fp, "%s%s\n", tmp->print_date, tmp->desc);                  (void)fprintf(fp, "%s%s\n", tmp->print_date, *(tmp->desc));
                 /* Can't free descriptions since they may be shared */                  tmp = tmp->next;
                 (void)realloc(tmp->desc, 0);          }
           tmp = events;
           while (tmp) {
                 events = tmp;                  events = tmp;
                   if (tmp->ldesc)
                           free(tmp->ldesc);
                 tmp = tmp->next;                  tmp = tmp->next;
                 free(events);                  free(events);
         }          }
Line 212 
Line 205 
         char *p, **endp;          char *p, **endp;
         int *flags;          int *flags;
 {  {
         int val, var;          int val, var, i;
         char *start, savech;          char *start, savech;
   
         for (; !isdigit(*p) && !isalpha(*p) && *p != '*'; ++p)          for (; !isdigit(*p) && !isalpha(*p) && *p != '*' && *p != '\t'; ++p)
                 ;                  ;
         if (*p == '*') {                        /* `*' is current month */          if (*p == '*') {                        /* `*' is every month */
                 *flags |= F_ISMONTH;                  *flags |= F_ISMONTH;
                 *endp = p+1;                  *endp = p+1;
                 return (tp->tm_mon + 1);                  return (-1);    /* means 'every month' */
         }          }
         if (isdigit(*p)) {          if (isdigit(*p)) {
                 val = strtol(p, &p, 10);        /* if 0, it's failure */                  val = strtol(p, &p, 10);        /* if 0, it's failure */
Line 250 
Line 243 
   
             /* variable weekday */              /* variable weekday */
             if ((var = getdayvar(start)) != 0) {              if ((var = getdayvar(start)) != 0) {
                 if (var <=5 && var >= -4)                  if (var <= 5 && var >= -4)
                     val += var * 10;                      val += var * 10;
 #ifdef DEBUG  #ifdef DEBUG
                 printf("var: %d\n", var);                  printf("var: %d\n", var);
Line 258 
Line 251 
             }              }
         }          }
   
         /* Easter */          /* Try specials (Easter, Paskha, ...) */
         else if ((val = geteaster(start, tp->tm_year + TM_YEAR_BASE)) != 0)  
             *flags |= F_EASTER;  
   
         /* Paskha */  
         else if ((val = getpaskha(start, tp->tm_year + TM_YEAR_BASE)) != 0)  
             *flags |= F_EASTER;  
   
         /* undefined rest */  
         else {          else {
                 *p = savech;                  for (i = 0; i < NUMEV; i++) {
                 return (0);                          if (strncasecmp(start, spev[i].name, spev[i].nlen) == 0) {
                                   start += spev[i].nlen;
                                   val = i + 1;
                                   i = NUMEV + 1;
                           } else if (spev[i].uname != NULL &&
                               strncasecmp(start, spev[i].uname, spev[i].ulen) == 0) {
                                   start += spev[i].ulen;
                                   val = i + 1;
                                   i = NUMEV + 1;
                           }
                   }
                   if (i > NUMEV) {
                           switch(*start) {
                           case '-':
                           case '+':
                              var = atoi(start);
                              if (var > 365 || var < -365)
                                      return (0); /* Someone is just being silly */
                              val += (NUMEV + 1) * var;
                              /* We add one to the matching event and multiply by
                               * (NUMEV + 1) so as not to return 0 if there's a match.
                               * val will overflow if there is an obscenely large
                               * number of special events. */
                              break;
                           }
                   *flags |= F_SPECIAL;
                   }
                   if (!(*flags & F_SPECIAL)) {
                   /* undefined rest */
                           *p = savech;
                           return (0);
                   }
         }          }
         for (*p = savech; !isdigit(*p) && !isalpha(*p) && *p != '*'; ++p)          for (*p = savech; !isdigit(*p) && !isalpha(*p) && *p != '*' && *p != '\t'; ++p)
                 ;                  ;
         *endp = p;          *endp = p;
         return (val);          return (val);
Line 279 
Line 295 
   
 char path[MAXPATHLEN];  char path[MAXPATHLEN];
   
   int
   openf(path)
           char *path;
   {
           struct stat st;
           int fd;
   
           fd = open(path, O_RDONLY|O_NONBLOCK);
           if (fd == -1)
                   return (-1);
           if (fstat(fd, &st) == -1) {
                   close(fd);
                   return (-1);
           }
           if ((st.st_mode & S_IFMT) != S_IFREG) {
                   close (fd);
                   return (-1);
           }
   
           fcntl(fd, F_SETFD, fcntl(fd, F_GETFD, 0) &~ O_NONBLOCK);
           return (fd);
   }
   
 FILE *  FILE *
 opencal()  opencal()
 {  {
         int fd, pdes[2];          int fd, pdes[2];
           int fdin;
         struct stat sbuf;          struct stat sbuf;
   
         /* open up calendar file as stdin */          /* open up calendar file as stdin */
         if (!freopen(calendarFile, "r", stdin)) {          if ((fdin = openf(calendarFile)) == -1) {
                 if (doall) {                  if (doall) {
                     if (chdir(calendarHome) != 0)                          if (chdir(calendarHome) != 0)
                         return (NULL);                                  return (NULL);
                     if (stat(calendarNoMail, &sbuf) == 0)                          if (stat(calendarNoMail, &sbuf) == 0)
                         return (NULL);                                  return (NULL);
                     if (!freopen(calendarFile, "r", stdin))                          if ((fdin = openf(calendarFile)) == -1)
                         return (NULL);                                  return (NULL);
                 } else {                  } else {
                         chdir(getenv("HOME"));                          char *home = getenv("HOME");
                         if (!(chdir(calendarHome) == 0 &&                          if (home == NULL || *home == '\0')
                               freopen(calendarFile, "r", stdin)))                                  errx(1, "cannot get home directory");
                           if (!(chdir(home) == 0 &&
                               chdir(calendarHome) == 0 &&
                               (fdin = openf(calendarFile)) != -1))
                                 errx(1, "no calendar file: ``%s'' or ``~/%s/%s",                                  errx(1, "no calendar file: ``%s'' or ``~/%s/%s",
                                     calendarFile, calendarHome, calendarFile);                                      calendarFile, calendarHome, calendarFile);
                 }                  }
Line 310 
Line 353 
                 (void)close(pdes[1]);                  (void)close(pdes[1]);
                 return (NULL);                  return (NULL);
         case 0:          case 0:
                 /* child -- stdin already setup, set stdout to pipe input */                  dup2(fdin, STDIN_FILENO);
                   /* child -- set stdout to pipe input */
                 if (pdes[1] != STDOUT_FILENO) {                  if (pdes[1] != STDOUT_FILENO) {
                         (void)dup2(pdes[1], STDOUT_FILENO);                          (void)dup2(pdes[1], STDOUT_FILENO);
                         (void)close(pdes[1]);                          (void)close(pdes[1]);
Line 369 
Line 413 
                 (void)setuid(geteuid());                  (void)setuid(geteuid());
                 (void)setgid(getegid());                  (void)setgid(getegid());
                 execl(_PATH_SENDMAIL, "sendmail", "-i", "-t", "-F",                  execl(_PATH_SENDMAIL, "sendmail", "-i", "-t", "-F",
                     "\"Reminder Service\"", "-f", "root", NULL);                      "\"Reminder Service\"", NULL);
                 warn(_PATH_SENDMAIL);                  warn(_PATH_SENDMAIL);
                 _exit(1);                  _exit(1);
         }          }
Line 384 
Line 428 
         (void)close(pdes[1]);          (void)close(pdes[1]);
 done:   (void)fclose(fp);  done:   (void)fclose(fp);
         (void)unlink(path);          (void)unlink(path);
         while (wait(&status) >= 0);          while (wait(&status) >= 0)
                   ;
 }  }
   
   

Legend:
Removed from v.1.5  
changed lines
  Added in v.1.10