code: plan9front

Download patch

ref: 806f97a47b461a8389d91b29f5cd5345774f84b0
parent: 6103d6ed2cff0be93d00a0a774b1c6ebb7071831
author: Sigrid <ftrvxmtrx@gmail.com>
date: Tue Jan 12 07:23:37 EST 2021

cal: add -s option to specify the starting day of the week

--- a/sys/man/1/cal
+++ b/sys/man/1/cal
@@ -4,9 +4,11 @@
 .SH SYNOPSIS
 .B cal
 [
+.B -s
+.I 1..7
+] [
 .I month
-]
-[
+] [
 .I year
 ]
 .SH DESCRIPTION
@@ -29,6 +31,15 @@
 otherwise a calendar for just one month is printed.
 The calendar
 produced is that for England and her colonies.
+.PP
+.B -s
+.I N
+makes
+.I cal
+display
+.I N,
+specified as a number between 1 to 7 (Monday to Sunday), as the
+first day of the week. The default is Sunday.
 .PP
 Try
 .EX
--- a/sys/src/cmd/cal.c
+++ b/sys/src/cmd/cal.c
@@ -2,9 +2,15 @@
 #include <libc.h>
 #include <bio.h>
 
-char	dayw[] =
+char	*dayw[] =
 {
-	" S  M Tu  W Th  F  S"
+	" S  M Tu  W Th  F  S",
+	" M Tu  W Th  F Sa Su",
+	"Tu  W Th  F Sa Su  M",
+	" W Th  F Sa Su  M Tu",
+	"Th  F Sa Su  M Tu  W",
+	" F Sa Su  M Tu  W Th",
+	"Sa Su  M Tu  W Th  F"
 };
 char	*smon[] =
 {
@@ -21,6 +27,7 @@
 };
 char	string[432];
 Biobuf	bout;
+int	wstart;
 
 void	main(int argc, char *argv[]);
 int	number(char *str);
@@ -30,21 +37,36 @@
 int	curmo(void);
 int	curyr(void);
 
+static void
+usage(void)
+{
+	fprint(2, "usage: cal [-s 1..7] [month] [year]\n");
+	exits("usage");
+}
+
 void
 main(int argc, char *argv[])
 {
 	int y, i, j, m;
 
-	if(argc > 3) {
-		fprint(2, "usage: cal [month] [year]\n");
-		exits("usage");
-	}
+	ARGBEGIN{
+	case 's':
+		wstart = atoi(EARGF(usage()));
+		if(wstart < 0 || wstart > 7)
+			usage();
+		break;
+	default:
+		usage();
+	}ARGEND
+
+	if(argc > 2)
+		usage();
 	Binit(&bout, 1, OWRITE);
 
 /*
  * no arg, print current month
  */
-	if(argc == 1) {
+	if(argc == 0) {
 		m = curmo();
 		y = curyr();
 		goto xshort;
@@ -55,8 +77,8 @@
  *	if looks like a month, print month
  *	else print year
  */
-	if(argc == 2) {
-		y = number(argv[1]);
+	if(argc == 1) {
+		y = number(argv[0]);
 		if(y < 0)
 			y = -y;
 		if(y >= 1 && y <= 12) {
@@ -70,10 +92,10 @@
 /*
  * two arg, month and year
  */
-	m = number(argv[1]);
+	m = number(argv[0]);
 	if(m < 0)
 		m = -m;
-	y = number(argv[2]);
+	y = number(argv[1]);
 	goto xshort;
 
 /*
@@ -85,17 +107,16 @@
 	if(y < 1 || y > 9999)
 		goto badarg;
 	Bprint(&bout, "   %s %ud\n", smon[m-1], y);
-	Bprint(&bout, "%s\n", dayw);
+	Bprint(&bout, "%s\n", dayw[wstart]);
 	cal(m, y, string, 24);
 	for(i=0; i<6*24; i+=24)
 		pstr(string+i, 24);
-	exits(0);
+	exits(nil);
 
 /*
  *	print out complete year
  */
 xlong:
-	y = number(argv[1]);
 	if(y<1 || y>9999)
 		goto badarg;
 	Bprint(&bout, "\n\n\n");
@@ -107,7 +128,7 @@
 		Bprint(&bout, "         %.3s", smon[i]);
 		Bprint(&bout, "                    %.3s", smon[i+1]);
 		Bprint(&bout, "                    %.3s\n", smon[i+2]);
-		Bprint(&bout, "%s   %s   %s\n", dayw, dayw, dayw);
+		Bprint(&bout, "%s   %s   %s\n", dayw[wstart], dayw[wstart], dayw[wstart]);
 		cal(i+1, y, string, 72);
 		cal(i+2, y, string+23, 72);
 		cal(i+3, y, string+46, 72);
@@ -115,7 +136,7 @@
 			pstr(string+j, 72);
 	}
 	Bprint(&bout, "\n\n\n");
-	exits(0);
+	exits(nil);
 
 badarg:
 	Bprint(&bout, "cal: bad argument\n");
@@ -232,6 +253,7 @@
 	}
 	for(i=1; i<m; i++)
 		d += mon[i];
+	d += 7 - wstart;
 	d %= 7;
 	s += 3*d;
 	for(i=1; i<=mon[m]; i++) {