From: Richard Russon Date: Wed, 15 Aug 2018 12:28:07 +0000 (+0100) Subject: move realpath to library X-Git-Tag: 2019-10-25~698 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=ad2427ad1e12288e1a1b50a069433171e49ee487;p=neomutt move realpath to library --- diff --git a/browser.c b/browser.c index afb4bdc3a..6d39df235 100644 --- a/browser.c +++ b/browser.c @@ -1649,7 +1649,7 @@ void mutt_select_file(char *file, size_t filelen, int flags, char ***files, int } } /* resolve paths navigated from GUI */ - if (mutt_realpath(LastDir) == 0) + if (mutt_path_realpath(LastDir) == 0) break; } @@ -1881,7 +1881,7 @@ void mutt_select_file(char *file, size_t filelen, int flags, char ***files, int /* Resolve path from * Avoids buildup such as /a/b/../../c * Symlinks are always unraveled to keep code simple */ - if (mutt_realpath(buf) == 0) + if (mutt_path_realpath(buf) == 0) break; if (stat(buf, &st) == 0) diff --git a/mutt/path.c b/mutt/path.c index b0e97e279..877f52754 100644 --- a/mutt/path.c +++ b/mutt/path.c @@ -412,3 +412,55 @@ int mutt_path_to_absolute(char *path, const char *reference) return true; } +/** + * mutt_path_realpath - resolve path, unraveling symlinks + * @param buf Buffer containing path + * @retval num String length of resolved path + * @retval 0 Error, buf is not overwritten + * + * Resolve and overwrite the path in buf. + * + * @note Size of buf should be at least PATH_MAX bytes. + */ +size_t mutt_path_realpath(char *buf) +{ + char s[PATH_MAX]; + + if (realpath(buf, s) == NULL) + return 0; + + return mutt_str_strfcpy(buf, s, sizeof(s)); +} + +/** + * mutt_path_get_parent - Find the parent of a path + * @param path Path to use + * @param buf Buffer for the result + * @param buflen Length of buffer + */ +void mutt_path_get_parent(char *path, char *buf, size_t buflen) +{ + if (!path || !buf || (buflen == 0)) + return; + + mutt_str_strfcpy(buf, path, buflen); + int n = mutt_str_strlen(buf); + if (n == 0) + return; + + /* remove any final trailing '/' */ + if (buf[n - 1] == '/') + buf[n - 1] = '\0'; + + /* Remove everything until the next slash */ + for (n--; ((n >= 0) && (buf[n] != '/')); n--) + ; + + if (n > 0) + buf[n] = '\0'; + else + { + buf[0] = '/'; + buf[1] = '\0'; + } +} diff --git a/mutt/path.h b/mutt/path.h index 385de6fcc..e762a015a 100644 --- a/mutt/path.h +++ b/mutt/path.h @@ -31,7 +31,9 @@ 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_pretty(char *buf, size_t buflen, const char *homedir); +size_t mutt_path_realpath(char *buf); bool mutt_path_tidy(char *buf); bool mutt_path_tidy_dotdot(char *buf); bool mutt_path_tidy_slash(char *buf); diff --git a/muttlib.c b/muttlib.c index bfef8655e..cbe5a254f 100644 --- a/muttlib.c +++ b/muttlib.c @@ -1619,26 +1619,6 @@ void mutt_get_parent_path(char *path, char *buf, size_t buflen) } } -/** - * mutt_realpath - resolve path, unraveling symlinks - * @param buf Buffer containing path - * @retval num String length of resolved path - * @retval 0 Error, buf is not overwritten - * - * Resolve and overwrite the path in buf. - * - * @note Size of buf should be at least PATH_MAX bytes. - */ -size_t mutt_realpath(char *buf) -{ - char s[PATH_MAX]; - - if (realpath(buf, s) == NULL) - return 0; - - return mutt_str_strfcpy(buf, s, sizeof(s)); -} - /** * mutt_inbox_cmp - do two folders share the same path and one is an inbox * @param a First path diff --git a/muttlib.h b/muttlib.h index 7cd1cb08e..29b2fbef4 100644 --- a/muttlib.h +++ b/muttlib.h @@ -62,7 +62,6 @@ uint32_t mutt_rand32(void); uint64_t mutt_rand64(void); void mutt_rand_base32(void *out, size_t len); int mutt_randbuf(void *out, size_t len); -size_t mutt_realpath(char *buf); void mutt_safe_path(char *s, size_t l, struct Address *a); int mutt_save_confirm(const char *s, struct stat *st); void mutt_save_path(char *d, size_t dsize, struct Address *a);