From 88187e7ef2eac32fd9ff85c20d3cbe83e168ebdb Mon Sep 17 00:00:00 2001 From: LibretroAdmin Date: Thu, 25 Aug 2022 06:51:39 +0200 Subject: [PATCH] * Start getting rid of strncpy * steam.c - cleanups: * Use string_to_lower from libretro-common/stdstring.c instead of its own version * Some stylistic changes * Rewrite strncpy calls into strlcpy/strlcat/manual assignment * Make it C89 compliant * Some unused variables --- frontend/drivers/platform_ps2.c | 2 +- gfx/drivers/gx2_gfx.c | 7 +- steam/steam.c | 389 +++++++++++++++++++------------- tasks/task_database_cue.c | 6 +- 4 files changed, 237 insertions(+), 167 deletions(-) diff --git a/frontend/drivers/platform_ps2.c b/frontend/drivers/platform_ps2.c index dce7c26a22..3c2bbeb6f2 100644 --- a/frontend/drivers/platform_ps2.c +++ b/frontend/drivers/platform_ps2.c @@ -271,7 +271,7 @@ static int mount_hdd_partition(void) if (bootDeviceID == BOOT_DEVICE_HDD || bootDeviceID == BOOT_DEVICE_HDD0) { /* If we're booting from HDD, we must update the cwd variable and add : to the mount point */ - strncpy(cwd, new_cwd, sizeof(cwd)); + strlcpy(cwd, new_cwd, sizeof(cwd)); strlcat(mountPoint, ":", sizeof(mountPoint)); } else diff --git a/gfx/drivers/gx2_gfx.c b/gfx/drivers/gx2_gfx.c index 5b09566115..1c8686b10c 100644 --- a/gfx/drivers/gx2_gfx.c +++ b/gfx/drivers/gx2_gfx.c @@ -1490,14 +1490,13 @@ static bool wiiu_gfx_set_shader(void *data, for (i = 0; i < wiiu->shader_preset->passes; i++) { unsigned j; + char *ptr; char gfdpath[PATH_MAX_LENGTH]; struct video_shader_pass *pass = &wiiu->shader_preset->pass[i]; - strncpy(gfdpath, pass->source.path, PATH_MAX_LENGTH); + strlcpy(gfdpath, pass->source.path, sizeof(gfdpath)); - char *ptr = strrchr(gfdpath, '.'); - - if (!ptr) + if (!(ptr = strrchr(gfdpath, '.'))) ptr = gfdpath + strlen(gfdpath); *ptr++ = '.'; diff --git a/steam/steam.c b/steam/steam.c index 9b7b331bb9..8c6f959e66 100644 --- a/steam/steam.c +++ b/steam/steam.c @@ -1,9 +1,12 @@ #include #include -#include #include #include +#include +#include +#include + #include "../input/input_driver.h" #include "../menu/menu_driver.h" #include "../menu/menu_entries.h" @@ -14,24 +17,14 @@ #include "steam.h" -static bool mist_initialized = false; -static bool mist_showing_osk = false; +static bool mist_initialized = false; +static bool mist_showing_osk = false; static steam_core_dlc_list_t *mist_dlc_list = NULL; -static enum presence last_presence = PRESENCE_NONE; - -void str_to_lower(char *str) -{ - for (size_t i = 0; str[i] != '\0'; i++) - { - str[i] = tolower(str[i]); - } -} +static enum presence last_presence = PRESENCE_NONE; void steam_init(void) { - MistResult result; - - result = mist_subprocess_init(); + MistResult result = mist_subprocess_init(); if (MIST_IS_SUCCESS(result)) mist_initialized = true; @@ -41,18 +34,17 @@ void steam_init(void) void steam_poll(void) { - MistResult result; MistCallbackMsg callback; steam_core_dlc_list_t *core_dlc_list; - bool has_callback = false; - settings_t* settings = config_get_ptr(); - static bool has_poll_errored = false; + bool has_callback = false; + settings_t* settings = config_get_ptr(); + static bool has_poll_errored = false; static bool has_rich_presence_enabled = false; - - result = mist_poll(); + MistResult result = mist_poll(); if (MIST_IS_ERROR(result)) { - if(has_poll_errored) return; + if (has_poll_errored) + return; RARCH_ERR("[Steam]: Error polling (%d-%d)\n", MIST_UNPACK_RESULT(result)); @@ -60,7 +52,8 @@ void steam_poll(void) } result = mist_next_callback(&has_callback, &callback); - if(MIST_IS_ERROR(result)) return; + if (MIST_IS_ERROR(result)) + return; while (has_callback && MIST_IS_SUCCESS(result)) { @@ -85,7 +78,7 @@ void steam_poll(void) } /* Ensure rich presence state is correct */ - if(settings->bools.steam_rich_presence_enable != has_rich_presence_enabled) + if (settings->bools.steam_rich_presence_enable != has_rich_presence_enabled) { steam_update_presence(last_presence, true); has_rich_presence_enabled = settings->bools.steam_rich_presence_enable; @@ -97,9 +90,8 @@ steam_core_dlc_list_t *steam_core_dlc_list_new(size_t count) steam_core_dlc_list_t *core_dlc_list = (steam_core_dlc_list_t*) malloc(sizeof(*core_dlc_list)); - core_dlc_list->list = (steam_core_dlc_t*) + core_dlc_list->list = (steam_core_dlc_t*) malloc(count * sizeof(*core_dlc_list->list)); - core_dlc_list->count = 0; /* This is incremented inside the setup function */ return core_dlc_list; @@ -107,13 +99,15 @@ steam_core_dlc_list_t *steam_core_dlc_list_new(size_t count) void steam_core_dlc_list_free(steam_core_dlc_list_t *list) { - if (list == NULL) return; + size_t i; + if (!list) + return; - for (size_t i = 0; list->count > i; i++) + for (i = 0; list->count > i; i++) { - if (list->list[i].name != NULL) + if (list->list[i].name) free(list->list[i].name); - if (list->list[i].name_lower != NULL) + if (list->list[i].name_lower) free(list->list[i].name_lower); } @@ -142,33 +136,34 @@ static int dlc_core_qsort_cmp(const void *a_, const void *b_) * TODO: This currently only uses core info for cores that are installed */ core_info_t* steam_find_core_info_for_dlc(const char* name) { - size_t name_len = strlen(name); - + int i; core_info_list_t *core_info_list = NULL; core_info_get_list(&core_info_list); - if (core_info_list == NULL) return NULL; + if (!core_info_list) + return NULL; - for (int i = 0; core_info_list->count > i; i++) + for (i = 0; core_info_list->count > i; i++) { - core_info_t *core_info = core_info_get(core_info_list, i); - - char core_info_name[256] = {0}; + char *start, *end; + char core_info_name[256]; + core_info_t *core_info = core_info_get(core_info_list, i); /* Find the opening parenthesis for the core name */ - char *start = strchr(core_info->display_name, '('); - if (start == NULL) continue; + if (!(start = strchr(core_info->display_name, '(')))) + continue; /* Skip the first parenthesis and copy it to the stack */ - strncpy(core_info_name, start + 1, sizeof(core_info_name) - 1); + strlcpy(core_info_name, start + 1, sizeof(core_info_name)); /* Null terminate at the closing parenthesis. */ - char *end = strchr((const char*)&core_info_name, ')'); - if (end == NULL) continue; - else *end = '\0'; + if (!(end = strchr((const char*)&core_info_name, ')')) + continue; + + *end = '\0'; /* Make it lowercase */ - str_to_lower((char*)&core_info_name); + string_to_lower((char*)&core_info_name); /* Check if it matches */ if (strcasecmp(core_info_name, name) == 0) @@ -182,22 +177,22 @@ core_info_t* steam_find_core_info_for_dlc(const char* name) * Needs to be called after initializion because it uses core info */ MistResult steam_generate_core_dlcs_list(steam_core_dlc_list_t **list) { - MistResult result; - int count; + int count, i; steam_core_dlc_list_t *dlc_list = NULL; - char dlc_name[PATH_MAX_LENGTH] = { 0 }; - bool avaliable; - - result = mist_steam_apps_get_dlc_count(&count); - if (MIST_IS_ERROR(result)) goto error; + char dlc_name[PATH_MAX_LENGTH] = { 0 }; + bool available = false; + MistResult result = mist_steam_apps_get_dlc_count(&count); + if (MIST_IS_ERROR(result)) + goto error; dlc_list = steam_core_dlc_list_new(count); - for (int i = 0; count > i; i++) + for (i = 0; count > i; i++) { steam_core_dlc_t core_dlc; - result = mist_steam_apps_get_dlc_data_by_index(i, &core_dlc.app_id, &avaliable, (char*)&dlc_name, PATH_MAX_LENGTH); - if (MIST_IS_ERROR(result)) goto error; + result = mist_steam_apps_get_dlc_data_by_index(i, &core_dlc.app_id, &available, (char*)&dlc_name, PATH_MAX_LENGTH); + if (MIST_IS_ERROR(result)) + goto error; /* Strip away the "RetroArch - " prefix if present */ if (strncmp(dlc_name, "RetroArch - ", sizeof("RetroArch - ") - 1) == 0) @@ -207,7 +202,7 @@ MistResult steam_generate_core_dlcs_list(steam_core_dlc_list_t **list) /* Make a lower case version */ core_dlc.name_lower = strdup(core_dlc.name); - str_to_lower(core_dlc.name_lower); + string_to_lower(core_dlc.name_lower); core_dlc.core_info = steam_find_core_info_for_dlc(core_dlc.name_lower); @@ -223,39 +218,47 @@ MistResult steam_generate_core_dlcs_list(steam_core_dlc_list_t **list) return MistResult_Success; error: - if (dlc_list != NULL) steam_core_dlc_list_free(dlc_list); + if (dlc_list) + steam_core_dlc_list_free(dlc_list); return result; } -MistResult steam_get_core_dlcs(steam_core_dlc_list_t **list, bool cached) { +MistResult steam_get_core_dlcs(steam_core_dlc_list_t **list, bool cached) +{ MistResult result; steam_core_dlc_list_t *new_list = NULL; - if (cached && mist_dlc_list != NULL) + if (cached && mist_dlc_list) { *list = mist_dlc_list; return MistResult_Success; } result = steam_generate_core_dlcs_list(&new_list); - if (MIST_IS_ERROR(result)) return result; + if (MIST_IS_ERROR(result)) + return result; - if (mist_dlc_list != NULL) steam_core_dlc_list_free(mist_dlc_list); + if (mist_dlc_list) + steam_core_dlc_list_free(mist_dlc_list); mist_dlc_list = new_list; - *list = new_list; + *list = new_list; return MistResult_Success; } -steam_core_dlc_t* steam_get_core_dlc_by_name(steam_core_dlc_list_t *list, const char *name) { +steam_core_dlc_t* steam_get_core_dlc_by_name( + steam_core_dlc_list_t *list, const char *name) +{ + int i; steam_core_dlc_t *core_info; - for (int i = 0; list->count > i; i++) + for (i = 0; list->count > i; i++) { core_info = steam_core_dlc_list_get(list, i); - if (strcasecmp(core_info->name, name) == 0) return core_info; + if (strcasecmp(core_info->name, name) == 0) + return core_info; } return NULL; @@ -263,46 +266,44 @@ steam_core_dlc_t* steam_get_core_dlc_by_name(steam_core_dlc_list_t *list, const void steam_install_core_dlc(steam_core_dlc_t *core_dlc) { - MistResult result; char msg[PATH_MAX_LENGTH] = { 0 }; - - bool downloading = false; - bool installed = false; + bool downloading = false; + bool installed = false; uint64_t bytes_downloaded = 0; - uint64_t bytes_total = 0; - + uint64_t bytes_total = 0; /* Check if the core is already being downloaded */ - result = mist_steam_apps_get_dlc_download_progress(core_dlc->app_id, &downloading, &bytes_downloaded, &bytes_total); - if (MIST_IS_ERROR(result)) goto error; + MistResult result = mist_steam_apps_get_dlc_download_progress(core_dlc->app_id, &downloading, &bytes_downloaded, &bytes_total); + if (MIST_IS_ERROR(result)) + goto error; /* Check if the core is already installed */ result = mist_steam_apps_is_dlc_installed(core_dlc->app_id, &installed); - if (MIST_IS_ERROR(result)) goto error; + if (MIST_IS_ERROR(result)) + goto error; if (downloading || installed) { runloop_msg_queue_push(msg_hash_to_str(MSG_CORE_STEAM_CURRENTLY_DOWNLOADING), 1, 180, true, NULL, MESSAGE_QUEUE_ICON_DEFAULT, MESSAGE_QUEUE_CATEGORY_ERROR); - return; } result = mist_steam_apps_install_dlc(core_dlc->app_id); - if (MIST_IS_ERROR(result)) goto error; + if (MIST_IS_ERROR(result)) + goto error; task_push_steam_core_dlc_install(core_dlc->app_id, core_dlc->name); return; error: - snprintf(msg, sizeof(msg), "%s: (%d-%d)", + snprintf(msg, sizeof(msg), "%s: (%d-%d)", msg_hash_to_str(MSG_ERROR), MIST_UNPACK_RESULT(result)); - runloop_msg_queue_push(msg, 1, 180, true, NULL, + runloop_msg_queue_push(msg, 1, 180, true, NULL, MESSAGE_QUEUE_ICON_DEFAULT, MESSAGE_QUEUE_CATEGORY_ERROR); - RARCH_ERR("[Steam]: Error installing DLC %d (%d-%d)\n", core_dlc->app_id, MIST_UNPACK_RESULT(result)); - return; + RARCH_ERR("[Steam]: Error installing DLC %d (%d-%d)\n", core_dlc->app_id, MIST_UNPACK_RESULT(result)); } void steam_uninstall_core_dlc(steam_core_dlc_t *core_dlc) @@ -311,36 +312,35 @@ void steam_uninstall_core_dlc(steam_core_dlc_t *core_dlc) MistResult result = mist_steam_apps_uninstall_dlc(core_dlc->app_id); - if (MIST_IS_ERROR(result)) goto error; + if (MIST_IS_ERROR(result)) + goto error; runloop_msg_queue_push(msg_hash_to_str(MSG_CORE_STEAM_UNINSTALLED), 1, 180, true, NULL, MESSAGE_QUEUE_ICON_DEFAULT, MESSAGE_QUEUE_CATEGORY_INFO); - - bool refresh = false; - return; + error: - snprintf(msg, sizeof(msg), "%s: (%d-%d)", + snprintf(msg, sizeof(msg), "%s: (%d-%d)", msg_hash_to_str(MSG_ERROR), MIST_UNPACK_RESULT(result)); - runloop_msg_queue_push(msg, 1, 180, true, NULL, + runloop_msg_queue_push(msg, 1, 180, true, NULL, MESSAGE_QUEUE_ICON_DEFAULT, MESSAGE_QUEUE_CATEGORY_ERROR); - RARCH_ERR("[Steam]: Error uninstalling DLC %d (%d-%d)\n", core_dlc->app_id, MIST_UNPACK_RESULT(result)); - return; + RARCH_ERR("[Steam]: Error uninstalling DLC %d (%d-%d)\n", core_dlc->app_id, MIST_UNPACK_RESULT(result)); } bool steam_open_osk(void) { - bool shown = false; - bool on_deck = false; + bool shown = false; + bool on_deck = false; video_driver_state_t *video_st = video_state_get_ptr(); /* Only open the Steam OSK if running on a Steam Deck, as currently the Big Picture OSK seems to be semi-broken */ mist_steam_utils_is_steam_running_on_steam_deck(&on_deck); - if(!on_deck) return false; + if (!on_deck) + return false; mist_steam_utils_show_floating_gamepad_text_input( MistFloatingGamepadTextInputMode_SingleLine, @@ -374,7 +374,7 @@ void steam_update_presence(enum presence presence, bool force) last_presence = presence; /* Ensure rich presence is enabled */ - if(!settings->bools.steam_rich_presence_enable) + if (!settings->bools.steam_rich_presence_enable) { mist_steam_friends_clear_rich_presence(); return; @@ -382,88 +382,159 @@ void steam_update_presence(enum presence presence, bool force) switch (presence) { - case PRESENCE_MENU: - mist_steam_friends_set_rich_presence("steam_display", "#Status_InMenu"); - break; - case PRESENCE_GAME_PAUSED: - mist_steam_friends_set_rich_presence("steam_display", "#Status_Paused"); - break; - case PRESENCE_GAME: - { - const char *label = NULL; - const struct playlist_entry *entry = NULL; - core_info_t *core_info = NULL; - playlist_t *current_playlist = playlist_get_cached(); - char content[PATH_MAX_LENGTH] = {0}; + case PRESENCE_MENU: + mist_steam_friends_set_rich_presence("steam_display", "#Status_InMenu"); + break; + case PRESENCE_GAME_PAUSED: + mist_steam_friends_set_rich_presence("steam_display", "#Status_Paused"); + break; + case PRESENCE_GAME: + { + size_t _len; + char content[PATH_MAX_LENGTH]; + const char *label = NULL; + const struct playlist_entry *entry = NULL; + core_info_t *core_info = NULL; + playlist_t *current_playlist = playlist_get_cached(); - core_info_get_current_core(&core_info); + core_info_get_current_core(&core_info); - if (current_playlist) - { - playlist_get_index_by_path( - current_playlist, - path_get(RARCH_PATH_CONTENT), - &entry); + if (current_playlist) + { + playlist_get_index_by_path( + current_playlist, + path_get(RARCH_PATH_CONTENT), + &entry); - if (entry && !string_is_empty(entry->label)) - label = entry->label; - } + if (entry && !string_is_empty(entry->label)) + label = entry->label; + } - if (!label) - label = path_basename(path_get(RARCH_PATH_BASENAME)); + if (!label) + label = path_basename(path_get(RARCH_PATH_BASENAME)); - switch(settings->uints.steam_rich_presence_format) - { - case STEAM_RICH_PRESENCE_FORMAT_CONTENT: - strncpy(content, label, sizeof(content) - 1); - break; - case STEAM_RICH_PRESENCE_FORMAT_CORE: - strncpy(content, core_info ? core_info->core_name : "N/A", sizeof(content) - 1); - break; - case STEAM_RICH_PRESENCE_FORMAT_SYSTEM: - strncpy(content, core_info ? core_info->systemname : "N/A", sizeof(content) - 1); - break; - case STEAM_RICH_PRESENCE_FORMAT_CONTENT_SYSTEM: - snprintf(content, sizeof(content) - 1, "%s (%s)", - label, - core_info ? core_info->systemname : "N/A"); - break; - case STEAM_RICH_PRESENCE_FORMAT_CONTENT_CORE: - snprintf(content, sizeof(content) - 1, "%s (%s)", - label, - core_info ? core_info->core_name : "N/A"); - break; - case STEAM_RICH_PRESENCE_FORMAT_CONTENT_SYSTEM_CORE: - snprintf(content, sizeof(content) - 1, "%s (%s - %s)", - label, - core_info ? core_info->systemname : "N/A", - core_info ? core_info->core_name : "N/A"); - break; - case STEAM_RICH_PRESENCE_FORMAT_NONE: - default: - break; - } + switch(settings->uints.steam_rich_presence_format) + { + case STEAM_RICH_PRESENCE_FORMAT_CONTENT: + strlcpy(content, label, sizeof(content)); + break; + case STEAM_RICH_PRESENCE_FORMAT_CORE: + if (core_info) + strlcpy(content, core_info->core_name, sizeof(content)); + else + { + content[0] = 'N'; + content[1] = '/'; + content[2] = 'A'; + content[3] = '\0'; + } + break; + case STEAM_RICH_PRESENCE_FORMAT_SYSTEM: + if (core_info) + strlcpy(content, core_info->systemname, sizeof(content)); + else + { + content[0] = 'N'; + content[1] = '/'; + content[2] = 'A'; + content[3] = '\0'; + } + break; + case STEAM_RICH_PRESENCE_FORMAT_CONTENT_SYSTEM: + _len = strlcpy(content, label, sizeof(content)); + content[_len ] = ' '; + content[_len+1] = '('; + content[_len+2] = '\0'; + if (core_info) + { + _len = strlcat(content, core_info->systemname); + content[_len ] = ')'; + content[_len+1] = '\0'; + } + else + { + content[_len+2] = 'N'; + content[_len+3] = '/'; + content[_len+4] = 'A'; + content[_len+5] = ')'; + content[_len+6] = '\0'; + } + break; + case STEAM_RICH_PRESENCE_FORMAT_CONTENT_CORE: + _len = strlcpy(content, label, sizeof(content)); + content[_len ] = ' '; + content[_len+1] = '('; + content[_len+2] = '\0'; + if (core_info) + { + _len = strlcat(content, core_info->core_name); + content[_len ] = ')'; + content[_len+1] = '\0'; + } + else + { + content[_len+2] = 'N'; + content[_len+3] = '/'; + content[_len+4] = 'A'; + content[_len+5] = ')'; + content[_len+6] = '\0'; + } + break; + case STEAM_RICH_PRESENCE_FORMAT_CONTENT_SYSTEM_CORE: + _len = strlcpy(content, label, sizeof(content)); + content[_len ] = ' '; + content[_len+1] = '('; + content[_len+2] = '\0'; + if (core_info) + { + _len = strlcat(content, core_info->systemname); + content[_len ] = ' '; + content[_len+1] = '-'; + content[_len+2] = ' '; + _len = strlcat(content, core_info->core_name); + content[_len ] = ')'; + content[_len+1] = '\0'; + } + else + { + content[_len+2] = 'N'; + content[_len+3] = '/'; + content[_len+4] = 'A'; + content[_len+5] = ' '; + content[_len+6] = '-'; + content[_len+7] = ' '; + content[_len+8] = 'N'; + content[_len+9] = '/'; + content[_len+10] = 'A'; + content[_len+11] = ')'; + content[_len+12] = '\0'; + } + break; + case STEAM_RICH_PRESENCE_FORMAT_NONE: + default: + content[0] = '\0'; + break; + } - mist_steam_friends_set_rich_presence("content", content); - mist_steam_friends_set_rich_presence("steam_display", - settings->uints.steam_rich_presence_format != STEAM_RICH_PRESENCE_FORMAT_NONE - ? "#Status_RunningContent" : "#Status_Running" ); - } - break; - default: - break; + mist_steam_friends_set_rich_presence("content", content); + mist_steam_friends_set_rich_presence("steam_display", + settings->uints.steam_rich_presence_format != STEAM_RICH_PRESENCE_FORMAT_NONE + ? "#Status_RunningContent" : "#Status_Running" ); + } + break; + default: + break; } } void steam_deinit(void) { - MistResult result; - - result = mist_subprocess_deinit(); + MistResult result = mist_subprocess_deinit(); /* Free the cached dlc list */ - if (mist_dlc_list != NULL) steam_core_dlc_list_free(mist_dlc_list); + if (mist_dlc_list) + steam_core_dlc_list_free(mist_dlc_list); if (MIST_IS_SUCCESS(result)) mist_initialized = false; diff --git a/tasks/task_database_cue.c b/tasks/task_database_cue.c index a0e614c0e9..dba2f2eabf 100644 --- a/tasks/task_database_cue.c +++ b/tasks/task_database_cue.c @@ -494,7 +494,7 @@ int detect_scd_game(intfstream_t *fd, char *s, size_t len, const char *filename) /** process raw serial to a pre serial without spaces **/ string_remove_all_whitespace(pre_game_id, raw_game_id); /** rule: remove all spaces from the raw serial globally **/ - /** disect this pre serial into parts **/ + /** Dissect this pre serial into parts **/ length = strlen(pre_game_id); lengthref = length - 2; strncpy(check_prefix_t_hyp, pre_game_id, 2); @@ -613,7 +613,7 @@ int detect_sat_game(intfstream_t *fd, char *s, size_t len, const char *filename) string_trim_whitespace(raw_game_id); - /** disect this raw serial into parts **/ + /** Dissect this raw serial into parts **/ strncpy(check_prefix_t_hyp, raw_game_id, 2); check_prefix_t_hyp[2] = '\0'; strncpy(check_prefix_mk_hyp, raw_game_id, 3); @@ -717,7 +717,7 @@ int detect_dc_game(intfstream_t *fd, char *s, size_t len, const char *filename) length = strlen(raw_game_id); total_hyphens = string_count_occurrences_single_character(raw_game_id, '-'); - /** disect this raw serial into parts **/ + /** Dissect this raw serial into parts **/ strncpy(check_prefix_t_hyp, raw_game_id, 2); check_prefix_t_hyp[2] = '\0'; strncpy(check_prefix_t, raw_game_id, 1);