title: Specify mtd:name instead of /dev/mtd# in call to open
body: <p>You can specify an MTD device by name on the kernel command line <code>root=mtd:name</code> and you can mount with a name <code>mount mtd:name</code>. I want my app to be able to do the same thing. It has to call open, but <code>open("mtd:name", ...)</code> fails with No such file or directory.</p>
  
  <p>How do you get from 'mtd:name' to '/dev/mtd#'?</p>
  
  <p><strong>Updated</strong></p>
  
  <p>This is the code I came up with in case it's useful to someone else.</p>
  
  <pre><code>int mtd_lookup_minor(const char * mtdname)
  {
      int minor;
      unsigned int size, erasesize;
      char name[65];
      char line[128];
  
      if (mtdname == NULL)
      {
          errno = EINVAL;
          return -1;
      }
  
      FILE * fp = fopen("/proc/mtd", "r");
      if (fp == NULL)
          return -1;
  
      while (fgets(line, sizeof(line), fp))
      {
          if (sscanf(line, "mtd%d: %x %x \"%64[^\"]\"", &amp;minor, &amp;size, &amp;erasesize, name) == 4 &amp;&amp; strcmp(name, mtdname) == 0)
          {
              fclose(fp);
              return minor;
          }
      }
  
      fclose(fp);
      errno = ENODEV;
      return -1;
  }
  
  /*
   * mtd_open_char allows specifying "mtd:name" instead of /dev/mtd#
   *  to refer to the MTD char device by name.
   */
  int mtd_open_char(const char * mtdpath, int oflag)
  {
      int minor;
      char pathbuffer[16];
  
      if (mtdpath == NULL)
      {
          errno = EINVAL;
          return -1;
      }
  
      if (strlen(mtdpath) &gt; 4 &amp;&amp; strncmp(mtdpath, "mtd:", 4) == 0)
      {
          minor = mtd_lookup_minor(mtdpath + 4);
          if (minor &lt; 0 || minor &gt; 9999999) // paranoia fit into pathbuffer
          {
              errno = ENODEV;
              return -1;
          }
  
          sprintf(pathbuffer, "/dev/mtd%d", minor);
          mtdpath = pathbuffer;
      }
  
      return open(mtdpath, oflag);
  }
  </code></pre>