comparison toys/posix/cal.c @ 694:786841fdb1e0

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