title: Detecting file MIME in C
body: <p>I have files with wrong extensions, and try to find the correct <code>MIME</code> in a C script.</p>
  <p>For a PDF file with txt extension, <code>magic</code> (<code>#include &lt;magic.h&gt;</code>)</p>
  <pre><code>  const char *mime;
    magic_t magic;
    magic = magic_open(MAGIC_MIME_TYPE); 
    magic_load(magic, NULL);
    magic_compile(magic, NULL);
    mime = magic_file(magic, filename);
    printf(&quot;%s\n&quot;, mime);
    magic_close(magic);
  </code></pre>
  <p>returned</p>
  <pre><code>application/octet-stream
  </code></pre>
  <p>which is not very helpful.</p>
  <p><code>GIO 2.0</code> (<code>#include &lt;gio/gio.h&gt;</code>)</p>
  <pre><code>  char *content_type = g_content_type_guess (file_name, NULL, 0, &amp;is_certain);
  
    if (content_type != NULL)
      {
        char *mime_type = g_content_type_get_mime_type (content_type);
  
        g_print (&quot;Content type for file '%s': %s (certain: %s)\n&quot;
                 &quot;MIME type for content type: %s\n&quot;,
                 file_name,
                 content_type,
                 is_certain ? &quot;yes&quot; : &quot;no&quot;,
                 mime_type);
  
        g_free (mime_type);
      }
  </code></pre>
  <p>returned</p>
  <pre><code>Content type for file 'test.txt': text/plain (certain: no)
  MIME type for content type: text/plain
  </code></pre>
  <p>However, <code>file</code> command in Linux returns the correct MIME:</p>
  <pre><code>file test.txt
  test.txt: PDF document, version 1.6
  </code></pre>
  <p>This should not be the expected behaviors of these well-established libraries in C. What do I do wrong?</p>