]> granicus.if.org Git - neomutt/commitdiff
move realpath to library
authorRichard Russon <rich@flatcap.org>
Wed, 15 Aug 2018 12:28:07 +0000 (13:28 +0100)
committerRichard Russon <rich@flatcap.org>
Wed, 15 Aug 2018 12:28:07 +0000 (13:28 +0100)
browser.c
mutt/path.c
mutt/path.h
muttlib.c
muttlib.h

index afb4bdc3a2c8f56a6ef36329e1f29d5a535bd66e..6d39df2357f45085484eefb01f14dcd391d94714 100644 (file)
--- 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 <chdir>
              * 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)
index b0e97e279dfa3c1508ec73584c17545f2adbcebe..877f5275478be8ed056aaa3cd20cea1423f4e134 100644 (file)
@@ -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';
+  }
+}
index 385de6fcc4f474dc919b13dcf34fdbdf5e164aff..e762a015a3ff92a1c675cb171cd8c152358e86de 100644 (file)
@@ -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);
index bfef8655e284fe64c69cc206f2270cd3511fff92..cbe5a254f8f264bf20aa9928bb1cdf9c82256d9c 100644 (file)
--- 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
index 7cd1cb08e1d0ba4ed8923212fa814e88b76400ff..29b2fbef40c55f8800c64c929b3341e16eff0e25 100644 (file)
--- 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);