home *** CD-ROM | disk | FTP | other *** search
- /*
- * $Header: expire.c,v 2.1 89/12/15 17:04:54 billr Exp $
- *
- * expire.c
- * expire outdated appts, i.e. remove them from the appts file, if they
- * are older than <n> days.
- *
- * Copyright (C) 1989 Tektronix, Inc.
- * All Rights Reserved
- * Permission is hereby granted to use and modify this code in source
- * or binary form as long as it is not sold for profit and this copyright
- * notice remains intact.
- */
-
- #include <stdio.h>
- #include <sys/time.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <sys/errno.h>
- #include "ct.h"
-
- extern struct tm today, current;
- extern char apts_pathname[], tmpapts_pathname[];
- extern char inbuf[], apts_dir[];
- extern int save_old;
- extern int errno;
- extern double julian_day();
- extern double nth_mday_of_month();
-
- /*
- * Scan appointments file for outdated appointments. If <save_old> is
- * TRUE then save them to a special file of the form ".appointments.YY",
- * where YY is the year of that appointment. If <edays> is zero, then
- * all other appointments are copied to the new appts file. If <edays>
- * > 0, then any appointments more than <edays> old are discarded.
- * Setting <save_old> TRUE and <edays> to 0 gives the behavior described
- * by the "-o" switch.
- */
- expire(edays)
- int edays;
- {
- FILE *apts, *temp_apts, *fp;
- int read_stat;
- int runl, week;
- char save_file[128];
- struct appt_entry appt;
- struct stat sbuf;
-
- /*
- * method: for each regular appt in the file (or limited
- * duration appt), compare the Julian date of that appt
- * and the Julian date of today, looking for a difference
- * greater than <edays>.
- */
- if ((apts = fopen(apts_pathname, "r")) == NULL)
- err_rpt("can't open appointments file", FATAL);
-
- if ((temp_apts = fopen(tmpapts_pathname, "w")) == NULL)
- err_rpt("can't open temp file for writing", FATAL);
-
- /*
- * now go thru the appointments file
- */
- while ((read_stat=get_aentry(apts, &appt, TRUE)) != EOF) {
- if (read_stat)
- continue; /* read error (ignore) */
- if (appt.flags & A_COMMENT) {
- fputs(inbuf, temp_apts);
- continue;
- }
- current.tm_year = appt.year;
- current.tm_mon = appt.month;
- current.tm_mday = appt.day;
- if (appt.flags & ALL_YEARS)
- /* force this to be saved */
- current.tm_year = today.tm_year + 1;
- if (appt.flags & ALL_MONTHS)
- /* maybe saved, pick worse case */
- current.tm_mon = DEC;
- if (appt.flags & ALL_DAYS) {
- if (current.tm_year < today.tm_year ||
- (current.tm_year == today.tm_year &&
- current.tm_mon < today.tm_mon))
- /* maybe saved, pick worse case */
- current.tm_mday = monthlength(current.tm_mon);
- }
- if (appt.flags & EVERY_SOMEDAY) {
- if ((appt.repeat & ALL_WEEKS) == ALL_WEEKS || appt.repeat & LAST_WEEK ||
- appt.repeat & WEEK5)
- week = 5;
- else if (appt.repeat & WEEK4)
- week = 4;
- else if (appt.repeat & WEEK3)
- week = 3;
- else if (appt.repeat & WEEK2)
- week = 2;
- else if (appt.repeat & WEEK1)
- week = 1;
- current.tm_mday = (int)nth_mday_of_month(week, Pickday(appt.flags), current.tm_mon, current.tm_year+1900);
- if (current.tm_mday > monthlength(current.tm_mon))
- current.tm_mday = (int)nth_mday_of_month(week-1, Pickday(appt.flags), current.tm_mon, current.tm_year+1900);
- if (appt.flags & RUN) {
- current.tm_mday += appt.runlength;
- fix_current_day();
- }
- } else if (appt.flags & REPEAT) {
- if (appt.flags & RUN)
- runl = appt.runlength;
- else
- runl = 1;
- while (ymd_compare(current, today) < 0 && runl) {
- if (appt.flags & RUN)
- --runl;
- if (runl) {
- current.tm_mday += appt.repeat;
- fix_current_day();
- }
- }
- }
- if (save_old && current.tm_year < today.tm_year) {
- /* prepend directory info */
- sprintf(save_file, "%s/.appointments.%02d",
- apts_dir, appt.year);
- if (stat(save_file, &sbuf) && errno == ENOENT) {
- /* new file*/
- if ((fp = fopen(save_file, "w")) == NULL)
- err_rpt("can't open save file, bailing out", FATAL);
- fputs(HEADER, fp);
- fclose(fp);
- }
- if ((fp = fopen(save_file, "a+")) == NULL)
- err_rpt("can't open save file, bailing out", FATAL);
- else {
- if (put_aentry(fp, &appt))
- err_rpt("write to save appt file failed, bailing out", FATAL);
- fclose(fp);
- }
- }
- current.tm_mday += edays; /* offset by expire days */
- fix_current_day();
- if (edays == 0 ||
- julian_day((double)current.tm_mday, current.tm_mon, current.tm_year+1900) >=
- julian_day((double)today.tm_mday, today.tm_mon, today.tm_year+1900)) {
- if (put_aentry(temp_apts, &appt)) {
- /* write error */
- break;
- }
- }
- }
- if (ferror(temp_apts))
- err_rpt("write on temp file failed", FATAL);
- fclose(temp_apts);
- fclose(apts);
- /* don't rename zero length files */
- stat(tmpapts_pathname, &sbuf);
- if (sbuf.st_size == (off_t) 0)
- err_rpt("zero length temp file - not renamed", NON_FATAL);
- else
- xrename(tmpapts_pathname, apts_pathname);
- }
-
-