From 0689d6bbbb0e6ecc3934d8e4a2e9b0fc66a0b29f Mon Sep 17 00:00:00 2001 From: zachir Date: Wed, 14 Feb 2024 23:54:52 -0600 Subject: even: Add bin to check if number is even even is a basic binary to check if the provided number is even. --- .gitignore | 5 +++++ LICENSE | 19 +++++++++++++++++ Makefile | 16 ++++++++++++++ main.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ main.h | 25 ++++++++++++++++++++++ 5 files changed, 137 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 Makefile create mode 100644 main.c create mode 100644 main.h 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 diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..e38857d --- /dev/null +++ b/LICENSE @@ -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} + 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; +} diff --git a/main.h b/main.h new file mode 100644 index 0000000..61f7b1c --- /dev/null +++ b/main.h @@ -0,0 +1,25 @@ +// Copyright (c) 2024 Zachary Smith +#ifndef MAIN_H_ +#define MAIN_H_ + +#include +#include +#include +#include + +#include +#include + +#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_ -- cgit v1.2.3