]> granicus.if.org Git - neomutt/commitdiff
Wrap dirname(3) inside a mutt_dirname() function
authorPietro Cerutti <gahr@gahr.ch>
Thu, 26 Oct 2017 16:08:41 +0000 (17:08 +0100)
committerRichard Russon <rich@flatcap.org>
Thu, 26 Oct 2017 16:08:41 +0000 (17:08 +0100)
* 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
lib/file.c
lib/file.h
muttlib.c

diff --git a/init.c b/init.c
index 3f5a166e57d954742ac42abddf187c752dc9a736..3cc762fe653a2601f159e69a3f30e9d097542f04 100644 (file)
--- a/init.c
+++ b/init.c
@@ -25,7 +25,6 @@
 #include <errno.h>
 #include <fcntl.h>
 #include <inttypes.h>
-#include <libgen.h>
 #include <limits.h>
 #include <pwd.h>
 #include <regex.h>
@@ -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);
index 30bb772b9fd45d007452f6283dc340d034b83201..48db318545ec916cd73d2261c67fc179d5958fb0 100644 (file)
@@ -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 <dirent.h>
 #include <errno.h>
 #include <fcntl.h>
+#include <libgen.h>
 #include <limits.h>
 #include <stdbool.h>
 #include <stdio.h>
@@ -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
index 34e08e973860f507ea7f6cc52d88ee145912e14a..5e611b85941bc2dbf45fc02fafa840ee888d729a 100644 (file)
@@ -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);
index 2a1e4c79e4772fc00c6c2969b065cdde3714bbd1..9cbeee730ebb46be03a1341fe13129d463644bec 100644 (file)
--- a/muttlib.c
+++ b/muttlib.c
@@ -26,7 +26,6 @@
 #include <ctype.h>
 #include <errno.h>
 #include <inttypes.h>
-#include <libgen.h>
 #ifdef ENABLE_NLS
 #include <libintl.h>
 #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);