diff --git a/src/file.c b/src/file.c index b7f5348..01cf769 100644 --- a/src/file.c +++ b/src/file.c @@ -22,7 +22,7 @@ static u_int32_t countSetBits(u_int32_t n) { int copy(const char * path, const char * dest, u_int32_t flags) { int flagCount = countSetBits(flags); if(flagCount > 3) { - printf("Invalid flags for cp command\n"); + fprintf(stderr, "Invalid flags for cp command\n"); return -1; } //flags + cp + path + dest @@ -105,7 +105,7 @@ void casefold(const char * folder) { int result = move(file, destination); if(result != EXIT_SUCCESS) { - printf("Move failed: %s => %s \n", dir->d_name, destinationName); + fprintf(stderr, "Move failed: %s => %s \n", dir->d_name, destinationName); } g_free(destinationName); g_free(destination); diff --git a/src/fomod.c b/src/fomod.c index b784fe0..9744847 100644 --- a/src/fomod.c +++ b/src/fomod.c @@ -142,7 +142,6 @@ int processFileOperations(GList * pendingFileOperations, const char * modFolder, if(copyResult != EXIT_SUCCESS) { fprintf(stderr, "Copy failed, some file might be corrupted\n"); } - printf("source: %s, destination: %s\n", source, destination); g_free(source); currentFileOperation = g_list_next(currentFileOperation); diff --git a/src/install.c b/src/install.c index 7578c83..f7d1a7b 100644 --- a/src/install.c +++ b/src/install.c @@ -30,10 +30,9 @@ static int unzip(char * path, char * outdir) { waitpid(pid, &returnValue, 0); if(returnValue != 0) { - printf("\nFailed to decompress archive\n"); - returnValue = EXIT_FAILURE; + fprintf(stderr, "\nFailed to decompress archive\n"); } - return EXIT_SUCCESS; + return returnValue; } } @@ -58,10 +57,9 @@ static int unrar(char * path, char * outdir) { waitpid(pid, &returnValue, 0); if(returnValue != 0) { - printf("\nFailed to decompress archive\n"); - returnValue = EXIT_FAILURE; + fprintf(stderr, "\nFailed to decompress archive\n"); } - return EXIT_SUCCESS; + return returnValue; } } @@ -89,12 +87,12 @@ static int un7z(char * path, const char * outdir) { waitpid(pid, &returnValue, 0); if(returnValue != 0) { - printf("\nFailed to decompress archive\n"); - returnValue = EXIT_FAILURE; + fprintf(stderr, "\nFailed to decompress archive\n"); + return returnValue; } //make everything lowercase since 7z don't have an argument for that. casefold(outdir); - return EXIT_SUCCESS; + return returnValue; } } @@ -124,14 +122,14 @@ int addMod(char * filePath, int appId) { int returnValue = EXIT_SUCCESS; if (access(filePath, F_OK) != 0) { - printf("File not found\n"); + fprintf(stderr, "File not found\n"); returnValue = EXIT_FAILURE; goto exit; } char * configFolder = g_build_filename(getenv("HOME"), MANAGER_FILES, NULL); if(g_mkdir_with_parents(configFolder, 0755) < 0) { - printf("Could not create mod folder"); + fprintf(stderr, "Could not create mod folder"); returnValue = EXIT_FAILURE; goto exit2; } @@ -153,7 +151,7 @@ int addMod(char * filePath, int appId) { } else if (strcmp(lowercaseExtension, "7z") == 0) { returnValue = un7z(filePath, outdir); } else { - printf("Unsupported format only zip/7z/rar are supported\n"); + fprintf(stderr, "Unsupported format only zip/7z/rar are supported\n"); returnValue = EXIT_FAILURE; } diff --git a/src/main.c b/src/main.c index 12122a4..c1e432b 100644 --- a/src/main.c +++ b/src/main.c @@ -18,6 +18,9 @@ #include "fomod.h" #include "order.h" +#define ENABLED_COLOR "\033[0;32m" +#define DISABLED_COLOR "\033[0;31m" + GHashTable * gamePaths; static bool isRoot() { @@ -43,12 +46,12 @@ static GList * listFilesInFolder(const char * path) { } static int noRoot() { - printf("Don't run this argument as root\n"); + fprintf(stderr, "Don't run this argument as root\n"); return EXIT_FAILURE; } static int needRoot() { - printf("Root is needed to bind with the game files\n"); + fprintf(stderr, "Root is needed to bind with the game files\n"); return EXIT_FAILURE; } @@ -71,18 +74,18 @@ static int validateAppId(const char * appIdStr) { //strtoul set EINVAL(after C99) if the string is invalid unsigned long appid = strtoul(appIdStr, &strtoulSentinel, 10); if(errno == EINVAL || strtoulSentinel == appIdStr) { - printf("Appid has to be a valid number\n"); + fprintf(stderr, "Appid has to be a valid number\n"); return -1; } int gameId = getGameIdFromAppId((int)appid); if(gameId < 0) { - printf("Game is not compatible\n"); + fprintf(stderr, "Game is not compatible\n"); return -1; } if(!g_hash_table_contains(gamePaths, &gameId)) { - printf("Game not found\n"); + fprintf(stderr, "Game not found\n"); return -1; } @@ -142,9 +145,9 @@ static int listAllMods(int argc, char ** argv) { char * modPath = g_build_filename(modFolder, modName, INSTALLED_FLAG_FILE, NULL); if(access(modPath, F_OK) == 0) { - printf("\033[0;32m %d | ✓ | %s\n", index, modName); + printf(ENABLED_COLOR " %d | ✓ | %s\n", index, modName); } else { - printf("\033[0;31m %d | ✕ | %s\n", index, modName); + printf(DISABLED_COLOR " %d | ✕ | %s\n", index, modName); } index++; @@ -176,7 +179,7 @@ static int installAndUninstallMod(int argc, char ** argv, bool install) { //strtoul set EINVAL if the string is invalid unsigned long modId = strtoul(argv[3], NULL, 10); if(errno == EINVAL) { - printf("ModId has to be a valid number\n"); + fprintf(stderr, "ModId has to be a valid number\n"); return -1; } @@ -192,7 +195,7 @@ static int installAndUninstallMod(int argc, char ** argv, bool install) { } if(mods == NULL) { - printf("Mod not found\n"); + fprintf(stderr, "Mod not found\n"); return EXIT_FAILURE; } @@ -201,7 +204,7 @@ static int installAndUninstallMod(int argc, char ** argv, bool install) { if(install) { if(access(modFlag, F_OK) == 0) { - printf("The mod is already activated \n"); + fprintf(stderr, "The mod is already activated \n"); returnValue = EXIT_SUCCESS; goto exit; } @@ -223,8 +226,8 @@ static int installAndUninstallMod(int argc, char ** argv, bool install) { } if(unlink(modFlag) != 0) { - printf("Error could not disable the mod.\n"); - printf("please remove this file '%s' as root\n", modFlag); + fprintf(stderr, "Error could not disable the mod.\n"); + fprintf(stderr, "please remove this file '%s'\n", modFlag); returnValue = EXIT_FAILURE; goto exit; } @@ -251,7 +254,10 @@ static int deploy(int argc, char ** argv) { char * gameFolder = g_build_filename(home, MANAGER_FILES, GAME_FOLDER_NAME, appIdStr, NULL); if(access(gameFolder, F_OK) != 0) { + free(home); + g_free(gameFolder); printf("Please run '%s --setup %d' first", APP_NAME, appid); + return EXIT_FAILURE; } //unmount everything beforhand @@ -314,13 +320,13 @@ static int deploy(int argc, char ** argv) { if(status == 0) { printf("Everything is ready, just launch the game\n"); } else if(status == -1) { - printf("Could not mount the mods overlay, try to install fuse-overlay\n"); + fprintf(stderr, "Could not mount the mods overlay, try to install fuse-overlay\n"); return EXIT_FAILURE; } else if(status == -2) { - printf("Could not mount the mods overlay\n"); + fprintf(stderr, "Could not mount the mods overlay\n"); return EXIT_FAILURE; } else { - printf("%d take a screenshot and go insult the dev that let this passthrough\n", __LINE__); + fprintf(stderr, "%d bug detected, please report this.\n", __LINE__); return EXIT_FAILURE; } @@ -403,14 +409,14 @@ static int removeMod(int argc, char ** argv) { const char * appIdStr = argv[2]; int appid = validateAppId(appIdStr); if(appid < 0) { - printf("Invalid appid"); + fprintf(stderr, "Invalid appid"); return EXIT_FAILURE; } //strtoul set EINVAL if the string is invalid unsigned long modId = strtoul(argv[3], NULL, 10); if(errno == EINVAL) { - printf("Modid has to be a valid number\n"); + fprintf(stderr, "Modid has to be a valid number\n"); return EXIT_FAILURE; } @@ -426,7 +432,7 @@ static int removeMod(int argc, char ** argv) { } if(mods == NULL) { - printf("Mod not found\n"); + fprintf(stderr, "Mod not found\n"); return EXIT_FAILURE; } @@ -445,14 +451,14 @@ static int fomod(int argc, char ** argv) { char * appIdStr = argv[2]; int appid = validateAppId(appIdStr); if(appid < 0) { - printf("Invalid appid"); + fprintf(stderr, "Invalid appid"); return EXIT_FAILURE; } //strtoul set EINVAL if the string is invalid unsigned long modId = strtoul(argv[3], NULL, 10); if(errno == EINVAL) { - printf("Modid has to be a valid number\n"); + fprintf(stderr, "Modid has to be a valid number\n"); return -1; } @@ -468,7 +474,7 @@ static int fomod(int argc, char ** argv) { } if(mods == NULL) { - printf("Mod not found\n"); + fprintf(stderr, "Mod not found\n"); return EXIT_FAILURE; } @@ -496,7 +502,7 @@ static int swapMod(int argc, char ** argv) { char * appIdStr = argv[2]; int appid = validateAppId(appIdStr); if(appid < 0) { - printf("Invalid appid"); + fprintf(stderr, "Invalid appid"); return EXIT_FAILURE; } @@ -505,12 +511,12 @@ static int swapMod(int argc, char ** argv) { //no mod will go over the MAX_INT value int modIdA = (int)strtoul(argv[3], &sentinel, 10); if(errno == EINVAL || sentinel == argv[3]) { - printf("Modid A has to be a valid number\n"); + fprintf(stderr, "Modid A has to be a valid number\n"); return -1; } int modIdB = (int)strtoul(argv[4], &sentinel, 10); if(errno == EINVAL || sentinel == argv[4]) { - printf("Modid B has to be a valid number\n"); + fprintf(stderr, "Modid B has to be a valid number\n"); return -1; } @@ -522,7 +528,7 @@ int main(int argc, char ** argv) { if(argc < 2 ) return usage(); if(audit_getloginuid() == 0) { - printf("Root user should not be using this\n"); + fprintf(stderr, "The root user should not be using this\n"); return EXIT_FAILURE; } @@ -532,7 +538,7 @@ int main(int argc, char ** argv) { int returnValue = EXIT_SUCCESS; if(searchStatus == EXIT_FAILURE) { returnValue = EXIT_FAILURE; - printf("Error while looking up libraries, do you have steam installed ?"); + fprintf(stderr, "Error while looking up libraries, do you have steam installed ?"); goto exit; } @@ -546,14 +552,15 @@ int main(int argc, char ** argv) { //such as problem with access rights and taking the risk of //running system as root (which is a big security issue) if(getuid() == 0) { - printf("For the first please run without sudo\n"); + fprintf(stderr, "For the first run please avoid sudo\n"); returnValue = EXIT_FAILURE; goto exit2; } else { //leading 0 == octal int i = g_mkdir_with_parents(configFolder, 0755); if(i < 0) { - printf("Could not create configs, check access rights for this path: %s", configFolder); + fprintf(stderr, "Could not create configs, check access rights for this path: %s", configFolder); + goto exit2; } } } diff --git a/src/order.c b/src/order.c index 7a048a2..ca9a163 100644 --- a/src/order.c +++ b/src/order.c @@ -114,7 +114,7 @@ int swapPlace(int appid, int modIdA, int modIdB) { } if(listA == NULL || listB == NULL) { - printf("Invalid modId\n"); + fprintf(stderr, "Invalid modId\n"); return EXIT_FAILURE; } diff --git a/src/steam.c b/src/steam.c index 2d3803e..92ebf92 100644 --- a/src/steam.c +++ b/src/steam.c @@ -29,7 +29,7 @@ static int getFiledId(const char * field) { } else if(strcmp(field, "apps") == 0) { return FIELD_APPS; } else { - printf("unknown field"); + fprintf(stderr, "unknown field"); return -1; } } @@ -140,7 +140,8 @@ static ValveLibraries_t * parseVDF(const char * path, size_t * size, int * statu break; case '}': if(inQuotes) { - printf("Syntax error in VDF\n"); + fprintf(stderr, "Syntax error in VDF\n"); + //TODO: fix this leak free(libraries); libraries = NULL; *status = EXIT_FAILURE;