summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorzachir <zachir@librem.one>2024-03-06 23:31:51 -0600
committerzachir <zachir@librem.one>2024-03-06 23:31:51 -0600
commita94711362f844d5de6b498d05f12611312e22e0d (patch)
treefbde8aea275820d82b486aa25079dbf87d37edc4
parentea86302d44a0370264959970eb624035aeb4cef4 (diff)
Use strtol instead of checking each digit
This makes it both faster and smaller.
-rw-r--r--main.c34
-rw-r--r--main.h1
2 files changed, 17 insertions, 18 deletions
diff --git a/main.c b/main.c
index c1a8b23..d986924 100644
--- a/main.c
+++ b/main.c
@@ -29,12 +29,13 @@ void printversion() {
int main(int argc, char **argv) {
bool quiet;
- size_t string_length;
- uint8_t digit;
+ bool value;
+ long result;
- digit = 0;
quiet = false;
- string_length = 0;
+ value = false;
+ result = 0;
+ char *output;
if (argc > 1) {
for (size_t i = 1; i < argc; i++) {
@@ -52,26 +53,23 @@ int main(int argc, char **argv) {
case 'q':
quiet = true;
break;
- default:
- printhelp();
- return -1;
}
}
+ result = strtol(argv[i], &output, 10);
+ value = true;
}
- assert(strlen(argv[argc - 1]) < SIZE_MAX);
- string_length = strlen(argv[argc - 1]);
- digit = argv[argc - 1][string_length - 1];
- digit -= '0';
- if (quiet) {
- if (digit <= 10)
- return ((digit & 1) == 0) ? SUCCESS : ERR;
- } else {
- if ((digit & 1) == 0) {
+ }
+ if (value) {
+ if (result % 2 == 0) {
+ if (quiet) {
fprintf(stdout, "even\n");
- } else {
+ }
+ return 0;
+ } else {
+ if (quiet) {
fprintf(stdout, "odd\n");
}
- return SUCCESS;
+ return 1;
}
}
return ERR;
diff --git a/main.h b/main.h
index 61f7b1c..d6d758e 100644
--- a/main.h
+++ b/main.h
@@ -6,6 +6,7 @@
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
+#include <stdlib.h>
#include <assert.h>
#include <string.h>