NetSurf
filetype.c
Go to the documentation of this file.
1/*
2 * Copyright 2010 Ole Loots <ole@monochrom.net>
3 *
4 * This file is part of NetSurf, http://www.netsurf-browser.org/
5 *
6 * NetSurf is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * NetSurf is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#include <sys/types.h>
20#include <stdlib.h>
21#include <stdio.h>
22#include <string.h>
23
24#include "utils/messages.h"
25#include "utils/utils.h"
26#include "utils/log.h"
27
28#include "atari/filetype.h"
29
30/**
31 * filetype -- determine the MIME type of a local file
32 */
33const char *fetch_filetype(const char *unix_path)
34{
35 int l;
36 char * res = (char*)"text/html";
37 l = strlen(unix_path);
38
39 NSLOG(netsurf, INFO, "unix path: %s", unix_path);
40
41 /* This line is added for devlopment versions running from the root dir: */
42 if( strchr( unix_path, (int)'.' ) ){
43 if (2 < l && strcasecmp(unix_path + l - 3, "f79") == 0)
44 res = (char*)"text/css";
45 else if (2 < l && strcasecmp(unix_path + l - 3, "css") == 0)
46 res = (char*)"text/css";
47 else if (2 < l && strcasecmp(unix_path + l - 3, "jpg") == 0)
48 res = (char*)"image/jpeg";
49 else if (3 < l && strcasecmp(unix_path + l - 4, "jpeg") == 0)
50 res = (char*)"image/jpeg";
51 else if (2 < l && strcasecmp(unix_path + l - 3, "gif") == 0)
52 res = (char*)"image/gif";
53 else if (2 < l && strcasecmp(unix_path + l - 3, "png") == 0)
54 res = (char*)"image/png";
55 else if (2 < l && strcasecmp(unix_path + l - 3, "jng") == 0)
56 res = (char*)"image/jng";
57 else if (2 < l && strcasecmp(unix_path + l - 3, "svg") == 0)
58 res = (char*)"image/svg";
59 else if (2 < l && strcasecmp(unix_path + l - 3, "txt") == 0)
60 res = (char*)"text/plain";
61 } else {
62 FILE * fp;
63 char buffer[16];
64 fp = fopen( unix_path, "r" );
65 if( fp ){
66 int n=0;
67 int c;
68 do {
69 c = fgetc (fp);
70 if( c != EOF )
71 buffer[n] = (char)c;
72 else
73 buffer[n] = 0;
74 n++;
75 } while (c != EOF && n<15);
76 fclose( fp );
77 if( n > 0 ){
78 if( n > 5 && strncasecmp("GIF89", buffer, 5) == 0 )
79 res = (char*)"image/gif";
80 else if( n > 4 && strncasecmp("PNG", &buffer[1], 3) ==0 )
81 res = (char*)"image/png";
82 else if( n > 10 && strncasecmp("JFIF", &buffer[5], 4) == 0 )
83 res = (char*)"image/jpeg";
84 }
85 }
86 }
87
88 NSLOG(netsurf, INFO, "mime type: %s", res);
89 return( res );
90}
const char * fetch_filetype(const char *unix_path)
Determine the MIME type of a local file.
Definition: filetype.c:58
static osspriteop_area * buffer
The buffer characteristics.
Definition: buffer.c:55
#define NSLOG(catname, level, logmsg, args...)
Definition: log.h:116
Localised message support (interface).
Interface to utility string handling.
Interface to a number of general purpose functionality.