]> mj.ucw.cz Git - pciutils.git/blob - lib/filter.c
Fixed calls to config_fetch()
[pciutils.git] / lib / filter.c
1 /*
2  *      Linux PCI Library -- Device Filtering
3  *
4  *      Copyright (c) 1998--2003 Martin Mares <mj@ucw.cz>
5  *
6  *      Can be freely distributed and used under the terms of the GNU GPL.
7  */
8
9 #include <stdlib.h>
10 #include <string.h>
11
12 #include "internal.h"
13
14 void
15 pci_filter_init(struct pci_access *a UNUSED, struct pci_filter *f)
16 {
17   f->bus = f->slot = f->func = -1;
18   f->vendor = f->device = -1;
19 }
20
21 /* Slot filter syntax: [[bus]:][slot][.[func]] */
22
23 char *
24 pci_filter_parse_slot(struct pci_filter *f, char *str)
25 {
26   char *colon = strchr(str, ':');
27   char *dot = strchr((colon ? colon + 1 : str), '.');
28   char *mid = str;
29   char *e;
30
31   if (colon)
32     {
33       *colon++ = 0;
34       mid = colon;
35       if (str[0] && strcmp(str, "*"))
36         {
37           long int x = strtol(str, &e, 16);
38           if ((e && *e) || (x < 0 || x > 0xff))
39             return "Invalid bus number";
40           f->bus = x;
41         }
42     }
43   if (dot)
44     *dot++ = 0;
45   if (mid[0] && strcmp(mid, "*"))
46     {
47       long int x = strtol(mid, &e, 16);
48       if ((e && *e) || (x < 0 || x > 0x1f))
49         return "Invalid slot number";
50       f->slot = x;
51     }
52   if (dot && dot[0] && strcmp(dot, "*"))
53     {
54       long int x = strtol(dot, &e, 16);
55       if ((e && *e) || (x < 0 || x > 7))
56         return "Invalid function number";
57       f->func = x;
58     }
59   return NULL;
60 }
61
62 /* ID filter syntax: [vendor]:[device] */
63
64 char *
65 pci_filter_parse_id(struct pci_filter *f, char *str)
66 {
67   char *s, *e;
68
69   if (!*str)
70     return NULL;
71   s = strchr(str, ':');
72   if (!s)
73     return "':' expected";
74   *s++ = 0;
75   if (str[0] && strcmp(str, "*"))
76     {
77       long int x = strtol(str, &e, 16);
78       if ((e && *e) || (x < 0 || x >= 0xffff))
79         return "Invalid vendor ID";
80       f->vendor = x;
81     }
82   if (s[0] && strcmp(s, "*"))
83     {
84       long int x = strtol(s, &e, 16);
85       if ((e && *e) || (x < 0 || x >= 0xffff))
86         return "Invalid device ID";
87       f->device = x;
88     }
89   return NULL;
90 }
91
92 int
93 pci_filter_match(struct pci_filter *f, struct pci_dev *d)
94 {
95   if ((f->bus >= 0 && f->bus != d->bus) ||
96       (f->slot >= 0 && f->slot != d->dev) ||
97       (f->func >= 0 && f->func != d->func))
98     return 0;
99   if (f->device >= 0 || f->vendor >= 0)
100     {
101       pci_fill_info(d, PCI_FILL_IDENT);
102       if ((f->device >= 0 && f->device != d->device_id) ||
103           (f->vendor >= 0 && f->vendor != d->vendor_id))
104         return 0;
105     }
106   return 1;
107 }