gcc include library

http://stackoverflow.com/questions/27660713/including-header-file-from-static-library

http://stackoverflow.com/questions/6016815/how-to-include-needed-c-library-using-gcc

gcc 不能include当前路径为header的搜索路径

```bash
vagrant@precise64:~/data/libmagic$ gcc -Isrc -o 4 4.c -Lsrc/.libs/ -lmagic
```

```c
#include <stdio.h>
#include "magic.h"

int main(int argc, char **argv){
  const char *mime;
  magic_t magic;

  printf("Getting magic from %s\n", argv[1]);

  //magic = magic_open(MAGIC_MIME|MAGIC_DEVICES|MAGIC_DEBUG); 
  //magic = magic_open(MAGIC_CONTINUE|MAGIC_MIME);
  magic = magic_open(MAGIC_ERROR/*|MAGIC_DEBUG*/|MAGIC_MIME);
  magic_load(magic, NULL);
  magic_compile(magic, NULL);
  mime = magic_file(magic, argv[1]);

  printf("%s\n", mime);
  magic_close(magic);

  return 0;
}
```

```c
#include <stdio.h>
#include "magic.h"

int main(int argc, char **argv){
  const char *mime;
  magic_t magic;

  printf("Getting magic from %d\n", magic_version());
  return 0;
}
```

https://github.com/blackwinter/ruby-filemagic

```c
#include <stdio.h>
#include <stdlib.h>
#include "magic.h"

int main(int argc, char **argv){
  const char *mime;
  magic_t magic;

  printf("Getting magic from %s\n", argv[1]);

	FILE *f = fopen(argv[1], "rb");
	fseek(f, 0, SEEK_END);
	long fsize = ftell(f);
	fseek(f, 0, SEEK_SET);

	char *string = malloc(fsize + 1);
	fread(string, fsize, 1, f);

	string[fsize] = 0;

  magic = magic_open(MAGIC_ERROR/*|MAGIC_DEBUG*/|MAGIC_MIME);
  magic_load(magic, NULL);
  magic_compile(magic, NULL);
  mime = magic_buffer(magic, string, (size_t)fsize);

  printf("%s\n", mime);
  magic_close(magic);

  free(string);
  fclose(f);

	return 0;
}
```

http://stackoverflow.com/questions/51572/determine-file-type-in-ruby


```bash
vagrant@precise64:~/data$ grep -rn "magic_buffer" ruby-filemagic/
ruby-filemagic/test/mahoro.c:90:        if(!(msg = magic_buffer(cookie, StringValuePtr(input),
ruby-filemagic/ext/filemagic/filemagic.h:22:#define RB_MAGIC_TYPE_BUFFER     magic_buffer(ms, StringValuePtr(arg), RSTRING_LEN(arg))
ruby-filemagic/ext/filemagic/filemagic.h:86:static VALUE rb_magic_buffer(int, VALUE*, VALUE);
ruby-filemagic/ext/filemagic/filemagic.c:231:  rb_define_method(cFileMagic, "buffer",     rb_magic_buffer,     -1);
```

ruby-filemagic用的是ruby.h自带的方法 rstring_len stringvalueptr

上面那种magic_buffer的写法遇到大文件会segment fault
用getdelim就不会 
ruby里面File.read遇到大文件也会卡住

http://stackoverflow.com/questions/174531/easiest-way-to-get-files-contents-in-c

```c
#include <stdio.h>
#include <stdlib.h>
#include "magic.h"

int main(int argc, char **argv){
  const char *mime;
  magic_t magic;

  printf("Getting magic from %s\n", argv[1]);

	FILE *f = fopen(argv[1], "rb");
	size_t fsize;
	
	char *string = NULL;
	size_t bytes_read = getdelim(&string, &fsize, '\0', f);

  magic = magic_open(MAGIC_ERROR|MAGIC_CONTINUE|MAGIC_MIME);
  magic_load(magic, NULL);
  magic_compile(magic, NULL);
  mime = magic_buffer(magic, string, fsize);

  printf("%s\n", mime);
  printf("%zu\n", bytes_read);
  magic_close(magic);

  fclose(f);

	return 0;
}
```

MAGIC_CONTINUE 

https://github.com/ahupp/python-magic/pull/28

magic_check

```c
#include <stdio.h>
#include "magic.h"

int main(int argc, char **argv){
  const char *mime;
  magic_t magic;
  int result;

  printf("Getting magic from %s\n", argv[1]);

  //magic = magic_open(MAGIC_MIME|MAGIC_DEVICES|MAGIC_DEBUG); 
  //magic = magic_open(MAGIC_CONTINUE|MAGIC_MIME);
  magic = magic_open(MAGIC_ERROR/*|MAGIC_DEBUG*/|MAGIC_MIME);
  magic_load(magic, NULL);
  magic_compile(magic, NULL);
  result = magic_check(magic, argv[1]);

  printf("%i\n", result);
  magic_close(magic);

  return 0;
}
```

```bash

     -c, --checking-printout
             Cause a checking printout of the parsed form of the magic file.  This is usually used in conjunction with the -m flag to debug a new magic file before
             installing it.

```

magic_check


magic_list

```c

#include <stdio.h>
#include "magic.h"

int main(int argc, char **argv){
  const char *mime;
  magic_t magic;
  int result;

  printf("Getting magic from %s\n", argv[1]);

  //magic = magic_open(MAGIC_MIME|MAGIC_DEVICES|MAGIC_DEBUG); 
  //magic = magic_open(MAGIC_CONTINUE|MAGIC_MIME);
  magic = magic_open(MAGIC_CHECK/*|MAGIC_DEBUG*/|MAGIC_MIME);
  magic_load(magic, argv[2]);
  magic_compile(magic, argv[2]);
  result = magic_list(magic, argv[1]);

  printf("%i\n", result);
  magic_close(magic);

  return 0;
}
```

```bash
vagrant@precise64:~/data/libmagic$ ./5 /tmp/magic.binwalk 
vagrant@precise64:~/data/libmagic$ ./5 /usr/share/misc/magic.mgc 
```

https://www.cs.swarthmore.edu/~newhall/unixhelp/howto_C_libraries.html

https://git.archlinux.org/svntogit/community.git/tree/trunk/PKGBUILD?h=packages/nemesis