utils: fix segfault in iwinfo_hardware_id_from_mtd()
[project/iwinfo.git] / iwinfo_utils.c
index 4a82433..3b993d1 100644 (file)
@@ -24,6 +24,7 @@
 
 
 static int ioctl_socket = -1;
+struct uci_context *uci_ctx = NULL;
 
 static int iwinfo_ioctl_socket(void)
 {
@@ -176,13 +177,14 @@ int iwinfo_hardware_id_from_mtd(struct iwinfo_hardware_id *id)
        FILE *mtd;
        uint16_t *bc;
 
-       int fd, len, off;
+       int fd, off;
+       unsigned int len;
        char buf[128];
 
        if (!(mtd = fopen("/proc/mtd", "r")))
                return -1;
 
-       while (fgets(buf, sizeof(buf), mtd) > 0)
+       while (fgets(buf, sizeof(buf), mtd) != NULL)
        {
                if (fscanf(mtd, "mtd%d: %x %*x %127s", &off, &len, buf) < 3 ||
                    (strcmp(buf, "\"boardconfig\"") && strcmp(buf, "\"EEPROM\"") &&
@@ -245,7 +247,7 @@ int iwinfo_hardware_id_from_mtd(struct iwinfo_hardware_id *id)
                                id->subsystem_vendor_id = 0x1814;
 
                                /* device */
-                               if (bc[off] & 0xf0 == 0x30)
+                               if ((bc[off] & 0xf0) == 0x30)
                                        id->device_id = (bc[off] >> 8) | (bc[off] & 0x00ff) << 8;
                                else
                                        id->device_id = bc[off];
@@ -365,3 +367,40 @@ void iwinfo_parse_rsn(struct iwinfo_crypto_entry *c, uint8_t *data, uint8_t len,
        data += 2 + (count * 4);
        len -= 2 + (count * 4);
 }
+
+struct uci_section *iwinfo_uci_get_radio(const char *name, const char *type)
+{
+       struct uci_ptr ptr = {
+               .package = "wireless",
+               .section = name,
+               .flags = (name && *name == '@') ? UCI_LOOKUP_EXTENDED : 0,
+       };
+       const char *opt;
+
+       if (!uci_ctx) {
+               uci_ctx = uci_alloc_context();
+               if (!uci_ctx)
+                       return NULL;
+       }
+
+       if (uci_lookup_ptr(uci_ctx, &ptr, NULL, true))
+               return NULL;
+
+       if (!ptr.s || strcmp(ptr.s->type, "wifi-device") != 0)
+               return NULL;
+
+       opt = uci_lookup_option_string(uci_ctx, ptr.s, "type");
+       if (!opt || strcmp(opt, type) != 0)
+               return NULL;
+
+       return ptr.s;
+}
+
+void iwinfo_uci_free(void)
+{
+       if (!uci_ctx)
+               return;
+
+       uci_free_context(uci_ctx);
+       uci_ctx = NULL;
+}