From: Bo Yu Date: Sat, 28 Apr 2018 03:40:24 +0000 (+0800) Subject: Eliminate static variable in mutt_file_dirname X-Git-Tag: neomutt-20180512~15 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=dddbd14ee50ac278412e7a94b7b11b8ee78d83b8;p=neomutt Eliminate static variable in mutt_file_dirname --- diff --git a/mutt/file.c b/mutt/file.c index 2017523d7..708fc7634 100644 --- a/mutt/file.c +++ b/mutt/file.c @@ -927,12 +927,10 @@ time_t mutt_file_decrease_mtime(const char *f, struct stat *st) * 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. - * - * @warning mutt_file_dirname() returns a static string which must not be free()'d. */ -const char *mutt_file_dirname(const char *p) +char *mutt_file_dirname(const char *p) { - static char buf[_POSIX_PATH_MAX]; + char *buf = mutt_mem_malloc(_POSIX_PATH_MAX); mutt_str_strfcpy(buf, p, sizeof(buf)); return dirname(buf); } @@ -1312,7 +1310,6 @@ int mutt_file_rename(char *oldfile, char *newfile) */ int mutt_file_to_absolute_path(char *path, const char *reference) { - const char *dirpath = NULL; char abs_path[PATH_MAX]; int path_len; @@ -1322,8 +1319,9 @@ int mutt_file_to_absolute_path(char *path, const char *reference) return true; } - dirpath = mutt_file_dirname(reference); + char *dirpath = mutt_file_dirname(reference); mutt_str_strfcpy(abs_path, dirpath, PATH_MAX); + FREE(&dirpath); mutt_str_strncat(abs_path, sizeof(abs_path), "/", 1); /* append a / at the end of the path */ path_len = PATH_MAX - strlen(path); diff --git a/mutt/file.h b/mutt/file.h index f83e1ee6d..e15ca8100 100644 --- a/mutt/file.h +++ b/mutt/file.h @@ -46,7 +46,7 @@ char * mutt_file_concat_path(char *d, const char *dir, const char *fname, s int mutt_file_copy_bytes(FILE *in, FILE *out, size_t size); int mutt_file_copy_stream(FILE *fin, FILE *fout); time_t mutt_file_decrease_mtime(const char *f, struct stat *st); -const char *mutt_file_dirname(const char *p); +char * mutt_file_dirname(const char *p); int mutt_file_fclose(FILE **f); FILE * mutt_file_fopen(const char *path, const char *mode); int mutt_file_fsync_close(FILE **f); diff --git a/muttlib.c b/muttlib.c index fe899b632..f565fe01c 100644 --- a/muttlib.c +++ b/muttlib.c @@ -1360,12 +1360,15 @@ int mutt_save_confirm(const char *s, struct stat *st) if (ret == 0) { /* create dir recursively */ - if (mutt_file_mkdir(mutt_file_dirname(s), S_IRWXU) == -1) + char *tmp_path = mutt_file_dirname(s); + if (mutt_file_mkdir(tmp_path, S_IRWXU) == -1) { /* report failure & abort */ mutt_perror(s); + FREE(&tmp_path); return 1; } + FREE(&tmp_path); } } else