summaryrefslogtreecommitdiff
path: root/src/shared/path-util.c
diff options
context:
space:
mode:
authorMichal Schmidt <mschmidt@redhat.com>2015-03-16 21:58:35 +0100
committerMichal Schmidt <mschmidt@redhat.com>2015-03-16 22:01:41 +0100
commit2230852bd9755e1b7bfd1260082471f559b0a005 (patch)
tree939362eed4ff66bb8b32f1791d0ae33840c8254a /src/shared/path-util.c
parent9a3d3aace311e85627115d89e4dfe1d71c01b7e3 (diff)
shared: add path_compare(), an ordering path comparison
... and make path_equal() a simple wrapper around it.
Diffstat (limited to 'src/shared/path-util.c')
-rw-r--r--src/shared/path-util.c37
1 files changed, 27 insertions, 10 deletions
diff --git a/src/shared/path-util.c b/src/shared/path-util.c
index cdc61b1a79..53c0079760 100644
--- a/src/shared/path-util.c
+++ b/src/shared/path-util.c
@@ -400,12 +400,18 @@ char* path_startswith(const char *path, const char *prefix) {
}
}
-bool path_equal(const char *a, const char *b) {
+int path_compare(const char *a, const char *b) {
+ int d;
+
assert(a);
assert(b);
- if ((a[0] == '/') != (b[0] == '/'))
- return false;
+ /* A relative path and an abolute path must not compare as equal.
+ * Which one is sorted before the other does not really matter.
+ * Here a relative path is ordered before an absolute path. */
+ d = (a[0] == '/') - (b[0] == '/');
+ if (d)
+ return d;
for (;;) {
size_t j, k;
@@ -414,25 +420,36 @@ bool path_equal(const char *a, const char *b) {
b += strspn(b, "/");
if (*a == 0 && *b == 0)
- return true;
+ return 0;
- if (*a == 0 || *b == 0)
- return false;
+ /* Order prefixes first: "/foo" before "/foo/bar" */
+ if (*a == 0)
+ return -1;
+ if (*b == 0)
+ return 1;
j = strcspn(a, "/");
k = strcspn(b, "/");
- if (j != k)
- return false;
+ /* Alphabetical sort: "/foo/aaa" before "/foo/b" */
+ d = memcmp(a, b, MIN(j, k));
+ if (d)
+ return (d > 0) - (d < 0); /* sign of d */
- if (memcmp(a, b, j) != 0)
- return false;
+ /* Sort "/foo/a" before "/foo/aaa" */
+ d = (j > k) - (j < k); /* sign of (j - k) */
+ if (d)
+ return d;
a += j;
b += k;
}
}
+bool path_equal(const char *a, const char *b) {
+ return path_compare(a, b) == 0;
+}
+
bool path_equal_or_files_same(const char *a, const char *b) {
return path_equal(a, b) || files_same(a, b) > 0;
}