commit 13eb71f4ee2d4231b67d8c4a2532fe63654fea13
parent b9025a01db609ca07fdb0a0b8b10c6404f2d20ff
Author: NunoSempere <nuno.sempere@protonmail.com>
Date: Sat, 9 Sep 2023 00:05:34 +0200
rework usage a bit.
Diffstat:
| M | makefile | | | 6 | +++--- |
| D | wc | | | 0 | |
| D | wc.c | | | 50 | -------------------------------------------------- |
| A | ww | | | 0 | |
| A | ww.c | | | 57 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
5 files changed, 60 insertions(+), 53 deletions(-)
diff --git a/makefile b/makefile
@@ -8,8 +8,8 @@ CC=gcc
# CC=tcc # <= faster compilation
# Main file
-SRC=wc.c
-OUT=wc
+SRC=ww.c
+OUT=ww
## Flags
DEBUG= #'-g'
@@ -31,7 +31,7 @@ format: $(SRC)
test: $(OUT)
/bin/echo -e "123\n45 67" | ./$(OUT)
- /bin/echo -n "" | ./wc
+ /bin/echo -n "" | ./ww
/bin/echo " xx x" | ./$(OUT) -w
./$(OUT) $(SRC)
./$(OUT) nonexistent_file || true
diff --git a/wc b/wc
Binary files differ.
diff --git a/wc.c b/wc.c
@@ -1,50 +0,0 @@
-#include <stdio.h>
-#include <unistd.h> // read, isatty
-
-// STDIN_FILENO
-int process_fn(int fn)
-{
- char c[1];
- int seen_word=0;
- int seen_sep_after_word=0;
- int num_words = 0;
- while (read(fn, c, sizeof(c)) > 0) {
- if(*c != '\n' && *c != ' ' && *c != '\t'){
- seen_word = 1;
- } else if(seen_word) {
- seen_sep_after_word = 1;
- } else {
- // see a separator, but haven't seen a word: do nothing
- // exercise: what happens if you only track seen_sep,
- // instead of seen_sep_after_word?
- // test with: $ echo " x x" | ./wc
- }
- if(seen_word && seen_sep_after_word){
- num_words++;
- seen_sep_after_word = seen_word = 0;
- }
- }
- if(seen_word){
- num_words++;
- }
- printf("%i\n",num_words);
- return 0;
-}
-
-int main(int argc, char** argv)
-{
- if (!isatty(STDIN_FILENO)) {
- return process_fn(STDIN_FILENO);
- } else if (argc > 1) {
- FILE* fp = fopen(argv[1], "r");
- if (!fp) {
- perror("Could not open file");
- return 1;
- }
- return process_fn(fileno(fp));
- } else {
- printf("Usage: ww file.txt\n");
- printf(" or: cat file.txt | ww\n");
- }
- return 0;
-}
diff --git a/ww b/ww
Binary files differ.
diff --git a/ww.c b/ww.c
@@ -0,0 +1,57 @@
+#include <stdio.h>
+#include <unistd.h> // read, isatty
+
+// STDIN_FILENO
+int process_fn(int fn)
+{
+ char c[1];
+ int seen_word=0;
+ int seen_sep_after_word=0;
+ int num_words = 0;
+ while (read(fn, c, sizeof(c)) > 0) {
+ if(*c != '\n' && *c != ' ' && *c != '\t'){
+ seen_word = 1;
+ } else if(seen_word) {
+ seen_sep_after_word = 1;
+ } else {
+ // see a separator, but haven't seen a word: do nothing
+ // exercise: what happens if you only track seen_sep,
+ // instead of seen_sep_after_word?
+ // test with: $ echo " x x" | ./wc
+ }
+ if(seen_word && seen_sep_after_word){
+ num_words++;
+ seen_sep_after_word = seen_word = 0;
+ }
+ }
+ if(seen_word){
+ num_words++;
+ }
+ printf("%i\n",num_words);
+ return 0;
+}
+
+void usage(){
+ printf("Usage: ww file.txt\n");
+ printf(" or: cat file.txt | ww\n");
+}
+
+int main(int argc, char** argv)
+{
+ if(argc > 1 && argv[1][0] == '-' && argv[1][1] == 'h'){
+ usage();
+ return 0;
+ } else if (!isatty(STDIN_FILENO)) {
+ return process_fn(STDIN_FILENO);
+ } else if (argc > 1) {
+ FILE* fp = fopen(argv[1], "r");
+ if (!fp) {
+ perror("Could not open file");
+ return 1;
+ }
+ return process_fn(fileno(fp));
+ } else {
+ usage();
+ }
+ return 0;
+}