annotate toys/cal.c @ 562:4d802d438983

Match uint64_t with PRIu64 to avoid warnings on 64 bit builds.
author Rob Landley <rob@landley.net>
date Sat, 14 Apr 2012 21:27:00 -0500
parents 92ef50262ec7
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
394
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
1 /* vi: set sw=4 ts=4:
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
2 *
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
3 * cal.c - show calendar.
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
4 *
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
5 * Copyright 2011 Rob Landley <rob@landley.net>
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
6 *
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
7 * See http://opengroup.org/onlinepubs/9699919799/utilities/cal.html
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
8
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
9 USE_CAL(NEWTOY(cal, ">2", TOYFLAG_USR|TOYFLAG_BIN))
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
10
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
11 config CAL
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
12 bool "cal"
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
13 default y
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
14 help
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
15 usage: cal [[month] year]
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
16 Print a calendar.
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
17
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
18 With one argument, prints all months of the specified year.
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
19 With two arguments, prints calendar for month and year.
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
20 */
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
21
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
22 #include "toys.h"
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
23
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
24 // Write calendar into buffer: each line is 20 chars wide, end indicated
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
25 // by empty string.
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
26
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
27 static char *calstrings(char *buf, struct tm *tm)
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
28 {
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
29 char temp[21];
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
30 int wday, mday, start, len, line;
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
31
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
32 // header
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
33 len = strftime(temp, 21, "%B %Y", tm);
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
34 len += (20-len)/2;
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
35 buf += sprintf(buf, "%*s%*s ", len, temp, 20-len, "");
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
36 buf++;
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
37 buf += sprintf(buf, "Su Mo Tu We Th Fr Sa ");
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
38 buf++;
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
39
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
40 // What day of the week does this month start on?
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
41 if (tm->tm_mday>1)
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
42 start = (36+tm->tm_wday-tm->tm_mday)%7;
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
43 else start = tm->tm_wday;
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
44
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
45 // What day does this month end on? Alas, libc doesn't tell us...
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
46 len = 31;
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
47 if (tm->tm_mon == 1) {
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
48 int year = tm->tm_year;
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
49 len = 28;
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
50 if (!(year & 3) && !((year&100) && !(year&400))) len++;
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
51 } else if ((tm->tm_mon+(tm->tm_mon>6 ? 1 : 0)) & 1) len = 30;
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
52
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
53 for (mday=line=0;line<6;line++) {
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
54 for (wday=0; wday<7; wday++) {
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
55 char *pat = " ";
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
56 if (!mday ? wday==start : mday<len) {
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
57 pat = "%2d ";
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
58 mday++;
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
59 }
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
60 buf += sprintf(buf, pat, mday);
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
61 }
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
62 buf++;
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
63 }
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
64
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
65 return buf;
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
66 }
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
67
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
68 void xcheckrange(long val, long low, long high)
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
69 {
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
70 char *err = "%ld %s than %ld";
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
71
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
72 if (val < low) error_exit(err, val, "less", low);
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
73 if (val > high) error_exit(err, val, "greater", high);
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
74 }
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
75
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
76 // Worst case scenario toybuf usage: sizeof(struct tm) plus 21 bytes/line
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
77 // plus 8 lines/month plus 12 months, comes to a bit over 2k of our 4k buffer.
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
78
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
79 void cal_main(void)
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
80 {
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
81 struct tm *tm;
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
82 char *buf = toybuf;
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
83
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
84 if (toys.optc) {
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
85 // Conveniently starts zeroed
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
86 tm = (struct tm *)toybuf;
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
87 buf += sizeof(struct tm);
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
88
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
89 // Last argument is year, one before that (if any) is month.
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
90 xcheckrange(tm->tm_year = atol(toys.optargs[--toys.optc]),1,9999);
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
91 tm->tm_year -= 1900;
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
92 tm->tm_mday = 1;
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
93 tm->tm_hour = 12; // noon to avoid timezone weirdness
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
94 if (toys.optc) {
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
95 xcheckrange(tm->tm_mon = atol(toys.optargs[--toys.optc]),1,12);
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
96 tm->tm_mon--;
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
97
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
98 // Print 12 months of the year
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
99
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
100 } else {
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
101 char *bufs[12];
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
102 int i, j, k;
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
103
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
104 for (i=0; i<12; i++) {
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
105 tm->tm_mon=i;
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
106 mktime(tm);
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
107 buf = calstrings(bufs[i]=buf, tm);
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
108 }
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
109
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
110 // 4 rows, 6 lines each, 3 columns
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
111 for (i=0; i<4; i++) {
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
112 for (j=0; j<8; j++) {
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
113 for(k=0; k<3; k++) {
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
114 char **b = bufs+(k+i*3);
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
115 *b += printf("%s ", *b);
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
116 }
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
117 puts("");
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
118 }
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
119 }
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
120 return;
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
121 }
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
122
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
123 // What day of the week does that start on?
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
124 mktime(tm);
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
125
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
126 } else {
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
127 time_t now;
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
128 time(&now);
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
129 tm = localtime(&now);
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
130 }
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
131
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
132 calstrings(buf, tm);
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
133 while (*buf) buf += printf("%s\n", buf);
92ef50262ec7 Implment cal.
Rob Landley <rob@landley.net>
parents:
diff changeset
134 }