Skip to content

Commit 324ae73

Browse files
Carlos Santosopcm
authored andcommitted
Look for pci.ids at a default path on Unix-like systems
On FreeBSD, try /usr/local/share/pciids/pci.ids (from "pciids" package). On Linux, try /usr/share/hwdata/pci.ids (from "hwdata" package). If the file is not found at the default path, try the current directory. Signed-off-by: Carlos Santos <[email protected]>
1 parent b291be1 commit 324ae73

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

lspci.h

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,25 @@
44
#include <vector>
55
#include <fstream>
66
#include "cpucounters.h"
7+
8+
#if defined(_MSC_VER)
9+
#define PCI_IDS_PATH "pci.ids"
10+
#define PCI_IDS_NOT_FOUND "pci.ids file is not available. Download it from" \
11+
" https://raw.githubusercontent.com/pciutils/pciids/master/pci.ids."
12+
#elif defined (__FreeBSD__) || defined(__DragonFly__)
13+
#define PCI_IDS_PATH "/usr/local/share/pciids/pci.ids"
14+
#define PCI_IDS_NOT_FOUND "/usr/local/share/pciids/pci.ids file is not available." \
15+
" Ensure that the \"pciids\" package is properly installed or download" \
16+
" https://raw.githubusercontent.com/pciutils/pciids/master/pci.ids and" \
17+
" copy it to the current directory."
18+
#else
19+
#define PCI_IDS_PATH "/usr/share/hwdata/pci.ids"
20+
#define PCI_IDS_NOT_FOUND "/usr/share/hwdata/pci.ids file is not available." \
21+
" Ensure that the \"hwdata\" package is properly installed or download" \
22+
" https://raw.githubusercontent.com/pciutils/pciids/master/pci.ids and" \
23+
" copy it to the current directory."
24+
#endif
25+
726
using namespace std;
827
typedef uint32_t h_id;
928
typedef uint32_t v_id;
@@ -179,12 +198,20 @@ void print_pci(struct pci p, const PCIDB & pciDB)
179198

180199
void load_PCIDB(PCIDB & pciDB)
181200
{
182-
std::ifstream in("pci.ids");
201+
std::ifstream in(PCI_IDS_PATH);
183202
std::string line, item;
184203

185204
if (!in.is_open())
186205
{
187-
std::cerr << "pci.ids file is not available. Download it from https://raw.githubusercontent.com/pciutils/pciids/master/pci.ids " << endl;
206+
#ifndef _MSC_VER
207+
// On Unix, try the current directory if the default path failed
208+
in.open("pci.ids");
209+
}
210+
211+
if (!in.is_open())
212+
{
213+
#endif
214+
std::cerr << PCI_IDS_NOT_FOUND << endl;
188215
return;
189216
}
190217

0 commit comments

Comments
 (0)