Add test to call mime lookup algorithms
[citadel.git] / libcitadel / tests / mime_xdg_lookup_type.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4
5 #include <sys/types.h>
6 #include <sys/stat.h>
7 #include <dirent.h>
8 #include <errno.h>
9
10 #include <fcntl.h>
11
12 #include <unistd.h>
13 #include <stddef.h>
14
15
16 #include "../lib/libcitadel.h"
17
18 int main(int argc, char* argv[])
19 {
20         char a;
21         int fd;
22         char *filename = NULL;
23         struct stat statbuf;
24         const char *Err;
25
26         StrBuf *MimeBuf;
27         long MimeLen;
28         char *MimeStr;
29         int by_extension = 0;
30
31         setvbuf(stdout, NULL, _IONBF, 0);
32
33
34         while ((a = getopt(argc, argv, "xf:")) != EOF)
35         {
36                 switch (a) {
37                 case 'x':
38                         by_extension = 1;
39                         break;
40                 case 'f':
41                         filename = optarg;
42                         break;
43                 }
44         }
45         StartLibCitadel(8);
46
47         if (filename == NULL) {
48                 printf("Filename requried! -f\n");
49                 return 1;
50         }
51
52         if (by_extension) {
53                 printf("Mimetype: %s\n", GuessMimeByFilename(filename, strlen(filename)));
54                 return 0;
55         }
56
57         fd = open(filename, 0);
58         if (fd < 0) {
59                 printf("Error opening file [%s] %d [%s]\n", filename, errno, strerror(errno));
60                 return 1;
61         }
62         if (fstat(fd, &statbuf) == -1) {
63                 printf("Error stating file [%s] %d [%s]\n", filename, errno, strerror(errno));
64                 return 1;
65         }
66         MimeBuf = NewStrBufPlain(NULL, statbuf.st_size + 1);
67         if (StrBufReadBLOB(MimeBuf, &fd, 1, statbuf.st_size, &Err) < 0) {
68                 printf("Error reading file [%s] %d [%s] [%s]\n", filename, errno, strerror(errno), Err);
69                 FreeStrBuf(&MimeBuf);
70                 return 1;
71         }
72         MimeLen = StrLength(MimeBuf);
73         MimeStr = SmashStrBuf(&MimeBuf);
74
75         printf("Mimetype: %s\n", GuessMimeType(MimeStr, MimeLen));
76
77         free(MimeStr);
78         return 0;
79 }