[libvirt PATCH v2 03/20] commandhelper: Simplify envsort

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



This saves two invocations of each `strndup` and `free`.

Signed-off-by: Tim Wiederhake <twiederh@xxxxxxxxxx>
---
 tests/commandhelper.c | 31 +++++++++++++------------------
 1 file changed, 13 insertions(+), 18 deletions(-)

diff --git a/tests/commandhelper.c b/tests/commandhelper.c
index 2b937979c0..22835302b8 100644
--- a/tests/commandhelper.c
+++ b/tests/commandhelper.c
@@ -37,24 +37,19 @@ extern char **environ;
 
 static int envsort(const void *a, const void *b)
 {
-    const char *const*astrptr = a;
-    const char *const*bstrptr = b;
-    const char *astr = *astrptr;
-    const char *bstr = *bstrptr;
-    char *aeq = strchr(astr, '=');
-    char *beq = strchr(bstr, '=');
-    char *akey;
-    char *bkey;
-    int ret;
-
-    if (!(akey = strndup(astr, aeq - astr)))
-        abort();
-    if (!(bkey = strndup(bstr, beq - bstr)))
-        abort();
-    ret = strcmp(akey, bkey);
-    free(akey);
-    free(bkey);
-    return ret;
+    const char *astr = *(const char**)a;
+    const char *bstr = *(const char**)b;
+
+    while (true) {
+        char achar = (*astr == '=') ? '\0' : *astr;
+        char bchar = (*bstr == '=') ? '\0' : *bstr;
+
+        if ((achar == '\0') || (achar != bchar))
+            return achar - bchar;
+
+        astr++;
+        bstr++;
+    }
 }
 
 int main(int argc, char **argv) {
-- 
2.26.2




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]

  Powered by Linux