[pacman-dev] [PATCH v2 1/2] libalpm: Introduce _alpm_mbasename and _alpm_mdirname
_alpm_mbasename and _alpm_mdirname are the same functions as mbasename and mdirname in src/pacman/util.c . Borrow them over to libalpm to prepare for the next patch which teaches pacman to save backup files with .pacsave.n extension. Signed-off-by: Pang Yan Han <pangyanhan@gmail.com> --- lib/libalpm/util.c | 42 ++++++++++++++++++++++++++++++++++++++++++ lib/libalpm/util.h | 2 ++ 2 files changed, 44 insertions(+), 0 deletions(-) diff --git a/lib/libalpm/util.c b/lib/libalpm/util.c index 123cd24..413e406 100644 --- a/lib/libalpm/util.c +++ b/lib/libalpm/util.c @@ -175,6 +175,48 @@ cleanup: return ret; } +/** Parse the basename of a program from a path. +* @param path path to parse basename from +* +* @return everything following the final '/' +*/ +const char *_alpm_mbasename(const char *path) +{ + const char *last = strrchr(path, '/'); + if(last) { + return last + 1; + } + return path; +} + +/** Parse the dirname of a program from a path. +* The path returned should be freed. +* @param path path to parse dirname from +* +* @return everything preceding the final '/' +*/ +char *_alpm_mdirname(const char *path) +{ + char *ret, *last; + + /* null or empty path */ + if(path == NULL || path == '\0') { + return strdup("."); + } + + ret = strdup(path); + last = strrchr(ret, '/'); + + if(last != NULL) { + /* we found a '/', so terminate our string */ + *last = '\0'; + return ret; + } + /* no slash found */ + free(ret); + return strdup("."); +} + /* Trim whitespace and newlines from a string */ char *_alpm_strtrim(char *str) diff --git a/lib/libalpm/util.h b/lib/libalpm/util.h index 9ee6370..febfef9 100644 --- a/lib/libalpm/util.h +++ b/lib/libalpm/util.h @@ -94,6 +94,8 @@ struct archive_read_buffer { int _alpm_makepath(const char *path); int _alpm_makepath_mode(const char *path, mode_t mode); int _alpm_copyfile(const char *src, const char *dest); +const char *_alpm_mbasename(const char *path); +char *_alpm_mdirname(const char *path); char *_alpm_strtrim(char *str); size_t _alpm_strip_newline(char *str); int _alpm_unpack_single(alpm_handle_t *handle, const char *archive, -- 1.7.6
participants (1)
-
Pang Yan Han