source: buchla-68k/orig/DATE/CALNDR.C@ d21fc6f

Last change on this file since d21fc6f was 3ae31e9, checked in by Thomas Lopatic <thomas@…>, 8 years ago

Imported original source code.

  • Property mode set to 100755
File size: 4.4 KB
Line 
1/*
2 =============================================================================
3 calndr.c -- print a calendar
4 Version 1 -- 1986-10-06
5 =============================================================================
6*/
7
8#include <time.h>
9
10char dayw[] = {
11 " S M Tu W Th F S"
12};
13
14char *smon[]= {
15 "January", "February", "March", "April",
16 "May", "June", "July", "August",
17 "September", "October", "November", "December",
18};
19
20char mon[] = {
21 0,
22 31, 29, 31, 30,
23 31, 30, 31, 31,
24 30, 31, 30, 31,
25};
26
27
28char string[432];
29
30extern struct tm *localtime();
31extern long time();
32
33struct tm *thetime;
34long timbuf;
35
36/*
37 =============================================================================
38 main function for calndr command
39 =============================================================================
40*/
41
42main(argc, argv)
43char *argv[];
44{
45 register y, i, j;
46 int m;
47
48 switch(argc) {
49
50 case 1:
51 timbuf = time(&timbuf);
52 thetime = localtime(&timbuf);
53 m = thetime->tm_mon + 1;
54 y = thetime->tm_year + 1900;
55 break;
56
57 case 2:
58 goto xlong;
59
60 case 3:
61 m = number(argv[1]);
62 y = number(argv[2]);
63 break;
64
65 default:
66 printf("usage: calndr [ [month] year ]\n");
67 exit(0);
68 }
69
70 /* print out just month */
71
72 if(m<1 || m>12)
73 goto badarg;
74
75 if(y<1 || y>9999)
76 goto badarg;
77
78 printf(" %s %u\n\n", smon[m-1], y);
79 printf("%s\n", dayw);
80
81 cal(m, y, string, 24);
82
83 for(i=0; i<6*24; i+=24)
84 pstr(string+i, 24);
85
86 exit(0);
87
88 /* print out complete year */
89
90xlong:
91 y = number(argv[1]);
92
93 if(y<1 || y>9999)
94 goto badarg;
95
96 printf("\n\n\n");
97 printf(" %u\n", y);
98 printf("\n");
99
100 for(i=0; i<12; i+=3) {
101
102 for(j=0; j<6*72; j++)
103 string[j] = '\0';
104
105 printf(" %.3s", smon[i]);
106 printf(" %.3s", smon[i+1]);
107 printf(" %.3s\n", smon[i+2]);
108 printf("%s %s %s\n", dayw, dayw, dayw);
109
110 cal(i+1, y, string, 72);
111 cal(i+2, y, string+23, 72);
112 cal(i+3, y, string+46, 72);
113
114 for(j=0; j<6*72; j+=72)
115 pstr(string+j, 72);
116 }
117
118 printf("\n\n\n");
119 exit(0);
120
121badarg:
122 printf("calndr: Bad argument\n");
123 exit(1);
124}
125
126/*
127 =============================================================================
128 number(str) -- extract an integer from str
129 =============================================================================
130*/
131
132number(str)
133char *str;
134{
135 register n, c;
136 register char *s;
137
138 n = 0;
139 s = str;
140
141 while(c = *s++) {
142
143 if(c<'0' || c>'9')
144 return(0);
145
146 n = n*10 + c-'0';
147 }
148
149 return(n);
150}
151
152/*
153 =============================================================================
154 pstr(str, n)
155 =============================================================================
156*/
157
158pstr(str, n)
159char *str;
160{
161 register i;
162 register char *s;
163
164 s = str;
165 i = n;
166
167 while(i--)
168 if(*s++ == '\0')
169 s[-1] = ' ';
170
171 i = n+1;
172
173 while(i--)
174 if(*--s != ' ')
175 break;
176
177 s[1] = '\0';
178 printf("%s\n", str);
179}
180
181/*
182 =============================================================================
183 cal(m, y, p, w) -- calculate calendar string
184 =============================================================================
185*/
186
187cal(m, y, p, w)
188char *p;
189{
190 register d, i;
191 register char *s;
192
193 s = p;
194 d = jan1(y);
195 mon[2] = 29;
196 mon[9] = 30;
197
198 switch((jan1(y+1)+7-d)%7) {
199
200 /*
201 * non-leap year
202 */
203 case 1:
204 mon[2] = 28;
205 break;
206
207 /*
208 * 1752
209 */
210 default:
211 mon[9] = 19;
212 break;
213
214 /*
215 * leap year
216 */
217 case 2:
218 ;
219 }
220
221 for(i=1; i<m; i++)
222 d += mon[i];
223
224 d %= 7;
225 s += 3*d;
226
227 for(i=1; i<=mon[m]; i++) {
228
229 if(i==3 && mon[m]==19) {
230 i += 11;
231 mon[m] += 11;
232 }
233
234 if(i > 9)
235 *s = i/10+'0';
236
237 s++;
238 *s++ = i%10+'0';
239 s++;
240
241 if(++d == 7) {
242 d = 0;
243 s = p+w;
244 p = s;
245 }
246 }
247}
248
249/*
250 =============================================================================
251 jan1(yr) -- return day of the week
252 of jan 1 of given year
253 =============================================================================
254 */
255
256jan1(yr)
257{
258 register y, d;
259
260 /* normal gregorian calendar -- one extra day per four years */
261
262 y = yr;
263 d = 4+y+(y+3)/4;
264
265 /* julian calendar -- regular gregorian less three days per 400 */
266
267 if(y > 1800) {
268 d -= (y-1701)/100;
269 d += (y-1601)/400;
270 }
271
272 /* great calendar changeover instant */
273
274 if(y > 1752)
275 d += 3;
276
277 return(d%7);
278}
279
Note: See TracBrowser for help on using the repository browser.