Path: icaen!news.uiowa.edu!uunet!dziuxsolim.rutgers.edu!pilot.njin.net!jac From: jac@pilot.njin.net (Jonathan Chandross) Newsgroups: comp.sources.apple2 Subject: v001SRC089: Calendar/Alarm Clock -- Datebook and Reminder (Unix/GS) Message-ID: Date: 13 Feb 94 23:56:09 GMT Organization: Rutgers Univ., New Brunswick, N.J. Lines: 387 Approved: jac@pilot.njin.net Submitted-by: Christopher Neufeld (neufeld@physics.utoronto.ca) Posting-number: Volume 1, Source:89 Archive-name: util/gs/cal_ac/cal_ac_v1.0 Architecture: Unix,2gs Version-number: 1.00 This package contains an alarm clock and a calendar/date-book. Both require GNO/ME or Unix. Enjoy. =Read.Me - -Alarm Clock/Calendar for GNO/ME - -This package contains an alarm clock and a calendar/date-book. Both -require GNO/ME and only run on a //gs or under Unix. - ------ -ALARM ------ -The alarm program is an alarm clock. The alarm goes off at the specified -hour and minute. When the alarm goes off, the specified message is output, -if present, or a beep is issued, if no message is present. An optional -delay controls the interval between beeps or messages. Alarm is typically -executed in the background. Because it uses sleep(3), very little CPU -time is consumed. - -Usage: - alarm [+]#### [ [""]] - -Where: - #### is a time in hhmm format - ie. 0010 is ten past midnight - +#### indicates that number of hhmm is after the current time - is the time delay between beeps or messages, if any. - is delivered to stdout at alarm condition - -Compiling: - cc -o alarm alarm.cc - --------- -CALENDAR --------- -This program is clone of the Unix calendar(1) program. Calendar reads -the "calendar" file in the current working directory and tells you if -there are any important events today or tomorrow. On weekends, the -concept of tomorrow extends to Monday. - -Compiling: - cc -o calendar calendar.cc - --------------------------- -Files In This Distribution --------------------------- -Read.Me This file -Makefile Make script -alarm.cc C Source to alarm program -alarm.man Manual for alarm program -calendar.cc C Source to alarm program -calendar.ex Example calendar file. Cannot be named calendar - until the "calendar" binary has been installed. - (Otherwise building the "calendar" binary clobbers - the example file.) -calendar.man Manual for alarm program - - -Christopher Neufeld -neufeld@physics.utoronto.ca -Version 1.00 -February 1994 - -COPYRIGHT 1994 BY CHRISTOPHER NEUFELD -ALL RIGHTS RESERVED - =Manifest -Makefile -Manifest -Read.Me -alarm.cc -alarm.man -calendar.cc -calendar.ex -calendar.man =Makefile -################################################################################ -# -# if your system doesn't have the difftime(3C) command, append the following -# on the DEFINES variable: "-DNODIFFTIME" -# -################################################################################ -#DEFINES = -DUNIX -DNODIFFTIME -#INSTALL_DIR = /usr/local/bin -#CC = gcc - -all: calendar alarm - - -calendar: calendar.cc - $(CC) $(DEFINES) -o calendar calendar.cc - -alarm: alarm.cc - $(CC) $(DEFINES) -o alarm alarm.cc - -install: - mv calendar alarm $(INSTALL_DIR) - -clean: - rm -f calendar alarm - =alarm.cc -/* Alarm v1.0. Copyright 1994 by Christopher Neufeld */ - -#ifndef UNIX -#include -#else -#include -#endif -#include -#include -#include -#include -#include - -#pragma stacksize 512 - -#define BEEP putchar((char)0x07) -#define TBEEPS 2 /* Time between beeps in seconds */ - -#define MAXBEEPS 5 - -#define SECSPERDAY (60 * 60 * 24) - -#define DIGTOI(x) ((int) ((x) - '0')) - -#ifdef NODIFFTIME -#define difftime(x1,x2) (double)((x1) - (x2)) -#endif /* Note that this is not necessarily portable, but works with */ - /* some systems which don't have 'difftime' */ - -void usage(char *exename) -{ - fprintf(stderr, "Alarm program. Copyright 1994 by Christopher Neufeld\n"); - fprintf(stderr, "Usage: %s [+]#### [ [\"\"]]\n", exename); - fprintf(stderr, "Where #### is a time in hhmm format, ie. 0010 is ten past midnight\n"); - fprintf(stderr, " +#### indicates that number of hhmm after the current time.\n"); - fprintf(stderr, " delay is the time delay between beeps or messages, if any.\n"); - fprintf(stderr, " message is delivered to stdout at alarm condition\n\n"); - fprintf(stderr, "This executable contains linked runtime libraries copyrighted\n"); - fprintf(stderr, "by The Byte Works. Used with Permission.\n\n"); - exit(1); -} - - -int main(int argc, char **argv) -{ - char *ptr1, *tptr, *msg; - static struct tm timenow, timethen; - static time_t curtime, alarmtime; - static int i, mins, hrs, offset, message, deltat; - unsigned long initwait; - - message = 0; - deltat = TBEEPS; - if (argc < 2 || argc > 4) usage(argv[0]); /* We need 1, 2, or 3 arguments */ - if (argc > 2) { /* Time delay override and possibly a message */ - deltat = (int) strtoul(argv[2], &ptr1, 0); /* Set time delay from arg #2 */ - if (*ptr1 != 0) usage(argv[0]); /* Unable to parse delay time */ - if (argc == 4) { - msg = argv[3]; /* Point the alarm message at arg #3, if present */ - message = 1; - } - } - tptr = argv[1] + (offset = (argv[1][0] == '+')); /* set offset=1 if the - alarm time argument has a '+' sign at the beginning, - and then point 'tptr' and the number after the '+' - sign. If there's no plus sign, offset is clear and - tptr point to the beginning of the argument #1 */ - if (strlen(tptr) != 4) usage(argv[0]); /* We require "hhmm" format, 4 digits */ - for (i=0;i<4;i++) - if (!isdigit(tptr[i])) usage(argv[0]); /* Check that they're all digits */ - hrs = 10 * DIGTOI(tptr[0]) + DIGTOI(tptr[1]); /* Convert first two digits to - a number of hours */ - mins = atoi(tptr + 2); /* Convert last two digits to a number of minutes */ - if (mins > 59 && !offset) - usage(argv[0]); /* You can't ring a bell at 0080 o'clock, flag an - error for such an invocation */ - if (hrs > 23 && !offset) usage(argv[0]); /* Similarly for times past 23 hrs */ - if (!offset) { - curtime = time(NULL); /* Get the current system time */ - timethen = *localtime(&curtime); /* Convert it to a formatted time record */ - timethen.tm_sec = 0; /* Ring the alarm at seconds=0 */ - timethen.tm_min = mins; /* ....minutes = 'mins' */ - timethen.tm_hour = hrs; /* ....hours = 'hrs' */ - alarmtime = mktime(&timethen); /* Convert this back to internal format */ - initwait = difftime(alarmtime, curtime); /* When will that time occur? */ - if (initwait <= 0) initwait += SECSPERDAY; /* If it's already past, ring - the bell at that time tomorrow */ - } else initwait = 60 * (mins + 60 * hrs); /* Convert the delay to seconds */ - sleep(initwait); /* Wait politely for the alarm time */ - for (i=0;i [""]] - -DESCRIPTION - alarm is a reminder program. Typically it is executed in the - background. The program sleeps until the alarm time, and so - uses very little CPU. - - The first argument defines the time, or time delay, for the - alarm to ring. It must consist of an optional leading plus - sign followed by four decimal digits. If the plus sign is - present the alarm will ring in 'hhmm' hours. If the plus sign - is absent, the alarm will ring at time 'hhmm' in 24-hour - format. The alarm rings five times, each time delivering a - beep, with beeps two seconds apart. The second argument, if - present, changes the time between beeps. The third argument, - if present, is echoed to stdout after each beep as a reminder - message. - -EXAMPLES - alarm +0080 5 "Your TV show is on in five minutes" & - rings the bell every five seconds and delivers the - message eighty minutes after the current time. - - alarm 1955 - rings the bell every two seconds at 7:55 PM - -FILES - none - -AUTHOR - Christopher Neufeld (neufeld@physics.utoronto.ca) - =calendar.cc -/* Calendar file v1.0. Copyright 1994 by Christopher Neufeld */ -/* The executable for this file contains linked runtime libraries - copyrighted by The Byte Works. Used with permission. */ - -#include -#include -#include -#include -#include -#include - -#pragma stacksize 512 - -#define NMONTHS 12 -#define NDAYS 31 -#define MAXLINELEN 255 - -#define CALFILE "calendar" - -#define SECSPERDAY (24 * 60 * 60) - -const char months[NMONTHS][4] = {"jan", "feb", "mar", "apr", "may", "jun", - "jul", "aug", "sep", "oct", "nov", "dec"}; - -#ifdef NODIFFTIME -#define difftime(x1,x2) (double)((x1) - (x2)) -#endif /* Note that this is not necessarily portable, but works with */ - /* some systems which don't have 'difftime' */ - -int main(void) -{ - FILE *ifile; - time_t t1, t2; - struct tm st1, st2; - int monthnum, daynum, dayschk, i, j; - char *ptr1, *ptr2, holdmnth[4]; - static char thislin[MAXLINELEN+1]; - long deltat; - - if ((ifile = fopen(CALFILE, "r")) == NULL) exit(0); /* Open calendar file - in CWD. If there is none, exit successfully */ - t1 = time(NULL); /* Get the current time */ - st1 = *localtime(&t1); /* Convert to formatted date/time */ - st1.tm_sec = st1.tm_min = st1.tm_hour = 0; /* Pretend it's midnight, it - makes the checking later much easier */ - t1 = mktime(&st1); /* Make an internal representation for that midnight - (the one which heralded today's date) */ - dayschk = (st1.tm_wday >= 5) ? 8 - st1.tm_wday : 1; /* Check today and - tomorrow, unless tomorrow is on the weekend, in - which case we check up to and including Monday */ - thislin[MAXLINELEN] = 0; - while (!feof(ifile)) { - if (fgets(thislin, MAXLINELEN, ifile) == NULL) { /* Get a line from the calendar file */ - if (feof(ifile)) break; /* Didn't read a line, if we're done, quit */ - fprintf(stderr, "Can't happen\n"); - continue; /* Something funny happened on the read */ - } - ptr1 = thislin; - while (isspace(*ptr1) && *ptr1 != 0) ptr1++; /* Flush initial whitespace */ - if (*ptr1 == 0) continue; /* Blank line */ - monthnum = -1; - if (isdigit(*ptr1)) { /* month/day format */ - monthnum = strtoul(ptr1, &ptr2, 10) - 1; - daynum = strtoul(ptr2+1, NULL, 10); - /* We've now parsed a month/day format line */ - if (monthnum < 0 || monthnum >= NMONTHS || daynum < 0 || daynum > NDAYS) - continue; /* Not a valid date, go on to the next line of the file */ - } else { - for (i=0; i<3; i++) holdmnth[i] = tolower(ptr1[i]); /* make the search - case-insensitive */ - holdmnth[3] = 0; - for (i = 0; i < NMONTHS; i++) - if (!(strcmp(holdmnth, months[i]))) { - monthnum = i; /* look for "jan", "feb", etc. */ - break; - } - if (monthnum == -1) continue; /* Didn't find a valid month, go to next line */ - while (!isspace(*ptr1) && *ptr1 != 0) ptr1++; /* flush text */ - if (*ptr1 == 0) continue; - while (isspace(*ptr1) && *ptr1 != 0) ptr1++; /* flush whitespace */ - if (*ptr1 == 0) continue; /* No day number, go to next line in file */ - daynum = atoi(ptr1); /* get day number */ - if (daynum < 1 || daynum > NDAYS) continue; /* invalid, go to next line */ - } - st2.tm_sec = st2.tm_min = st2.tm_hour = st2.tm_isdst = 0; - st2.tm_mday = daynum; - st2.tm_mon = monthnum; - st2.tm_year = st1.tm_year; - /* We've now set up the time for midnight at the - beginning of the day represented by the line we - found in the calendar file */ - t2 = mktime(&st2); /* Change to internal format */ - if ((deltat = difftime(t2, t1)) < 0) { /* The day was in the past, check - the same date next year */ - st2.tm_year++; - t2 = mktime(&st2); - deltat = difftime(t2, t1); - } - if (deltat <= dayschk * SECSPERDAY) printf(thislin); - /* print the entire line if it is inside our acceptance window */ - } - fclose(ifile); - exit(0); -} =calendar.man -CALENDAR(1) CALENDAR(1) - -NAME - calendar - reminder service - -SYNOPSIS - calendar - -DESCRIPTION - - calendar consults the file "calendar" in the current directory and - prints out lines that contain today's or tomorrow's date at the - beginning of the line. Most reasonable month-day dates such as - ``Aug. 24,'' ``august 24,'' ``8/24,'' etc., are recognized, but not - ``24 August'' or ``24/8''. On weekends ``tomorrow'' extends through - Monday. - -BUGS - calendar's extended idea of ``tomorrow'' does not account for holidays. - -AUTHOR - Christopher Neufeld (neufeld@physics.utoronto.ca) - - -Page 1 - =calendar.ex -Dec 24 Buy those Christmas presents -5/10 You forgot her birthday again, didn't you? Buy her some flowers. -5/11 Buy more flowers. -5/12 Buy more flowers. -May 13 Try chocolates if the flowers aren't working. -May 14 Meet lawyer. + END OF ARCHIVE