Skip to content

Commit

Permalink
Cut down on snprintf
Browse files Browse the repository at this point in the history
  • Loading branch information
LibretroAdmin committed Jun 14, 2024
1 parent f423b62 commit c022e4e
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 68 deletions.
41 changes: 20 additions & 21 deletions libretro-common/file/file_path.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,13 @@ void path_linked_list_add_path(struct path_linked_list *in_path_linked_list,
char *path)
{
/* If the first item does not have a path this is
a list which has just been created, so we just fill
a list which has just been created, so we just fill
the path for the first item
*/
if (!in_path_linked_list->path)
in_path_linked_list->path = strdup(path);
else
{
{
struct path_linked_list *node = (struct path_linked_list*) malloc(sizeof(*node));

if (node)
Expand Down Expand Up @@ -620,7 +620,7 @@ void path_parent_dir(char *path, size_t len)
{
if (!path)
return;

if (len && PATH_CHAR_IS_SLASH(path[len - 1]))
{
bool path_was_absolute = path_is_absolute(path);
Expand Down Expand Up @@ -697,7 +697,7 @@ bool path_is_absolute(const char *path)
/* Many roads lead to Rome...
* Note: Drive letter can only be 1 character long */
return ( string_starts_with_size(path, "\\\\", STRLEN_CONST("\\\\"))
|| string_starts_with_size(path + 1, ":/", STRLEN_CONST(":/"))
|| string_starts_with_size(path + 1, ":/", STRLEN_CONST(":/"))
|| string_starts_with_size(path + 1, ":\\", STRLEN_CONST(":\\")));
#elif defined(__wiiu__) || defined(VITA)
{
Expand Down Expand Up @@ -842,7 +842,7 @@ char *path_resolve_realpath(char *buf, size_t size, bool resolve_symlinks)
tmp[t++] = *p++;
}
}while(next < buf_end);


end:
tmp[t] = '\0';
Expand Down Expand Up @@ -880,11 +880,11 @@ size_t path_relative_to(char *out,
if (
path
&& base
&& path[0] != '\0'
&& path[0] != '\0'
&& path[1] != '\0'
&& base[0] != '\0'
&& base[1] != '\0'
&& path[1] == ':'
&& path[1] == ':'
&& base[1] == ':'
&& path[0] != base[0])
return strlcpy(out, path, size);
Expand Down Expand Up @@ -1156,7 +1156,7 @@ size_t fill_pathname_abbreviate_special(char *out_path,
*
* Leaf function.
*
* Changes the slashes to the correct kind for the OS
* Changes the slashes to the correct kind for the OS
* So forward slash on linux and backslash on Windows
**/
void pathname_conform_slashes_to_os(char *path)
Expand All @@ -1174,7 +1174,7 @@ void pathname_conform_slashes_to_os(char *path)
*
* Leaf function.
*
* Change all slashes to forward so they are more
* Change all slashes to forward so they are more
* portable between Windows and Linux
**/
void pathname_make_slashes_portable(char *path)
Expand Down Expand Up @@ -1215,9 +1215,9 @@ static int get_pathname_num_slashes(const char *in_path)
/**
* fill_pathname_abbreviated_or_relative:
*
* Fills the supplied path with either the abbreviated path or
* Fills the supplied path with either the abbreviated path or
* the relative path, which ever one has less depth / number of slashes
*
*
* If lengths of abbreviated and relative paths are the same,
* the relative path will be used
* @in_path can be an absolute, relative or abbreviated path
Expand All @@ -1233,7 +1233,7 @@ size_t fill_pathname_abbreviated_or_relative(char *out_path,
char absolute_path[PATH_MAX_LENGTH];
char relative_path[PATH_MAX_LENGTH];
char abbreviated_path[PATH_MAX_LENGTH];

expanded_path[0] = '\0';
absolute_path[0] = '\0';
relative_path[0] = '\0';
Expand Down Expand Up @@ -1267,7 +1267,7 @@ size_t fill_pathname_abbreviated_or_relative(char *out_path,
absolute_path, sizeof(abbreviated_path));

/* Use the shortest path, preferring the relative path*/
if ( get_pathname_num_slashes(relative_path) <=
if ( get_pathname_num_slashes(relative_path) <=
get_pathname_num_slashes(abbreviated_path))
return strlcpy(out_path, relative_path, size);
return strlcpy(out_path, abbreviated_path, size);
Expand Down Expand Up @@ -1348,7 +1348,7 @@ void fill_pathname_application_path(char *s, size_t len)
CFStringGetCString(bundle_path, s, len, kCFStringEncodingUTF8);
#ifdef HAVE_COCOATOUCH
{
/* This needs to be done so that the path becomes
/* This needs to be done so that the path becomes
* /private/var/... and this
* is used consistently throughout for the iOS bundle path */
char resolved_bundle_dir_buf[PATH_MAX_LENGTH] = {0};
Expand All @@ -1364,7 +1364,7 @@ void fill_pathname_application_path(char *s, size_t len)
CFRelease(bundle_path);
CFRelease(bundle_url);
#ifndef HAVE_COCOATOUCH
/* Not sure what this does but it breaks
/* Not sure what this does but it breaks
* stuff for iOS, so skipping */
strlcat(s, "nobin", len);
#endif
Expand All @@ -1388,20 +1388,19 @@ void fill_pathname_application_path(char *s, size_t len)
free(buff);
#else
{
pid_t pid;
static const char *exts[] = { "exe", "file", "path/a.out" };
char link_path[255];
pid_t pid = getpid();
size_t _len = snprintf(link_path, sizeof(link_path), "/proc/%u/",
(unsigned)pid);

link_path[0] = *s = '\0';
pid = getpid();
*s = '\0';

/* Linux, BSD and Solaris paths. Not standardized. */
for (i = 0; i < ARRAY_SIZE(exts); i++)
{
ssize_t ret;

snprintf(link_path, sizeof(link_path), "/proc/%u/%s",
(unsigned)pid, exts[i]);
strlcpy(link_path + _len, exts[i], sizeof(link_path) - _len);

if ((ret = readlink(link_path, s, len - 1)) >= 0)
{
Expand Down
7 changes: 3 additions & 4 deletions menu/menu_driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -4605,11 +4605,10 @@ void menu_entries_get_core_title(char *s, size_t len)
#if defined(_MSC_VER)
_len += strlcpy(s + _len, msvc_vercode_to_str(_MSC_VER), len - _len);
#endif

_len += strlcpy(s + _len, " - ", len - _len);
_len += strlcpy(s + _len, core_name, len - _len);
if (!string_is_empty(core_version))
snprintf(s + _len, len - _len, " - %s (%s)", core_name, core_version);
else
snprintf(s + _len, len - _len, " - %s", core_name);
snprintf(s + _len, len - _len, " (%s)", core_version);
}

static bool menu_driver_init_internal(
Expand Down
102 changes: 59 additions & 43 deletions retroarch.c
Original file line number Diff line number Diff line change
Expand Up @@ -3206,7 +3206,7 @@ bool command_event(enum event_command cmd, void *data)
gfx_widgets_ai_service_overlay_unload();
#endif
}
else
else
{
command_event(CMD_EVENT_AI_SERVICE_CALL, NULL);
}
Expand Down Expand Up @@ -8112,64 +8112,80 @@ bool retroarch_override_setting_is_set(
int retroarch_get_capabilities(enum rarch_capabilities type,
char *str_out, size_t str_len)
{
size_t _len = 0;
switch (type)
{
case RARCH_CAPABILITIES_CPU:
{
uint64_t cpu = cpu_features_get();
snprintf(str_out, str_len,
"%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
cpu & RETRO_SIMD_MMX ? "MMX " : "",
cpu & RETRO_SIMD_MMXEXT ? "MMXEXT " : "",
cpu & RETRO_SIMD_SSE ? "SSE " : "",
cpu & RETRO_SIMD_SSE2 ? "SSE2 " : "",
cpu & RETRO_SIMD_SSE3 ? "SSE3 " : "",
cpu & RETRO_SIMD_SSSE3 ? "SSSE3 " : "",
cpu & RETRO_SIMD_SSE4 ? "SSE4 " : "",
cpu & RETRO_SIMD_SSE42 ? "SSE42 " : "",
cpu & RETRO_SIMD_AES ? "AES " : "",
cpu & RETRO_SIMD_AVX ? "AVX " : "",
cpu & RETRO_SIMD_AVX2 ? "AVX2 " : "",
cpu & RETRO_SIMD_NEON ? "NEON " : "",
cpu & RETRO_SIMD_VFPV3 ? "VFPV3 " : "",
cpu & RETRO_SIMD_VFPV4 ? "VFPV4 " : "",
cpu & RETRO_SIMD_VMX ? "VMX " : "",
cpu & RETRO_SIMD_VMX128 ? "VMX128 " : "",
cpu & RETRO_SIMD_VFPU ? "VFPU " : "",
cpu & RETRO_SIMD_PS ? "PS " : "",
cpu & RETRO_SIMD_ASIMD ? "ASIMD " : "");
if (cpu & RETRO_SIMD_MMX)
_len += strlcpy(str_out + _len, "MMX ", str_len - _len);
if (cpu & RETRO_SIMD_MMXEXT)
_len += strlcpy(str_out + _len, "MMXEXT ", str_len - _len);
if (cpu & RETRO_SIMD_SSE)
_len += strlcpy(str_out + _len, "SSE ", str_len - _len);
if (cpu & RETRO_SIMD_SSE2)
_len += strlcpy(str_out + _len, "SSE2 ", str_len - _len);
if (cpu & RETRO_SIMD_SSE3)
_len += strlcpy(str_out + _len, "SSE3 ", str_len - _len);
if (cpu & RETRO_SIMD_SSSE3)
_len += strlcpy(str_out + _len, "SSSE3 ", str_len - _len);
if (cpu & RETRO_SIMD_SSE4)
_len += strlcpy(str_out + _len, "SSE4 ", str_len - _len);
if (cpu & RETRO_SIMD_SSE42)
_len += strlcpy(str_out + _len, "SSE42 ", str_len - _len);
if (cpu & RETRO_SIMD_AES)
_len += strlcpy(str_out + _len, "AES ", str_len - _len);
if (cpu & RETRO_SIMD_AVX)
_len += strlcpy(str_out + _len, "AVX ", str_len - _len);
if (cpu & RETRO_SIMD_AVX2)
_len += strlcpy(str_out + _len, "AVX2 ", str_len - _len);
if (cpu & RETRO_SIMD_NEON)
_len += strlcpy(str_out + _len, "NEON ", str_len - _len);
if (cpu & RETRO_SIMD_VFPV3)
_len += strlcpy(str_out + _len, "VFPV3 ", str_len - _len);
if (cpu & RETRO_SIMD_VFPV4)
_len += strlcpy(str_out + _len, "VFPV4 ", str_len - _len);
if (cpu & RETRO_SIMD_VMX)
_len += strlcpy(str_out + _len, "VMX ", str_len - _len);
if (cpu & RETRO_SIMD_VMX128)
_len += strlcpy(str_out + _len, "VMX128 ", str_len - _len);
if (cpu & RETRO_SIMD_VFPU)
_len += strlcpy(str_out + _len, "VFPU ", str_len - _len);
if (cpu & RETRO_SIMD_PS)
_len += strlcpy(str_out + _len, "PS ", str_len - _len);
if (cpu & RETRO_SIMD_ASIMD)
_len += strlcpy(str_out + _len, "ASIMD ", str_len - _len);
break;
}
break;
case RARCH_CAPABILITIES_COMPILER:
#if defined(_MSC_VER)
snprintf(str_out, str_len, "%s: MSVC (%d) %u-bit",
msg_hash_to_str(MSG_COMPILER),
_MSC_VER, (unsigned)
(CHAR_BIT * sizeof(size_t)));
_len = strlcpy(str_out, msg_hash_to_str(MSG_COMPILER), str_len);
_len += snprintf(str_out + _len, str_len - _len, ": MSVC (%d)",
_MSC_VER);
#elif defined(__SNC__)
snprintf(str_out, str_len, "%s: SNC (%d) %u-bit",
msg_hash_to_str(MSG_COMPILER),
__SN_VER__, (unsigned)(CHAR_BIT * sizeof(size_t)));
_len = strlcpy(str_out, msg_hash_to_str(MSG_COMPILER), str_len);
_len += snprintf(str_out + _len, str_len - _Len, ": SNC (%d)",
__SN_VER__);
#elif defined(_WIN32) && defined(__GNUC__)
snprintf(str_out, str_len, "%s: MinGW (%d.%d.%d) %u-bit",
msg_hash_to_str(MSG_COMPILER),
__GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__, (unsigned)
(CHAR_BIT * sizeof(size_t)));
_len = strlcpy(str_out, msg_hash_to_str(MSG_COMPILER), str_len);
_len += snprintf(str_out + _len, str_len - _len, ": MinGW (%d.%d.%d)",
__GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__);
#elif defined(__clang__)
snprintf(str_out, str_len, "%s: Clang/LLVM (%s) %u-bit",
msg_hash_to_str(MSG_COMPILER),
__clang_version__, (unsigned)(CHAR_BIT * sizeof(size_t)));
_len = strlcpy(str_out, msg_hash_to_str(MSG_COMPILER), str_len);
_len += strlcpy(str_out + _len, ": Clang/LLVM (", str_len - _len);
_len += strlcpy(str_out + _len, __clang_version__, str_len - _len);
_len += strlcpy(str_out + _len, ")", str_len - _len);
#elif defined(__GNUC__)
snprintf(str_out, str_len, "%s: GCC (%d.%d.%d) %u-bit",
msg_hash_to_str(MSG_COMPILER),
__GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__, (unsigned)
(CHAR_BIT * sizeof(size_t)));
_len = strlcpy(str_out, msg_hash_to_str(MSG_COMPILER), str_len);
_len += snprintf(str_out + _len, str_len - _len, ": GCC (%d.%d.%d)",
__GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__);
#else
snprintf(str_out, str_len, "%s %u-bit",
msg_hash_to_str(MSG_UNKNOWN_COMPILER),
(unsigned)(CHAR_BIT * sizeof(size_t)));
_len = strlcpy(str_out, msg_hash_to_str(MSG_UNKNOWN_COMPILER), str_len);
#endif
snprintf(str_out + _len, str_len - _len, " %u-bit",
(unsigned)(CHAR_BIT * sizeof(size_t)));
break;
default:
case RARCH_CAPABILITIES_NONE:
Expand Down

0 comments on commit c022e4e

Please sign in to comment.