Mercurial > hg > toybox
diff 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 |
line wrap: on
line diff
--- a/toys/posix/cal.c Tue Nov 13 16:13:45 2012 -0600 +++ b/toys/posix/cal.c Tue Nov 13 17:14:08 2012 -0600 @@ -1,6 +1,4 @@ -/* vi: set sw=4 ts=4: - * - * cal.c - show calendar. +/* cal.c - show calendar. * * Copyright 2011 Rob Landley <rob@landley.net> * @@ -9,14 +7,14 @@ USE_CAL(NEWTOY(cal, ">2", TOYFLAG_USR|TOYFLAG_BIN)) config CAL - bool "cal" - default y - help - usage: cal [[month] year] - Print a calendar. + bool "cal" + default y + help + usage: cal [[month] year] + Print a calendar. - With one argument, prints all months of the specified year. - With two arguments, prints calendar for month and year. + With one argument, prints all months of the specified year. + With two arguments, prints calendar for month and year. */ #include "toys.h" @@ -26,51 +24,51 @@ static char *calstrings(char *buf, struct tm *tm) { - char temp[21]; - int wday, mday, start, len, line; + char temp[21]; + int wday, mday, start, len, line; - // header - len = strftime(temp, 21, "%B %Y", tm); - len += (20-len)/2; - buf += sprintf(buf, "%*s%*s ", len, temp, 20-len, ""); - buf++; - buf += sprintf(buf, "Su Mo Tu We Th Fr Sa "); - buf++; + // header + len = strftime(temp, 21, "%B %Y", tm); + len += (20-len)/2; + buf += sprintf(buf, "%*s%*s ", len, temp, 20-len, ""); + buf++; + buf += sprintf(buf, "Su Mo Tu We Th Fr Sa "); + buf++; - // What day of the week does this month start on? - if (tm->tm_mday>1) - start = (36+tm->tm_wday-tm->tm_mday)%7; - else start = tm->tm_wday; + // What day of the week does this month start on? + if (tm->tm_mday>1) + start = (36+tm->tm_wday-tm->tm_mday)%7; + else start = tm->tm_wday; - // What day does this month end on? Alas, libc doesn't tell us... - len = 31; - if (tm->tm_mon == 1) { - int year = tm->tm_year; - len = 28; - if (!(year & 3) && !((year&100) && !(year&400))) len++; - } else if ((tm->tm_mon+(tm->tm_mon>6 ? 1 : 0)) & 1) len = 30; + // What day does this month end on? Alas, libc doesn't tell us... + len = 31; + if (tm->tm_mon == 1) { + int year = tm->tm_year; + len = 28; + if (!(year & 3) && !((year&100) && !(year&400))) len++; + } else if ((tm->tm_mon+(tm->tm_mon>6 ? 1 : 0)) & 1) len = 30; - for (mday=line=0;line<6;line++) { - for (wday=0; wday<7; wday++) { - char *pat = " "; - if (!mday ? wday==start : mday<len) { - pat = "%2d "; - mday++; - } - buf += sprintf(buf, pat, mday); - } - buf++; - } + for (mday=line=0;line<6;line++) { + for (wday=0; wday<7; wday++) { + char *pat = " "; + if (!mday ? wday==start : mday<len) { + pat = "%2d "; + mday++; + } + buf += sprintf(buf, pat, mday); + } + buf++; + } - return buf; + return buf; } void xcheckrange(long val, long low, long high) { - char *err = "%ld %s than %ld"; + char *err = "%ld %s than %ld"; - if (val < low) error_exit(err, val, "less", low); - if (val > high) error_exit(err, val, "greater", high); + if (val < low) error_exit(err, val, "less", low); + if (val > high) error_exit(err, val, "greater", high); } // Worst case scenario toybuf usage: sizeof(struct tm) plus 21 bytes/line @@ -78,57 +76,57 @@ void cal_main(void) { - struct tm *tm; - char *buf = toybuf; + struct tm *tm; + char *buf = toybuf; - if (toys.optc) { - // Conveniently starts zeroed - tm = (struct tm *)toybuf; - buf += sizeof(struct tm); + if (toys.optc) { + // Conveniently starts zeroed + tm = (struct tm *)toybuf; + buf += sizeof(struct tm); - // Last argument is year, one before that (if any) is month. - xcheckrange(tm->tm_year = atol(toys.optargs[--toys.optc]),1,9999); - tm->tm_year -= 1900; - tm->tm_mday = 1; - tm->tm_hour = 12; // noon to avoid timezone weirdness - if (toys.optc) { - xcheckrange(tm->tm_mon = atol(toys.optargs[--toys.optc]),1,12); - tm->tm_mon--; + // Last argument is year, one before that (if any) is month. + xcheckrange(tm->tm_year = atol(toys.optargs[--toys.optc]),1,9999); + tm->tm_year -= 1900; + tm->tm_mday = 1; + tm->tm_hour = 12; // noon to avoid timezone weirdness + if (toys.optc) { + xcheckrange(tm->tm_mon = atol(toys.optargs[--toys.optc]),1,12); + tm->tm_mon--; - // Print 12 months of the year + // Print 12 months of the year - } else { - char *bufs[12]; - int i, j, k; + } else { + char *bufs[12]; + int i, j, k; - for (i=0; i<12; i++) { - tm->tm_mon=i; - mktime(tm); - buf = calstrings(bufs[i]=buf, tm); - } + for (i=0; i<12; i++) { + tm->tm_mon=i; + mktime(tm); + buf = calstrings(bufs[i]=buf, tm); + } - // 4 rows, 6 lines each, 3 columns - for (i=0; i<4; i++) { - for (j=0; j<8; j++) { - for(k=0; k<3; k++) { - char **b = bufs+(k+i*3); - *b += printf("%s ", *b); - } - puts(""); - } - } - return; - } + // 4 rows, 6 lines each, 3 columns + for (i=0; i<4; i++) { + for (j=0; j<8; j++) { + for(k=0; k<3; k++) { + char **b = bufs+(k+i*3); + *b += printf("%s ", *b); + } + puts(""); + } + } + return; + } - // What day of the week does that start on? - mktime(tm); + // What day of the week does that start on? + mktime(tm); - } else { - time_t now; - time(&now); - tm = localtime(&now); - } + } else { + time_t now; + time(&now); + tm = localtime(&now); + } - calstrings(buf, tm); - while (*buf) buf += printf("%s\n", buf); + calstrings(buf, tm); + while (*buf) buf += printf("%s\n", buf); }