Skip to content

Commit 2e96d2b

Browse files
committed
utils: simplify the parsing of pci[vendor:device] filters
Signed-off-by: Brice Goglin <[email protected]>
1 parent 103f1fa commit 2e96d2b

File tree

1 file changed

+15
-21
lines changed

1 file changed

+15
-21
lines changed

utils/hwloc/hwloc-calc.h

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -184,27 +184,21 @@ hwloc_calc_parse_level_filter(hwloc_topology_t topology __hwloc_attribute_unused
184184

185185
if (level->type == HWLOC_OBJ_PCI_DEVICE) {
186186
/* try to match by [vendor:device] */
187-
char *endp;
188-
189-
level->pci_vendor = strtoul(current, &endp, 16);
190-
if (*endp != ':') {
191-
fprintf(stderr, "invalid PCI vendor:device filter specification %s\n", filter);
192-
return -1;
193-
}
194-
if (endp == current)
195-
level->pci_vendor = -1;
196-
current = endp+1;
197-
198-
level->pci_device = strtoul(current, &endp, 16);
199-
if (*endp != ']') {
200-
fprintf(stderr, "invalid PCI vendor:device filter specification %s\n", filter);
201-
return -1;
202-
}
203-
if (endp == current)
204-
level->pci_device = -1;
205-
current = endp+1;
206-
207-
if (*current != ':' && *current != '\0') {
187+
unsigned vendor, device;
188+
if (sscanf(current, "%x:%x]", &vendor, &device) == 2) {
189+
level->pci_vendor = (int) vendor;
190+
level->pci_device = (int) device;
191+
return 0;
192+
} else if (sscanf(current, ":%x]", &device) == 1) {
193+
level->pci_device = (int) device;
194+
return 0;
195+
} else if (sscanf(current, "%x:]", &vendor) == 1) {
196+
level->pci_vendor = (int) vendor;
197+
return 0;
198+
} else if (!strncmp(current, ":]", 2)) {
199+
/* nothing */
200+
return 0;
201+
} else if (strchr(current, ':')) {
208202
fprintf(stderr, "invalid PCI vendor:device filter specification %s\n", filter);
209203
return -1;
210204
}

0 commit comments

Comments
 (0)