-
Notifications
You must be signed in to change notification settings - Fork 0
/
mimeTypeManager.c
57 lines (48 loc) · 1.26 KB
/
mimeTypeManager.c
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <string.h>
#include "mimeTypeManager.h"
int isBinaryMimeType(char *mimeType)
{
return (strncmp(mimeType, "text/", 5) == 0) ? 0 : 1;
}
char *getMimeTypFromFilename(char *filename)
{
char *mimeType = NULL;
const char *ext = getExt(filename);
ext++; // jumping the '.'
FILE *mimeTypeFile = fopen(MIME_TYPE_FILE_LOCATION, "r");
if (mimeTypeFile == NULL)
{
fprintf(stderr, "Cannot open the File (%s) containing the mimetypes!", MIME_TYPE_FILE_LOCATION);
}
char *line = NULL;
size_t linecap = 0;
ssize_t linelen;
while ((linelen = getline(&line, &linecap, mimeTypeFile)) > 0)
{
//printf("%s\n", line);
if (strncmp(ext, line, strlen(ext)) == 0)
{
// we found the extension
char *e = strrchr(line, ' ');
e++;
mimeType = e;
break;
}
}
// removing the \n
char *pos;
if ((pos = strchr(mimeType, '\n')) != NULL)
{
*pos = '\0';
}
else
{
/* input too long for buffer, flag error */
}
if (fclose(mimeTypeFile) < 0)
{
fprintf(stderr, "Cannot close the File (%s) containing the mimetypes!", MIME_TYPE_FILE_LOCATION);
exit(EXIT_FAILURE);
}
return mimeType;
}