From 635b1adcaf10044517ddc14a0c5ea3d2a299ce2e Mon Sep 17 00:00:00 2001 From: Marc Date: Wed, 5 Oct 2022 13:55:04 +0200 Subject: [PATCH] [Untested] fixed the compiler/linter options and fixed the corresponding code. the code fixes have not been tested. some are trivial orthers might be a bit worrying. --- CMakeLists.txt | 6 +++--- src/file.c | 10 ++++++---- src/fomod.c | 6 +++--- src/fomod/group.c | 1 - src/fomod/parser.c | 4 ++-- src/getHome.c | 3 ++- src/getHome.h | 2 +- src/install.c | 2 +- src/main.c | 6 +++--- src/steam.c | 18 +++++++++++++----- src/steam.h | 17 ++++++----------- 11 files changed, 40 insertions(+), 35 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 21c78dd..8deda90 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,9 +4,9 @@ if(NOT CMAKE_BUILD_TYPE) set(CMAKE_BUILD_TYPE Release) endif() -set(CMAKE_CXX_FLAGS "-Wall -Wextra -pedantic -Werror -fsanitize=address") -set(CMAKE_CXX_FLAGS_DEBUG "-g") -set(CMAKE_CXX_FLAGS_RELEASE "-O2") +set(CMAKE_C_FLAGS "-Wall -Wextra -pedantic -fsanitize=address -Wcast-qual -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -Wno-pointer-arith") +set(CMAKE_C_FLAGS_DEBUG "-g -Werror") +set(CMAKE_C_FLAGS_RELEASE "-O2") # generate the compile_commands for vscode / clang set(CMAKE_EXPORT_COMPILE_COMMANDS ON CACHE INTERNAL "") diff --git a/src/file.c b/src/file.c index 01cf769..2f0177d 100644 --- a/src/file.c +++ b/src/file.c @@ -26,10 +26,12 @@ int copy(const char * path, const char * dest, u_int32_t flags) { return -1; } //flags + cp + path + dest - const char * args[flagCount + 4]; + char * args[flagCount + 4]; args[0] = "/bin/cp"; - args[1] = path; - args[2] = dest; + args[1] = alloca((strlen(path) + 1) * sizeof(char)); + strcpy(args[1], path); + args[2] = alloca((strlen(dest) + 1) * sizeof(char)); + strcpy(args[2], dest); int argIndex = 3; if(flags & CP_LINK) { @@ -50,7 +52,7 @@ int copy(const char * path, const char * dest, u_int32_t flags) { int pid = fork(); if(pid == 0) { //discard the const. since we are in a fork we don't care. - execv("/bin/cp", (char **)args); + execv("/bin/cp", args); return 0; } else { int returnValue; diff --git a/src/fomod.c b/src/fomod.c index 9744847..51c4bab 100644 --- a/src/fomod.c +++ b/src/fomod.c @@ -21,7 +21,7 @@ static int getInputCount(const char * input) { int elementCount = 0; bool prevWasValue = false; - for(int i = 0; input + i != NULL && input[i] != '\0' && input[i] != '\n'; i++) { + for(int i = 0; input[i] != '\0' && input[i] != '\n'; i++) { buff[0] = input[i]; //if the character is a valid character @@ -156,7 +156,7 @@ GList * processCondFiles(const FOMod_t * fomod, GList * flagList, GList * pendin bool areAllFlagsValid = true; //checking if all flags are valid - for(int flagId = 0; flagId < condFile->flagCount; flagId++) { + for(long flagId = 0; flagId < condFile->flagCount; flagId++) { const GList * link = g_list_find_custom(flagList, &(condFile->requiredFlags[flagId]), (GCompareFunc)flagEqual); if(link == NULL) { areAllFlagsValid = false; @@ -165,7 +165,7 @@ GList * processCondFiles(const FOMod_t * fomod, GList * flagList, GList * pendin } if(areAllFlagsValid) { - for(int fileId = 0; fileId < condFile->flagCount; fileId++) { + for(long fileId = 0; fileId < condFile->flagCount; fileId++) { const FOModFile_t * file = &(condFile->files[fileId]); FOModFile_t * fileCopy = malloc(sizeof(*file)); diff --git a/src/fomod/group.c b/src/fomod/group.c index 87cc98a..60d434d 100644 --- a/src/fomod/group.c +++ b/src/fomod/group.c @@ -90,7 +90,6 @@ static int parseConditionFlags(FOModPlugin_t * plugin, xmlNodePtr nodeElement) { } static int parseGroupFiles(FOModPlugin_t * plugin, xmlNodePtr nodeElement) { - FOModFile_t * files = NULL; xmlNodePtr fileNode = nodeElement->children; while(fileNode != NULL) { if(!validateNode(&fileNode, true, "folder", "file", NULL)) { diff --git a/src/fomod/parser.c b/src/fomod/parser.c index 78dd2a8..3703cc8 100644 --- a/src/fomod/parser.c +++ b/src/fomod/parser.c @@ -8,12 +8,12 @@ void freeFOMod(FOMod_t * fomod) { for(int i = 0; i < fomod->condFilesCount; i++) { FOModCondFile_t * condFile = &(fomod->condFiles[i]); - for(int fileId = 0; fileId < condFile->fileCount; fileId++) { + for(long fileId = 0; fileId < condFile->fileCount; fileId++) { free(condFile->files[fileId].destination); free(condFile->files[fileId].source); } - for(int flagId = 0; flagId < condFile->flagCount; flagId++) { + for(long flagId = 0; flagId < condFile->flagCount; flagId++) { FOModFlag_t * flag = &(condFile->requiredFlags[flagId]); free(flag->name); free(flag->value); diff --git a/src/getHome.c b/src/getHome.c index e294d51..3025efe 100644 --- a/src/getHome.c +++ b/src/getHome.c @@ -1,8 +1,9 @@ #include #include #include +#include "getHome.h" -char * getHome() { +char * getHome(void) { //not getting home from the env enable us to use sudo struct passwd *pw = getpwuid(audit_getloginuid()); return strdup(pw->pw_dir); diff --git a/src/getHome.h b/src/getHome.h index 141b34c..9ce8b04 100644 --- a/src/getHome.h +++ b/src/getHome.h @@ -5,4 +5,4 @@ * * @return char* path to the home dir */ -char * getHome(); +char * getHome(void); diff --git a/src/install.c b/src/install.c index f7d1a7b..a3d9c3f 100644 --- a/src/install.c +++ b/src/install.c @@ -97,7 +97,7 @@ static int un7z(char * path, const char * outdir) { } static const char * extractLastPart(const char * filePath, const char delimeter) { - const unsigned long length = strlen(filePath); + const int length = strlen(filePath); long index = -1; for(long i= length - 1; i >= 0; i--) { if(filePath[i] == delimeter) { diff --git a/src/main.c b/src/main.c index c1e432b..59b014e 100644 --- a/src/main.c +++ b/src/main.c @@ -190,7 +190,7 @@ static int installAndUninstallMod(int argc, char ** argv, bool install) { GList * mods = listFilesInFolder(modFolder); GList * modsFirstPointer = mods; - for(int i = 0; i < modId; i++) { + for(unsigned long i = 0; i < modId; i++) { mods = g_list_next(mods); } @@ -427,7 +427,7 @@ static int removeMod(int argc, char ** argv) { GList * mods = listFilesInFolder(modFolder); GList * modsFirstPointer = mods; - for(int i = 0; i < modId; i++) { + for(unsigned long i = 0; i < modId; i++) { mods = g_list_next(mods); } @@ -469,7 +469,7 @@ static int fomod(int argc, char ** argv) { GList * mods = listFilesInFolder(modFolder); GList * modsFirstPointer = mods; - for(int i = 0; i < modId; i++) { + for(unsigned long i = 0; i < modId; i++) { mods = g_list_next(mods); } diff --git a/src/steam.c b/src/steam.c index 92ebf92..4703c29 100644 --- a/src/steam.c +++ b/src/steam.c @@ -12,6 +12,15 @@ enum FieldIds { FIELD_PATH, FIELD_LABEL, FIELD_CONTENT_ID, FIELD_TOTAL_SIZE, FIELD_CLEAN_BYTES, FIELD_CORRUPTION, FIELD_APPS }; +// relative to the home directory +static const char * steamLibraries[] = { + "/.steam/root/", + "/.steam/steam/", + "/.local/share/steam", + //flatpack steam. + "/.var/app/com.valvesoftware.Steam/.local/share/Steam" +}; + static int getFiledId(const char * field) { //replace with a hash + switch if(strcmp(field, "path") == 0) { @@ -38,7 +47,6 @@ static ValveLibraries_t * parseVDF(const char * path, size_t * size, int * statu FILE * fd = fopen(path, "r"); char * line = NULL; size_t len = 0; - ssize_t read; *status = EXIT_SUCCESS; @@ -185,7 +193,7 @@ GHashTable* search_games(int * status) { char * home = getHome(); *status = EXIT_SUCCESS; - for(int i = 0; i < LEN(steamLibraries); i++) { + for(unsigned long i = 0; i < LEN(steamLibraries); i++) { char * path = g_build_filename(home, steamLibraries[i], "steamapps/libraryfolders.vdf", NULL); if (access(path, F_OK) == 0) { int parserStatus; @@ -208,8 +216,8 @@ GHashTable* search_games(int * status) { GHashTable* table = g_hash_table_new_full(g_int_hash, g_int_equal, free, free); //fill the table - for(int i = 0; i < size; i++) { - for(int j = 0; j < libraries[i].appsCount; j++) { + for(unsigned long i = 0; i < size; i++) { + for(unsigned long j = 0; j < libraries[i].appsCount; j++) { int gameId = getGameIdFromAppId(libraries[i].apps[j].appid); if(gameId >= 0) { int * key = malloc(sizeof(int)); @@ -225,7 +233,7 @@ GHashTable* search_games(int * status) { int getGameIdFromAppId(u_int32_t appid) { - for(int k = 0; k < LEN(GAMES_APPIDS); k++) { + for(unsigned long k = 0; k < LEN(GAMES_APPIDS); k++) { if(appid == GAMES_APPIDS[k]) { return k; } diff --git a/src/steam.h b/src/steam.h index 983ce4a..838287e 100644 --- a/src/steam.h +++ b/src/steam.h @@ -1,6 +1,8 @@ #ifndef __STEAM_H__ #define __STEAM_H__ +#include "macro.h" + #include #include #include @@ -23,30 +25,23 @@ typedef struct ValveLibraries { size_t appsCount; } ValveLibraries_t; -// relative to the home directory -const static char * steamLibraries[] = { - "/.steam/root/", - "/.steam/steam/", - "/.local/share/steam", - //flatpack steam. - "/.var/app/com.valvesoftware.Steam/.local/share/Steam" -}; - //todo add the older games // order has to be the same as in GAMES_NAMES -const static u_int32_t GAMES_APPIDS[] = { +static const u_int32_t GAMES_APPIDS[] = { 489830, 22330, 377160 }; //the name of the game in the steamapps/common folder -const static char * GAMES_NAMES[] = { +static const char * GAMES_NAMES[] = { "Skyrim Special Edition", "Oblivion", "Fallout 4" }; +_Static_assert(LEN(GAMES_APPIDS) == LEN(GAMES_NAMES), "Game APPIDS and Game Names doesn't match"); + /** * @brief list all installed games and the paths to the game's files *