-
Notifications
You must be signed in to change notification settings - Fork 2
/
cal.c
269 lines (217 loc) · 4.29 KB
/
cal.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/* cal.c - print calendar months */
/* cal.c
Taken from RetroBSD source (2.11BSD Unix port for embedded systems):
http://retrobsd.org/
Converted to MESCC by Miguel I. Garcia Lopez / FloppySoftware, Spain
15 Dec 2014 : Version for CP/M.
02 Apr 2015 : Version for SamaruX as external command.
23 Aug 2015 : Minor changes.
To build the CP/M version:
cc cal
zsm cal
hexcom cal
To build the SamaruX version:
cc cal
zmac cal.zsm --rel
link cal [op]
ren cal.x=cal.prl
Define only one of following defs.:
*/
/* #define FOR_CPM */
#define FOR_SAMARUX
#ifdef FOR_CPM
/* CP/M version
------------
*/
#define CC_STDIO /* Support for stdin, stdout, stderr */
#define CC_REDIR /* Support for CP/M command line redirection */
#include <mescc.h>
#include <conio.h>
#include <string.h>
#include <printf.h>
#include <fileio.h>
#include <fprintf.h>
#include <redir.h>
#endif
#ifdef FOR_SAMARUX
/* SamaruX version
---------------
*/
#include <samarux.h>
#endif
char *dayw;
unsigned int smon[12]; /* char *smon[12] */
char mon[13];
char string[432];
main(argc, argv)
int argc, argv[]; /* char *argv[] */
{
int y, i, j;
int m;
if(argc < 2) {
fprintf(stderr, "Usage: cal [month] year\n");
return(0);
}
dayw = "Su Mo Tu We Th Fr Sa";
smon[0] = "Jan"; smon[1] = "Feb"; smon[2] = "Mar";
smon[3] = "Apr"; smon[4] = "May"; smon[5] = "Jun";
smon[6] = "Jul"; smon[7] = "Aug"; smon[8] = "Sep";
smon[9] = "Oct"; smon[10]= "Nov"; smon[11]= "Dec";
mon[0] = 0;
mon[1] = 31; mon[2] = 29; mon[3] = 31; mon[4] = 30;
mon[5] = 31; mon[6] = 30; mon[7] = 31; mon[8] = 31;
mon[9] = 30; mon[10]= 31; mon[11]= 30; mon[12]= 31;
if(argc == 2) {
/*
* print out complete year
*/
y = number(argv[1]);
if(y<1 || y>9999)
return badarg();
printf("\n\n\n");
printf(" %u\n", y);
printf("\n");
for(i=0; i<12; i+=3) {
for(j=0; j<6*72; j++)
string[j] = '\0';
printf(" %s %s %s\n", smon[i], smon[i+1], smon[i+2]);
printf("%s %s %s\n", dayw, dayw, dayw);
cal(i+1, y, string, 72);
cal(i+2, y, string+23, 72);
cal(i+3, y, string+46, 72);
for(j=0; j<6*72; j+=72)
pstr(string+j, 72);
}
printf("\n\n\n");
return(0);
}
/*
* print out just month
*/
m = number(argv[1]);
if(m<1 || m>12)
return badarg();
y = number(argv[2]);
if(y<1 || y>9999)
return badarg();
printf(" %s %u\n", smon[m-1], y);
printf("%s\n", dayw);
cal(m, y, string, 24);
for(i=0; i<6*24; i+=24)
pstr(string+i, 24);
return(0);
}
badarg()
{
fprintf(stderr, "Bad argument"); return 1;
}
number(str)
char *str;
{
int n, c;
char *s;
n = 0;
s = str;
while(c = *s++) {
if(c<'0' || c>'9')
return(0);
n = n*10 + c-'0';
}
return(n);
}
pstr(str, n)
char *str; int n;
{
int i;
char *s;
s = str;
i = n;
while(i--)
if(*s++ == '\0')
s[-1] = ' ';
i = n+1;
while(i--)
if(*--s != ' ')
break;
s[1] = '\0';
printf("%s\n", str);
}
cal(m, y, p, w)
int m, y; char *p; int w;
{
int d, i;
char *s;
s = p;
d = jan1(y);
mon[2] = 29;
mon[9] = 30;
switch((jan1(y+1)+7-d)%7) {
/*
* non-leap year
*/
case 1:
mon[2] = 28;
break;
/*
* 1752
*/
default:
mon[9] = 19;
break;
/*
* leap year
*/
case 2:
;
}
for(i=1; i<m; i++)
d += mon[i];
d %= 7;
s += 3*d;
for(i=1; i<=mon[m]; i++) {
if(i==3 && mon[m]==19) {
i += 11;
mon[m] += 11;
}
if(i > 9)
*s = i/10+'0';
s++;
*s++ = i%10+'0';
s++;
if(++d == 7) {
d = 0;
s = p+w;
p = s;
}
}
}
/*
* return day of the week
* of jan 1 of given year
*/
jan1(yr)
int yr;
{
int y, d;
/*
* normal gregorian calendar
* one extra day per four years
*/
y = yr;
d = 4+y+(y+3)/4;
/*
* julian calendar
* regular gregorian
* less three days per 400
*/
if(y > 1800) {
d -= (y-1701)/100;
d += (y-1601)/400;
}
/*
* great calendar changeover instant
*/
if(y > 1752)
d += 3;
return(d%7);
}