From f90f7b57a383758382e111b33c0a51b2500c5ac1 Mon Sep 17 00:00:00 2001 From: wollman Date: Tue, 23 Nov 2004 22:57:17 +0000 Subject: [PATCH] Add a `-m month' flag to provide a more convenient interface for displaying a calendar for a specific month of the current year than `cal $(date +"%Y") month'. A few minor code cleanups. Set WARNS=1. (This code is WARNS=5 clean except for "`O' modifier used with `%B' strftime format", which is legal in FreeBSD but GCC doesn't know about.) MFC after: 1 week --- usr.bin/ncal/Makefile | 1 + usr.bin/ncal/ncal.1 | 16 ++++++++++--- usr.bin/ncal/ncal.c | 56 ++++++++++++++++++++++++------------------- 3 files changed, 45 insertions(+), 28 deletions(-) diff --git a/usr.bin/ncal/Makefile b/usr.bin/ncal/Makefile index 16690278dfc6..b3e22848c382 100644 --- a/usr.bin/ncal/Makefile +++ b/usr.bin/ncal/Makefile @@ -5,6 +5,7 @@ SRCS= ncal.c DPADD= ${LIBCALENDAR} LDADD= -lcalendar +WARNS= 1 LINKS= ${BINDIR}/ncal ${BINDIR}/cal MLINKS= ncal.1 cal.1 diff --git a/usr.bin/ncal/ncal.1 b/usr.bin/ncal/ncal.1 index f5734b77c852..02d3851ee2d6 100644 --- a/usr.bin/ncal/ncal.1 +++ b/usr.bin/ncal/ncal.1 @@ -24,7 +24,7 @@ .\" .\" $FreeBSD$ .\" -.Dd December 16, 1997 +.Dd November 23, 2004 .Dt CAL 1 .Os .Sh NAME @@ -38,6 +38,10 @@ .Op Ar month .Ar year .Oc +.Nm +.Op Fl j +.Fl m Ar month +.Op Ar year .Nm ncal .Op Fl jJpwy .Op Fl s Ar country_code @@ -69,6 +73,9 @@ option, display date of easter according to the Julian Calendar. Display date of easter (for western churches). .It Fl j Display Julian days (days one-based, numbered from January 1). +.It Fl m Ar month +Display the specified +.Ar month . .It Fl o Display date of orthodox easter (Greek and Russian Orthodox Churches). @@ -91,7 +98,7 @@ Britain and her colonies switched to the Gregorian Calendar. .It Fl w Print the number of the week below each week column. .It Fl y -Display a calendar for the current year. +Display a calendar for the specified year. .El .Pp A single parameter specifies the year (1 - 9999) to be displayed; @@ -102,6 +109,9 @@ will display a calendar for 1989. 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. +Month and year default to those of the current system clock and time zone (so +.Dq Li cal -m 8 +will display a calendar for the month of August in the current year). .Pp A year starts on Jan 1. .Sh SEE ALSO @@ -122,5 +132,5 @@ The command and manual were written by .An Wolfgang Helbig Aq helbig@FreeBSD.org . .Sh BUGS -The assignment of Julian - Gregorian switching dates to +The assignment of Julian\(EmGregorian switching dates to country codes is historically naive for many countries. diff --git a/usr.bin/ncal/ncal.c b/usr.bin/ncal/ncal.c index 4fa3fd4b82f4..7e0d5eed09cc 100644 --- a/usr.bin/ncal/ncal.c +++ b/usr.bin/ncal/ncal.c @@ -194,6 +194,7 @@ main(int argc, char *argv[]) int flag_orthodox = 0; /* use wants Orthodox easter */ int flag_easter = 0; /* use wants easter date */ char *cp; /* character pointer */ + char *flag_month = NULL; /* requested month as string */ const char *locale; /* locale to get country code */ /* @@ -226,18 +227,16 @@ main(int argc, char *argv[]) * Get the filename portion of argv[0] and set flag_backward if * this program is called "cal". */ - for (cp = argv[0]; *cp; cp++) - ; - while (cp >= argv[0] && *cp != '/') - cp--; - if (strcmp("cal", ++cp) == 0) + cp = strrchr(argv[0], '/'); + cp = (cp == NULL) ? argv[0] : cp + 1; + if (strcmp("cal", cp) == 0) flag_backward = 1; /* Set the switch date to United Kingdom if backwards compatible */ if (flag_backward) nswitchb = ndaysj(&ukswitch); - while ((ch = getopt(argc, argv, "Jejops:wy")) != -1) + while ((ch = getopt(argc, argv, "Jejm:ops:wy")) != -1) switch (ch) { case 'J': if (flag_backward) @@ -253,6 +252,9 @@ main(int argc, char *argv[]) case 'j': flag_julian_day = 1; break; + case 'm': + flag_month = optarg; + break; case 'o': if (flag_backward) usage(); @@ -293,25 +295,11 @@ 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) usage(); - m = parsemonth(*argv++); - if (m < 1 || m > 12) - errx(EX_USAGE, - "%s is neither a month number (1..12) nor a name", - argv[-1]); + flag_month = *argv++; /* FALLTHROUGH */ case 1: y = atoi(*argv++); @@ -319,11 +307,28 @@ main(int argc, char *argv[]) errx(EX_USAGE, "year %d not in range 1..9999", y); break; case 0: + { + time_t t; + struct tm *tm; + + t = time(NULL); + tm = localtime(&t); + y = tm->tm_year + 1900; + m = tm->tm_mon + 1; + } break; default: usage(); } + if (flag_month != NULL) { + m = parsemonth(flag_month); + if (m < 1 || m > 12) + errx(EX_USAGE, + "%s is neither a month number (1..12) nor a name", + flag_month); + } + if (flag_easter) printeaster(y, flag_julian_cal, flag_orthodox); else if (argc == 1 || flag_hole_year) @@ -344,10 +349,11 @@ static void usage(void) { - fprintf(stderr, "%s\n%s\n%s\n", - "usage: cal [-jy] [[month] year]", - " ncal [-Jjpwy] [-s country_code] [[month] year]", - " ncal [-Jeo] [year]"); + fputs( + "usage: cal [-jy] [[month] year]\n" + " cal [-j] [-m month] [year]\n" + " ncal [-Jjpwy] [-s country_code] [[month] year]\n" + " ncal [-Jeo] [year]\n", stderr); exit(EX_USAGE); }