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

This commit is contained in:
Sigrid 2021-01-12 13:23:37 +01:00
parent 6103d6ed2c
commit 806f97a47b
2 changed files with 51 additions and 18 deletions

View file

@ -4,9 +4,11 @@ cal \- print calendar
.SH SYNOPSIS
.B cal
[
.B -s
.I 1..7
] [
.I month
]
[
] [
.I year
]
.SH DESCRIPTION
@ -30,6 +32,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
cal sep 1752

View file

@ -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 mon[] =
};
char string[432];
Biobuf bout;
int wstart;
void main(int argc, char *argv[]);
int number(char *str);
@ -30,21 +37,36 @@ int jan1(int yr);
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 @@ main(int argc, char *argv[])
* 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 @@ main(int argc, char *argv[])
/*
* 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 @@ xshort:
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 @@ xlong:
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 @@ xlong:
pstr(string+j, 72);
}
Bprint(&bout, "\n\n\n");
exits(0);
exits(nil);
badarg:
Bprint(&bout, "cal: bad argument\n");
@ -232,6 +253,7 @@ cal(int m, int y, char *p, int w)
}
for(i=1; i<m; i++)
d += mon[i];
d += 7 - wstart;
d %= 7;
s += 3*d;
for(i=1; i<=mon[m]; i++) {