home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume17 / calentool / part11 / expire.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-04-06  |  5.6 KB  |  176 lines

  1. /*
  2.  * $Header: expire.c,v 2.3 91/03/27 16:45:39 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 and store them in .appointmentYY if <save_old>.
  7.  *
  8.  * Copyright (C) 1989, 1991 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,read_only;
  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.  Outdated appointments are
  34.  * appointments that are older that <edays> old.  If <edays> is zero then
  35.  * Outdated appointments are appointments from previous years. If <save_old>
  36.  * is not set, then Outdated appointments are just deleted.
  37.  * Setting <save_old> TRUE (-o) and <edays> to 0 (-x 0) gives the behavior
  38.  * described by the "-o" switch.
  39.  */
  40. expire(edays)
  41. int edays;
  42. {
  43.     FILE *apts, *temp_apts, *fp;
  44.     int read_stat;
  45.     int runl, week;
  46.     char save_file[128];
  47.     struct appt_entry appt;
  48.     struct stat sbuf;
  49.     struct tm savecurrent;
  50.     int successful = 1;    /* assume this worked */
  51.  
  52.     /*
  53.      * method: for each regular appt in the file (or limited
  54.      * duration appt), compare the Julian date of that appt
  55.      * and the Julian date of today, looking for a difference
  56.      * greater than <edays>.
  57.      */
  58.     if (read_only) /* we can't expire from a calendar if read_only */
  59.         return;
  60.     if ((apts = fopen(apts_pathname, "r")) == NULL)
  61.         successful = err_rpt("can't open appointments file, aborting expire", NON_FATAL);
  62.     if ((temp_apts = fopen(tmpapts_pathname, "w")) == NULL)
  63.         successful = err_rpt("can't open temp file for writing, aborting expire", NON_FATAL);
  64.     /*
  65.      * now go thru the appointments file
  66.      */
  67.     savecurrent = current;  /* save so can restore after loop */
  68.     while (successful && (read_stat=get_aentry(apts, &appt, TRUE, 0, 0)) != EOF) {
  69.         if (read_stat)
  70.             continue;    /* read error (ignore) */
  71.         if (appt.flags & A_COMMENT) {
  72.             fputs(inbuf, temp_apts);
  73.             continue;
  74.         }
  75.         current.tm_year = appt.year;
  76.         current.tm_mon = appt.month;
  77.         current.tm_mday = appt.day;
  78.         if (appt.flags & ALL_YEARS)
  79.             /* force this to be saved */
  80.             current.tm_year = today.tm_year + 1;
  81.         if (appt.flags & ALL_MONTHS)
  82.             /* maybe saved, pick worse case */
  83.             current.tm_mon = DEC;
  84.         if (appt.flags & ALL_DAYS || appt.flags & EVERY_MON_FRI) {
  85.             if (current.tm_year < today.tm_year ||
  86.                (current.tm_year == today.tm_year &&
  87.                 current.tm_mon < today.tm_mon)) {
  88.                 /* maybe saved, pick worse case */
  89.                 current.tm_mday = monthlength(current.tm_mon);
  90.                 fix_current_day();
  91.                 if (appt.flags & EVERY_MON_FRI)
  92.                     while (current.tm_wday == SAT
  93.                         || current.tm_wday == SUN) {
  94.                         current.tm_mday--;
  95.                         fix_current_day();
  96.                     }
  97.             }
  98.         }
  99.         if (appt.flags & EVERY_SOMEDAY) {
  100.             if ((appt.repeat & ALL_WEEKS) == ALL_WEEKS || appt.repeat & LAST_WEEK ||
  101.                 appt.repeat & WEEK5)
  102.                 week = 5;
  103.             else if (appt.repeat & WEEK4)
  104.                 week = 4;
  105.             else if (appt.repeat & WEEK3)
  106.                 week = 3;
  107.             else if (appt.repeat & WEEK2)
  108.                 week = 2;
  109.             else if (appt.repeat & WEEK1)
  110.                 week = 1;
  111.             current.tm_mday = (int)nth_mday_of_month(week, Pickday(appt.flags), current.tm_mon, current.tm_year+1900);
  112.             if (current.tm_mday > monthlength(current.tm_mon))
  113.                 current.tm_mday = (int)nth_mday_of_month(week-1, Pickday(appt.flags), current.tm_mon, current.tm_year+1900);
  114.             if (appt.flags & RUN) {
  115.                 current.tm_mday += appt.runlength;
  116.                 fix_current_day();
  117.             }
  118.         } else if (appt.flags & REPEAT) {
  119.             if (appt.flags & RUN)
  120.                 runl = appt.runlength;
  121.             else
  122.                 runl = 1;
  123.             while (ymd_compare(current, today) < 0 && runl) {
  124.                 if (appt.flags & RUN)
  125.                     --runl;
  126.                 if (runl) {
  127.                     current.tm_mday += appt.repeat;
  128.                     fix_current_day();
  129.                 }
  130.             }
  131.         }
  132.         current.tm_mday += edays;  /* offset by expire days */
  133.         fix_current_day();
  134.         if (((edays == 0) && (current.tm_year >= today.tm_year)) ||
  135.             julian_day((double)current.tm_mday, current.tm_mon, current.tm_year+1900) >=
  136.             julian_day((double)today.tm_mday, today.tm_mon, today.tm_year+1900)) {
  137.             if (put_aentry(temp_apts, &appt)) {
  138.                 /* write error */
  139.                 break;
  140.             }
  141.         } else {
  142.             if (save_old) {
  143.                 /* prepend directory info */
  144.                 sprintf(save_file, "%s.%02d",
  145.                     apts_pathname, appt.year);
  146.                 if (stat(save_file, &sbuf) && errno == ENOENT) {
  147.                     /* new file*/
  148.                     if ((fp = fopen(save_file, "w")) == NULL)
  149.                         successful = err_rpt("can't open save file, aborting expire", NON_FATAL);
  150.                     fputs(HEADER, fp);
  151.                     fclose(fp);
  152.                 }
  153.                 if ((fp = fopen(save_file, "a+")) == NULL)
  154.                     successful = err_rpt("can't open save file, aborting expire", NON_FATAL);
  155.                 else {
  156.                     if (put_aentry(fp, &appt))
  157.                         successful = err_rpt("write to save appt file failed, aborting expire", NON_FATAL);
  158.                     fclose(fp);
  159.                 }
  160.             }
  161.         }
  162.         }
  163.     if (ferror(temp_apts))
  164.         successful = err_rpt("write on temp file failed", NON_FATAL);
  165.     fclose(temp_apts);
  166.         fclose(apts);
  167.     current = savecurrent;   /* restore current from temp */
  168.     /* don't rename zero length files */
  169.     stat(tmpapts_pathname, &sbuf);
  170.     if (sbuf.st_size == (off_t) 0)
  171.         err_rpt("zero length temp file - not renamed", NON_FATAL);
  172.     else if (successful)
  173.         xrename(tmpapts_pathname, apts_pathname);
  174. }
  175.  
  176.