]> granicus.if.org Git - neomutt/commitdiff
Eliminate static variable in mutt_file_dirname
authorBo Yu <tsu.yubo@gmail.com>
Sat, 28 Apr 2018 03:40:24 +0000 (11:40 +0800)
committerRichard Russon <rich@flatcap.org>
Thu, 3 May 2018 11:24:31 +0000 (12:24 +0100)
mutt/file.c
mutt/file.h
muttlib.c

index 2017523d789450e1f0143ad55d90e35698ebae86..708fc7634264f05aa56231a6da2df9dbdf1af79f 100644 (file)
@@ -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);
index f83e1ee6d004fa918fc964d93e43162d6234f1dd..e15ca8100c0e9f32ab621578540d6aa5d259c787 100644 (file)
@@ -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);
index fe899b632b4bc82e70ca027c0ef85afff32c057f..f565fe01ca3a66dcc57319bd0b570b3594454c78 100644 (file)
--- 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