rework usage a bit.

This commit is contained in:
NunoSempere 2023-09-09 00:05:34 +02:00
parent b9025a01db
commit 13eb71f4ee
3 changed files with 13 additions and 6 deletions

View File

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

BIN
wc → ww

Binary file not shown.

View File

@ -31,9 +31,17 @@ int process_fn(int fn)
return 0; return 0;
} }
void usage(){
printf("Usage: ww file.txt\n");
printf(" or: cat file.txt | ww\n");
}
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
if (!isatty(STDIN_FILENO)) { if(argc > 1 && argv[1][0] == '-' && argv[1][1] == 'h'){
usage();
return 0;
} else if (!isatty(STDIN_FILENO)) {
return process_fn(STDIN_FILENO); return process_fn(STDIN_FILENO);
} else if (argc > 1) { } else if (argc > 1) {
FILE* fp = fopen(argv[1], "r"); FILE* fp = fopen(argv[1], "r");
@ -43,8 +51,7 @@ int main(int argc, char** argv)
} }
return process_fn(fileno(fp)); return process_fn(fileno(fp));
} else { } else {
printf("Usage: ww file.txt\n"); usage();
printf(" or: cat file.txt | ww\n");
} }
return 0; return 0;
} }