/*
- * Copyright (C) 2010, 2012-2013 Joel Rosdahl
+ * Copyright (C) 2010, 2012-2014 Joel Rosdahl
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
CHECK_UNS_EQ(2, common_dir_prefix_length("/a", "/a/b"));
CHECK_UNS_EQ(2, common_dir_prefix_length("/a/b", "/a/c"));
CHECK_UNS_EQ(4, common_dir_prefix_length("/a/b", "/a/b"));
+ CHECK_INT_EQ(2, common_dir_prefix_length("/a/bc", "/a/b"));
+ CHECK_INT_EQ(2, common_dir_prefix_length("/a/b", "/a/bc"));
}
TEST(get_relative_path)
/*
* Copyright (C) 2002 Andrew Tridgell
- * Copyright (C) 2009-2013 Joel Rosdahl
+ * Copyright (C) 2009-2014 Joel Rosdahl
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
++p1;
++p2;
}
- if (*p2 == '/') {
- /* s2 starts with "s1/". */
- return p1 - s1;
- }
- if (!*p2) {
- /* s2 is equal to s1. */
- if (p2 == s2 + 1) {
- /* Special case for s1 and s2 both being "/". */
- return 0;
- } else {
- return p1 - s1;
- }
- }
- /* Compute the common directory prefix */
- while (p1 > s1 && *p1 != '/') {
+ while ((*p1 && *p1 != '/') || (*p2 && *p2 != '/')) {
p1--;
p2--;
}
+ if (!*p1 && !*p2 && p2 == s2 + 1) {
+ /* Special case for s1 and s2 both being "/". */
+ return 0;
+ }
return p1 - s1;
}