summaryrefslogtreecommitdiff
path: root/src/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.c')
-rw-r--r--src/main.c76
1 files changed, 76 insertions, 0 deletions
diff --git a/src/main.c b/src/main.c
new file mode 100644
index 0000000..0e06431
--- /dev/null
+++ b/src/main.c
@@ -0,0 +1,76 @@
+#include "./main.h"
+
+void printhelp(void) {
+ fprintf(stderr, "even: a binary by ZachIR to check if a number is even or ");
+ fprintf(stderr, "odd.\n");
+ fprintf(stderr, "even [-hcv]\n");
+ fprintf(stderr, "even [--help]\n");
+ fprintf(stderr, "even [--copyright]\n");
+ fprintf(stderr, "even [--version]\n");
+ fprintf(stderr, "even [-q] [--quiet] NUMBER\n");
+ fprintf(stderr, "\t-h: prints this help message\n");
+ fprintf(stderr, "\t-c: prints the copyright information\n");
+ fprintf(stderr, "\t-v: prints the version\n");
+ fprintf(stderr, "\t-q: just uses error codes, no text\n");
+ return;
+}
+
+void printcopyright(void) {
+ 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(void) {
+ fprintf(stderr, "even version %s, 2024\n", VERSION);
+ return;
+}
+
+int main(int argc, char **argv) {
+ bool quiet;
+ bool value;
+ long result;
+
+ quiet = false;
+ value = false;
+ result = 0;
+ char *output;
+
+ if (argc > 1) {
+ for (int 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;
+ }
+ }
+ result = strtol(argv[i], &output, 10);
+ value = true;
+ }
+ }
+ if (value) {
+ if (result % 2 == 0) {
+ if (quiet) {
+ fprintf(stdout, "even\n");
+ }
+ return 0;
+ } else {
+ if (quiet) {
+ fprintf(stdout, "odd\n");
+ }
+ return 1;
+ }
+ }
+ return ERR;
+}