struct folder_file *pb = (struct folder_file *) b;
/* inbox should be sorted ahead of its siblings */
- int same_path = mutt_same_path (pa->name, pb->name);
- int r = (same_path && mutt_is_inbox (pa->name)) ? -1 :
- (same_path && mutt_is_inbox (pb->name)) ? 1 :
+ int common_subpath = mutt_have_common_subpath (pa->name, pb->name);
+ int r = (common_subpath && mutt_is_inbox (pa->name)) ? -1 :
+ (common_subpath && mutt_is_inbox (pb->name)) ? 1 :
mutt_strcoll (pa->name, pb->name);
return ((BrowserSort & SORT_REVERSE) ? -r : r);
return ((plen >= 6) && (mutt_strcasecmp (path + plen - 6, "/inbox") == 0));
}
-int mutt_same_path (const char *a, const char *b)
+/**
+ * mutt_have_common_subpath - check whether two paths have a common subpath.
+ * @param a First path.
+ * @param b Second path.
+ *
+ * @return 1 if a and b have a common subpath, 0 otherwise
+ *
+ * This function checks whether two paths (where the path separator is assumed
+ * to be '/', have a common subpath. Examples:
+ *
+ * /foo/bar/baz and /foo/hey/you do have a common subpath (/foo/)
+ * /apple/banana and /apples/bananas do NOT have a common subpath
+ */
+int mutt_have_common_subpath(const char *a, const char *b)
{
const char *a_end = strrchr (a, '/');
const char *b_end = strrchr (b, '/');
/* Compare the paths */
size_t a_len = a_end - a;
- return ((a_len == (b_end - b)) && (mutt_strncasecmp (a, b, a_len) == 0));
+ size_t b_len = b_end - b;
+ size_t min = MIN(a_len, b_len);
+ return (a[min] == '/') && (b[min] == '/') &&
+ (mutt_strncasecmp(a, b, min) == 0);
}
char * strfcpy (char *dest, const char *src, size_t dlen)
void safe_free (void *);
void safe_realloc (void *, size_t);
int mutt_is_inbox (const char *path);
-int mutt_same_path (const char *a, const char *b);
+int mutt_have_common_subpath (const char *a, const char *b);
const char *mutt_strsysexit(int e);
#endif
break;
case SORT_PATH:
{
- int same_path = mutt_same_path (b1->path, b2->path);
- result = (same_path && mutt_is_inbox (b1->path)) ? -1 :
- (same_path && mutt_is_inbox (b2->path)) ? 1 :
+ int common_subpath = mutt_have_common_subpath (b1->path, b2->path);
+ result = (common_subpath && mutt_is_inbox (b1->path)) ? -1 :
+ (common_subpath && mutt_is_inbox (b2->path)) ? 1 :
mutt_strcoll (b1->path, b2->path);
break;
}