commit 16168840c3172c0f3297452652c5002f995aca3b
parent 496cd811a1fb586b2834b79b629383fdb6a89974
Author: NunoSempere <nuno.sempere@protonmail.com>
Date: Sun, 10 Sep 2023 15:05:28 +0200
add lc version, various improvements.
Diffstat:
| M | README.md | | | 11 | ++++++++++- |
| A | lc/lc | | | 0 | |
| A | lc/lc.c | | | 35 | +++++++++++++++++++++++++++++++++++ |
| A | lc/makefile | | | 40 | ++++++++++++++++++++++++++++++++++++++++ |
| D | makefile | | | 40 | ---------------------------------------- |
| A | wc/makefile | | | 40 | ++++++++++++++++++++++++++++++++++++++++ |
| A | wc/wc | | | 0 | |
| A | wc/wc.c | | | 41 | +++++++++++++++++++++++++++++++++++++++++ |
| D | ww | | | 0 | |
| D | ww.c | | | 41 | ----------------------------------------- |
10 files changed, 166 insertions(+), 82 deletions(-)
diff --git 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? <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")
diff --git a/lc/lc b/lc/lc
Binary files differ.
diff --git a/lc/lc.c b/lc/lc.c
@@ -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;
+}
diff --git a/lc/makefile b/lc/makefile
@@ -0,0 +1,40 @@
+# Interface:
+# make
+# make build
+# make format
+
+# Compiler
+CC=gcc
+# CC=tcc # <= faster compilation
+
+# Main file
+SRC=lc.c
+OUT=lc
+
+## 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/$(OUT)
+
+test: $(OUT)
+ /bin/echo -e "123\n45 67" | ./$(OUT)
+ /bin/echo -n "" | ./lc
+ /bin/echo " xx x" | ./$(OUT) -w
+ ./$(OUT) $(SRC)
+ ./$(OUT) nonexistent_file || true
diff --git a/makefile b/makefile
@@ -1,40 +0,0 @@
-# Interface:
-# make
-# make build
-# make format
-
-# Compiler
-CC=gcc
-# CC=tcc # <= faster compilation
-
-# Main file
-SRC=ww.c
-OUT=ww
-
-## 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/$(OUT)
-
-test: $(OUT)
- /bin/echo -e "123\n45 67" | ./$(OUT)
- /bin/echo -n "" | ./ww
- /bin/echo " xx x" | ./$(OUT) -w
- ./$(OUT) $(SRC)
- ./$(OUT) nonexistent_file || true
diff --git a/wc/makefile 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/wc/wc b/wc/wc
Binary files differ.
diff --git a/wc/wc.c b/wc/wc.c
@@ -0,0 +1,41 @@
+#include <stdio.h>
+#include <unistd.h>
+
+int wc(FILE* fp)
+{
+ char c[1];
+ int word = 0, num_words = 0;
+ int fn = fileno(fp);
+ while (read(fn, c, sizeof(c)) > 0) {
+ if (*c != ' ' && *c != '\n' && *c != '\t') {
+ word = 1;
+ } else {
+ if (word) {
+ num_words++;
+ word = 0;
+ }
+ }
+ }
+ num_words+=word;
+ printf("%i\n", num_words);
+ return 0;
+}
+
+int main(int argc, char** argv)
+{
+ if (argc == 1) {
+ return wc(stdin);
+ } else if (argc > 1) {
+ FILE* fp = fopen(argv[1], "r");
+ if (!fp) {
+ perror("Could not open file");
+ return 1;
+ }
+ return wc(fp) && fclose(fp);
+ } else {
+ printf("Usage: wc file.txt\n");
+ printf(" or: cat file.txt | wc\n");
+ printf(" or: wc # read from user-inputted stdin\n");
+ }
+ return 0;
+}
diff --git a/ww b/ww
Binary files differ.
diff --git a/ww.c b/ww.c
@@ -1,41 +0,0 @@
-#include <stdio.h>
-#include <unistd.h>
-
-int wc(FILE* fp)
-{
- char c[1];
- int word = 0, num_words = 0;
- int fn = fileno(fp);
- while (read(fn, c, sizeof(c)) > 0) {
- if (*c != ' ' && *c != '\n' && *c != '\t') {
- word = 1;
- } else {
- if (word) {
- num_words++;
- word = 0;
- }
- }
- }
- num_words+=word;
- printf("%i\n", num_words);
- return 0;
-}
-
-int main(int argc, char** argv)
-{
- if (argc == 1) {
- return wc(stdin);
- } else if (argc > 1) {
- FILE* fp = fopen(argv[1], "r");
- if (!fp) {
- perror("Could not open file");
- return 1;
- }
- 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");
- }
- return 0;
-}