]> granicus.if.org Git - neomutt/commitdiff
improve mutt_path_parent
authorRichard Russon <rich@flatcap.org>
Sat, 25 Aug 2018 12:20:44 +0000 (13:20 +0100)
committerRichard Russon <rich@flatcap.org>
Sun, 26 Aug 2018 21:11:03 +0000 (22:11 +0100)
mutt/path.c
mutt/path.h

index 927214b61f8fc8ba58133878c95aa7c9aa4175a0..4c3f55ee9aadec87f690b193892c6cb4d46b582c 100644 (file)
@@ -468,36 +468,33 @@ size_t mutt_path_realpath(char *buf)
 }
 
 /**
- * mutt_path_get_parent - Find the parent of a path
+ * mutt_path_parent - Find the parent of a path
  * @param path   Path to use
  * @param buf    Buffer for the result
  * @param buflen Length of buffer
+ * @retval true  Success
  */
-void mutt_path_get_parent(char *path, char *buf, size_t buflen)
+bool mutt_path_parent(char *buf, size_t buflen)
 {
-  if (!path || !buf || (buflen == 0))
-    return;
+  if (!buf)
+    return false;
 
-  mutt_str_strfcpy(buf, path, buflen);
   int n = mutt_str_strlen(buf);
-  if (n == 0)
-    return;
+  if (n < 2)
+    return false;
 
-  /* remove any final trailing '/' */
   if (buf[n - 1] == '/')
-    buf[n - 1] = '\0';
+    n--;
 
-  /* Remove everything until the next slash */
+  // Find the previous '/'
   for (n--; ((n >= 0) && (buf[n] != '/')); n--)
     ;
 
-  if (n > 0)
-    buf[n] = '\0';
-  else
-  {
-    buf[0] = '/';
-    buf[1] = '\0';
-  }
+  if (n == 0) // Always keep at least one '/'
+    n++;
+
+  buf[n] = '\0';
+  return true;
 }
 
 /**
index ae83bdb555b59ebe49bfe2ccbd5c006332d962d8..758bb5f31ee4a9cb5697d9a7688a08a3aa0f4fe2 100644 (file)
@@ -32,7 +32,7 @@ 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_parent(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);