1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24#define FOR_cal
25#include "toys.h"
26
27GLOBALS(
28 struct tm *now;
29)
30
31
32static int monthlen(struct tm *tm)
33{
34 int len = 31, month = tm->tm_mon, year = tm->tm_year;
35
36 if (tm->tm_mon==1) {
37 len = 28;
38 if (!(year&3) && !((year%100) && !(year%400))) len++;
39 } else if ((month+(month>6))&1) len = 30;
40
41 return len;
42}
43
44
45
46
47static char *calstrings(char *buf, struct tm *tm)
48{
49 int wday, mday, start, len, line;
50 char temp[21];
51
52
53 len = strftime(temp, 21, "%B %Y", tm);
54 len += (20-len)/2;
55 buf += sprintf(buf, "%*s%*s ", len, temp, 20-len, "")+1;
56 buf += sprintf(buf, "Su Mo Tu We Th Fr Sa ")+1;
57
58
59 if (tm->tm_mday>1) start = (36+tm->tm_wday-tm->tm_mday)%7;
60 else start = tm->tm_wday;
61
62
63 len = monthlen(tm);
64
65 for (mday = line = 0; line<6; line++) {
66 for (wday=0; wday<7; wday++) {
67 char *pat = " ";
68 if (!mday ? wday==start : mday<len) {
69 pat = "%2d ";
70 if (!FLAG(h) && tm->tm_year == TT.now->tm_year &&
71 tm->tm_mon == TT.now->tm_mon && mday == TT.now->tm_mday-1) {
72 pat = "\x1b[7m%2d\x1b[m ";
73 }
74 mday++;
75 }
76 buf += sprintf(buf, pat, mday);
77 }
78 buf++;
79 }
80
81 return buf;
82}
83
84
85
86
87
88void cal_main(void)
89{
90 time_t now = time(0);
91 struct tm *tm = localtime(&now);
92 char *buf = toybuf;
93
94 TT.now = tm;
95 if (!isatty(1)) toys.optflags |= FLAG_h;
96
97 if (toys.optc) {
98
99 tm = (struct tm *)toybuf;
100 buf += sizeof(struct tm);
101
102
103 tm->tm_year = atolx_range(toys.optargs[--toys.optc], 1, 9999) - 1900;
104 tm->tm_mday = 1;
105 tm->tm_hour = 12;
106 if (toys.optc) {
107 tm->tm_mon = atolx_range(toys.optargs[--toys.optc], 1, 12)-1;
108 if (toys.optc) {
109 tm->tm_mday = atolx_range(toys.optargs[--toys.optc], 1, monthlen(tm));
110 TT.now = tm;
111 }
112
113
114
115 } else {
116 char *bufs[12];
117 int i, j, k;
118
119 for (i=0; i<12; i++) {
120 tm->tm_mon=i;
121 mktime(tm);
122 buf = calstrings(bufs[i]=buf, tm);
123 }
124
125
126 for (i=0; i<4; i++) {
127 for (j=0; j<8; j++) {
128 for(k=0; k<3; k++) {
129 char **b = bufs+(k+i*3);
130 *b += printf("%s ", *b);
131 }
132 puts("");
133 }
134 }
135 return;
136 }
137
138
139 mktime(tm);
140 }
141
142 calstrings(buf, tm);
143 while (*buf) buf += printf("%s\n", buf);
144}
145