From 2608d8f72d587f5913998e2483c841dc211f9307 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Pali=20Roh=C3=A1r?= Date: Sun, 26 Dec 2021 19:06:53 +0100 Subject: [PATCH] libpci: For PCI_OS_WINDOWS allows to load pci.ids from executable directory For Windows applications it is common to have all support data files in the same directory where is stored executable itself, instead of in directory hardcoded at compile time. When PCI_PATH_IDS_DIR is set to "." it means that pci.ids file is located in the current working directory. This is also unsuitable for Windows command line applications stored in %PATH% because cmd.exe starts in some default user or system location. Adds a new option to allow specifying PCI_PATH_IDS_DIR to empty string "" and for PCI_OS_WINDOWS platform it would mean to locate pci.ids file in the same directory where is stored currently running executable. On Windows it is always possible to detected this directory. --- lib/configure | 2 +- lib/init.c | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/lib/configure b/lib/configure index 2663b92..40f9ff8 100755 --- a/lib/configure +++ b/lib/configure @@ -9,7 +9,7 @@ echo_n() { printf '%s' "$*" } -if [ -z "$VERSION" -o -z "$IDSDIR" ] ; then +if [ -z "$VERSION" ] ; then echo >&2 "Please run the configure script from the top-level Makefile" exit 1 fi diff --git a/lib/init.c b/lib/init.c index 47cdd6f..fa06729 100644 --- a/lib/init.c +++ b/lib/init.c @@ -13,6 +13,10 @@ #include "internal.h" +#ifdef PCI_OS_WINDOWS +#include +#endif + static struct pci_methods *pci_methods[PCI_ACCESS_MAX] = { NULL, #ifdef PCI_HAVE_PM_LINUX_SYSFS @@ -189,7 +193,28 @@ pci_alloc(void) int i; memset(a, 0, sizeof(*a)); +#ifdef PCI_OS_WINDOWS + if ((PCI_PATH_IDS_DIR)[0]) + pci_set_name_list_path(a, PCI_PATH_IDS_DIR "\\" PCI_IDS, 0); + else + { + char *path, *sep; + DWORD len; + + path = pci_malloc(a, MAX_PATH+1); + len = GetModuleFileNameA(NULL, path, MAX_PATH+1); + sep = (len > 0) ? strrchr(path, '\\') : NULL; + if (len == 0 || len == MAX_PATH+1 || !sep || MAX_PATH-(size_t)(sep+1-path) < sizeof(PCI_IDS)) + free(path); + else + { + memcpy(sep+1, PCI_IDS, sizeof(PCI_IDS)); + pci_set_name_list_path(a, path, 1); + } + } +#else pci_set_name_list_path(a, PCI_PATH_IDS_DIR "/" PCI_IDS, 0); +#endif #ifdef PCI_USE_DNS pci_define_param(a, "net.domain", PCI_ID_DOMAIN, "DNS domain used for resolving of ID's"); pci_define_param(a, "net.cache_name", "~/.pciids-cache", "Name of the ID cache file"); -- 2.39.2