{"id":2666,"date":"2022-09-23T02:10:22","date_gmt":"2022-09-22T18:10:22","guid":{"rendered":"https:\/\/guerilla.ddns.net\/wp\/?p=2666"},"modified":"2025-08-24T22:46:41","modified_gmt":"2025-08-24T14:46:41","slug":"libmagic-library","status":"publish","type":"post","link":"https:\/\/guerilla.ddns.net\/wp\/?p=2666","title":{"rendered":"libmagic library"},"content":{"rendered":"<p>Libmagic is the library that commonly supports the file command on Unix system, other than Max OSX which has its own implementation. The library handles the loading of database files that describe the magic numbers used to identify various file types, as well as the associated mime types. The library also handles character set detections.<\/p>\n<h2>Installation<\/h2>\n<p>Ubuntu<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">sudo apt-get install libmagic-dev<\/code><\/pre>\n<p>&nbsp;<br \/>\nFedora<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">sudo yum install file-devel<\/code><\/pre>\n<p>&nbsp;<br \/>\nopenSUSE<\/p>\n<pre><code class=\"language-bash\" data-line=\"\">sudo zypper install file-devel<\/code><\/pre>\n<p>&nbsp;<br \/>\n&nbsp;<\/p>\n<h2>Example<\/h2>\n<p>test.c<\/p>\n<pre><code class=\"language-c\" data-line=\"\">#include &lt;stdio.h&gt;\n#include &lt;magic.h&gt;\n\nint main(void)\n{\n\u00a0 \u00a0 char *actual_file = &quot;\/home\/test\/Downloads\/image.jpg&quot;;\n\u00a0 \u00a0 const char *magic_full;\n\u00a0 \u00a0 magic_t magic_cookie;\n\n\u00a0 \u00a0 \/* MAGIC_MIME tells magic to return a mime of the file,\n\u00a0 \u00a0 \u00a0 but you can specify different things\u00a0 \u00a0 *\/\n\u00a0 \u00a0 magic_cookie = magic_open(MAGIC_MIME);\n\n\u00a0 \u00a0 if (magic_cookie == NULL)\n\u00a0 \u00a0 {\n\u00a0 \u00a0 \u00a0 \u00a0 printf(&quot;unable to initialize magic library\\n&quot;);\n\u00a0 \u00a0 \u00a0 \u00a0 return 1;\n\u00a0 \u00a0 }\n\n\u00a0 \u00a0 printf(&quot;Loading default magic database\\n&quot;);\n\n\u00a0 \u00a0 if (magic_load(magic_cookie, NULL) != 0)\n\u00a0 \u00a0 {\n\u00a0 \u00a0 \u00a0 \u00a0 printf(&quot;cannot load magic database - %s\\n&quot;, magic_error(magic_cookie));\n\u00a0 \u00a0 \u00a0 \u00a0 magic_close(magic_cookie);\n\u00a0 \u00a0 \u00a0 \u00a0 return 1;\n\u00a0 \u00a0 }\n\n\u00a0 \u00a0 magic_full = magic_file(magic_cookie, actual_file);\n\u00a0 \u00a0 printf(&quot;%s\\n&quot;, magic_full);\n\u00a0 \u00a0 magic_close(magic_cookie);\n\u00a0 \u00a0 return 0;\n}<\/code><\/pre>\n<p>&nbsp;<br \/>\n<strong>Build the code<\/strong><\/p>\n<pre><code class=\"language-bash\" data-line=\"\">gcc -g test.c -lmagic<\/code><\/pre>\n<p>&nbsp;<br \/>\n<strong>Run<\/strong><\/p>\n<pre><code class=\"language-bash\" data-line=\"\">.\/a.out<\/code><\/pre>\n<pre>Loading default magic database\r\nimage\/jpeg; charset=binary<\/pre>\n<p>&nbsp;<\/p>\n<p>ref:<br \/>\n\u2022 <a href=\"https:\/\/www.quora.com\/What-is-libmagic-in-Linux\" target=\"_blank\" rel=\"noopener\"><em>What is libmagic in Linux?<\/em><\/a><br \/>\n\u2022 <a href=\"https:\/\/www.commandlinux.com\/man-page\/man5\/magic.5.html\" target=\"_blank\" rel=\"noopener\"><em>libmagic linux command man page<\/em><\/a><br \/>\n\u2022 <a href=\"https:\/\/gist.github.com\/vivithemage\/9489378\" target=\"_blank\" rel=\"noopener\"><em>libmagic.h example<\/em><\/a><br \/>\n&nbsp;<\/p>\n<p>#ubuntu #fedora #opensuse<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Libmagic is the library that commonly supports the file [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[212],"tags":[27,63,94],"class_list":["post-2666","post","type-post","status-publish","format-standard","hentry","category-coding","tag-coding","tag-linux","tag-programming"],"_links":{"self":[{"href":"https:\/\/guerilla.ddns.net\/wp\/index.php?rest_route=\/wp\/v2\/posts\/2666","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/guerilla.ddns.net\/wp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/guerilla.ddns.net\/wp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/guerilla.ddns.net\/wp\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/guerilla.ddns.net\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2666"}],"version-history":[{"count":14,"href":"https:\/\/guerilla.ddns.net\/wp\/index.php?rest_route=\/wp\/v2\/posts\/2666\/revisions"}],"predecessor-version":[{"id":3470,"href":"https:\/\/guerilla.ddns.net\/wp\/index.php?rest_route=\/wp\/v2\/posts\/2666\/revisions\/3470"}],"wp:attachment":[{"href":"https:\/\/guerilla.ddns.net\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2666"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/guerilla.ddns.net\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2666"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/guerilla.ddns.net\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2666"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}