diff --git a/README.md b/README.md index f11ecde..c5dd2dc 100644 --- a/README.md +++ b/README.md @@ -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? ? 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") diff --git a/lc/lc b/lc/lc new file mode 100755 index 0000000..02d05d2 Binary files /dev/null and b/lc/lc differ diff --git a/lc/lc.c b/lc/lc.c new file mode 100644 index 0000000..01c7043 --- /dev/null +++ b/lc/lc.c @@ -0,0 +1,35 @@ +#include +#include + +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; +} diff --git a/makefile b/lc/makefile similarity index 88% rename from makefile rename to lc/makefile index 5ea6258..b23e67b 100644 --- a/makefile +++ b/lc/makefile @@ -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 diff --git a/wc/makefile b/wc/makefile new file mode 100644 index 0000000..fbe9998 --- /dev/null +++ b/wc/makefile @@ -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 diff --git a/ww b/wc/wc similarity index 98% rename from ww rename to wc/wc index 58c8de8..fd53c56 100755 Binary files a/ww and b/wc/wc differ diff --git a/ww.c b/wc/wc.c similarity index 83% rename from ww.c rename to wc/wc.c index b966769..0cdc4ef 100644 --- a/ww.c +++ b/wc/wc.c @@ -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; }