version 1.22, 2008/04/10 15:07:04 |
version 1.23, 2008/04/18 14:41:04 |
|
|
|
|
int julian; |
int julian; |
int mflag = 0; |
int mflag = 0; |
|
int wflag = 0; |
|
|
void ascii_day(char *, int); |
void ascii_day(char *, int); |
void center(const char *, int, int); |
void center(const char *, int, int); |
void day_array(int, int, int *); |
void day_array(int, int, int *); |
int day_in_week(int, int, int); |
int day_in_week(int, int, int); |
int day_in_year(int, int, int); |
int day_in_year(int, int, int); |
|
int week(int, int, int); |
void j_yearly(int); |
void j_yearly(int); |
void monthly(int, int); |
void monthly(int, int); |
void trim_trailing_spaces(char *); |
void trim_trailing_spaces(char *); |
|
|
const char *errstr; |
const char *errstr; |
|
|
yflag = year = 0; |
yflag = year = 0; |
while ((ch = getopt(argc, argv, "jmy")) != -1) |
while ((ch = getopt(argc, argv, "jmwy")) != -1) |
switch(ch) { |
switch(ch) { |
case 'j': |
case 'j': |
julian = 1; |
julian = 1; |
|
|
case 'm': |
case 'm': |
mflag = 1; |
mflag = 1; |
break; |
break; |
|
case 'w': |
|
wflag = 1; |
|
break; |
case 'y': |
case 'y': |
yflag = 1; |
yflag = 1; |
break; |
break; |
|
|
argc -= optind; |
argc -= optind; |
argv += optind; |
argv += optind; |
|
|
|
if (julian && wflag) |
|
usage(); |
|
|
day_headings = DAY_HEADINGS_S; |
day_headings = DAY_HEADINGS_S; |
sep1752 = sep1752s; |
sep1752 = sep1752s; |
if (mflag && julian) { |
if (mflag && julian) { |
|
|
#define DAY_LEN 3 /* 3 spaces per day */ |
#define DAY_LEN 3 /* 3 spaces per day */ |
#define J_DAY_LEN 4 /* 4 spaces per day */ |
#define J_DAY_LEN 4 /* 4 spaces per day */ |
#define WEEK_LEN 20 /* 7 * 3 - one space at the end */ |
#define WEEK_LEN 20 /* 7 * 3 - one space at the end */ |
|
#define WEEKNUMBER_LEN 5 /* 5 spaces per week number */ |
#define J_WEEK_LEN 27 /* 7 * 4 - one space at the end */ |
#define J_WEEK_LEN 27 /* 7 * 4 - one space at the end */ |
#define HEAD_SEP 2 /* spaces between day headings */ |
#define HEAD_SEP 2 /* spaces between day headings */ |
#define J_HEAD_SEP 2 |
#define J_HEAD_SEP 2 |
|
|
|
int |
|
week(int day, int month, int year) |
|
{ |
|
int leap; |
|
int prevleap; |
|
int yearday; |
|
int firstweekday; |
|
int weekday; |
|
int firstday; |
|
int firstsunday; |
|
int shift; |
|
|
|
yearday = day_in_year(day, month, year); |
|
firstweekday = day_in_week(1, 1, year) + 1; |
|
weekday = day_in_week(day, month, year) + 1; |
|
leap = leap_year(year); |
|
prevleap = leap_year(year - 1); |
|
firstday = firstsunday = day_in_year(1, 1, year); |
|
firstsunday = firstday + (8 - firstweekday); |
|
|
|
if (!mflag) |
|
goto sunbased; |
|
|
|
if (yearday <= (8 - firstweekday) && firstweekday > 4) { |
|
if (firstweekday == 5 || (firstweekday == 6 && prevleap)) |
|
return (53); |
|
return (52); |
|
} |
|
|
|
if (((leap ? 366 : 365) - yearday) < (4 - weekday)) |
|
return (1); |
|
|
|
return (((yearday + (7 - weekday) + (firstweekday - 1)) / 7) |
|
- (firstweekday > 4)); |
|
|
|
sunbased: |
|
shift = 1; |
|
if (yearday < firstsunday) |
|
return (1); |
|
if (firstweekday > THURSDAY - 1) |
|
shift = 2; |
|
return ((((yearday + 1) - (weekday - 1)) / 7) + shift); |
|
} |
|
|
void |
void |
monthly(int month, int year) |
monthly(int month, int year) |
{ |
{ |
int col, row, len, days[MAXDAYS]; |
int col, row, len, days[MAXDAYS], firstday; |
char *p, lineout[30]; |
char *p, lineout[30]; |
|
|
day_array(month, year, days); |
day_array(month, year, days); |
|
|
((julian ? J_WEEK_LEN : WEEK_LEN) - len) / 2, "", |
((julian ? J_WEEK_LEN : WEEK_LEN) - len) / 2, "", |
lineout, day_headings); |
lineout, day_headings); |
for (row = 0; row < 6; row++) { |
for (row = 0; row < 6; row++) { |
|
firstday = SPACE; |
for (col = 0, p = lineout; col < 7; col++, |
for (col = 0, p = lineout; col < 7; col++, |
p += julian ? J_DAY_LEN : DAY_LEN) |
p += julian ? J_DAY_LEN : DAY_LEN) { |
|
if (firstday == SPACE && days[row * 7 + col] != SPACE) |
|
firstday = days[row * 7 + col]; |
ascii_day(p, days[row * 7 + col]); |
ascii_day(p, days[row * 7 + col]); |
|
} |
*p = '\0'; |
*p = '\0'; |
trim_trailing_spaces(lineout); |
trim_trailing_spaces(lineout); |
(void)printf("%s\n", lineout); |
(void)printf("%-20s", lineout); |
|
if (wflag && firstday != SPACE) |
|
printf(" [%2d]", week(firstday - mflag, month, year)); |
|
printf("\n"); |
} |
} |
} |
} |
|
|
|
|
void |
void |
yearly(int year) |
yearly(int year) |
{ |
{ |
int col, *dp, i, month, row, which_cal; |
int col, *dp, i, month, row, which_cal, week_len, wn, firstday; |
int days[12][MAXDAYS]; |
int days[12][MAXDAYS]; |
char *p, lineout[80]; |
char *p, lineout[81]; |
|
|
|
week_len = WEEK_LEN; |
|
if (wflag) |
|
week_len += WEEKNUMBER_LEN; |
(void)snprintf(lineout, sizeof(lineout), "%d", year); |
(void)snprintf(lineout, sizeof(lineout), "%d", year); |
center(lineout, WEEK_LEN * 3 + HEAD_SEP * 2, 0); |
center(lineout, week_len * 3 + HEAD_SEP * 2, 0); |
(void)printf("\n\n"); |
(void)printf("\n\n"); |
for (i = 0; i < 12; i++) |
for (i = 0; i < 12; i++) |
day_array(i + 1, year, days[i]); |
day_array(i + 1, year, days[i]); |
(void)memset(lineout, ' ', sizeof(lineout) - 1); |
(void)memset(lineout, ' ', sizeof(lineout) - 1); |
lineout[sizeof(lineout) - 1] = '\0'; |
lineout[sizeof(lineout) - 1] = '\0'; |
for (month = 0; month < 12; month += 3) { |
for (month = 0; month < 12; month += 3) { |
center(month_names[month], WEEK_LEN, HEAD_SEP); |
center(month_names[month], week_len, HEAD_SEP); |
center(month_names[month + 1], WEEK_LEN, HEAD_SEP); |
center(month_names[month + 1], week_len, HEAD_SEP); |
center(month_names[month + 2], WEEK_LEN, 0); |
center(month_names[month + 2], week_len, 0); |
(void)printf("\n%s%*s%s%*s%s\n", day_headings, |
(void)printf("\n%s%*s%s%*s%s\n", day_headings, |
HEAD_SEP, "", day_headings, HEAD_SEP, "", day_headings); |
HEAD_SEP + (wflag ? WEEKNUMBER_LEN : 0), "", day_headings, |
|
HEAD_SEP + (wflag ? WEEKNUMBER_LEN : 0), "", day_headings); |
|
|
for (row = 0; row < 6; row++) { |
for (row = 0; row < 6; row++) { |
for (which_cal = 0; which_cal < 3; which_cal++) { |
for (which_cal = 0; which_cal < 3; which_cal++) { |
p = lineout + which_cal * (WEEK_LEN + 2); |
p = lineout + which_cal * (week_len + 2); |
|
|
dp = &days[month + which_cal][row * 7]; |
dp = &days[month + which_cal][row * 7]; |
for (col = 0; col < 7; col++, p += DAY_LEN) |
firstday = SPACE; |
|
for (col = 0; col < 7; col++, p += DAY_LEN) { |
|
if (firstday == SPACE && *dp != SPACE) |
|
firstday = *dp; |
ascii_day(p, *dp++); |
ascii_day(p, *dp++); |
|
} |
|
if (wflag && firstday != SPACE) { |
|
wn = week(firstday - mflag, |
|
month + which_cal + 1, year); |
|
(void)snprintf(p, 5, "[%2d]", wn); |
|
p += strlen(p); |
|
*p = ' '; |
|
} else |
|
memset(p, ' ', 4); |
} |
} |
*p = '\0'; |
*p = '\0'; |
trim_trailing_spaces(lineout); |
trim_trailing_spaces(lineout); |
|
|
usage(void) |
usage(void) |
{ |
{ |
|
|
(void)fprintf(stderr, "usage: cal [-jmy] [month] [year]\n"); |
(void)fprintf(stderr, "usage: cal [-jmwy] [month] [year]\n"); |
exit(1); |
exit(1); |
} |
} |
|
|