aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2008-09-28 16:30:33 -0700
committerEric Wong <normalperson@yhbt.net>2008-09-28 19:38:38 -0700
commit482fe8bf05d9d95fa262be3bd815d686740bf449 (patch)
treee72912afa477b9da9304166fc79730f0233d397b
parent3ab37f7580f97987f8c4c9787c07715b8279db57 (diff)
downloadmpd-482fe8bf05d9d95fa262be3bd815d686740bf449.tar.gz
mpd-482fe8bf05d9d95fa262be3bd815d686740bf449.tar.xz
mpd-482fe8bf05d9d95fa262be3bd815d686740bf449.zip
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).
-rw-r--r--src/path.c12
-rw-r--r--src/path.h8
2 files changed, 20 insertions, 0 deletions
diff --git a/src/path.c b/src/path.c
index ceb00c5de..8d1b0018d 100644
--- a/src/path.c
+++ b/src/path.c
@@ -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;
+}
diff --git a/src/path.h b/src/path.h
index a91728126..2c5801528 100644
--- a/src/path.h
+++ b/src/path.h
@@ -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