-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_mime.c
More file actions
41 lines (36 loc) · 847 Bytes
/
get_mime.c
File metadata and controls
41 lines (36 loc) · 847 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <string.h>
#include <stdio.h>
char* get_mime(char* path, FILE* mimefile)
{
rewind(mimefile);
// get file name
const char* file = strrchr(path, '/') + 1;
// get file extension
const char* dot = strrchr(file, '.');
if(!dot || dot == file) dot = " ";
dot++;
// open mime file
char* line = NULL;
size_t len = 0;
// read mime file
while(getline(&line, &len, mimefile) != -1)
{
if((strstr(line, "#") == NULL) && (strcmp(line, "\n") != 0)
&& (strstr(line, "\t") != NULL))
{
char tokens [64];
strcpy(tokens, strrchr(line, '\t') + 1);
char* token;
char* saveptr;
for(token = strtok_r(tokens, " \n", &saveptr); token != NULL; token = strtok_r(NULL, " \n", &saveptr))
{
if(strcmp(token, dot) == 0)
{
char* mimetype = strtok(line, "\t");
return mimetype;
}
}
}
}
return NULL;
}