[comp.sources.misc] v03i001: do.c - a calendar-like utility

geof@nswitgould.OZ (Geoffrey Jones) (04/20/88)

comp.sources.misc: Volume 3, Issue 1
Submitted-By: "Geoffrey Jones" <geof@nswitgould.OZ>
Archive-Name: do

[1.  This was not shar'ed; I "cheap-shar"'ed it from an editor.
 2.  So why didn't this go to the Australian submoderator?  ++bsa]

echo x - do.c
sed 's/^X//' <<\EOF >do.c
X/*	NAME	
X
X		do.c
X
X
X	SYNTAX
X
X		do [option] ...   command ...
X	
X
X	OPTIONS
X
X		All options take the form -Xnumber, where X is
X		a single alphabetic character and number is a
X		sensible integer. Do not include spaces between
X		the option flag and number.
X
X		-d	specifies the day of the month (1 -31).
X		-m	specifies the month (1 - 12).
X		-y	specifies the year (0 - 99).
X		-D	specifies the day of the week (Mon = 1).
X		-W	specifies the week of the month (1 - 5)
X			calculated such that week 1 is the first 
X			7-day block, week 2 the 2nd 7-day block, etc.
X
X
X	DESCRIPTION
X
X		do executes command(s) if the system calendar agrees
X		with the dates coded in the options. If no options are
X		set then the command(s) are executed regardless of the
X		system date.
X
X		if the commands are started they are run under bourne shell
X		(sh). do returns the exit code returned by sh for the
X		last of the executed commands.
X
X		If any command contains spaces, metacharacters, etc., then
X		the whole command should be quoted so that the command is
X		passed through to the command interpreter (sh) as a
X		single string.
X
X
X	EXAMPLE
X
X		do -D2 -m11 -W1 "echo Melbourne Cup Day\!"
X
X		First Tuesday in November.
X
X		do -d13 -D5 date "cat Black" "alias rm rm -i"
X
X		Do the obvious on black Fridays.
X
X
X	COMPILE
X
X		cc do.c -o do
X
X
X	AUTHOR, etc.
X
X		Geoffrey Jones, April 19 1988
X		Computer Science Department,
X		University of Technology, Sydney
X
X
X	BUGS
X
X		Falls over after 1999.
X
X
X	SEE ALSO
X
X		ctime(3)  calendar(1)
X
X*/
X
X#include <stdio.h>
X#include <time.h>
X
Xmain(argc, argv)
Xint	argc;
Xchar	*argv[];
X{
X	long            time(),ti;
X	static int	status, i;
X	struct tm       *localtime(),*tms;
X
X	static int	day, month, year, wday, mweek;
X	static int	dayflg, monthflg, yearflg, wdayflg, mweekflg;
X
X	for (i = 1; i < argc; i++)
X		if (argv[i][0] == '-')
X			switch (argv[i][1]) {
X				case 'd' : day = atoi(&argv[i][2]);
X					   dayflg++;
X					   break;
X				case 'm' : month = atoi(&argv[i][2]) - 1;
X					   monthflg++;
X					   break;
X				case 'y' : year = atoi(&argv[i][2]);
X					   yearflg++;
X					   break;
X				case 'D' : wday = atoi(&argv[i][2]) % 7;
X					   wdayflg++;
X					   break;
X				case 'W' : mweek = atoi(&argv[i][2]) - 1;
X					   mweekflg++;
X					   break;
X				default  : usage(argv);
X			};
X
X	time(&ti);
X	tms=localtime(&ti);
X
X	if (dayflg   && tms->tm_mday != day)             exit(0);
X	if (monthflg && tms->tm_mon  != month)           exit(0);
X	if (yearflg  && tms->tm_year != year)            exit(0);
X	if (wdayflg  && tms->tm_wday != wday)            exit(0);
X	if (mweekflg && (tms->tm_mday - 1) / 7 != mweek) exit(0);
X
X	for (i = 1; i < argc; i++)
X		if (argv[i][0] != '-')
X			status = system(argv[i]); 
X
X	exit(status);
X}
X
Xusage(argv)
Xchar	*argv[];
X{
X	printf("\tUsage:  %s -[dmyDW]number] ...  [command] ... \n",
X	       argv[0]);
X	exit(1);
X}
EOF
exit 0