summaryrefslogtreecommitdiffstats
path: root/misc-utils/cal.c
diff options
context:
space:
mode:
authorPádraig Brady2007-10-02 01:22:45 +0200
committerKarel Zak2007-10-11 14:19:39 +0200
commitd7a92b89944d16410c03bfc58f360f783cc435c4 (patch)
treef2be51e34688df4d0b1c5f83fb1768df6ab8f18f /misc-utils/cal.c
parenttailf: add option -n to specifying output lines (diff)
downloadkernel-qcow2-util-linux-d7a92b89944d16410c03bfc58f360f783cc435c4.tar.gz
kernel-qcow2-util-linux-d7a92b89944d16410c03bfc58f360f783cc435c4.tar.xz
kernel-qcow2-util-linux-d7a92b89944d16410c03bfc58f360f783cc435c4.zip
cal: add support for highlighting an arbitrary date
This is done by calling cal with the extra day parameter like: cal 14 9 1752 Note the tests were updated to use the new syntax. Note also that this patch changes the -y option to always print a full year, even if a month or the -[13] options are specified. This matches the cal operation from bsdmainutils on debian and also allows one to print a full year while highlighting a particular date. Signed-off-by: Pádraig Brady <P@draigBrady.com>
Diffstat (limited to 'misc-utils/cal.c')
-rw-r--r--misc-utils/cal.c49
1 files changed, 28 insertions, 21 deletions
diff --git a/misc-utils/cal.c b/misc-utils/cal.c
index 10bb8b5cd..d5bcee641 100644
--- a/misc-utils/cal.c
+++ b/misc-utils/cal.c
@@ -341,6 +341,10 @@ main(int argc, char **argv) {
day = month = year = 0;
switch(argc) {
+ case 3:
+ if ((day = atoi(*argv++)) < 1 || month > 31)
+ errx(1, _("illegal day value: use 1-%d"), 31);
+ /* FALLTHROUGH */
case 2:
if ((month = atoi(*argv++)) < 1 || month > 12)
errx(1, _("illegal month value: use 1-12"));
@@ -348,38 +352,38 @@ main(int argc, char **argv) {
case 1:
if ((year = atoi(*argv)) < 1 || year > 9999)
errx(1, _("illegal year value: use 1-9999"));
+ if (day) {
+ int dm = days_in_month[leap_year(year)][month];
+ if (day > dm)
+ errx(1, _("illegal day value: use 1-%d"), dm);
+ day = day_in_year(day, month, year);
+ }
+ if (!month)
+ yflag=1;
break;
case 0:
- {
-#ifdef TEST_CAL
- char *e = getenv("TEST_TIME");
-
- if (e && isdigit((unsigned char) *e))
- now = atol(e);
- else
-#endif
- time(&now);
- }
+ time(&now);
local_time = localtime(&now);
- if (isatty(1))
- day = local_time->tm_yday + 1;
+ day = local_time->tm_yday + 1;
year = local_time->tm_year + 1900;
- if (!yflag)
- month = local_time->tm_mon + 1;
+ month = local_time->tm_mon + 1;
break;
default:
usage();
}
headers_init();
- if (month && num_months == 1)
- monthly(day, month, year);
- else if (month && num_months == 3)
- monthly3(day, month, year);
- else if (julian)
+ if (!isatty(1))
+ day = 0; /* don't highlight */
+
+ if (yflag && julian)
j_yearly(day, year);
- else
+ else if (yflag)
yearly(day, year);
+ else if (num_months == 1)
+ monthly(day, month, year);
+ else if (num_months == 3)
+ monthly3(day, month, year);
exit(0);
}
@@ -633,6 +637,9 @@ day_array(int day, int month, int year, int *days) {
if (month == 9 && year == 1752) {
d_sep1752 = julian ? j_sep1752 : sep1752;
memcpy(days, d_sep1752 + week1stday, MAXDAYS * sizeof(int));
+ for (dm=0; dm<MAXDAYS; dm++)
+ if (j_sep1752[dm] == day)
+ days[dm] |= TODAY_FLAG;
return;
}
memcpy(days, empty, MAXDAYS * sizeof(int));
@@ -806,6 +813,6 @@ void
usage()
{
- (void)fprintf(stderr, _("usage: cal [-13smjyV] [[month] year]\n"));
+ (void)fprintf(stderr, _("usage: cal [-13smjyV] [[[day] month] year]\n"));
exit(1);
}