diff options
author | zachir <zachir@librem.one> | 2024-02-14 23:54:52 -0600 |
---|---|---|
committer | zachir <zachir@librem.one> | 2024-02-14 23:54:52 -0600 |
commit | 0689d6bbbb0e6ecc3934d8e4a2e9b0fc66a0b29f (patch) | |
tree | 8736d669c2f2a3f1d2f31eb56730ce3803091e47 |
even: Add bin to check if number is even
even is a basic binary to check if the provided number is even.
-rw-r--r-- | .gitignore | 5 | ||||
-rw-r--r-- | LICENSE | 19 | ||||
-rw-r--r-- | Makefile | 16 | ||||
-rw-r--r-- | main.c | 72 | ||||
-rw-r--r-- | main.h | 25 |
5 files changed, 137 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4c6f5e9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +# Ignore object files +*.o + +#Ignore the compiled binary +even @@ -0,0 +1,19 @@ +Copyright (c) 2024 Zachary Smith + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..1d7d879 --- /dev/null +++ b/Makefile @@ -0,0 +1,16 @@ +C_SRC := main.c +C_OBJ := main.o +B_NAM := even +CC := gcc +C_FLG := -O3 -Wall -Wpedantic -Werror +O_FLG := ${C_FLG} + +${B_NAM}: ${C_OBJ} + ${CC} ${O_FLG} -o ${B_NAM} ${C_OBJ} + +${C_OBJ}: ${C_SRC} + ${CC} ${C_FLG} -c ${C_SRC} + +clean: + rm -f ${B_NAM} ${C_OBJ} + @@ -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; +} @@ -0,0 +1,25 @@ +// Copyright (c) 2024 Zachary Smith +#ifndef MAIN_H_ +#define MAIN_H_ + +#include <stdbool.h> +#include <stddef.h> +#include <stdint.h> +#include <stdio.h> + +#include <assert.h> +#include <string.h> + +#define VERSION "1.0.0" + +enum return_t { + SUCCESS = 0, + ERR +}; + +void printhelp(void); +void printcopyright(void); +void printversion(void); +int main(int, char**); + +#endif // MAIN_H_ |