virStringFreeList;
virStringJoin;
virStringListLength;
+virStringSortCompare;
+virStringSortRevCompare;
virStringSplit;
virStrncpy;
virStrndup;
/*_syscall2(int, pivot_root, char *, newroot, const char *, oldroot)*/
extern int pivot_root(const char * new_root, const char * put_old);
-static int lxcContainerChildMountSort(const void *a, const void *b)
-{
- const char **sa = (const char**)a;
- const char **sb = (const char**)b;
-
- /* Deliberately reversed args - we need to unmount deepest
- children first */
- return strcmp(*sb, *sa);
-}
-
#ifndef MS_REC
# define MS_REC 16384
#endif
if (mounts)
qsort(mounts, nmounts, sizeof(mounts[0]),
- lxcContainerChildMountSort);
+ virStringSortRevCompare);
ret = 0;
cleanup:
if (mounts)
qsort(mounts, nmounts, sizeof(mounts[0]),
- lxcContainerChildMountSort);
+ virStringSortRevCompare);
for (i = 0; i < nmounts; i++) {
VIR_DEBUG("Bind readonly %s", mounts[i]);
return i;
}
+
+
+/**
+ * virStringSortCompare:
+ *
+ * A comparator function for sorting strings in
+ * normal order with qsort().
+ */
+int virStringSortCompare(const void *a, const void *b)
+{
+ const char **sa = (const char**)a;
+ const char **sb = (const char**)b;
+
+ return strcmp(*sa, *sb);
+}
+
+/**
+ * virStringSortRevCompare:
+ *
+ * A comparator function for sorting strings in
+ * reverse order with qsort().
+ */
+int virStringSortRevCompare(const void *a, const void *b)
+{
+ const char **sa = (const char**)a;
+ const char **sb = (const char**)b;
+
+ return strcmp(*sb, *sa);
+}
virAsprintfInternal(false, 0, NULL, NULL, 0, \
strp, __VA_ARGS__)
+int virStringSortCompare(const void *a, const void *b);
+int virStringSortRevCompare(const void *a, const void *b);
+
#endif /* __VIR_STRING_H__ */
return ret;
}
+
+static int
+testStringSortCompare(const void *opaque ATTRIBUTE_UNUSED)
+{
+ const char *randlist[] = {
+ "tasty", "astro", "goat", "chicken", "turducken",
+ };
+ const char *randrlist[] = {
+ "tasty", "astro", "goat", "chicken", "turducken",
+ };
+ const char *sortlist[] = {
+ "astro", "chicken", "goat", "tasty", "turducken",
+ };
+ const char *sortrlist[] = {
+ "turducken", "tasty", "goat", "chicken", "astro",
+ };
+ int ret = -1;
+ size_t i;
+
+ qsort(randlist, ARRAY_CARDINALITY(randlist), sizeof(randlist[0]),
+ virStringSortCompare);
+ qsort(randrlist, ARRAY_CARDINALITY(randrlist), sizeof(randrlist[0]),
+ virStringSortRevCompare);
+
+ for (i = 0; i < ARRAY_CARDINALITY(randlist); i++) {
+ if (STRNEQ(randlist[i], sortlist[i])) {
+ fprintf(stderr, "sortlist[%zu] '%s' != randlist[%zu] '%s'\n",
+ i, sortlist[i], i, randlist[i]);
+ goto cleanup;
+ }
+ if (STRNEQ(randrlist[i], sortrlist[i])) {
+ fprintf(stderr, "sortrlist[%zu] '%s' != randrlist[%zu] '%s'\n",
+ i, sortrlist[i], i, randrlist[i]);
+ goto cleanup;
+ }
+ }
+
+ ret = 0;
+ cleanup:
+ return ret;
+}
+
+
static int
mymain(void)
{
if (virtTestRun("strdup", testStrndupNegative, NULL) < 0)
ret = -1;
+ if (virtTestRun("virStringSortCompare", testStringSortCompare, NULL) < 0)
+ ret = -1;
+
return ret==0 ? EXIT_SUCCESS : EXIT_FAILURE;
}