libaltos: Avoid using strcmp and strchr on windows
authorKeith Packard <keithp@keithp.com>
Wed, 30 Sep 2020 22:53:38 +0000 (15:53 -0700)
committerKeith Packard <keithp@keithp.com>
Wed, 30 Sep 2020 22:53:38 +0000 (15:53 -0700)
At least one of these caused the library to not load on Windows
10. Work around that by changing the code to use vid/pid instead of
matching on the name provided back by the OS.

Signed-off-by: Keith Packard <keithp@keithp.com>
libaltos/libaltos_windows.c

index 162c85bab8fa6aa658479017bb920395fb678e8a..b92df70857fb03b1bbf61220cd91608eb307051d 100644 (file)
@@ -132,12 +132,12 @@ altos_ftdi_list_start(void)
 }
 
 static struct {
-       char    *windows;
-       char    *real;
+       unsigned int    vid, pid;
+       char    *name;
 } name_map[] = {
-       { .windows = "AltusMetrum28", .real = "EasyMega" },
-       { .windows = "AltusMetrum2c", .real = "EasyMotor" },
-       { 0, 0 },
+       { .vid = 0xfffe, .pid = 0x0028, .name = "EasyMega" },
+       { .vid = 0xfffe, .pid = 0x002c, .name = "EasyMotor" },
+       { .name = NULL },
 };
 
 PUBLIC int
@@ -240,15 +240,22 @@ altos_list_next(struct altos_list *list, struct altos_device *device)
                        altos_set_last_windows_error();
                        continue;
                }
-               for (i = 0; name_map[i].windows; i++)
-                       if (!strcmp(name_map[i].windows, friendlyname)) {
-                               strcpy(friendlyname, name_map[i].real);
+
+               char *space = friendlyname;
+               while (*space) {
+                       if (*space == ' ') {
+                               *space = '\0';
                                break;
                        }
+                       space++;
+               }
 
-               char *space = strchr(friendlyname, ' ');
-               if  (space)
-                       *space = '\0';
+               for (i = 0; name_map[i].name; i++) {
+                       if (name_map[i].vid == vid && name_map[i].pid == pid) {
+                               strcpy(friendlyname, name_map[i].name);
+                               break;
+                       }
+               }
 
                device->vendor = vid;
                device->product = pid;