From 42f3913a6edd20f10d58e547f7e803282c34e8f5 Mon Sep 17 00:00:00 2001 From: Pietro Cerutti Date: Thu, 26 Oct 2017 17:08:41 +0100 Subject: [PATCH] Wrap dirname(3) inside a mutt_dirname() function * Wrap dirname(3) inside a mutt_dirname() function This new function, opposite to the original dirname(3), does not modify its argument and can be passed a const char * argument. This means that callers mustn't copy their strings before calling dirname anymore. --- init.c | 7 ++----- lib/file.c | 20 ++++++++++++++++++++ lib/file.h | 1 + muttlib.c | 5 +---- 4 files changed, 24 insertions(+), 9 deletions(-) diff --git a/init.c b/init.c index 3f5a166e5..3cc762fe6 100644 --- a/init.c +++ b/init.c @@ -25,7 +25,6 @@ #include #include #include -#include #include #include #include @@ -3116,7 +3115,7 @@ static struct ListHead MuttrcStack = STAILQ_HEAD_INITIALIZER(MuttrcStack); */ static int to_absolute_path(char *path, const char *reference) { - char *ref_tmp = NULL, *dirpath = NULL; + const char *dirpath = NULL; char abs_path[PATH_MAX]; int path_len; @@ -3126,12 +3125,10 @@ static int to_absolute_path(char *path, const char *reference) return true; } - ref_tmp = safe_strdup(reference); - dirpath = dirname(ref_tmp); /* get directory name of */ + dirpath = mutt_dirname(reference); strfcpy(abs_path, dirpath, PATH_MAX); safe_strncat(abs_path, sizeof(abs_path), "/", 1); /* append a / at the end of the path */ - FREE(&ref_tmp); path_len = PATH_MAX - strlen(path); safe_strncat(abs_path, sizeof(abs_path), path, path_len > 0 ? path_len : 0); diff --git a/lib/file.c b/lib/file.c index 30bb772b9..48db31854 100644 --- a/lib/file.c +++ b/lib/file.c @@ -33,6 +33,7 @@ * | mutt_copy_bytes() | Copy some content from one file to another * | mutt_copy_stream() | Copy the contents of one file into another * | mutt_decrease_mtime() | Decrease a file's modification time by 1 second + * | mutt_dirname() | Return a path up to, but not including, the final '/' * | mutt_lock_file() | (try to) lock a file * | mutt_mkdir() | Recursively create directories * | mutt_quote_filename() | Quote a filename to survive the shell's quoting rules @@ -58,6 +59,7 @@ #include #include #include +#include #include #include #include @@ -912,6 +914,24 @@ time_t mutt_decrease_mtime(const char *f, struct stat *st) return mtime; } +/** + * mutt_dirname - Return a path up to, but not including, the final '/' + * @param p Path + * @retval ptr The directory containing p + * + * Unlike the IEEE Std 1003.1-2001 specification of dirname(3), this + * implementation does not modify its parameter, so callers need not manually + * copy their paths into a modifiable buffer prior to calling this function. + * + * mutt_dirname() returns a static string which must not be free()'d. + */ +const char *mutt_dirname(const char *p) +{ + static char buf[_POSIX_PATH_MAX]; + strfcpy(buf, p, sizeof(buf)); + return dirname(buf); +} + /** * mutt_set_mtime - Set the modification time of one file from another * @param from Filename whose mtime should be copied diff --git a/lib/file.h b/lib/file.h index 34e08e973..5e611b859 100644 --- a/lib/file.h +++ b/lib/file.h @@ -39,6 +39,7 @@ char * mutt_concat_path(char *d, const char *dir, const char *fname, size_t int mutt_copy_bytes(FILE *in, FILE *out, size_t size); int mutt_copy_stream(FILE *fin, FILE *fout); time_t mutt_decrease_mtime(const char *f, struct stat *st); +const char *mutt_dirname(const char *p); int mutt_lock_file(const char *path, int fd, int excl, int timeout); int mutt_mkdir(const char *path, mode_t mode); size_t mutt_quote_filename(char *d, size_t l, const char *f); diff --git a/muttlib.c b/muttlib.c index 2a1e4c79e..9cbeee730 100644 --- a/muttlib.c +++ b/muttlib.c @@ -26,7 +26,6 @@ #include #include #include -#include #ifdef ENABLE_NLS #include #endif @@ -1544,10 +1543,8 @@ int mutt_save_confirm(const char *s, struct stat *st) /* user confirmed with MUTT_YES or set OPT_CONFIRMCREATE */ if (ret == 0) { - strncpy(tmp, s, sizeof(tmp) - 1); - /* create dir recursively */ - if (mutt_mkdir(dirname(tmp), S_IRWXU) == -1) + if (mutt_mkdir(mutt_dirname(s), S_IRWXU) == -1) { /* report failure & abort */ mutt_perror(s); -- 2.40.0