grub-devel
[Top][All Lists]
Advanced

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

[PATCH] add grub_qsort_strcmp to use when sorting array of strings


From: Andrey Borzenkov
Subject: [PATCH] add grub_qsort_strcmp to use when sorting array of strings
Date: Fri, 29 Nov 2013 21:01:00 +0400

Compare function used in qsort gets arguments by reference, so strcmp
cannot be used directly - it expects pointer to char, but gets pointer
to pointer to char.

Introduce new helper grub_qsort_strcmp and use it in grub-install.
This helper is going to be used in a couple more places as well so
add it to global file, not in grub-install.c.

---
 include/grub/util/misc.h | 2 ++
 util/grub-install.c      | 2 +-
 util/misc.c              | 8 ++++++++
 3 files changed, 11 insertions(+), 1 deletion(-)

diff --git a/include/grub/util/misc.h b/include/grub/util/misc.h
index 6aacf23..192874d 100644
--- a/include/grub/util/misc.h
+++ b/include/grub/util/misc.h
@@ -47,4 +47,6 @@ void grub_util_init_nls (void);
 
 void grub_util_host_init (int *argc, char ***argv);
 
+int grub_qsort_strcmp (const void *, const void *);
+
 #endif /* ! GRUB_UTIL_MISC_HEADER */
diff --git a/util/grub-install.c b/util/grub-install.c
index 5354a6d..1870182 100644
--- a/util/grub-install.c
+++ b/util/grub-install.c
@@ -583,7 +583,7 @@ device_map_check_duplicates (const char *dev_map)
 
   fclose (fp);
 
-  qsort (d, filled, sizeof (d[0]), (int (*) (const void *, const void 
*))strcmp);
+  qsort (d, filled, sizeof (d[0]), grub_qsort_strcmp);
 
   for (i = 0; i + 1 < filled; i++)
     if (strcmp (d[i], d[i+1]) == 0)
diff --git a/util/misc.c b/util/misc.c
index 0de340b..9eb1fc1 100644
--- a/util/misc.c
+++ b/util/misc.c
@@ -256,3 +256,11 @@ void
 grub_register_exported_symbols (void)
 {
 }
+
+/* Used in comparison of arrays of strings with qsort */
+int
+grub_qsort_strcmp (const void *p1, const void *p2)
+{
+  return strcmp(*(char **)p1, *(char **)p2);
+}
+
-- 
tg: (69ca97c..) u/qsort-fix (depends on: master)



reply via email to

[Prev in Thread] Current Thread [Next in Thread]