diff options
| Commit | 6f75a91408459b3093eedb23f4db130595da565f (patch) | |
|---|---|---|
| Author | 2026-11-08 19:12:34 +0200 | |
| Committer | 2026-11-08 19:12:34 +0200 | |
| Files | b827313461d68805292396d696a9e9d41b3c351a | |
| cli-6f75a91408459b3093eedb23f4db130595da565f.tar cli-6f75a91408459b3093eedb23f4db130595da565f.tar.gz cli-6f75a91408459b3093eedb23f4db130595da565f.zip | ||
| Parent | e5823c8bc7cadeb97f55327c38b0ee91098e0ca2 (diff) | |
fix(chwrite): Correct argument parsing
Make it conform strictly to the help message
1 files changed, 21 insertions, 3 deletions
| -rw-r--r-- | chwrite/chwrite.c | 24 |
diff --git a/chwrite/chwrite.c b/chwrite/chwrite.c index 6be8ec8..395cf91 100644 --- a/chwrite/chwrite.c +++ b/chwrite/chwrite.c @@ -4,6 +4,7 @@ #include <fcntl.h> #include <errno.h> #include <stdio.h> +#include <string.h> static void help_message(void) { @@ -25,14 +26,31 @@ main(int argc, char *argv[]) { help_message(); return 0; } + for (i = 1; i < argc; ++i) { + if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) { + help_message(); + return 0; + } + } - operation = argv[1][0]; - - if (operation != '+' && operation != '-') { + if (!strcmp(argv[1], "+") || !strcmp(argv[1], "+w")) { + operation = '+'; + } + else if (!strcmp(argv[1], "-") || !strcmp(argv[1], "-w")) { + operation = '-'; + } + else { + printf("Invalid argument '%s'!\n", argv[1]); help_message(); return 1; } + if (argc == 2) { + printf("Not enough arguments!\n"); + help_message(); + return 2; + } + for (i = 2; i < argc; ++i) { f = open(argv[i], 0); if (f == -1) |
