avoid PATH_MAX/string copies usage
authorMike Frysinger <vapier@gentoo.org>
Tue, 29 Dec 2009 08:37:15 +0000 (03:37 -0500)
committerGerd von Egidy <gerd.von.egidy@intra2net.com>
Mon, 4 Jan 2010 09:46:49 +0000 (10:46 +0100)
The new ftdi_usb_open_string() func breaks mingw builds where PATH_MAX is
not defined.  Rather than adding fallback code, compare the path strings
piece by piece to avoid the string copy overhead in the process.

Signed-off-by: Mike Frysinger <vapier@gentoo.org>

src/ftdi.c

index 6068e7e..9740fca 100644 (file)
@@ -715,7 +715,6 @@ int ftdi_usb_open_string(struct ftdi_context *ftdi, const char* description)
     {
         struct usb_bus *bus;
         struct usb_device *dev;
-        char dev_name[PATH_MAX+1];
 
         usb_init();
 
@@ -728,9 +727,18 @@ int ftdi_usb_open_string(struct ftdi_context *ftdi, const char* description)
         {
             for (dev = bus->devices; dev; dev = dev->next)
             {
-                snprintf(dev_name, sizeof(dev_name), "%s/%s",bus->dirname,dev->filename);
-                if (strcmp(description+2,dev_name) == 0)
-                    return ftdi_usb_open_dev(ftdi, dev);
+                /* XXX: This doesn't handle symlinks/odd paths/etc... */
+                const char *desc = description + 2;
+                size_t len = strlen(bus->dirname);
+                if (strncmp(desc, bus->dirname, len))
+                    continue;
+                desc += len;
+                if (desc[0] != '/')
+                    continue;
+                ++desc;
+                if (strcmp(desc, dev->filename))
+                    continue;
+                return ftdi_usb_open_dev(ftdi, dev);
             }
         }