Re: cal's -y flag usage

Jeremie Courreges-Anglas <[email protected]>
Newsgroups gmane.os.openbsd.tech
Message-ID <[email protected]>
On Fri, Jun 26, 2026 at 12:40:23PM +0000, Johannes Thyssen Tishman wrote:
> 2026-06-25T21:47:47+0200 Jeremie Courreges-Anglas <[email protected]>:
> > On Thu, Jun 25, 2026 at 09:13:44AM +0000, Johannes Thyssen Tishman wrote:
> > > While playing with cal I noticed that using the -y flag along with a
> > > single non-numerical argument or with two arguments would essentially
> > > cause the option to be ignored:
> > > 
> > > $ cal -y jun
> > > $ cal -y jun 2026
> > > $ cal -y 06 2026
> > > 
> > > All three commands output the calendar for June 2026 instead of the
> > > entire year. Before I try to patch this, is this the desired/intended
> > > behaviour?
> > 
> > IMO -y should be ignored when a year is later specified on the command
> > line (current behavior), and should conflict with specifying a month.
> 
> So, like this (diff below)?
> 
> $ cal -y          # calendar for year 2026
> $ cal -y 2000     # calendar for year 2000
> $ cal -y jul      # error
> $ cal -y jul 2000 # error
> 
> > > In case a change is desired, would it make sense to split the SYNOPSIS
> > > in the man page into two lines? E.g.:
> > > 
> > > cal [-jmw] [month] [year]
> > > cal [-jmwy] [year]
> > 
> > It doesn't really make sense to specify [year] when you specify -y.
> > I'm not sure the synopsis needs to be amended.
> 
> I agree, I forgot that cal yyyy would print the year calendar already.
> 
> Index: usr.bin/cal/cal.1
> ===================================================================
> RCS file: /cvs/src/usr.bin/cal/cal.1,v
> diff -u -p -r1.33 cal.1
> --- usr.bin/cal/cal.1	31 Jul 2024 17:09:23 -0000	1.33
> +++ usr.bin/cal/cal.1	26 Jun 2026 12:35:41 -0000
> @@ -72,6 +72,10 @@ and
>  are mutually exclusive.
>  .It Fl y
>  Display a calendar for the current year.
> +This option is ignored if
> +.Ar year
> +is specified and conflicts with specifying a
> +.Ar month .
>  .El
>  .Pp
>  A single numerical parameter specifies the

ok jca@

> Index: usr.bin/cal/cal.c
> ===================================================================
> RCS file: /cvs/src/usr.bin/cal/cal.c,v
> diff -u -p -r1.33 cal.c
> --- usr.bin/cal/cal.c	26 Jun 2026 12:34:45 -0000	1.33
> +++ usr.bin/cal/cal.c	26 Jun 2026 12:35:41 -0000
> @@ -193,10 +193,14 @@ main(int argc, char *argv[])
>  	month = 0;
>  	switch(argc) {
>  	case 2:
> +		if (yflag)
> +			errx(1, "illegal year value: use 1-9999");
>  		month = parsemonth(*argv++);
>  		/* FALLTHROUGH */
>  	case 1:
>  		if (argc == 1 && !isdigit((unsigned char)*argv[0])) {
> +			if (yflag)
> +				errx(1, "illegal year value: use 1-9999");
>  			month = parsemonth(*argv);
>  			(void)time(&now);
>  			local_time = localtime(&now);

The error message isn't accurate.  I'd rather you use something like
below.  ok jca@ if you like it.


Index: cal.c
===================================================================
RCS file: /cvs/src/usr.bin/cal/cal.c,v
diff -u -p -r1.33 cal.c
--- cal.c	26 Jun 2026 12:34:45 -0000	1.33
+++ cal.c	26 Jun 2026 14:32:12 -0000
@@ -193,10 +193,14 @@ main(int argc, char *argv[])
 	month = 0;
 	switch(argc) {
 	case 2:
+		if (yflag)
+			errx(1, "specifying a month conflicts with -y");
 		month = parsemonth(*argv++);
 		/* FALLTHROUGH */
 	case 1:
 		if (argc == 1 && !isdigit((unsigned char)*argv[0])) {
+			if (yflag)
+				errx(1, "specifying a month conflicts with -y");
 			month = parsemonth(*argv);
 			(void)time(&now);
 			local_time = localtime(&now);


-- 
jca
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.