From 4930cda4c9156deb1888ed299fd41fccce921070 Mon Sep 17 00:00:00 2001 From: Thierry Vignaud Date: Sun, 25 Aug 2002 22:47:22 +0000 Subject: - kill last fseek - change if(cdt) cascade into if(!cdt) continue - move loop invariant out of loop --- pci.c | 47 +++++++++++++++++++++-------------------------- 1 file changed, 21 insertions(+), 26 deletions(-) diff --git a/pci.c b/pci.c index 05d1c40..662caf8 100644 --- a/pci.c +++ b/pci.c @@ -10,11 +10,13 @@ char *proc_pci_path = "/proc/bus/pci/devices"; extern struct pciusb_entries pci_probe(int probe_type) { - FILE *f; + FILE *f, *devf; char buf[BUF_SIZE]; + unsigned short *bufi = (unsigned short *) &buf; unsigned short devbusfn; unsigned int id; struct pciusb_entries r; + char file[25]; r.nb = 0; if (!(f = fopen(proc_pci_path, "r"))) { @@ -31,6 +33,7 @@ extern struct pciusb_entries pci_probe(int probe_type) { for (; fgets(buf, sizeof(buf) - 1, f) && r.nb < MAX_DEVICES; r.nb++) { struct pciusb_entry *e = &r.entries[r.nb]; + unsigned char class_prog = 0; pciusb_initialize(e); sscanf(buf, "%hx %x", &devbusfn, &id); @@ -39,33 +42,25 @@ extern struct pciusb_entries pci_probe(int probe_type) { e->pci_bus = devbusfn >> 8; e->pci_device = (devbusfn & 0xff) >> 3; e->pci_function = (devbusfn & 0xff) & 0x07; - if (probe_type == 1) { - char file[25]; - FILE *f; - snprintf(file, sizeof(file), "/proc/bus/pci/%02x/%02x.%d", e->pci_bus, e->pci_device, e->pci_function); - if ((f = fopen(file, "r"))) { - unsigned short *bufi = (unsigned short *) &buf; - fread(&buf, 0x30, 1, f); /* these files're 256 bytes but we only need first 48 bytes*/ - e->class_ = bufi[5]; - e->subvendor = bufi[0x16]; - e->subdevice = bufi[0x17]; + if (probe_type != 1) + continue; + snprintf(file, sizeof(file), "/proc/bus/pci/%02x/%02x.%d", e->pci_bus, e->pci_device, e->pci_function); + if ((devf = fopen(file, "r")) == NULL) + continue; + fread(&buf, 0x30, 1, devf); /* these files're 256 bytes but we only need first 48 bytes*/ + e->class_ = bufi[0x5]; + e->subvendor = bufi[0x16]; + e->subdevice = bufi[0x17]; - if ((e->subvendor == 0 && e->subdevice == 0) || - (e->subvendor == e->vendor && e->subdevice == e->device)) { - e->subvendor = 0xffff; - e->subdevice = 0xffff; - } - - if (fseek(f, 9, SEEK_SET) == 0) { - unsigned char class_prog = 0; - fread(&class_prog, 1, 1, f); - - if (e->class_ == PCI_CLASS_SERIAL_USB) /* taken from kudzu's pci.c */ - e->module = strdup(class_prog == 0 ? "usb-uhci" : "usb-ohci"); - } - fclose(f); - } + if ((e->subvendor == 0 && e->subdevice == 0) || + (e->subvendor == e->vendor && e->subdevice == e->device)) { + e->subvendor = 0xffff; + e->subdevice = 0xffff; } + class_prog = bufi[0x9]; + if (e->class_ == PCI_CLASS_SERIAL_USB) /* taken from kudzu's pci.c */ + e->module = strdup(class_prog == 0 ? "usb-uhci" : "usb-ohci"); + fclose(devf); } fclose(f); realloc(r.entries, sizeof(struct pciusb_entry) * r.nb); -- cgit v1.2.1