cal(1) cleanup

According to suckless coding guidelines.
This commit is contained in:
FRIGN 2015-01-16 22:17:54 +01:00 committed by sin
parent bdca40494a
commit f8177c8ca4

57
cal.c
View File

@ -5,14 +5,14 @@
#include "util.h" #include "util.h"
enum {Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec}; enum { JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC };
enum caltype {Julian, Gregorian}; enum caltype { JULIAN, GREGORIAN };
enum {TRANS_YEAR = 1752, TRANS_MONTH = Sep, TRANS_DAY = 2}; enum { TRANS_YEAR = 1752, TRANS_MONTH = SEP, TRANS_DAY = 2 };
static int static int
isleap(int year, enum caltype cal) isleap(int year, enum caltype cal)
{ {
if (cal == Gregorian) { if (cal == GREGORIAN) {
if (year % 400 == 0) if (year % 400 == 0)
return 1; return 1;
if (year % 100 == 0) if (year % 100 == 0)
@ -28,7 +28,8 @@ static int
monthlength(int year, int month, enum caltype cal) monthlength(int year, int month, enum caltype cal)
{ {
int mdays[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; int mdays[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
return (month==Feb && isleap(year,cal)) ? 29 : mdays[month];
return (month == FEB && isleap(year,cal)) ? 29 : mdays[month];
} }
/* From http://www.tondering.dk/claus/cal/chrweek.php#calcdow */ /* From http://www.tondering.dk/claus/cal/chrweek.php#calcdow */
@ -36,15 +37,16 @@ static int
dayofweek(int year, int month, int dom, enum caltype cal) dayofweek(int year, int month, int dom, enum caltype cal)
{ {
int m, y, a; int m, y, a;
month += 1; /* in this formula, 1 <= month <= 12 */ month += 1; /* in this formula, 1 <= month <= 12 */
a = (14 - month) / 12; a = (14 - month) / 12;
y = year - a; y = year - a;
m = month + 12*a - 2; m = month + 12 * a - 2;
if (cal == Gregorian) if (cal == GREGORIAN)
return (dom + y + y/4 - y/100 + y/400 + (31*m)/12) % 7; return (dom + y + y / 4 - y / 100 + y / 400 + (31 * m) / 12) % 7;
else /* cal == Julian */ else /* cal == Julian */
return (5 + dom + y + y/4 + (31*m)/12) % 7; return (5 + dom + y + y / 4 + (31 * m) / 12) % 7;
} }
static void static void
@ -52,23 +54,23 @@ printgrid(int year, int month, int fday, int line)
{ {
enum caltype cal; enum caltype cal;
int trans; /* are we in the transition from Julian to Gregorian? */ int trans; /* are we in the transition from Julian to Gregorian? */
int offset, dom, d=0; int offset, dom, d = 0;
if (year < TRANS_YEAR || (year == TRANS_YEAR && month <= TRANS_MONTH)) if (year < TRANS_YEAR || (year == TRANS_YEAR && month <= TRANS_MONTH))
cal = Julian; cal = JULIAN;
else else
cal = Gregorian; cal = GREGORIAN;
trans = (year == TRANS_YEAR && month == TRANS_MONTH); trans = (year == TRANS_YEAR && month == TRANS_MONTH);
offset = dayofweek(year, month, 1, cal) - fday; offset = dayofweek(year, month, 1, cal) - fday;
if (offset < 0) if (offset < 0)
offset += 7; offset += 7;
if (line==1) { if (line == 1) {
for ( ; d < offset; ++d) for ( ; d < offset; ++d)
printf(" "); printf(" ");
dom = 1; dom = 1;
} else { } else {
dom = 8-offset + (line-2)*7; dom = 8 - offset + (line - 2) * 7;
if (trans && !(line==2 && fday==3)) if (trans && !(line == 2 && fday == 3))
dom += 11; dom += 11;
} }
for ( ; d < 7 && dom <= monthlength(year, month, cal); ++d, ++dom) { for ( ; d < 7 && dom <= monthlength(year, month, cal); ++d, ++dom) {
@ -92,8 +94,8 @@ drawcal(int year, int month, int ncols, int nmons, int fday)
for (m = 0; m < nmons; ) { for (m = 0; m < nmons; ) {
n = m; n = m;
for (col = 0; m < nmons && col < ncols; ++col, ++m) { for (col = 0; m < nmons && col < ncols; ++col, ++m) {
cur_year = year + m/12; cur_year = year + m / 12;
cur_month = month + m%12; cur_month = month + m % 12;
if (cur_month > 11) { if (cur_month > 11) {
cur_month -= 12; cur_month -= 12;
cur_year += 1; cur_year += 1;
@ -103,15 +105,15 @@ drawcal(int year, int month, int ncols, int nmons, int fday)
} }
printf("\n"); printf("\n");
for (col = 0, m = n; m < nmons && col < ncols; ++col, ++m) { for (col = 0, m = n; m < nmons && col < ncols; ++col, ++m) {
for (dow = fday; dow < (fday+7); ++dow) for (dow = fday; dow < (fday + 7); ++dow)
printf("%s ", days[dow%7]); printf("%s ", days[dow % 7]);
printf(" "); printf(" ");
} }
printf("\n"); printf("\n");
for (line=1; line<=6; ++line) { for (line = 1; line <= 6; ++line) {
for (col=0, m=n; m<nmons && col<ncols; ++col, ++m) { for (col = 0, m = n; m < nmons && col < ncols; ++col, ++m) {
cur_year = year + m/12; cur_year = year + m / 12;
cur_month = month + m%12; cur_month = month + m % 12;
if (cur_month > 11) { if (cur_month > 11) {
cur_month -= 12; cur_month -= 12;
cur_year += 1; cur_year += 1;
@ -127,9 +129,8 @@ drawcal(int year, int month, int ncols, int nmons, int fday)
static void static void
usage(void) usage(void)
{ {
eprintf("usage: %s [-1] [-3] [-m] [-s] [-y] [-c columns]" eprintf("usage: %s [-1 | -3 | -y | -n nmonths] "
" [-f firstday] [-n nmonths] [ [month] year]\n", "[-s | -m | -f firstday] [-c columns] [[[day] month] year]\n", argv0);
argv0);
} }
int int
@ -155,7 +156,7 @@ main(int argc, char *argv[])
case '3': case '3':
nmons = 3; nmons = 3;
month -= 1; month -= 1;
if(month == 0) { if (month == 0) {
month = 12; month = 12;
year--; year--;
} }
@ -209,7 +210,7 @@ main(int argc, char *argv[])
usage(); usage();
} }
drawcal(year, month-1, ncols, nmons, fday); drawcal(year, month - 1, ncols, nmons, fday);
return 0; return 0;
} }