home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Simtel MSDOS 1992 September
/
Simtel20_Sept92.cdr
/
msdos
/
ddjmag
/
ddj8711.arc
/
HOLUBLST.NOV
< prev
next >
Wrap
Text File
|
1987-10-08
|
15KB
|
400 lines
Listing 1 -- calendar.c, Printed 12/31/1987
____________________________________________________________________________
1| #include <stdio.h>
2| #include <time.h>
3| #include <ctype.h>
4| /*------------------------------------------------------------
5| * CALENDAR.C This program searches a file called "calendar"
6| * in the current directory and prints all lines
7| * that start with today's or tomorrow's date. Note that this
8| * behaviour differs from Unix, which allows the date to be
9| * anywhere on the line. Leading whitespace on the line is
10| * ignored, however. Like Unix, "tomorrow" is extended to
11| * Monday if calandar is run on a weekend.
12| *
13| * If the date is a weekday (monday, tuesday, wednesday),
14| * which can be abbreviated by the first few character of the
15| * name (mon, tu, wed) then the line is printed on that day,
16| * reguardless of the actual date. For example, lines starting
17| * with "tue" will be printed every tuesday. Explicit dates
18| * can be listed too and most common abreviations work:
19| *
20| * Sept. 7, 1987
21| * September 7 '87
22| * sept. 7
23| * Sep 7 87
24| * 9-7
25| * 9/7
26| * 9/7/87
27| * 9 7 1987
28| * 9-7-1987
29| *
30| * and so forth. Like unix, "7 September" doesn't work, however.
31| * The day must follow the month.
32| *
33| * If a * is found in the month's section of any of the above
34| * dates (as in "*-7-87" or "* 7)" then that day in any month
35| * will match. If no day is specified, then all days in the
36| * indicated month will match.
37| *
38| * Abbreviations are formed by looking at the first few
39| * characters of the word. The shortest possible abbreviations
40| * are:
41| *
42| * su sunday mo monday
43| * tu tuesday w wednesday
44| * th thursday fr friday
45| * sa saturday ja january
46| * fe february mar march
47| * ap april may may
48| * jun june jul july
49| * au august se september
50| * o october n novemeber
51| * d december
52| *
53| * BUGS: * If you specify both the day of the week and the date,
54| * as in "Tuesday, July 7, 1987", the "Tuesday" is
55| * processed and the date is ignored.
56| *
57| * * The following won't work:
58| * Jun 1 49er's today
59| * because calendar will pick off 49 as the current year.
60| * Put some nonnumeric character that can't be part of a
61| * date string in front of the "49." You can use any
62| * character but a number or one of: { - . / ' }.
63| */
64|
65| #define JAN 31 /* Days in the month */
66| #define FEB 28
67| #define MAR 31
68| #define APR 30
69| #define MAY 31
70| #define JUN 30
71| #define JUL 31
72| #define AUG 31
73| #define SEP 30
74| #define OCT 31
75| #define NOV 30
76| #define DEC 31
77|
78| int offset_to_first_of_month[] =
79| {
80| 0, /* Offset to the first of */
81| JAN, /* of the month from */
82| JAN +FEB, /* January, 1st. */
83| JAN +FEB +MAR,
84| JAN +FEB +MAR +APR,
85| JAN +FEB +MAR +APR +MAY,
86| JAN +FEB +MAR +APR +MAY +JUN,
87| JAN +FEB +MAR +APR +MAY +JUN +JUL,
88| JAN +FEB +MAR +APR +MAY +JUN +JUL +AUG,
89| JAN +FEB +MAR +APR +MAY +JUN +JUL +AUG +SEP,
90| JAN +FEB +MAR +APR +MAY +JUN +JUL +AUG +SEP +OCT,
91| JAN +FEB +MAR +APR +MAY +JUN +JUL +AUG +SEP +OCT +NOV
92| };
93|
94| /*------------------------------------------------------------*/
95|
96| char *skipstuff(p)
97| char *p;
98| {
99| /* All of the following can be used in a date string */
100|
101| while(isspace(*p)||*p=='-'||*p=='/'||*p=='.'||*p==','
102| ||*p=='\'')
103| ++p;
104|
105| return p;
106| }
107|
108| /*------------------------------------------------------------*/
109|
110| main()
111| {
112| char buf[132], *p;
113| FILE *fd;
114| struct tm *t;
115| long thetime;
116| int month, day, year, wday;
117| int c1, c2, c3;
118|
119| if( !(fd = fopen("calendar", "r") ) )
120| {
121| perror( "calandar" );
122| exit(1);
123| }
124|
125| thetime = time(NULL) ;
126| t = localtime( &thetime );
127| printf("Today is %s\n", asctime( t ) );
128|
129| while( fgets( buf, 132, fd ) )
130| {
131| wday = -1;
132| month = -1;
133| day = 0;
134| year = 0;
135| p = buf;
136|
137| while( isspace(*p) )
138| ++p;
139|
140| if( isalpha(*p) )
141| {
142| c1 = tolower( p[0] );
143| c2 = tolower( p[1] );
144| c3 = tolower( p[2] );
145|
146| if ( c1=='a' && c2=='p' ) month = 3 ;
147| else if( c1=='a' && c2=='u' ) month = 7 ;
148| else if( c1=='d' ) month = 11;
149| else if( c1=='f' && c2=='e' ) month = 1 ;
150| else if( c1=='f' && c2=='r' ) wday = 5 ;
151| else if( c1=='j' && c2=='a' ) month = 0 ;
152| else if( c1=='j' && c2=='u' && c3=='l' ) month = 6 ;
153| else if( c1=='j' && c2=='u' && c3=='n' ) month = 5 ;
154| else if( c1=='m' && c2=='a' && c3=='r' ) month = 2 ;
155| else if( c1=='m' && c2=='a' && c3=='y' ) month = 4 ;
156| else if( c1=='m' && c2=='o' ) wday = 1 ;
157| else if( c1=='n' ) month = 10;
158| else if( c1=='o' ) month = 9 ;
159| else if( c1=='s' && c2=='a' ) wday = 6 ;
160| else if( c1=='s' && c2=='e' ) month = 8 ;
161| else if( c1=='s' && c2=='u' ) wday = 0 ;
162| else if( c1=='t' && c2=='h' ) wday = 4 ;
163| else if( c1=='t' && c2=='u' ) wday = 2 ;
164| else if( c1=='w' ) wday = 3 ;
165|
166| if( wday >0 )
167| {
168| if( t->tm_wday == wday || (t->tm_wday +1) == wday)
169| fputs( buf, stdout );
170|
171| continue;
172| }
173|
174| while( isalpha( *p ) || *p == '.' )
175| p++;
176|
177| p = skipstuff(p);
178| }
179|
180| if( isdigit( *p ) || *p == '*' )
181| {
182| if( *p == '*' )
183| {
184| month = t->tm_mon;
185| ++p;
186| }
187| else if( month < 0 )
188| month = stoi( &p ) - 1;
189|
190| p = skipstuff( p );
191| day = stoi ( &p );
192| p = skipstuff( p );
193|
194| if( (year = stoi(&p)) > 99 )
195| year -= 1900;
196| }
197|
198| if( !year ) year = t->tm_year ;
199| if( month < 0 ) month = t->tm_mon ;
200| if( !day ) day = t->tm_mday ;
201|
202| /* Convert the specified date to an offset, in days,
203| * from Jan 1. Unix provides the offset for today in the
204| * tm_yday field. It makes life difficult by making 1 Jan.
205| * be 0, however. We also have to compensate for leap
206| * year. If the year is a multiple of 4, it's a leap year
207| * unless it's also the first year of the century (1900
208| * was not a leap year, even though it's a multiple of 4).
209| */
210|
211| day += offset_to_first_of_month[ month ] - 1;
212| if( year % 4 == 0 && year % 100 != 0 && month >= 2 )
213| day++;
214|
215| /* Print the line if required. The first test is the
216| * normal situation (today or tomorrow). The second
217| * test takes care of Saturday, which must include
218| * the following Monday in its definition of "tomorrow."
219| * The third test takes care of December 31, which must
220| * recognize January 1 as "tomorrow." Note that I'm
221| * intentionally not testing for the years being different
222| * because it seems reasonable that, if no year is
223| * specified and today is December 31, that the next year
224| * is implied by January 1, with no date.
225| */
226|
227| if( t->tm_year == year &&
228| (day == t->tm_yday || day == t->tm_yday +1) )
229| fputs( buf, stdout );
230|
231| if( t->tm_wday == 6 && day == t->tm_yday + 2 )
232| fputs( buf, stdout );
233|
234| if( t->tm_mday == 31 && t->tm_mon == 11 && day == 0 )
235| fputs( buf, stdout );
236| }
237| }
Listing 2 -- dateh.c, Printed 7/26/1987
____________________________________________________________________________
1| #include <stdio.h>
2| #include <dos.h>
3| #include <time.h>
4| #include <sys/types.h>
5| #include <sys/stat.h>
6|
7| /*------------------------------------------------------------
8| * Creates or updates file called date.h that looks like:
9| *
10| * #define _DAY_ "6-17-87"
11| * #define _TIME_24_ "12:5:23"
12| * #define _TIME_ "12:5 PM"
13| * #define _DATE_ "6-17-87 12:5 PM"
14| *
15| * and holds the current time and date. The file is created
16| * if it doesn't exist. If it does exist, it is modified, but
17| * only if it was not modified or created earlier on the same
18| * day. All of this behaviour can be modified by command-line
19| * switches [see usage(), below].
20| *
21| * This program uses the Unix time functions. For them to work,
22| * you need to set the various codes in the TZ environment
23| * variable. For example:
24| * setenv TZ=PST8PDT (Unix or Sh)
25| * set TZ=PST8PDT (COMMAND.COM)
26| *
27| * Says the Pacific Standard Time is 8 hours off of Greenwich
28| * and the PST is used as the abbreviation for it. PDT is used
29| * as an abbreviation for daylight savings time.
30| *
31| * Exit status is 0 if the file is untouched, 1 if it's created
32| * or modified, 2 on an error of some sort.
33| */
34|
35| main( argc, argv )
36| char **argv;
37| {
38| FILE *fd;
39| struct stat stats;
40| struct tm *t;
41| int day ;
42| long thetime;
43| int pm;
44| char *p;
45|
46| int verbose = 0;
47| int force = 0;
48| int create = 0;
49|
50| if( argc != 1 )
51| {
52| p = argv[1];
53|
54| if( *p != '-' )
55| usage();
56| else
57| for( ++p ; *p ; p++ )
58| switch( *p )
59| {
60| case 'v': verbose = 1; break;
61| case 'f': force = 1; break;
62| case 'c': create = 1; break;
63| default : usage();
64| }
65| }
66|
67| thetime = time(NULL) ;
68|
69| if( stat("date.h", &stats) != 0 )
70| {
71| if( !create ) /* File doesn't exist */
72| exit( 0 );
73|
74| t = localtime( &thetime );
75| printf("Creating DATE.H: %s\n", asctime(t) );
76| }
77| else
78| {
79| t = localtime( &(stats.st_mtime) );
80|
81| if( verbose )
82| printf( "DATE.H last modified: %s", asctime(t) );
83|
84| day = t->tm_yday;
85|
86| t = localtime( &thetime );
87|
88| if( verbose )
89| printf( "Today is: %s", asctime(t) );
90|
91| if( t->tm_yday == day && !force )
92| exit( 0 );
93| else
94| printf("Modifying DATE.H\n");
95| }
96|
97| if( !(fd = fopen( "date.h", "w" )) )
98| {
99| perror( "date.h" );
100| exit( 2 );
101| }
102| else
103| {
104| fprintf(fd, "#define _DAY_ \"%d-%d-%d\"\n",
105| t->tm_mon,
106| t->tm_mday,
107| t->tm_year );
108|
109| fprintf(fd, "#define _TIME_24_ \"%d:%d:%d\"\n",
110| t->tm_hour,
111| t->tm_min,
112| t->tm_sec );
113| pm = t->tm_hour >= 12;
114|
115| if( t->tm_hour > 12 )
116| t->tm_hour -= 12;
117|
118| else if( t->tm_hour == 0 )
119| t->tm_hour = 12;
120|
121| fprintf(fd, "#define _TIME_ \"%d:%d %s\"\n",
122| t->tm_hour,
123| t->tm_min,
124| pm ? "PM" : "AM" );
125|
126| fprintf(fd, "#define _DATE_ \"%d-%d-%d %d:%d %s\"\n",
127| t->tm_mon,
128| t->tm_mday,
129| t->tm_year,
130| t->tm_hour,
131| t->tm_min,
132| pm ? "PM" : "AM" );
133| fclose(fd);
134| }
135|
136| exit( 1 );
137| }
138|
139| /*------------------------------------------------------------*/
140|
141| #define E(x) fprintf(stderr,x)
142|
143| usage()
144| {
145| E("Usage dateh [-fvc]\n");
146| E("If date.h doesn't exist, create it, otherwise if\n");
147| E("the date stamp isn't today, update it\n");
148| E("\n");
149| E("-f forces an update, even if the date stamp is ok\n");
150| E("-v verbose operation\n");
151| E("-c create file if it doesn't exist\n");
152| E("\n");
153| exit( 2 );
154| }