]> mj.ucw.cz Git - pciutils.git/commitdiff
libpci: For PCI_OS_WINDOWS allows to load pci.ids from executable directory
authorPali Rohár <pali@kernel.org>
Sun, 26 Dec 2021 18:06:53 +0000 (19:06 +0100)
committerMartin Mareš <mj@ucw.cz>
Sun, 26 Dec 2021 21:39:38 +0000 (22:39 +0100)
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
lib/init.c

index 2663b92c76e70ba76ea1f0c06a535b1818d4565f..40f9ff8d454812fabd0357143b76820fd17f6f20 100755 (executable)
@@ -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
index 47cdd6f1c9b9d255c316ab0aa005428410bebaf4..fa06729013b3513f0f1892b228a2d514ec2638a1 100644 (file)
 
 #include "internal.h"
 
+#ifdef PCI_OS_WINDOWS
+#include <windows.h>
+#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");