aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2007-07-26 11:12:51 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2007-07-26 11:12:51 +0000
commit661f6fad77b672a5f6648b01275eb9ff19c59139 (patch)
treeb2fc7dc0541e35a214c4dfa03ec126c4b83cce0f
parent972fa099e6b830f7888adf5882ebad6530369b0a (diff)
downloadbusybox-w32-661f6fad77b672a5f6648b01275eb9ff19c59139.tar.gz
busybox-w32-661f6fad77b672a5f6648b01275eb9ff19c59139.tar.bz2
busybox-w32-661f6fad77b672a5f6648b01275eb9ff19c59139.zip
cal: small code shrink
-rw-r--r--coreutils/cal.c105
1 files changed, 50 insertions, 55 deletions
diff --git a/coreutils/cal.c b/coreutils/cal.c
index ce910bc63..f8fc0b0d3 100644
--- a/coreutils/cal.c
+++ b/coreutils/cal.c
@@ -19,6 +19,8 @@
19 19
20#include "libbb.h" 20#include "libbb.h"
21 21
22/* We often use "unsigned" intead of "int", it's easier to div on most CPUs */
23
22#define THURSDAY 4 /* for reformation */ 24#define THURSDAY 4 /* for reformation */
23#define SATURDAY 6 /* 1 Jan 1 was a Saturday */ 25#define SATURDAY 6 /* 1 Jan 1 was a Saturday */
24 26
@@ -28,29 +30,25 @@
28#define MAXDAYS 42 /* max slots in a month array */ 30#define MAXDAYS 42 /* max slots in a month array */
29#define SPACE -1 /* used in day array */ 31#define SPACE -1 /* used in day array */
30 32
31static const char days_in_month[] = { 33static const unsigned char days_in_month[] = {
32 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 34 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
33}; 35};
34 36
35static const char sep1752[] = { 37static const unsigned char sep1752[] = {
36 1, 2, 14, 15, 16, 38 1, 2, 14, 15, 16,
37 17, 18, 19, 20, 21, 22, 23, 39 17, 18, 19, 20, 21, 22, 23,
38 24, 25, 26, 27, 28, 29, 30 40 24, 25, 26, 27, 28, 29, 30
39}; 41};
40 42
41static int julian; 43static unsigned julian;
42 44
43/* leap year -- account for Gregorian reformation in 1752 */ 45/* leap year -- account for Gregorian reformation in 1752 */
44#define leap_year(yr) \ 46static int leap_year(unsigned yr)
45 ((yr) <= 1752 ? !((yr) % 4) : \
46 (!((yr) % 4) && ((yr) % 100)) || !((yr) % 400))
47
48static int is_leap_year(int year)
49{ 47{
50 return leap_year(year); 48 if (yr <= 1752)
49 return !(yr % 4);
50 return (!(yr % 4) && (yr % 100)) || !(yr % 400);
51} 51}
52#undef leap_year
53#define leap_year(yr) is_leap_year(yr)
54 52
55/* number of centuries since 1700, not inclusive */ 53/* number of centuries since 1700, not inclusive */
56#define centuries_since_1700(yr) \ 54#define centuries_since_1700(yr) \
@@ -64,12 +62,12 @@ static int is_leap_year(int year)
64#define leap_years_since_year_1(yr) \ 62#define leap_years_since_year_1(yr) \
65 ((yr) / 4 - centuries_since_1700(yr) + quad_centuries_since_1700(yr)) 63 ((yr) / 4 - centuries_since_1700(yr) + quad_centuries_since_1700(yr))
66 64
67static void center (char *, int, int); 65static void center(char *, unsigned, unsigned);
68static void day_array (int, int, int *); 66static void day_array(unsigned, unsigned, unsigned *);
69static void trim_trailing_spaces_and_print (char *); 67static void trim_trailing_spaces_and_print(char *);
70 68
71static void blank_string(char *buf, size_t buflen); 69static void blank_string(char *buf, size_t buflen);
72static char *build_row(char *p, int *dp); 70static char *build_row(char *p, unsigned *dp);
73 71
74#define DAY_LEN 3 /* 3 spaces per day */ 72#define DAY_LEN 3 /* 3 spaces per day */
75#define J_DAY_LEN (DAY_LEN + 1) 73#define J_DAY_LEN (DAY_LEN + 1)
@@ -83,20 +81,18 @@ int cal_main(int argc, char **argv)
83 struct tm *local_time; 81 struct tm *local_time;
84 struct tm zero_tm; 82 struct tm zero_tm;
85 time_t now; 83 time_t now;
86 int month, year, flags, i; 84 unsigned month, year, flags, i;
87 char *month_names[12]; 85 char *month_names[12];
88 char day_headings[28]; /* 28 for julian, 21 for nonjulian */ 86 char day_headings[28]; /* 28 for julian, 21 for nonjulian */
89 char buf[40]; 87 char buf[40];
90 88
91 flags = getopt32(argc, argv, "jy"); 89 flags = getopt32(argc, argv, "jy");
92
93 julian = flags & 1; 90 julian = flags & 1;
94
95 argv += optind;
96
97 month = 0; 91 month = 0;
92 argv += optind;
93 argc -= optind;
98 94
99 if ((argc -= optind) > 2) { 95 if (argc > 2) {
100 bb_show_usage(); 96 bb_show_usage();
101 } 97 }
102 98
@@ -109,12 +105,12 @@ int cal_main(int argc, char **argv)
109 } 105 }
110 } else { 106 } else {
111 if (argc == 2) { 107 if (argc == 2) {
112 month = xatoul_range(*argv++, 1, 12); 108 month = xatou_range(*argv++, 1, 12);
113 } 109 }
114 year = xatoul_range(*argv, 1, 9999); 110 year = xatou_range(*argv, 1, 9999);
115 } 111 }
116 112
117 blank_string(day_headings, sizeof(day_headings) - 7 + 7*julian); 113 blank_string(day_headings, sizeof(day_headings) - 7 + 7*julian);
118 114
119 i = 0; 115 i = 0;
120 do { 116 do {
@@ -130,8 +126,8 @@ int cal_main(int argc, char **argv)
130 } while (++i < 12); 126 } while (++i < 12);
131 127
132 if (month) { 128 if (month) {
133 int row, len, days[MAXDAYS]; 129 unsigned row, len, days[MAXDAYS];
134 int *dp = days; 130 unsigned *dp = days;
135 char lineout[30]; 131 char lineout[30];
136 132
137 day_array(month, year, dp); 133 day_array(month, year, dp);
@@ -145,8 +141,8 @@ int cal_main(int argc, char **argv)
145 trim_trailing_spaces_and_print(lineout); 141 trim_trailing_spaces_and_print(lineout);
146 } 142 }
147 } else { 143 } else {
148 int row, which_cal, week_len, days[12][MAXDAYS]; 144 unsigned row, which_cal, week_len, days[12][MAXDAYS];
149 int *dp; 145 unsigned *dp;
150 char lineout[80]; 146 char lineout[80];
151 147
152 sprintf(lineout, "%d", year); 148 sprintf(lineout, "%d", year);
@@ -193,19 +189,21 @@ int cal_main(int argc, char **argv)
193 * out end to end. You would have 42 numbers or spaces. This routine 189 * out end to end. You would have 42 numbers or spaces. This routine
194 * builds that array for any month from Jan. 1 through Dec. 9999. 190 * builds that array for any month from Jan. 1 through Dec. 9999.
195 */ 191 */
196static void day_array(int month, int year, int *days) 192static void day_array(unsigned month, unsigned year, unsigned *days)
197{ 193{
198 long temp; 194 unsigned long temp;
199 int i; 195 unsigned i;
200 int j_offset; 196 unsigned day, dw, dm;
201 int day, dw, dm;
202 197
203 memset(days, SPACE, MAXDAYS * sizeof(int)); 198 memset(days, SPACE, MAXDAYS * sizeof(int));
204 199
205 if ((month == 9) && (year == 1752)) { 200 if ((month == 9) && (year == 1752)) {
201 /* Assumes the Gregorian reformation eliminates
202 * 3 Sep. 1752 through 13 Sep. 1752.
203 */
204 unsigned j_offset = julian * 244;
206 size_t oday = 0; 205 size_t oday = 0;
207 206
208 j_offset = julian * 244;
209 do { 207 do {
210 days[oday+2] = sep1752[oday] + j_offset; 208 days[oday+2] = sep1752[oday] + j_offset;
211 } while (++oday < sizeof(sep1752)); 209 } while (++oday < sizeof(sep1752));
@@ -214,7 +212,7 @@ static void day_array(int month, int year, int *days)
214 } 212 }
215 213
216 /* day_in_year 214 /* day_in_year
217 * return the 1 based day number within the year 215 * return the 1 based day number within the year
218 */ 216 */
219 day = 1; 217 day = 1;
220 if ((month > 2) && leap_year(year)) { 218 if ((month > 2) && leap_year(year)) {
@@ -227,17 +225,15 @@ static void day_array(int month, int year, int *days)
227 } 225 }
228 226
229 /* day_in_week 227 /* day_in_week
230 * return the 0 based day number for any date from 1 Jan. 1 to 228 * return the 0 based day number for any date from 1 Jan. 1 to
231 * 31 Dec. 9999. Assumes the Gregorian reformation eliminates 229 * 31 Dec. 9999. Assumes the Gregorian reformation eliminates
232 * 3 Sep. 1752 through 13 Sep. 1752. Returns Thursday for all 230 * 3 Sep. 1752 through 13 Sep. 1752. Returns Thursday for all
233 * missing days. 231 * missing days.
234 */ 232 */
235 dw = THURSDAY; 233 temp = (long)(year - 1) * 365 + leap_years_since_year_1(year - 1) + day;
236 temp = (long)(year - 1) * 365 + leap_years_since_year_1(year - 1)
237 + day;
238 if (temp < FIRST_MISSING_DAY) { 234 if (temp < FIRST_MISSING_DAY) {
239 dw = ((temp - 1 + SATURDAY) % 7); 235 dw = ((temp - 1 + SATURDAY) % 7);
240 } else if (temp >= (FIRST_MISSING_DAY + NUMBER_MISSING_DAYS)) { 236 } else {
241 dw = (((temp - 1 + SATURDAY) - NUMBER_MISSING_DAYS) % 7); 237 dw = (((temp - 1 + SATURDAY) - NUMBER_MISSING_DAYS) % 7);
242 } 238 }
243 239
@@ -250,10 +246,9 @@ static void day_array(int month, int year, int *days)
250 ++dm; 246 ++dm;
251 } 247 }
252 248
253 while (dm) { 249 do {
254 days[dw++] = day++; 250 days[dw++] = day++;
255 --dm; 251 } while (--dm);
256 }
257} 252}
258 253
259static void trim_trailing_spaces_and_print(char *s) 254static void trim_trailing_spaces_and_print(char *s)
@@ -263,7 +258,7 @@ static void trim_trailing_spaces_and_print(char *s)
263 while (*p) { 258 while (*p) {
264 ++p; 259 ++p;
265 } 260 }
266 while (p > s) { 261 while (p != s) {
267 --p; 262 --p;
268 if (!(isspace)(*p)) { /* We want the function... not the inline. */ 263 if (!(isspace)(*p)) { /* We want the function... not the inline. */
269 p[1] = '\0'; 264 p[1] = '\0';
@@ -274,9 +269,9 @@ static void trim_trailing_spaces_and_print(char *s)
274 puts(s); 269 puts(s);
275} 270}
276 271
277static void center(char *str, int len, int separate) 272static void center(char *str, unsigned len, unsigned separate)
278{ 273{
279 int n = strlen(str); 274 unsigned n = strlen(str);
280 len -= n; 275 len -= n;
281 printf("%*s%*s", (len/2) + n, str, (len/2) + (len % 2) + separate, ""); 276 printf("%*s%*s", (len/2) + n, str, (len/2) + (len % 2) + separate, "");
282} 277}
@@ -287,15 +282,16 @@ static void blank_string(char *buf, size_t buflen)
287 buf[buflen-1] = '\0'; 282 buf[buflen-1] = '\0';
288} 283}
289 284
290static char *build_row(char *p, int *dp) 285static char *build_row(char *p, unsigned *dp)
291{ 286{
292 int col, val, day; 287 unsigned col, val, day;
293 288
294 memset(p, ' ', (julian + DAY_LEN) * 7); 289 memset(p, ' ', (julian + DAY_LEN) * 7);
295 290
296 col = 0; 291 col = 0;
297 do { 292 do {
298 if ((day = *dp++) != SPACE) { 293 day = *dp++;
294 if (day != SPACE) {
299 if (julian) { 295 if (julian) {
300 ++p; 296 ++p;
301 if (day >= 100) { 297 if (day >= 100) {
@@ -304,7 +300,8 @@ static char *build_row(char *p, int *dp)
304 day %= 100; 300 day %= 100;
305 } 301 }
306 } 302 }
307 if ((val = day / 10) > 0) { 303 val = day / 10;
304 if (val > 0) {
308 *p = val + '0'; 305 *p = val + '0';
309 } 306 }
310 *++p = day % 10 + '0'; 307 *++p = day % 10 + '0';
@@ -348,5 +345,3 @@ static char *build_row(char *p, int *dp)
348 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 345 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
349 * SUCH DAMAGE. 346 * SUCH DAMAGE.
350 */ 347 */
351
352