* 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
This commit is contained in:
LibretroAdmin 2022-08-25 06:51:39 +02:00
parent 8017410098
commit 88187e7ef2
4 changed files with 237 additions and 167 deletions

View File

@ -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

View File

@ -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++ = '.';

View File

@ -1,9 +1,12 @@
#include <ctype.h>
#include <mist.h>
#include <retro_timers.h>
#include <stdio.h>
#include <string.h>
#include <retro_timers.h>
#include <compat/strl.h>
#include <string/stdstring.h>
#include "../input/input_driver.h"
#include "../menu/menu_driver.h"
#include "../menu/menu_entries.h"
@ -19,19 +22,9 @@ 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]);
}
}
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;
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))
{
@ -99,7 +92,6 @@ steam_core_dlc_list_t *steam_core_dlc_list_new(size_t count)
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++)
{
char *start, *end;
char core_info_name[256];
core_info_t *core_info = core_info_get(core_info_list, i);
char core_info_name[256] = {0};
/* 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;
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,25 +218,29 @@ 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;
@ -249,13 +248,17 @@ MistResult steam_get_core_dlcs(steam_core_dlc_list_t **list, bool cached) {
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,32 +266,31 @@ 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;
uint64_t bytes_downloaded = 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);
@ -302,7 +304,6 @@ error:
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;
}
void steam_uninstall_core_dlc(steam_core_dlc_t *core_dlc)
@ -311,14 +312,13 @@ 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)",
msg_hash_to_str(MSG_ERROR),
@ -328,7 +328,6 @@ error:
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;
}
bool steam_open_osk(void)
@ -340,7 +339,8 @@ bool steam_open_osk(void)
/* 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,
@ -390,11 +390,12 @@ void steam_update_presence(enum presence presence, bool force)
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();
char content[PATH_MAX_LENGTH] = {0};
core_info_get_current_core(&core_info);
@ -415,32 +416,103 @@ void steam_update_presence(enum presence presence, bool force)
switch(settings->uints.steam_rich_presence_format)
{
case STEAM_RICH_PRESENCE_FORMAT_CONTENT:
strncpy(content, label, sizeof(content) - 1);
strlcpy(content, label, sizeof(content));
break;
case STEAM_RICH_PRESENCE_FORMAT_CORE:
strncpy(content, core_info ? core_info->core_name : "N/A", sizeof(content) - 1);
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:
strncpy(content, core_info ? core_info->systemname : "N/A", sizeof(content) - 1);
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:
snprintf(content, sizeof(content) - 1, "%s (%s)",
label,
core_info ? core_info->systemname : "N/A");
_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:
snprintf(content, sizeof(content) - 1, "%s (%s)",
label,
core_info ? core_info->core_name : "N/A");
_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:
snprintf(content, sizeof(content) - 1, "%s (%s - %s)",
label,
core_info ? core_info->systemname : "N/A",
core_info ? core_info->core_name : "N/A");
_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;
}
@ -458,12 +530,11 @@ void steam_update_presence(enum presence presence, bool force)
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;

View File

@ -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);