mtd: improve mtd detection
[openwrt/openwrt.git] / package / system / mtd / src / mtd.c
index 604ca28ae29e7411739285cf66fc8a1fa97b3cae..7b80193b44c63d2540337d4a1e417147aa5f8d7a 100644 (file)
@@ -67,10 +67,12 @@ int mtd_open(const char *mtd, bool block)
        int i;
        int ret;
        int flags = O_RDWR | O_SYNC;
+       char name[PATH_MAX];
 
+       snprintf(name, sizeof(name), "\"%s\"", mtd);
        if ((fp = fopen("/proc/mtd", "r"))) {
                while (fgets(dev, sizeof(dev), fp)) {
-                       if (sscanf(dev, "mtd%d:", &i) && strstr(dev, mtd)) {
+                       if (sscanf(dev, "mtd%d:", &i) && strstr(dev, name)) {
                                snprintf(dev, sizeof(dev), "/dev/mtd%s/%d", (block ? "block" : ""), i);
                                if ((ret=open(dev, flags))<0) {
                                        snprintf(dev, sizeof(dev), "/dev/mtd%s%d", (block ? "block" : ""), i);