From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:41837) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WgDFY-0003mj-0e for qemu-devel@nongnu.org; Fri, 02 May 2014 09:11:41 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1WgDFS-0004Cb-2k for qemu-devel@nongnu.org; Fri, 02 May 2014 09:11:35 -0400 Received: from mail-qc0-f177.google.com ([209.85.216.177]:60573) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WgDFR-0004BP-Ux for qemu-devel@nongnu.org; Fri, 02 May 2014 09:11:30 -0400 Received: by mail-qc0-f177.google.com with SMTP id i17so3922625qcy.22 for ; Fri, 02 May 2014 06:11:29 -0700 (PDT) From: Mike Day Date: Fri, 2 May 2014 09:11:15 -0400 Message-Id: <1399036275-9219-1-git-send-email-ncmike@ncultra.org> Subject: [Qemu-devel] [PATCH] qemu-img: sort block formats in help message List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: kwolf@redhat.com, Mike Day , stefanha@redhat.com The help message for qemu-img lists the supported block formats, of which there are 27 as of version 2.0.50. The formats are printed in the order of their driver's position in a linked list, which appears random. This patch prints the formats in sorted order, making it easier to read and to find a specific format in the list. Signed-off-by: Mike Day --- qemu-img.c | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/qemu-img.c b/qemu-img.c index 96f4463..d8b7ef4 100644 --- a/qemu-img.c +++ b/qemu-img.c @@ -32,6 +32,7 @@ #include "block/block_int.h" #include "block/qapi.h" #include +#include #define QEMU_IMG_VERSION "qemu-img version " QEMU_VERSION \ ", Copyright (c) 2004-2008 Fabrice Bellard\n" @@ -60,6 +61,32 @@ static void format_print(void *opaque, const char *name) printf(" %s", name); } +static gint compare_data(gconstpointer a, gconstpointer b, gpointer user) +{ + return g_strcmp0((const char *)a, (const char *)b); +} + +static void GFunc_print_format(gpointer data, gpointer user) +{ + format_print(user, data); +} + +static GSequence *init_sequence(void) +{ + return g_sequence_new(NULL); +} + +static void add_format_to_seq(void *opaque, const char *fmt_name) +{ + GSequence *seq = (GSequence *)opaque; + + if (!g_sequence_lookup(seq, (gpointer)fmt_name, + compare_data, NULL)) { + g_sequence_insert_sorted(seq, (gpointer)fmt_name, + compare_data, NULL); + } +} + static void QEMU_NORETURN GCC_FMT_ATTR(1, 2) error_exit(const char *fmt, ...) { va_list ap; @@ -144,8 +171,12 @@ static void QEMU_NORETURN help(void) " '-s' run in Strict mode - fail on different image size or sector allocation\n"; printf("%s\nSupported formats:", help_msg); - bdrv_iterate_format(format_print, NULL); + GSequence *seq = init_sequence(); + bdrv_iterate_format(add_format_to_seq, seq); + g_sequence_foreach(seq, GFunc_print_format, NULL); printf("\n"); + g_sequence_free(seq); + exit(EXIT_SUCCESS); } -- 1.9.0