I discovered the static keyword

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