add lc version, various improvements.

This commit is contained in:
NunoSempere 2023-09-10 15:05:28 +02:00
parent 496cd811a1
commit 16168840c3
7 changed files with 91 additions and 7 deletions

View File

@ -30,9 +30,18 @@ The plan9port version of wc ([github](https://github.com/9fans/plan9port/blob/ma
- ~~Maybe make some pull requests, if I'm doing something better? => doesn't seem like it~~
- [ ] Install to ww, but check that ww is empty (installing to wc2 or smth would mean that you don't save that many keypresses vs wc -w)
- ~~[ ] Could use zig? => Not for now~~
- [ ] Look specifically at how other versions
- [ ] Look specifically at how other versions do stuff.
- [ ] Distinguish between reading from stdin and reading from a file
- If it doesn't have arguments, read from stdin.
- [ ] Open files, read characters.
- [ ] Write version that counts lines
- [ ] Document reading from user-inputed stdin (end with Ctrl+D)
- [ ] Write man files?
- [ ] Write a version for other coreutils? <https://git.busybox.net/busybox/tree/coreutils/>? Would be a really nice project.
- Simple utils.
- zig?
- https://github.com/leecannon/zig-coreutils
- https://github.com/keiranrowan/tiny-core/tree/master
- [x] Add lc
- Take into account what happens if file doesn't end in newline.
- [ ] add chc (cc is "c compiler")

BIN
lc/lc Executable file

Binary file not shown.

35
lc/lc.c Normal file
View File

@ -0,0 +1,35 @@
#include <stdio.h>
#include <unistd.h>
int lc(FILE* fp)
{
char c[1];
int num_lines = 0;
int fn = fileno(fp);
while (read(fn, c, sizeof(c)) > 0) {
if (*c == '\n' ) {
num_lines ++;
}
}
printf("%i\n", num_lines);
return 0;
}
int main(int argc, char** argv)
{
if (argc == 1) {
return lc(stdin);
} else if (argc > 1) {
FILE* fp = fopen(argv[1], "r");
if (!fp) {
perror("Could not open file");
return 1;
}
return lc(fp) && fclose(fp);
} else {
printf("Usage: lc file.txt\n");
printf(" or: cat file.txt | lc\n");
printf(" or: lc # read from user-inputted stdin\n");
}
return 0;
}

View File

@ -8,8 +8,8 @@ CC=gcc
# CC=tcc # <= faster compilation
# Main file
SRC=ww.c
OUT=ww
SRC=lc.c
OUT=lc
## Flags
DEBUG= #'-g'
@ -34,7 +34,7 @@ install:
test: $(OUT)
/bin/echo -e "123\n45 67" | ./$(OUT)
/bin/echo -n "" | ./ww
/bin/echo -n "" | ./lc
/bin/echo " xx x" | ./$(OUT) -w
./$(OUT) $(SRC)
./$(OUT) nonexistent_file || true

40
wc/makefile Normal file
View File

@ -0,0 +1,40 @@
# Interface:
# make
# make build
# make format
# Compiler
CC=gcc
# CC=tcc # <= faster compilation
# Main file
SRC=wc.c
OUT=wc
## Flags
DEBUG= #'-g'
STANDARD=-std=c99
WARNINGS=-Wall
OPTIMIZED=-O3
# OPTIMIZED=-O3 #-Ofast
## Formatter
STYLE_BLUEPRINT=webkit
FORMATTER=clang-format -i -style=$(STYLE_BLUEPRINT)
## make build
build: $(SRC)
$(CC) $(OPTIMIZED) $(DEBUG) $(SRC) -o $(OUT)
format: $(SRC)
$(FORMATTER) $(SRC)
install:
cp -n $(OUT) /bin/ww ## don't overwrite old standard wc.
test: $(OUT)
/bin/echo -e "123\n45 67" | ./$(OUT)
/bin/echo -n "" | ./wc
/bin/echo " xx x" | ./$(OUT) -w
./$(OUT) $(SRC)
./$(OUT) nonexistent_file || true

Binary file not shown.

View File

@ -33,9 +33,9 @@ int main(int argc, char** argv)
}
return wc(fp) && fclose(fp);
} else {
printf("Usage: ww file.txt\n");
printf(" or: cat file.txt | ww\n");
printf(" or: ww # read from user-inputted stdin\n");
printf("Usage: wc file.txt\n");
printf(" or: cat file.txt | wc\n");
printf(" or: wc # read from user-inputted stdin\n");
}
return 0;
}