From e9490940d01e7ab2b9479a38325a60be096787af Mon Sep 17 00:00:00 2001 From: Bryson Steck Date: Tue, 22 Mar 2022 14:22:32 -0600 Subject: fixed segfault with new function --- .gitignore | 10 +++++++ Makefile | 7 ++++- uirc.c | 90 +++++++++++++++++++++++++++++++++++++++++++------------------- 3 files changed, 79 insertions(+), 28 deletions(-) diff --git a/.gitignore b/.gitignore index 2b07042..07cee1c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,15 @@ +# ide directories .vscode/ +# binaries uirc +d_uirc +# gcc output *.o + +# common image types +*.png +*.jpg +*.jpeg +*.bmp diff --git a/Makefile b/Makefile index 236977f..7458674 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,12 @@ include config.mk SRC = uirc.c uirc: ${SRC} - ${CC} -DLICENSE_DIR='"./"' -o uirc -O ${SRC} + ${CC} -o uirc -O ${SRC} -lm + +debug: uirc + ${CC} -g -o d_uirc -O ${SRC} -lm + gdb d_uirc + rm d_uirc install: uirc mkdir -p ${PREFIX} diff --git a/uirc.c b/uirc.c index 1d62fe9..4399d3f 100644 --- a/uirc.c +++ b/uirc.c @@ -32,11 +32,15 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#define STB_IMAGE_IMPLEMENTATION + #include #include #include #include #include +#include +#include const char *VERSION = "0.1.0"; @@ -67,6 +71,8 @@ int handleArg(arg) char *arg; { firstTwo[2] = '\0'; size = sizeof arg; + bool rFlag; + // determine if any arguments are flags if (strcmp(longFlag, firstTwo) == 0) { printf("ok"); @@ -76,49 +82,79 @@ int handleArg(arg) char *arg; { switch (arg[i]) { case 'h': printf("an unneccessary image ratio calculator (uirc) v%s\n\n", VERSION); - printf("Copyright 2022 Bryson Steck\nLicensed under the BSD 2-Clause. You can read the license by running 'uirc -l'\n\n"); + printf("Copyright 2022 Bryson Steck\n\n\n"); printf("%s\n", help); - break; + exit(1); case 'l': - readLicense(); + //readLicense(); + exit(1); + case 'r': + rFlag = true; break; case '\0': + break; } } - return 0; } // if no more flags, run ratio calculations - - return 0; + return readFile(arg, rFlag); } -int readFile() { - +int readFile(file, showRes) char *file; bool showRes;{ + int width, height, channels, biggestFactor; + unsigned char *img = stbi_load(file, &width, &height, &channels, 0); + if (img == NULL) { + printf("uirc: could not open file %s\n", file); + exit(3); + } + printf("Opened file \n"); + + biggestFactor = getBiggestFactor(width, height); + stbi_image_free(img); + + printf("Ratio of %s > %d:%d\n", file, width / biggestFactor, height / biggestFactor); + return 0; + } -void readLicense() { - FILE *license; - char ch; - - license = fopen(LICENSE_DIR "LICENSE", "r"); - if (license == NULL) { - printf("uirc: cannot find LICENSE in %s\n", LICENSE_DIR); - printf("uirc: if you changed the location of the file, recompile uirc with the correct location or move the file back\n"); - exit(2); - } else { - printf("uirc is Free and Open Source Software under the BSD 2-Clause License:\n\n"); - do { - ch = fgetc(license); - printf("%c", ch); - } while (ch != EOF); +int getBiggestFactor(int width, int height) { + int *widthFactors, *heightFactors; + int bcf; + for (int i = 1; i <= width; i++) { + for (int j = 1; j <= height; j++) { + if (width % i == 0) { + if (height % j == 0 && i == j) { + bcf = j; + } + } + } } - fclose(license); - return; + return bcf; } -int main(argc, argv) int argc; char *argv[]; { +//void readLicense() { +// FILE *license; +// char ch; +// +// license = fopen(LICENSE_DIR "LICENSE", "r"); +// if (license == NULL) { +// printf("uirc: cannot find LICENSE in %s\n", LICENSE_DIR); +// printf("uirc: if you changed the location of the file, recompile uirc with the correct location or move the file back\n"); +// exit(2); +// } else { +// printf("uirc is Free and Open Source Software under the BSD 2-Clause License. You can view it by visiting the GitHub repository for uirc:\nhttps://github.com/brysonsteck/uirc/blob/master/LICENSE\n"); +// do { +// ch = fgetc(license); +// printf("%c", ch); +// } while (ch != EOF); +// } +// fclose(license); +// return; +//} + +int main(int argc, char *argv[]) { //int i; char *i; @@ -128,7 +164,7 @@ int main(argc, argv) int argc; char *argv[]; { } for (int i = 1; i < argc; i++) { - char *a = argv[1]; + char *a = argv[i]; int returned = handleArg(a); if (returned != 0) return returned; -- cgit v1.2.3