Last active
August 26, 2024 02:25
-
-
Save vivithemage/9489378 to your computer and use it in GitHub Desktop.
libmagic.h example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <magic.h> | |
int main(void) | |
{ | |
char *actual_file = "/file/you/want.yay"; | |
const char *magic_full; | |
magic_t magic_cookie; | |
/* MAGIC_MIME tells magic to return a mime of the file, | |
but you can specify different things */ | |
magic_cookie = magic_open(MAGIC_MIME); | |
if (magic_cookie == NULL) { | |
printf("unable to initialize magic library\n"); | |
return 1; | |
} | |
printf("Loading default magic database\n"); | |
if (magic_load(magic_cookie, NULL) != 0) { | |
printf("cannot load magic database - %s\n", magic_error(magic_cookie)); | |
magic_close(magic_cookie); | |
return 1; | |
} | |
magic_full = magic_file(magic_cookie, actual_file); | |
printf("%s\n", magic_full); | |
magic_close(magic_cookie); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
No,
libmagic
is managing its own storage and magic_file returns a pointer into that storage.magic_close()
probably frees the heap.The manpage gives the declaration
const char *magic_file(magic_t cookie, const char *filename);
, theconst
qualifier seems to mean that they retains «ownership» over the data and so the storage.