After finding a way to determine the file stream file type, I found that the unix command uses libmagic, and I'm trying to use the library myself, but I cannot get it to work. I rarely integrated third-party code into my own, so probably most of my problem as well.
Why:
I do this because I have a portable application for viewing a graphic image, which will have to determine the types of archive files (rar, zip, more?) From a given file name, and then the types of image files inside. I hope I can use libmagic for Windows and Linux (and Mac), so if that is not the case, stop me now b / c I need to find something else.
Attempt
I found someone doing something similar , but I canโt keep track of what they are doing, and I have no idea how to compile / run anything at all to start a mess.
My first instinct was to do something like:
// fileTypeTest.cpp, placed in file-5.03/src/ (source from link above) #include <stdio.h> #include "magic.h" int main() { magic_t myt = magic_open(MAGIC_CONTINUE|MAGIC_ERROR/*|MAGIC_DEBUG*/|MAGIC_MIME); magic_load(myt,NULL); printf("magic output: '%s'\n",magic_file(myt,__FILE__)); magic_close(myt); return 0; }
then compile something like:
$ gcc magic.c -o magic.o $ g++ fileTypeTest.cpp -o fileTypeTest magic.o
which (obviously?) doesn't work. I donโt even know where to start, what questions to ask, or if this is the right direction to solve my initial problem in the first place.
Edit: now I have
#include <stdio.h> #include <magic.h> int main(int argc, char* argv[]) { if (argc != 2) { printf("bad arguments"); return 0; } magic_t myt = magic_open(MAGIC_CONTINUE|MAGIC_ERROR/*|MAGIC_DEBUG*/|MAGIC_MIME); magic_load(myt,NULL); printf("magic output: '%s'\n", magic_file(myt, argv[1])); magic_close(myt); return 0; }
compilation with:
$ g++ -L/usr/lib -libmagic fileTypeTest.cpp -o fileTypeTest
working. I had to go to synaptic and install libmagic-dev. I will need to check if I can just copy the /usr/lib/libmagic.a file to the source directory when compiling my application in Windows MingW, but that will be on a different issue later, I suppose.