From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:59757) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dwnhI-00054e-CM for qemu-devel@nongnu.org; Tue, 26 Sep 2017 07:06:45 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dwnhC-0001Hj-DD for qemu-devel@nongnu.org; Tue, 26 Sep 2017 07:06:40 -0400 Received: from mx1.redhat.com ([209.132.183.28]:35146) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dwnhC-0001HJ-6Y for qemu-devel@nongnu.org; Tue, 26 Sep 2017 07:06:34 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 24B3E81DE5 for ; Tue, 26 Sep 2017 11:06:33 +0000 (UTC) From: Gerd Hoffmann Date: Tue, 26 Sep 2017 13:06:27 +0200 Message-Id: <20170926110628.2705-2-kraxel@redhat.com> In-Reply-To: <20170926110628.2705-1-kraxel@redhat.com> References: <20170926110628.2705-1-kraxel@redhat.com> Subject: [Qemu-devel] [PULL 1/2] add qemu_add_data_dir() List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: Gerd Hoffmann , Paolo Bonzini Add helper function to add a directory to the qemu search path, so we don't duplicate the checks. Add a check for duplicate entries, so we stop trying to open files twice. Signed-off-by: Gerd Hoffmann Message-id: 20170914114236.25343-2-kraxel@redhat.com --- vl.c | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/vl.c b/vl.c index 4fd01fda91..19633423aa 100644 --- a/vl.c +++ b/vl.c @@ -2348,6 +2348,24 @@ char *qemu_find_file(int type, const char *name) return NULL; } +static void qemu_add_data_dir(const char *path) +{ + int i; + + if (path == NULL) { + return; + } + if (data_dir_idx == ARRAY_SIZE(data_dir)) { + return; + } + for (i = 0; i < data_dir_idx; i++) { + if (strcmp(data_dir[i], path) == 0) { + return; /* duplicate */ + } + } + data_dir[data_dir_idx++] = path; +} + static inline bool nonempty_str(const char *str) { return str && *str; @@ -3527,8 +3545,8 @@ int main(int argc, char **argv, char **envp) case QEMU_OPTION_L: if (is_help_option(optarg)) { list_data_dirs = true; - } else if (data_dir_idx < ARRAY_SIZE(data_dir)) { - data_dir[data_dir_idx++] = optarg; + } else { + qemu_add_data_dir(optarg); } break; case QEMU_OPTION_bios: @@ -4293,16 +4311,10 @@ int main(int argc, char **argv, char **envp) /* If no data_dir is specified then try to find it relative to the executable path. */ - if (data_dir_idx < ARRAY_SIZE(data_dir)) { - data_dir[data_dir_idx] = os_find_datadir(); - if (data_dir[data_dir_idx] != NULL) { - data_dir_idx++; - } - } + qemu_add_data_dir(os_find_datadir()); + /* If all else fails use the install path specified when building. */ - if (data_dir_idx < ARRAY_SIZE(data_dir)) { - data_dir[data_dir_idx++] = CONFIG_QEMU_DATADIR; - } + qemu_add_data_dir(CONFIG_QEMU_DATADIR); /* -L help lists the data directories and exits. */ if (list_data_dirs) { -- 2.9.3