From f8c5020a877b663dfc4267c4a94df906bb181e78 Mon Sep 17 00:00:00 2001 From: Marc Date: Tue, 4 Oct 2022 16:25:44 +0200 Subject: [PATCH] I discovered the static keyword --- CMakeLists.txt | 10 +++++++--- src/file.c | 14 +++++++------- src/fomod.c | 20 ++++++++++---------- src/fomod/group.c | 10 +++++----- src/fomod/parser.c | 12 ++++++------ src/install.c | 13 ++++++------- src/main.c | 32 ++++++++++++++++---------------- src/order.c | 2 +- src/steam.c | 6 +++--- 9 files changed, 61 insertions(+), 58 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index bdcb15c..21c78dd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,12 @@ cmake_minimum_required(VERSION 3.10) +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") # generate the compile_commands for vscode / clang set(CMAKE_EXPORT_COMPILE_COMMANDS ON CACHE INTERNAL "") @@ -24,8 +31,6 @@ set(SOURCES # add the executable add_executable(ModManager src/main.c ${SOURCES}) -add_compile_options(-Wall -Wextra -pedantic -Werror) - find_package(PkgConfig REQUIRED) pkg_search_module(GLIB REQUIRED glib-2.0) pkg_search_module(AUDIT REQUIRED audit) @@ -33,7 +38,6 @@ pkg_search_module(LIBXML REQUIRED libxml-2.0) target_include_directories(ModManager PRIVATE ${GLIB_INCLUDE_DIRS} ${LIBXML_INCLUDE_DIRS} ${AUDIT_INCLUDE_DIRS}) -add_compile_options(-fsanitize=address) add_link_options(-fsanitize=address) target_link_libraries(ModManager ${GLIB_LDFLAGS}) diff --git a/src/file.c b/src/file.c index b4439a7..b7f5348 100644 --- a/src/file.c +++ b/src/file.c @@ -8,13 +8,13 @@ #include #include "file.h" -u_int32_t countSetBits(u_int32_t n) { - // base case - if (n == 0) - return 0; - else - // if last bit set add 1 else add 0 - return (n & 1) + countSetBits(n >> 1); +static u_int32_t countSetBits(u_int32_t n) { + // base case + if (n == 0) + return 0; + else + // if last bit set add 1 else add 0 + return (n & 1) + countSetBits(n >> 1); } //TODO: add interruption support diff --git a/src/fomod.c b/src/fomod.c index 3d5c2e8..b784fe0 100644 --- a/src/fomod.c +++ b/src/fomod.c @@ -14,7 +14,7 @@ #include "file.h" #include "libxml/globals.h" -int getInputCount(const char * input) { +static int getInputCount(const char * input) { char buff[2]; buff[1] = '\0'; @@ -40,21 +40,21 @@ int getInputCount(const char * input) { return elementCount; } -gint priorityCmp(gconstpointer a, gconstpointer b) { +static gint priorityCmp(gconstpointer a, gconstpointer b) { const FOModFile_t * fileA = (const FOModFile_t *)a; const FOModFile_t * fileB = (const FOModFile_t *)b; return fileA->priority - fileB->priority; } -void printfOptionsInOrder(FOModGroup_t group) { +static void printfOptionsInOrder(FOModGroup_t group) { for(int i = 0; i < group.pluginCount; i++) { printf("%d, %s\n", i, group.plugins[i].name); printf("%s\n", group.plugins[i].description); } } -gint flagEqual(const FOModFlag_t * a, const FOModFlag_t * b) { +static gint flagEqual(const FOModFlag_t * a, const FOModFlag_t * b) { int nameCmp = strcmp(a->name, b->name); if(nameCmp == 0) { if(strcmp(a->value, b->value) == 0) @@ -66,20 +66,20 @@ gint flagEqual(const FOModFlag_t * a, const FOModFlag_t * b) { return nameCmp; } -int stepCmpAsc(const void * stepA, const void * stepB) { +static int stepCmpAsc(const void * stepA, const void * stepB) { const FOModStep_t * step1 = (const FOModStep_t *)stepA; const FOModStep_t * step2 = (const FOModStep_t *)stepB; return strcmp(step1->name, step2->name); } -int stepCmpDesc(const void * stepA, const void * stepB) { +static int stepCmpDesc(const void * stepA, const void * stepB) { const FOModStep_t * step1 = (const FOModStep_t *)stepA; const FOModStep_t * step2 = (const FOModStep_t *)stepB; return 1 - strcmp(step1->name, step2->name); } -void sortSteps(FOMod_t * fomod) { +static void sortSteps(FOMod_t * fomod) { switch(fomod->stepOrder) { case ASC: qsort(fomod->steps, fomod->stepCount, sizeof(*fomod->steps), stepCmpAsc); @@ -93,19 +93,19 @@ void sortSteps(FOMod_t * fomod) { } } -int groupCmpAsc(const void * stepA, const void * stepB) { +static int groupCmpAsc(const void * stepA, const void * stepB) { const FOModGroup_t * step1 = (const FOModGroup_t *)stepA; const FOModGroup_t * step2 = (const FOModGroup_t *)stepB; return strcmp(step1->name, step2->name); } -int groupCmpDesc(const void * stepA, const void * stepB) { +static int groupCmpDesc(const void * stepA, const void * stepB) { const FOModGroup_t * step1 = (const FOModGroup_t *)stepA; const FOModGroup_t * step2 = (const FOModGroup_t *)stepB; return 1 - strcmp(step1->name, step2->name); } -void sortGroup(FOModGroup_t * group) { +static void sortGroup(FOModGroup_t * group) { switch(group->order) { case ASC: qsort(group->plugins, group->pluginCount, sizeof(*group->plugins), groupCmpAsc); diff --git a/src/fomod/group.c b/src/fomod/group.c index 470153a..87cc98a 100644 --- a/src/fomod/group.c +++ b/src/fomod/group.c @@ -3,7 +3,7 @@ #include "string.h" #include -GroupType_t getGroupType(const char * type) { +static GroupType_t getGroupType(const char * type) { if(strcmp(type, "SelectAtLeastOne") == 0) { return AT_LEAST_ONE; } else if(strcmp(type, "SelectAtMostOne") == 0) { @@ -19,7 +19,7 @@ GroupType_t getGroupType(const char * type) { } } -TypeDescriptor_t getDescriptor(const char * descriptor) { +static TypeDescriptor_t getDescriptor(const char * descriptor) { if(strcmp(descriptor, "Optional") == 0) { return OPTIONAL; } else if(strcmp(descriptor, "Required") == 0) { @@ -64,7 +64,7 @@ void freeGroup(FOModGroup_t * group) { group->pluginCount = 0; } -int parseConditionFlags(FOModPlugin_t * plugin, xmlNodePtr nodeElement) { +static int parseConditionFlags(FOModPlugin_t * plugin, xmlNodePtr nodeElement) { xmlNodePtr flagNode = nodeElement->children; while(flagNode != NULL) { if(!validateNode(&flagNode, true, "flag", NULL)) { @@ -89,7 +89,7 @@ int parseConditionFlags(FOModPlugin_t * plugin, xmlNodePtr nodeElement) { return EXIT_SUCCESS; } -int parseGroupFiles(FOModPlugin_t * plugin, xmlNodePtr nodeElement) { +static int parseGroupFiles(FOModPlugin_t * plugin, xmlNodePtr nodeElement) { FOModFile_t * files = NULL; xmlNodePtr fileNode = nodeElement->children; while(fileNode != NULL) { @@ -123,7 +123,7 @@ int parseGroupFiles(FOModPlugin_t * plugin, xmlNodePtr nodeElement) { return EXIT_SUCCESS; } -int parseNodeElement(FOModPlugin_t * plugin, xmlNodePtr nodeElement) { +static int parseNodeElement(FOModPlugin_t * plugin, xmlNodePtr nodeElement) { if(xmlStrcmp(nodeElement->name, (const xmlChar *) "description") == 0) { plugin->description = freeAndDup(xmlNodeGetContent(nodeElement)); } else if(xmlStrcmp(nodeElement->name, (const xmlChar *) "image") == 0) { diff --git a/src/fomod/parser.c b/src/fomod/parser.c index fe8c437..78dd2a8 100644 --- a/src/fomod/parser.c +++ b/src/fomod/parser.c @@ -51,7 +51,7 @@ void freeFOMod(FOMod_t * fomod) { memset(fomod, 0, sizeof(FOMod_t)); } -int parseVisibleNode(xmlNodePtr node, FOModStep_t * step) { +static int parseVisibleNode(xmlNodePtr node, FOModStep_t * step) { xmlNodePtr requiredFlagsNode = node->children; while (requiredFlagsNode != NULL) { @@ -76,7 +76,7 @@ int parseVisibleNode(xmlNodePtr node, FOModStep_t * step) { return EXIT_SUCCESS; } -int parseOptionalFileGroup(xmlNodePtr node, FOModStep_t * step) { +static int parseOptionalFileGroup(xmlNodePtr node, FOModStep_t * step) { xmlChar * optionOrder = xmlGetProp(node, (const xmlChar *)"order"); step->optionOrder = getFOModOrder((char *)optionOrder); xmlFree(optionOrder); @@ -105,7 +105,7 @@ int parseOptionalFileGroup(xmlNodePtr node, FOModStep_t * step) { return EXIT_SUCCESS; } -FOModStep_t * parseInstallSteps(xmlNodePtr installStepsNode, int * stepCount) { +static FOModStep_t * parseInstallSteps(xmlNodePtr installStepsNode, int * stepCount) { FOModStep_t * steps = NULL; *stepCount = 0; @@ -146,7 +146,7 @@ FOModStep_t * parseInstallSteps(xmlNodePtr installStepsNode, int * stepCount) { return steps; } -int parseDependencies(xmlNodePtr node, FOModCondFile_t * condFile) { +static int parseDependencies(xmlNodePtr node, FOModCondFile_t * condFile) { xmlNodePtr flagNode = node->children; if(!validateNode(&flagNode, true, "flagDependency", NULL)) { @@ -167,7 +167,7 @@ int parseDependencies(xmlNodePtr node, FOModCondFile_t * condFile) { return EXIT_SUCCESS; } -int parseFiles(xmlNodePtr node, FOModCondFile_t * condFile) { +static int parseFiles(xmlNodePtr node, FOModCondFile_t * condFile) { xmlNodePtr filesNode = node->children; @@ -192,7 +192,7 @@ int parseFiles(xmlNodePtr node, FOModCondFile_t * condFile) { return EXIT_SUCCESS; } -int parseConditionalInstalls(xmlNodePtr node, FOMod_t * fomod) { +static int parseConditionalInstalls(xmlNodePtr node, FOMod_t * fomod) { xmlNodePtr patterns = node->children; if(patterns != NULL) { if(!validateNode(&patterns, true, "patterns", NULL)) { diff --git a/src/install.c b/src/install.c index f212610..7578c83 100644 --- a/src/install.c +++ b/src/install.c @@ -9,7 +9,7 @@ #include "main.h" #include "file.h" -int unzip(char * path, char * outdir) { +static int unzip(char * path, char * outdir) { char * const args[] = { "unzip", "-LL", // to lowercase @@ -37,7 +37,7 @@ int unzip(char * path, char * outdir) { } } -int unrar(char * path, char * outdir) { +static int unrar(char * path, char * outdir) { char * const args[] = { "unrar", "x", @@ -66,7 +66,7 @@ int unrar(char * path, char * outdir) { } -int un7z(char * path, const char * outdir) { +static int un7z(char * path, const char * outdir) { gchar * outParameter = g_strjoin("", "-o", outdir, NULL); char * const args[] = { @@ -98,7 +98,7 @@ int un7z(char * path, const char * outdir) { } } -const char * extractLastPart(const char * filePath, const char delimeter) { +static const char * extractLastPart(const char * filePath, const char delimeter) { const unsigned long length = strlen(filePath); long index = -1; for(long i= length - 1; i >= 0; i--) { @@ -112,15 +112,14 @@ const char * extractLastPart(const char * filePath, const char delimeter) { return &filePath[index]; } -const char * extractExtension(const char * filePath) { +static const char * extractExtension(const char * filePath) { return extractLastPart(filePath, '.'); } -const char * extractFileName(const char * filePath) { +static const char * extractFileName(const char * filePath) { return extractLastPart(filePath, '/'); } - int addMod(char * filePath, int appId) { int returnValue = EXIT_SUCCESS; diff --git a/src/main.c b/src/main.c index e77a4bc..12122a4 100644 --- a/src/main.c +++ b/src/main.c @@ -20,11 +20,11 @@ GHashTable * gamePaths; -bool isRoot() { +static bool isRoot() { return getuid() == 0; } -GList * listFilesInFolder(const char * path) { +static GList * listFilesInFolder(const char * path) { GList * list = NULL; DIR *d; struct dirent *dir; @@ -42,17 +42,17 @@ GList * listFilesInFolder(const char * path) { return list; } -int noRoot() { +static int noRoot() { printf("Don't run this argument as root\n"); return EXIT_FAILURE; } -int needRoot() { +static int needRoot() { printf("Root is needed to bind with the game files\n"); return EXIT_FAILURE; } -int usage() { +static int usage() { printf("Use --list-games or -l to list compatible games\n"); printf("Use --add or -a to add a mod to a game\n"); printf("Use --list-mods or -m to list all mods for a game\n"); @@ -66,7 +66,7 @@ int usage() { return EXIT_FAILURE; } -int validateAppId(const char * appIdStr) { +static int validateAppId(const char * appIdStr) { char * strtoulSentinel; //strtoul set EINVAL(after C99) if the string is invalid unsigned long appid = strtoul(appIdStr, &strtoulSentinel, 10); @@ -90,7 +90,7 @@ int validateAppId(const char * appIdStr) { return (int)appid; } -int listGames(int argc, char **) { +static int listGames(int argc, char **) { if(argc != 2) return usage(); GList * gamesIds = g_hash_table_get_keys(gamePaths); GList * gamesIdsFirstPointer = gamesIds; @@ -107,7 +107,7 @@ int listGames(int argc, char **) { return EXIT_SUCCESS; } -int add(int argc, char ** argv) { +static int add(int argc, char ** argv) { if(argc != 4) return usage(); const char * appIdStr = argv[2]; @@ -120,7 +120,7 @@ int add(int argc, char ** argv) { return EXIT_SUCCESS; } -int listAllMods(int argc, char ** argv) { +static int listAllMods(int argc, char ** argv) { if(argc != 3) return usage(); char * appIdStr = argv[2]; int appid = validateAppId(appIdStr); @@ -163,7 +163,7 @@ int listAllMods(int argc, char ** argv) { * this will just flag the mod as needed to be deployed * it's the deploying process that handle the rest */ -int installAndUninstallMod(int argc, char ** argv, bool install) { +static int installAndUninstallMod(int argc, char ** argv, bool install) { if(argc != 4) return usage(); char * appIdStr = argv[2]; int appid = validateAppId(appIdStr); @@ -238,7 +238,7 @@ exit: return returnValue; } -int deploy(int argc, char ** argv) { +static int deploy(int argc, char ** argv) { if(argc != 3) return usage(); char * appIdStr = argv[2]; @@ -331,7 +331,7 @@ int deploy(int argc, char ** argv) { return EXIT_SUCCESS; } -int setup(int argc, char ** argv) { +static int setup(int argc, char ** argv) { if(argc != 3 ) return usage(); const char * appIdStr = argv[2]; int appid = validateAppId(appIdStr); @@ -381,7 +381,7 @@ int setup(int argc, char ** argv) { } -int unbind(int argc, char ** argv) { +static int unbind(int argc, char ** argv) { if(argc != 3 ) return usage(); const char * appIdStr = argv[2]; int appid = validateAppId(appIdStr); @@ -398,7 +398,7 @@ int unbind(int argc, char ** argv) { return EXIT_SUCCESS; } -int removeMod(int argc, char ** argv) { +static int removeMod(int argc, char ** argv) { if(argc != 4) return usage(); const char * appIdStr = argv[2]; int appid = validateAppId(appIdStr); @@ -440,7 +440,7 @@ int removeMod(int argc, char ** argv) { return EXIT_SUCCESS; } -int fomod(int argc, char ** argv) { +static int fomod(int argc, char ** argv) { if(argc != 4) return usage(); char * appIdStr = argv[2]; int appid = validateAppId(appIdStr); @@ -491,7 +491,7 @@ int fomod(int argc, char ** argv) { return returnValue; } -int swapMod(int argc, char ** argv) { +static int swapMod(int argc, char ** argv) { if(argc != 5) return usage(); char * appIdStr = argv[2]; int appid = validateAppId(appIdStr); diff --git a/src/order.c b/src/order.c index 35bd256..7a048a2 100644 --- a/src/order.c +++ b/src/order.c @@ -12,7 +12,7 @@ typedef struct Mod { char * name; } Mod_t; -gint compareOrder(const void * a, const void * b) { +static gint compareOrder(const void * a, const void * b) { const Mod_t * ModA = a; const Mod_t * ModB = b; diff --git a/src/steam.c b/src/steam.c index ee77166..2d3803e 100644 --- a/src/steam.c +++ b/src/steam.c @@ -12,7 +12,7 @@ enum FieldIds { FIELD_PATH, FIELD_LABEL, FIELD_CONTENT_ID, FIELD_TOTAL_SIZE, FIELD_CLEAN_BYTES, FIELD_CORRUPTION, FIELD_APPS }; -int getFiledId(const char * field) { +static int getFiledId(const char * field) { //replace with a hash + switch if(strcmp(field, "path") == 0) { return FIELD_PATH; @@ -34,7 +34,7 @@ int getFiledId(const char * field) { } } -ValveLibraries_t * parseVDF(const char * path, size_t * size, int * status) { +static ValveLibraries_t * parseVDF(const char * path, size_t * size, int * status) { FILE * fd = fopen(path, "r"); char * line = NULL; size_t len = 0; @@ -166,7 +166,7 @@ exit: return libraries; } -void freeLibraries(ValveLibraries_t * libraries, int size) { +static void freeLibraries(ValveLibraries_t * libraries, int size) { for(int i = 0; i < size; i++) { free(libraries[i].path); free(libraries[i].label);