path: add mpd_basename() function

This is like basename(3) but with predictable semantics independent
of C library or build options used.  This is also much more strict
and does not account for trailing slashes (mpd should never deal with
trailing slashes on internal functions).
This commit is contained in:
Eric Wong 2008-09-29 13:16:48 +02:00 committed by Max Kellermann
parent 5c516b8421
commit 8bb96d46e8
2 changed files with 20 additions and 0 deletions

View File

@ -290,3 +290,15 @@ void utf8_to_fs_playlist_path(char *path_max_tmp, const char *utf8path)
rpp2app_r(path_max_tmp, path_max_tmp);
strncat(path_max_tmp, "." PLAYLIST_FILE_SUFFIX, MPD_PATH_MAX - 1);
}
/* Only takes sanitized paths w/o trailing slashes */
const char *mpd_basename(const char *path)
{
const char *ret = strrchr(path, '/');
if (!ret)
return path;
++ret;
assert(*ret != '\0');
return ret;
}

View File

@ -88,4 +88,12 @@ void pathcpy_trunc(char *dest, const char *src);
*/
void utf8_to_fs_playlist_path(char *path_max_tmp, const char *utf8path);
/*
* Like basename(3) but with predictable semantics independent
* of C library or build options used. This is also much more strict
* and does not account for trailing slashes (mpd should never deal with
* trailing slashes on internal functions).
*/
const char *mpd_basename(const char *path);
#endif