From 62cacab6b2e7097786064157b99981811810d432 Mon Sep 17 00:00:00 2001 From: Kay Sievers Date: Thu, 26 Feb 2009 21:56:37 +0100 Subject: usb-db: also handle the passed device, not only the parents --- usb-db/usb-db.c | 37 +++++++++++++++++++++++++++---------- 1 file changed, 27 insertions(+), 10 deletions(-) (limited to 'usb-db') diff --git a/usb-db/usb-db.c b/usb-db/usb-db.c index b53e4e5..db1f843 100644 --- a/usb-db/usb-db.c +++ b/usb-db/usb-db.c @@ -172,7 +172,6 @@ static int lookup_vid_pid( finish: free(line); - fclose(f); if (ret < 0) { @@ -185,6 +184,28 @@ finish: return ret; } +static struct udev_device *find_device(struct udev_device *dev, const char *subsys, const char *devtype) +{ + const char *str; + + str = udev_device_get_subsystem(dev); + if (str == NULL) + goto try_parent; + if (strcmp(str, subsys) != 0) + goto try_parent; + + if (devtype != NULL) { + str = udev_device_get_devtype(dev); + if (str == NULL) + goto try_parent; + if (strcmp(str, devtype) != 0) + goto try_parent; + } + return dev; +try_parent: + return udev_device_get_parent_with_subsystem_devtype(dev, SUBSYSTEM, DEVTYPE); +} + int main(int argc, char*argv[]) { struct udev *udev = NULL; @@ -215,8 +236,9 @@ int main(int argc, char*argv[]) { goto finish; } - if (!(parent = udev_device_get_parent_with_subsystem_devtype(dev, SUBSYSTEM, DEVTYPE))) { - fprintf(stderr, "Failed to find parent device.\n"); + parent = find_device(dev, SUBSYSTEM, DEVTYPE); + if (!parent) { + fprintf(stderr, "Failed to find device.\n"); goto finish; } @@ -235,15 +257,10 @@ int main(int argc, char*argv[]) { ret = 0; finish: - if (dev) - udev_device_unref(dev); - - if (udev) - udev_unref(udev); - + udev_device_unref(dev); + udev_unref(udev); free(vendor); free(product); return ret; - } -- cgit