From: Richard Russon Date: Sat, 25 Aug 2018 12:20:44 +0000 (+0100) Subject: improve mutt_path_parent X-Git-Tag: 2019-10-25~682^2~8 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=8f06305424a48c9d9b05cd9d6041b08f45fa1452;p=neomutt improve mutt_path_parent --- diff --git a/mutt/path.c b/mutt/path.c index 927214b61..4c3f55ee9 100644 --- a/mutt/path.c +++ b/mutt/path.c @@ -468,36 +468,33 @@ size_t mutt_path_realpath(char *buf) } /** - * mutt_path_get_parent - Find the parent of a path + * mutt_path_parent - Find the parent of a path * @param path Path to use * @param buf Buffer for the result * @param buflen Length of buffer + * @retval true Success */ -void mutt_path_get_parent(char *path, char *buf, size_t buflen) +bool mutt_path_parent(char *buf, size_t buflen) { - if (!path || !buf || (buflen == 0)) - return; + if (!buf) + return false; - mutt_str_strfcpy(buf, path, buflen); int n = mutt_str_strlen(buf); - if (n == 0) - return; + if (n < 2) + return false; - /* remove any final trailing '/' */ if (buf[n - 1] == '/') - buf[n - 1] = '\0'; + n--; - /* Remove everything until the next slash */ + // Find the previous '/' for (n--; ((n >= 0) && (buf[n] != '/')); n--) ; - if (n > 0) - buf[n] = '\0'; - else - { - buf[0] = '/'; - buf[1] = '\0'; - } + if (n == 0) // Always keep at least one '/' + n++; + + buf[n] = '\0'; + return true; } /** diff --git a/mutt/path.h b/mutt/path.h index ae83bdb55..758bb5f31 100644 --- a/mutt/path.h +++ b/mutt/path.h @@ -32,7 +32,7 @@ bool mutt_path_canon(char *buf, size_t buflen, const char *homedir); char * mutt_path_concat(char *d, const char *dir, const char *fname, size_t l); char * mutt_path_concatn(char *dst, size_t dstlen, const char *dir, size_t dirlen, const char *fname, size_t fnamelen); char * mutt_path_dirname(const char *path); -void mutt_path_get_parent(char *path, char *buf, size_t buflen); +bool mutt_path_parent(char *buf, size_t buflen); bool mutt_path_pretty(char *buf, size_t buflen, const char *homedir); size_t mutt_path_realpath(char *buf); bool mutt_path_tidy(char *buf);