summaryrefslogtreecommitdiff
path: root/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'main.c')
-rw-r--r--main.c72
1 files changed, 72 insertions, 0 deletions
diff --git a/main.c b/main.c
new file mode 100644
index 0000000..5d4716d
--- /dev/null
+++ b/main.c
@@ -0,0 +1,72 @@
+#include "./main.h"
+
+void printhelp() {
+ fprintf(stderr, "even: a binary by ZachIR to check if a number is even or ");
+ fprintf(stderr, "odd.\n");
+ fprintf(stderr, "\t-h: prints this help message\n");
+ fprintf(stderr, "\t-v: prints the version\n");
+ fprintf(stderr, "\t-c: prints the copyright information\n");
+ return;
+}
+
+void printcopyright() {
+ fprintf(stderr, "Copyright (c) 2024 Zachary Smith\n");
+ fprintf(stderr, "even is Free software under the MIT license, and is ");
+ fprintf(stderr, "provided without warranty of any kind.\n");
+ return;
+}
+
+void printversion() {
+ fprintf(stderr, "even version %s, 2024\n", VERSION);
+ return;
+}
+
+int main(int argc, char **argv) {
+ bool quiet;
+ size_t string_length;
+ uint8_t digit;
+
+ digit = 0;
+ quiet = false;
+ string_length = 0;
+
+ if (argc > 1) {
+ for (size_t i = 1; i < argc; i++) {
+ if (argv[i][0] == '-') {
+ switch (argv[i][1]) {
+ case 'h':
+ printhelp();
+ return SUCCESS;
+ case 'c':
+ printcopyright();
+ return SUCCESS;
+ case 'v':
+ printversion();
+ return SUCCESS;
+ case 'q':
+ quiet = true;
+ break;
+ default:
+ printhelp();
+ return -1;
+ }
+ }
+ }
+ 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) {
+ fprintf(stdout, "even\n");
+ } else {
+ fprintf(stdout, "odd\n");
+ }
+ return SUCCESS;
+ }
+ }
+ return ERR;
+}