Skip to content

Commit a38e451

Browse files
Add calendar program
1 parent ebed5b5 commit a38e451

File tree

8 files changed

+244
-1
lines changed

8 files changed

+244
-1
lines changed

Makefile

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
ASM=ez80asm
22

33
.PHONY: binaries
4-
binaries: mos/memfill.bin mos/more.bin mos/font.bin mos/fontctl.bin mos/comp.bin mos/nano.bin bin/loadfont.bin bin/recode.bin mos/find.bin mos/grep.bin mos/wc.bin bin/sort.bin bin/mc.bin bin/12amc.ovl
4+
binaries: mos/memfill.bin mos/more.bin mos/font.bin mos/fontctl.bin mos/comp.bin mos/nano.bin bin/loadfont.bin bin/recode.bin mos/find.bin mos/grep.bin mos/wc.bin mos/concat.bin mos/cal.bin bin/sort.bin bin/mc.bin bin/12amc.ovl
55

66
loadfont/src/codepages.h: loadfont/src/gen_codepages.py
77
cd loadfont/src;python3 gen_codepages.py >codepages.h
@@ -44,6 +44,14 @@ mos/wc.bin: wc/src/*.[ch]
4444
mkdir -p bin
4545
cd wc;make;mv bin/wc.bin ../mos
4646

47+
mos/concat.bin: concat/src/*.[ch]
48+
mkdir -p bin
49+
cd concat;make;mv bin/concat.bin ../mos
50+
51+
mos/cal.bin: cal/src/*.[ch]
52+
mkdir -p bin
53+
cd cal;make;mv bin/cal.bin ../mos
54+
4755
bin/sort.bin: sort/src/*.[ch]
4856
mkdir -p bin
4957
cd sort;make;mv bin/sort.bin ../bin

README.md

+24
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,30 @@ characters to LF only.
154154
This converts a text file from CP1252 to Latin9 (ISO8859-15) without
155155
changing the end-of-line characters.
156156

157+
### cal
158+
159+
This is a simple Unix-style calender program. Specify month and year to
160+
show a calendar of just one month. Example:
161+
162+
`cal 5 2024`
163+
164+
shows a calendar of just May 2024. Specify just a year to show the calendar
165+
of the whole yar. Example:
166+
167+
`cal 2024`
168+
169+
Options: -s shows Sunday as the first day of the week (default is Monday).
170+
171+
### concat
172+
173+
This program concatenates one or more files to form another file:
174+
175+
Example:
176+
177+
`concat prog1.bas prog2.bas >prog.bas`
178+
179+
Concatenates prog1.bas and prog2.bas and writes the whole to
180+
prog.bas. Comparable to the Unix cat command,
157181

158182
### find
159183

cal/Makefile

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# ----------------------------
2+
# Makefile Options
3+
# ----------------------------
4+
5+
NAME = cal
6+
ICON = icon.png
7+
DESCRIPTION = "Unix-style calendar"
8+
COMPRESSED = NO
9+
ARCHIVED = NO
10+
INIT_LOC = 0b0000
11+
BSSHEAP_LOW = 0b4000
12+
BSSHEAP_HIGH = 0B7FFF
13+
STACK_HIGH = 0B7FFF
14+
15+
CFLAGS = -Werror -Wall -Wextra -Oz -DCEDEV
16+
CXXFLAGS = -Werror -Wall -Wextra -Oz - DCEDEV
17+
LDHAS_ARG_PROCESSING = 1
18+
LDHAS_EXIT_HANDLER:=0
19+
# ----------------------------
20+
21+
include $(shell cedev-config --makefile)
22+

cal/src/main.c

+131
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/* cal
2+
* Unix style calendar program.
3+
*/
4+
5+
#include <stdlib.h>
6+
#include <stdio.h>
7+
#include <string.h>
8+
#include <stdbool.h>
9+
10+
bool start_sunday;
11+
bool whole_year;
12+
13+
char *month_names[] = {
14+
"January",
15+
"February",
16+
"March",
17+
"April",
18+
"May",
19+
"June",
20+
"July",
21+
"August",
22+
"September",
23+
"October",
24+
"November",
25+
"December"
26+
};
27+
28+
char days[] = " 1 2 3 4 5 6 7 8 9"
29+
" 10 11 12 13 14 15 16 17 18 19"
30+
" 20 21 22 23 24 25 26 27 28 29"
31+
" 30 31";
32+
char daynames1[] = " Mo Tu We Th Fr Sa Su";
33+
char daynames2[] = " Su Mo Tu We Th Fr Sa";
34+
35+
unsigned int month_lengths[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
36+
37+
char month_buf1[6*22];
38+
char month_buf2[6*22];
39+
char month_buf3[6*22];
40+
41+
void fill_month(char *buf, unsigned int startday, unsigned int month_length)
42+
{
43+
unsigned int i;
44+
char *p;
45+
for (i=0; i<6; i++)
46+
memset(buf+i*22,32,21);
47+
p=buf+startday*3;
48+
for (i=0; i<month_length*3; i+=3) {
49+
memcpy(p,days+i+3,3);
50+
p+=3;
51+
if (*p==0)p++;
52+
}
53+
}
54+
55+
56+
unsigned int month_length(unsigned int m,unsigned int year)
57+
{
58+
unsigned int ml=month_lengths[m-1];
59+
if (m==2) {
60+
if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0))
61+
ml++;
62+
}
63+
return ml;
64+
}
65+
66+
int
67+
main(int argc, char *argv[])
68+
{
69+
int nopts=0;
70+
unsigned int i,j,y;
71+
unsigned int year, month;
72+
for (;;) {
73+
if (argc+nopts < 2) {
74+
fprintf(stderr,"Usage: cal [-s] [month] <year>\n");
75+
return 19;
76+
}
77+
if (strcmp(argv[1+nopts],"-s")==0) {
78+
start_sunday = true;
79+
nopts++;
80+
}
81+
else {
82+
break;
83+
}
84+
}
85+
if (argc == 2+nopts) {
86+
month = 0;
87+
year = atoi(argv[nopts+1]);
88+
}
89+
else {
90+
month = atoi(argv[nopts+1]);
91+
if (month <1 || month >12) {
92+
fprintf(stderr,"Invalid month\n");
93+
return 19;
94+
}
95+
year = atoi(argv[nopts+2]);
96+
}
97+
y=year-1;
98+
y=y+y/4-y/100+y/400+start_sunday; // First day of current year.
99+
if (month==0) {
100+
printf("%36u\n",year);
101+
for (i=0; i<12; i+=3) {
102+
unsigned int k1,k2,k3;
103+
k1=strlen(month_names[i]);
104+
k2=strlen(month_names[i+1]);
105+
k3=strlen(month_names[i+2]);
106+
printf("%*s%*s%*s\n",12+k1/2,month_names[i],
107+
22-k1/2+k2/2,month_names[i+1],
108+
22-k2/2+k3/2,month_names[i+2]);
109+
printf("%s %s %s\n",start_sunday?daynames2:daynames1,
110+
start_sunday?daynames2:daynames1,
111+
start_sunday?daynames2:daynames1);
112+
fill_month(month_buf1, y%7, month_length(i+1,year));
113+
y+=month_length(i+1,year);
114+
fill_month(month_buf2, y%7, month_length(i+2,year));
115+
y+=month_length(i+2,year);
116+
fill_month(month_buf3, y%7, month_length(i+3,year));
117+
y+=month_length(i+3,year);
118+
for (j=0; j<6; j++)
119+
printf("%s %s %s\n",month_buf1+22*j,month_buf2+22*j,month_buf3+22*j);
120+
if (i<12) printf("\n");
121+
}
122+
} else {
123+
printf("%*s %u\n",9+strlen(month_names[month-1])/2,month_names[month-1],year);
124+
printf("%s\n",start_sunday?daynames2:daynames1);
125+
for (j=1; j<month; j++) y+= month_length(j,year); // skip past preceding months.
126+
fill_month(month_buf1, y%7, month_length(month,year));
127+
for (j=0; j<6; j++)
128+
printf("%s\n",month_buf1+22*j);
129+
}
130+
return 0;
131+
}

concat/Makefile

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# ----------------------------
2+
# Makefile Options
3+
# ----------------------------
4+
5+
NAME = concat
6+
ICON = icon.png
7+
DESCRIPTION = "Utility to concatenate files"
8+
COMPRESSED = NO
9+
ARCHIVED = NO
10+
INIT_LOC = 0b0000
11+
BSSHEAP_LOW = 0b4000
12+
BSSHEAP_HIGH = 0B7FFF
13+
STACK_HIGH = 0B7FFF
14+
15+
CFLAGS = -Werror -Wall -Wextra -Oz -DCEDEV
16+
CXXFLAGS = -Werror -Wall -Wextra -Oz - DCEDEV
17+
LDHAS_ARG_PROCESSING = 1
18+
LDHAS_EXIT_HANDLER:=0
19+
# ----------------------------
20+
21+
include $(shell cedev-config --makefile)
22+

concat/src/main.c

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/* find
2+
* search the entire file system for file names conforming to a certain pattern
3+
*/
4+
5+
#include <stdio.h>
6+
7+
8+
#define BUF_SIZE 1024
9+
unsigned char buf[BUF_SIZE];
10+
11+
int
12+
main(int argc, char *argv[])
13+
{
14+
FILE *f;
15+
int i,j;
16+
int nbytes;
17+
if (argc < 2) {
18+
fprintf(stderr,"Usage: concat <pattern+>\n");
19+
return 19;
20+
}
21+
22+
for (i=1; i<argc; i++) {
23+
f=fopen(argv[i],"rb");
24+
if (f==NULL) {
25+
fprintf(stderr,"Cannot open file %s\n",argv[i]);
26+
continue;
27+
}
28+
while ((nbytes=fread(buf,1,BUF_SIZE,f)) > 0) {
29+
for (j=0; j<nbytes; j++) {
30+
if (buf[j] != '\r') fputc(buf[j],stdout);
31+
}
32+
}
33+
fclose(f);
34+
}
35+
return 0;
36+
}

mos/cal.bin

13.9 KB
Binary file not shown.

mos/concat.bin

12.1 KB
Binary file not shown.

0 commit comments

Comments
 (0)