home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / sun / volume2 / calentool / patch5a / expire.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-02-04  |  4.8 KB  |  162 lines

  1. /*
  2.  * $Header: expire.c,v 2.1 89/12/15 17:04:54 billr Exp $
  3.  *
  4.  * expire.c
  5.  * expire outdated appts, i.e. remove them from the appts file, if they
  6.  * are older than <n> days.
  7.  *
  8.  * Copyright (C) 1989 Tektronix, Inc.
  9.  *    All Rights Reserved
  10.  * Permission is hereby granted to use and modify this code in source
  11.  * or binary form as long as it is not sold for profit and this copyright
  12.  * notice remains intact.
  13.  */
  14.  
  15. #include <stdio.h>
  16. #include <sys/time.h>
  17. #include <sys/types.h>
  18. #include <sys/stat.h>
  19. #include <sys/errno.h>
  20. #include "ct.h"
  21.  
  22. extern struct tm today, current;
  23. extern char apts_pathname[], tmpapts_pathname[];
  24. extern char inbuf[], apts_dir[];
  25. extern int save_old;
  26. extern int errno;
  27. extern double julian_day();
  28. extern double nth_mday_of_month();
  29.  
  30. /*
  31.  * Scan appointments file for outdated appointments. If <save_old> is
  32.  * TRUE then save them to a special file of the form ".appointments.YY",
  33.  * where YY is the year of that appointment.  If <edays> is zero, then
  34.  * all other appointments are copied to the new appts file.  If <edays>
  35.  * > 0, then any appointments more than <edays> old are discarded.
  36.  * Setting <save_old> TRUE and <edays> to 0 gives the behavior described
  37.  * by the "-o" switch.
  38.  */
  39. expire(edays)
  40. int edays;
  41. {
  42.     FILE *apts, *temp_apts, *fp;
  43.     int read_stat;
  44.     int runl, week;
  45.     char save_file[128];
  46.     struct appt_entry appt;
  47.     struct stat sbuf;
  48.  
  49.     /*
  50.      * method: for each regular appt in the file (or limited
  51.      * duration appt), compare the Julian date of that appt
  52.      * and the Julian date of today, looking for a difference
  53.      * greater than <edays>.
  54.      */
  55.     if ((apts = fopen(apts_pathname, "r")) == NULL)
  56.         err_rpt("can't open appointments file", FATAL);
  57.  
  58.     if ((temp_apts = fopen(tmpapts_pathname, "w")) == NULL)
  59.         err_rpt("can't open temp file for writing", FATAL);
  60.  
  61.     /*
  62.      * now go thru the appointments file
  63.      */
  64.     while ((read_stat=get_aentry(apts, &appt, TRUE)) != EOF) {
  65.         if (read_stat)
  66.             continue;    /* read error (ignore) */
  67.         if (appt.flags & A_COMMENT) {
  68.             fputs(inbuf, temp_apts);
  69.             continue;
  70.         }
  71.         current.tm_year = appt.year;
  72.         current.tm_mon = appt.month;
  73.         current.tm_mday = appt.day;
  74.         if (appt.flags & ALL_YEARS)
  75.             /* force this to be saved */
  76.             current.tm_year = today.tm_year + 1;
  77.         if (appt.flags & ALL_MONTHS)
  78.             /* maybe saved, pick worse case */
  79.             current.tm_mon = DEC;
  80.         if (appt.flags & ALL_DAYS) {
  81.             if (current.tm_year < today.tm_year ||
  82.                (current.tm_year == today.tm_year &&
  83.                 current.tm_mon < today.tm_mon))
  84.                 /* maybe saved, pick worse case */
  85.                 current.tm_mday = monthlength(current.tm_mon);
  86.         }
  87.         if (appt.flags & EVERY_SOMEDAY) {
  88.             if ((appt.repeat & ALL_WEEKS) == ALL_WEEKS || appt.repeat & LAST_WEEK ||
  89.                 appt.repeat & WEEK5)
  90.                 week = 5;
  91.             else if (appt.repeat & WEEK4)
  92.                 week = 4;
  93.             else if (appt.repeat & WEEK3)
  94.                 week = 3;
  95.             else if (appt.repeat & WEEK2)
  96.                 week = 2;
  97.             else if (appt.repeat & WEEK1)
  98.                 week = 1;
  99.             current.tm_mday = (int)nth_mday_of_month(week, Pickday(appt.flags), current.tm_mon, current.tm_year+1900);
  100.             if (current.tm_mday > monthlength(current.tm_mon))
  101.                 current.tm_mday = (int)nth_mday_of_month(week-1, Pickday(appt.flags), current.tm_mon, current.tm_year+1900);
  102.             if (appt.flags & RUN) {
  103.                 current.tm_mday += appt.runlength;
  104.                 fix_current_day();
  105.             }
  106.         } else if (appt.flags & REPEAT) {
  107.             if (appt.flags & RUN)
  108.                 runl = appt.runlength;
  109.             else
  110.                 runl = 1;
  111.             while (ymd_compare(current, today) < 0 && runl) {
  112.                 if (appt.flags & RUN)
  113.                     --runl;
  114.                 if (runl) {
  115.                     current.tm_mday += appt.repeat;
  116.                     fix_current_day();
  117.                 }
  118.             }
  119.         }
  120.         if (save_old && current.tm_year < today.tm_year) {
  121.             /* prepend directory info */
  122.             sprintf(save_file, "%s/.appointments.%02d",
  123.                 apts_dir, appt.year);
  124.             if (stat(save_file, &sbuf) && errno == ENOENT) {
  125.                 /* new file*/
  126.                 if ((fp = fopen(save_file, "w")) == NULL)
  127.                     err_rpt("can't open save file, bailing out", FATAL);
  128.                 fputs(HEADER, fp);
  129.                 fclose(fp);
  130.             }
  131.             if ((fp = fopen(save_file, "a+")) == NULL)
  132.                 err_rpt("can't open save file, bailing out", FATAL);
  133.             else {
  134.                 if (put_aentry(fp, &appt))
  135.                     err_rpt("write to save appt file failed, bailing out", FATAL);
  136.                 fclose(fp);
  137.             }
  138.         }
  139.         current.tm_mday += edays;  /* offset by expire days */
  140.         fix_current_day();
  141.         if (edays == 0 ||
  142.             julian_day((double)current.tm_mday, current.tm_mon, current.tm_year+1900) >=
  143.             julian_day((double)today.tm_mday, today.tm_mon, today.tm_year+1900)) {
  144.             if (put_aentry(temp_apts, &appt)) {
  145.                 /* write error */
  146.                 break;
  147.             }
  148.         }
  149.         }
  150.     if (ferror(temp_apts))
  151.         err_rpt("write on temp file failed", FATAL);
  152.     fclose(temp_apts);
  153.         fclose(apts);
  154.     /* don't rename zero length files */
  155.     stat(tmpapts_pathname, &sbuf);
  156.     if (sbuf.st_size == (off_t) 0)
  157.         err_rpt("zero length temp file - not renamed", NON_FATAL);
  158.     else
  159.         xrename(tmpapts_pathname, apts_pathname);
  160. }
  161.  
  162.