From 60a378fe3202f14f4d57b4b376bf33919128aed6 Mon Sep 17 00:00:00 2001 From: Simon Schubert Date: Wed, 6 May 2009 11:21:14 +0200 Subject: [PATCH] ncal: interpret a `.' as current year There doesn't seem to be an easy way to refer to the current year, for instance if I wanted to show the calendar of this year's June. Now there is: ncal 6 . --- usr.bin/ncal/ncal.1 | 7 ++++++- usr.bin/ncal/ncal.c | 30 +++++++++++------------------- 2 files changed, 17 insertions(+), 20 deletions(-) diff --git a/usr.bin/ncal/ncal.1 b/usr.bin/ncal/ncal.1 index d6353921d1..5db4862182 100644 --- a/usr.bin/ncal/ncal.1 +++ b/usr.bin/ncal/ncal.1 @@ -25,7 +25,7 @@ .\" $FreeBSD: src/usr.bin/ncal/ncal.1,v 1.8.2.7 2002/10/23 08:10:00 roam Exp $ .\" $DragonFly: src/usr.bin/ncal/ncal.1,v 1.4 2005/07/31 11:46:52 asmodai Exp $ .\" -.Dd December 16, 1997 +.Dd March 12, 2009 .Dt CAL 1 .Os .Sh NAME @@ -98,6 +98,11 @@ A single parameter specifies the year (1 - 9999) to be displayed; note the year must be fully specified. Two parameters denote the month and year; the month is either a number between 1 and 12, or a full or abbreviated name as specified by the current locale. +A single dot, +.Ql \&. , +for the +.Ar year +parameter refers to the current year. .Pp A year starts on Jan 1. .Sh DIAGNOSTICS diff --git a/usr.bin/ncal/ncal.c b/usr.bin/ncal/ncal.c index 1b800b195a..5bc0f48b2d 100644 --- a/usr.bin/ncal/ncal.c +++ b/usr.bin/ncal/ncal.c @@ -185,6 +185,7 @@ main(int argc, char *argv[]) struct djswitch *p, *q; /* to search user defined switch date */ date never = {10000, 1, 1}; /* outside valid range of dates */ date ukswitch = {1752, 9, 2};/* switch date for Great Britain */ + date dt; /* today as date */ int ch; /* holds the option character */ int m = 0; /* month */ int y = 0; /* year */ @@ -203,18 +204,15 @@ main(int argc, char *argv[]) term_se = term_so = NULL; today = 0; + t = time(NULL); + tm1 = localtime(&t); + y = dt.y = tm1->tm_year + 1900; + m = dt.m = tm1->tm_mon + 1; + dt.d = tm1->tm_mday; if (isatty(STDOUT_FILENO) && tgetent(tbuf, NULL) == 1) { - date dt; /* handy date */ - b = cbuf; term_so = tgetstr("so", &b); term_se = tgetstr("se", &b); - t = time(NULL); - tm1 = localtime(&t); - dt.y = tm1->tm_year + 1900; - dt.m = tm1->tm_mon + 1; - dt.d = tm1->tm_mday; - today = sndaysb(&dt); } @@ -315,16 +313,6 @@ main(int argc, char *argv[]) argc -= optind; argv += optind; - if (argc == 0) { - time_t t; - struct tm *tm; - - t = time(NULL); - tm = localtime(&t); - y = tm->tm_year + 1900; - m = tm->tm_mon + 1; - } - switch (argc) { case 2: if (flag_easter) @@ -336,7 +324,11 @@ main(int argc, char *argv[]) argv[-1]); /* FALLTHROUGH */ case 1: - y = atoi(*argv++); + if (strcmp(*argv, ".") == 0) + /* year was already set above */ + argv++; + else + y = atoi(*argv++); if (y < 1 || y > 9999) errx(EX_USAGE, "year %d not in range 1..9999", y); break; -- 2.41.0