Skip to content

Commit dd4e53a

Browse files
Added wc command, accept multiple files or wildcards on grep, improved option parsing
1 parent fbf8a23 commit dd4e53a

File tree

7 files changed

+113
-44
lines changed

7 files changed

+113
-44
lines changed

Makefile

+5-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 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 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
@@ -40,6 +40,10 @@ mos/grep.bin: grep/src/*.[ch]
4040
mkdir -p bin
4141
cd grep;make;mv bin/grep.bin ../mos
4242

43+
mos/wc.bin: wc/src/*.[ch]
44+
mkdir -p bin
45+
cd wc;make;mv bin/wc.bin ../mos
46+
4347
bin/sort.bin: sort/src/*.[ch]
4448
mkdir -p bin
4549
cd sort;make;mv bin/sort.bin ../bin

README.md

+26-2
Original file line numberDiff line numberDiff line change
@@ -161,19 +161,22 @@ This program finds files (in the entire file system) whose name matches a
161161
specific pattern.
162162

163163
Example:
164+
164165
`find *.c`
165166

166167
This will find all files whose name matches the pattern `*.c` in the whole
167168
file system.
168169

169170
You can use the '>' character to redirect the output to a file. Example:
171+
170172
`find *.asm >all_asmfiles.txt`
171173

172174
### grep
173175

174176
This program searches test files for specific strings.
175177

176178
Example:
179+
177180
`grep dirlist *.c`
178181

179182
This will find all lines containing "dirlist" in all C source files.
@@ -183,10 +186,13 @@ ASCII letters. You can also enclose the search string in double quotes to
183186
search for a string containint spaces"
184187

185188
Example:
189+
186190
`grep -i "these few words" *.txt`
187191

188-
You can use the '>' character to redirect the output to a file. Example:
189-
`grep dirlist *.c >results.txt`
192+
You can use the '>' character to redirect the output to a file and you
193+
can have multiple file names or wildcards. Example:
194+
195+
`grep dirlist *.c *.h >results.txt`
190196

191197
### sort
192198

@@ -201,6 +207,24 @@ Example:
201207

202208
`sort myfile.txt >sorted.txt`
203209

210+
THe proghram has the follwoignoptions: -f to sort case-insensitive and -r
211+
to sort in reverse order.
212+
213+
### wc
214+
215+
This program counts lines, words and characters in one or more files.
216+
It uses the options -l, -w and -c to specify that lines words and characters
217+
must be counted. If none are specified, all are counted.
218+
219+
Example:
220+
221+
`wc -w *.c`
222+
223+
Count only words in all C source files
224+
225+
`wc readme.txt info.txt`
226+
227+
Count lines, words and characters in readme.txt and info.txt
204228

205229
### nano
206230

bin/sort.bin

61 Bytes
Binary file not shown.

grep/src/main.c

+46-20
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@
1010
#include <stdbool.h>
1111
#include <mos_api.h>
1212

13+
static char *my_index(char *s, char c)
14+
{
15+
while (*s) {
16+
if (*s == c)
17+
return s;
18+
s++;
19+
}
20+
return NULL;
21+
}
1322

1423
static bool glob_is_match(char *name, char *pat)
1524
{
@@ -34,6 +43,7 @@ static bool glob_is_match(char *name, char *pat)
3443
}
3544

3645
bool nocase = false;
46+
bool singlefile = false;
3747

3848
static char buf[1024];
3949
static char linebuf[256];
@@ -107,8 +117,11 @@ static void search_file(char *fname, char *string_pat)
107117
file_idx = 0;
108118
buf_filled = 0;
109119
while (nextline()) {
110-
if (str_match(linebuf,string_pat))
111-
printf("%s: %s\n",fname,linebuf);
120+
if (str_match(linebuf,string_pat)) {
121+
if (!singlefile)
122+
printf("%s:",fname);
123+
printf("%s\n",linebuf);
124+
}
112125
}
113126
fclose(f);
114127
}
@@ -117,31 +130,44 @@ static void search_file(char *fname, char *string_pat)
117130
int
118131
main(int argc, char *argv[])
119132
{
120-
unsigned int nopts = 0;
133+
int nopts = 0;
121134
DIR dir_struct;
122135
FILINFO file_struct;
123136
int res;
124-
125-
if (argc < 3) {
126-
fprintf(stderr,"Usage: grep [-i] <string> <files>\n");
127-
return 19;
137+
char *pat;
138+
139+
for (;;) {
140+
if (argc < nopts+3) {
141+
fprintf(stderr,"Usage: grep [-i] <string> <files>\n");
142+
return 19;
143+
}
144+
if (strcmp(argv[nopts+1],"-i")==0) {
145+
nocase = true;
146+
nopts += 1;
147+
} else {
148+
break;
149+
}
128150
}
129-
if (strcmp(argv[1],"-i")==0) {
130-
nocase = true;
131-
nopts += 1;
151+
pat = argv[1+nopts];
152+
if (nopts+3 == argc &&
153+
my_index(argv[nopts+2],'*')==0 &&
154+
my_index(argv[nopts+2],'?')==0) { /* single file no wildcard */
155+
singlefile = true;
132156
}
133-
res = ffs_dopen(&dir_struct,".");
134-
if (res == 0) {
135-
for (;;) {
136-
res = ffs_dread(&dir_struct,&file_struct);
137-
if (res != 0 || file_struct.fname[0]==0)
138-
break;
139-
if ((file_struct.fattrib & 0x10) == 0 &&
140-
glob_is_match(file_struct.fname,argv[2+nopts])) {
141-
search_file(file_struct.fname,argv[1+nopts]);
157+
for (; nopts<argc-2; nopts++) {
158+
res = ffs_dopen(&dir_struct,".");
159+
if (res == 0) {
160+
for (;;) {
161+
res = ffs_dread(&dir_struct,&file_struct);
162+
if (res != 0 || file_struct.fname[0]==0)
163+
break;
164+
if ((file_struct.fattrib & 0x10) == 0 &&
165+
glob_is_match(file_struct.fname,argv[2+nopts])) {
166+
search_file(file_struct.fname,pat);
167+
}
142168
}
169+
ffs_dclose(&dir_struct);
143170
}
144-
ffs_dclose(&dir_struct);
145171
}
146172
return 0;
147173
}

mos/grep.bin

423 Bytes
Binary file not shown.

sort/src/main.c

+23-10
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,20 @@ int my_strcasecmp(const char *p,const char *q)
2525
}
2626

2727
bool nocase;
28+
bool reverse;
2829

2930
static int line_compare(const void *a, const void *b)
3031
{
3132
const char * const *l1 = a;
3233
const char * const *l2 = b;
34+
int v;
3335
if (nocase)
34-
return my_strcasecmp(*l1,*l2);
36+
v=my_strcasecmp(*l1,*l2);
3537
else
36-
return strcmp(*l1,*l2);
38+
v=strcmp(*l1,*l2);
39+
if (reverse)
40+
v=-v;
41+
return v;
3742
}
3843

3944

@@ -83,15 +88,23 @@ unsigned int nlines=0;
8388
int
8489
main(int argc, char *argv[])
8590
{
86-
unsigned int nopts = 0;
91+
int nopts = 0;
8792
unsigned int i;
88-
if (argc < 2) {
89-
fprintf(stderr,"Usage: sort [-f] <file>\n");
90-
return 19;
91-
}
92-
if (strcmp(argv[1],"-f")==0) {
93-
nocase = true;
94-
nopts += 1;
93+
94+
for (;;) {
95+
if (argc < nopts+2) {
96+
fprintf(stderr,"Usage: sort [-f][-r] <file>\n");
97+
return 19;
98+
}
99+
if (strcmp(argv[nopts+1],"-f")==0) {
100+
nocase = true;
101+
nopts += 1;
102+
} else if (strcmp(argv[nopts+1],"-r")==0) {
103+
reverse = true;
104+
nopts += 1;
105+
} else {
106+
break;
107+
}
95108
}
96109
f = fopen(argv[1+nopts],"r");
97110
while (nlines<MAX_LINES && nextline()) {

wc/src/main.c

+13-11
Original file line numberDiff line numberDiff line change
@@ -124,19 +124,21 @@ main(int argc, char *argv[])
124124
count_words = true;
125125
count_chars = true;
126126
}
127-
res = ffs_dopen(&dir_struct,".");
128-
if (res == 0) {
129-
for (;;) {
130-
res = ffs_dread(&dir_struct,&file_struct);
131-
if (res != 0 || file_struct.fname[0]==0)
132-
break;
133-
if ((file_struct.fattrib & 0x10) == 0 &&
134-
glob_is_match(file_struct.fname,argv[1+nopts])) {
135-
nfiles++;
136-
count_file(file_struct.fname);
127+
for (; nopts<argc-1; nopts++) {
128+
res = ffs_dopen(&dir_struct,".");
129+
if (res == 0) {
130+
for (;;) {
131+
res = ffs_dread(&dir_struct,&file_struct);
132+
if (res != 0 || file_struct.fname[0]==0)
133+
break;
134+
if ((file_struct.fattrib & 0x10) == 0 &&
135+
glob_is_match(file_struct.fname,argv[1+nopts])) {
136+
nfiles++;
137+
count_file(file_struct.fname);
138+
}
137139
}
140+
ffs_dclose(&dir_struct);
138141
}
139-
ffs_dclose(&dir_struct);
140142
}
141143
if (nfiles>1) show_line(total_lines, total_words, total_chars, "total");
142144
return 0;

0 commit comments

Comments
 (0)