Skip to content

Commit

Permalink
Change return types for some path functions to size_t (#17303)
Browse files Browse the repository at this point in the history
  • Loading branch information
warmenhoven authored Dec 28, 2024
1 parent 9d15900 commit ef1b325
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
26 changes: 18 additions & 8 deletions libretro-common/file/file_path.c
Original file line number Diff line number Diff line change
Expand Up @@ -437,12 +437,14 @@ size_t fill_pathname_base(char *s, const char *in_path, size_t len)
* Copies base directory of @in_path into @s.
* If in_path is a path without any slashes (relative current directory),
* @s will get path "./".
*
* @return Length of the string copied in @s
**/
void fill_pathname_basedir(char *s, const char *in_path, size_t len)
size_t fill_pathname_basedir(char *s, const char *in_path, size_t len)
{
if (s != in_path)
strlcpy(s, in_path, len);
path_basedir(s);
return path_basedir(s);
}

/**
Expand Down Expand Up @@ -585,24 +587,30 @@ size_t fill_str_dated_filename(char *s,
*
* Extracts base directory by mutating path.
* Keeps trailing '/'.
*
* @return The new size of @s
**/
void path_basedir(char *s)
size_t path_basedir(char *s)
{
const char *slash;
const char *backslash;
char *last_slash = NULL;
if (!s || s[0] == '\0' || s[1] == '\0')
return;
return (s && s[0] != '\0') ? 1 : 0;
slash = strrchr(s, '/');
backslash = strrchr(s, '\\');
last_slash = (!slash || (backslash > slash)) ? (char*)backslash : (char*)slash;
if (last_slash)
{
last_slash[1] = '\0';
return last_slash + 1 - s;
}
else
{
s[0] = '.';
s[1] = PATH_DEFAULT_SLASH_C();
s[2] = '\0';
return 2;
}
}

Expand All @@ -614,11 +622,13 @@ void path_basedir(char *s)
* Extracts parent directory by mutating path.
* Assumes that @s is a directory. Keeps trailing '/'.
* If the path was already at the root directory, returns empty string
*
* @return The new size of @s
**/
void path_parent_dir(char *s, size_t len)
size_t path_parent_dir(char *s, size_t len)
{
if (!s)
return;
return 0;

if (len && PATH_CHAR_IS_SLASH(s[len - 1]))
{
Expand All @@ -641,10 +651,10 @@ void path_parent_dir(char *s, size_t len)
* gets erroneously treated as a relative one by path_basedir and returns "./".
* What we really wanted is an empty string. */
s[0] = '\0';
return;
return 0;
}
}
path_basedir(s);
return path_basedir(s);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions libretro-common/include/file/file_path.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ const char *path_basename_nocompression(const char *path);
* Extracts base directory by mutating path.
* Keeps trailing '/'.
**/
void path_basedir(char *path);
size_t path_basedir(char *path);

/**
* path_parent_dir:
Expand All @@ -201,7 +201,7 @@ void path_basedir(char *path);
* Assumes that path is a directory. Keeps trailing '/'.
* If the path was already at the root directory, returns empty string
**/
void path_parent_dir(char *path, size_t len);
size_t path_parent_dir(char *path, size_t len);

/**
* path_resolve_realpath:
Expand Down Expand Up @@ -400,7 +400,7 @@ size_t fill_pathname_base(char *out_path, const char *in_path, size_t size);
* - Calls strlcpy
* - Calls path_basedir()
**/
void fill_pathname_basedir(char *out_path, const char *in_path, size_t size);
size_t fill_pathname_basedir(char *out_path, const char *in_path, size_t size);

/**
* fill_pathname_parent_dir_name:
Expand Down

0 comments on commit ef1b325

Please sign in to comment.