Back to index

libcitadel  8.12
mime_xdg_lookup_type.c
Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <stdlib.h>
00003 #include <string.h>
00004 
00005 #include <sys/types.h>
00006 #include <sys/stat.h>
00007 #include <dirent.h>
00008 #include <errno.h>
00009 
00010 #include <fcntl.h>
00011 
00012 #include <unistd.h>
00013 #include <stddef.h>
00014 
00015 
00016 #include "../lib/libcitadel.h"
00017 
00018 int main(int argc, char* argv[])
00019 {
00020        char a;
00021        int fd;
00022        char *filename = NULL;
00023        char *IconDir = NULL;
00024        struct stat statbuf;
00025        const char *Err;
00026 
00027        StrBuf *MimeBuf;
00028        long MimeLen;
00029        char *MimeStr;
00030        int by_extension = 0;
00031        const char *GuessedMimeType;
00032        const char *GuessedMimeIcon;
00033        char MimeTypeStr[SIZ];
00034 
00035        setvbuf(stdout, NULL, _IONBF, 0);
00036 
00037 
00038        while ((a = getopt(argc, argv, "xf:i:")) != EOF)
00039        {
00040               switch (a) {
00041               case 'x':
00042                      by_extension = 1;
00043                      break;
00044               case 'f':
00045                      filename = optarg;
00046                      break;
00047               case 'i':
00048                      IconDir = optarg;
00049                      break;
00050               }
00051        }
00052        StartLibCitadel(8);
00053 
00054        if (IconDir != NULL)
00055               LoadIconDir(IconDir);
00056 
00057        if (filename == NULL) {
00058               ShutDownLibCitadel();
00059               printf("Filename requried! -f\n");
00060               return 1;
00061        }
00062 
00063        if (by_extension) {
00064               GuessedMimeType = GuessMimeByFilename(filename, strlen(filename));
00065               printf("Mimetype: %s\n", GuessedMimeType);
00066               if (IconDir != NULL) {
00067                      strcpy(MimeTypeStr, GuessedMimeType);
00068                      GuessedMimeIcon = GetIconFilename(MimeTypeStr, strlen(MimeTypeStr));
00069                      if (GuessedMimeIcon != NULL)
00070                             printf("Associated Icon [%s]\n", GuessedMimeIcon);
00071                      else 
00072                             printf("no icon associated.\n");
00073               }
00074               ShutDownLibCitadel();
00075               return 0;
00076        }
00077 
00078        fd = open(filename, 0);
00079        if (fd < 0) {
00080               printf("Error opening file [%s] %d [%s]\n", filename, errno, strerror(errno));
00081               ShutDownLibCitadel();
00082               return 1;
00083        }
00084        if (fstat(fd, &statbuf) == -1) {
00085               printf("Error stating file [%s] %d [%s]\n", filename, errno, strerror(errno));
00086               ShutDownLibCitadel();
00087               return 1;
00088        }
00089        MimeBuf = NewStrBufPlain(NULL, statbuf.st_size + 1);
00090        if (StrBufReadBLOB(MimeBuf, &fd, 1, statbuf.st_size, &Err) < 0) {
00091               printf("Error reading file [%s] %d [%s] [%s]\n", filename, errno, strerror(errno), Err);
00092               FreeStrBuf(&MimeBuf);
00093               ShutDownLibCitadel();
00094               return 1;
00095        }
00096        MimeLen = StrLength(MimeBuf);
00097        MimeStr = SmashStrBuf(&MimeBuf);
00098        GuessedMimeType = GuessMimeType(MimeStr, MimeLen);
00099 
00100        printf("Mimetype: %s\n", GuessedMimeType);
00101        if (IconDir != NULL) {
00102               strcpy(MimeTypeStr, GuessedMimeType);
00103               GuessedMimeIcon = GetIconFilename(MimeTypeStr, strlen(MimeTypeStr));
00104               if (GuessedMimeIcon != NULL)
00105                      printf("Associated Icon [%s]\n", GuessedMimeIcon);
00106               else 
00107                      printf("no icon associated.\n");
00108        }
00109 
00110 
00111        free(MimeStr);
00112        ShutDownLibCitadel();
00113        return 0;
00114 }