summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorzachir <zachir@librem.one>2024-03-11 02:00:31 -0500
committerzachir <zachir@librem.one>2024-03-11 02:00:31 -0500
commit3d95ee49b4e45c11d0a9d6368ea3ec6808006a35 (patch)
tree43dea24fe43c9b8affadf1e3493501bfe39c130d
parent45933ed41766da5e08b59f970ff58df63eb18c8b (diff)
Add error handling for non-numbers passedHEADmaster
Non-numbers that are passed will return -1, and print an error message. This includes decimal places, as only integers can be even or odd.
-rw-r--r--src/main.c8
1 files changed, 6 insertions, 2 deletions
diff --git a/src/main.c b/src/main.c
index e415020..ac45fd5 100644
--- a/src/main.c
+++ b/src/main.c
@@ -35,7 +35,7 @@ int main(int argc, char **argv) {
quiet = false;
value = false;
result = 0;
- char *output;
+ char *end = NULL;
if (argc > 1) {
for (int i = 1; i < argc; i++) {
@@ -55,7 +55,11 @@ int main(int argc, char **argv) {
break;
}
}
- result = strtol(argv[i], &output, 10);
+ result = strtol(argv[i], &end, 10);
+ if ((end == argv[i]) || (end[0] != '\0')) {
+ fprintf(stderr, "Please provide an integer, not %s!\n", argv[i]);
+ return ERR;
+ }
value = true;
}
} else {