7 typedef struct Suffix Suffix;
17 Suffix *suffixes = nil;
19 static Suffix* parsesuffix(char*, Suffix*);
20 static char* skipwhite(char*);
21 static HContents suffixclass(char*);
22 static char* towhite(char*);
25 updateQid(int fd, Qid *q)
32 sysfatal("can't dirfstat");
35 if(q->path == dq.path && q->vers == dq.vers && q->type == dq.type)
44 static Biobuf *b = nil;
49 file = "/sys/lib/mimetype";
50 if(b == nil){ /* first time */
51 b = Bopen(file, OREAD);
53 sysfatal("can't read from %s", file);
55 if(updateQid(Bfildes(b), &qid) == 0)
60 suffixes = suffixes->next;
68 while((s = Brdline(b, '\n')) != nil){
69 s[Blinelen(b) - 1] = 0;
70 suffixes = parsesuffix(s, suffixes);
75 parsesuffix(char *line, Suffix *suffix)
81 p = strchr(line, '#');
84 nf = tokenize(line, fields, 5);
85 for(i = 0; i < 4; i++)
86 if(i >= nf || fields[i][0] == '-')
91 if(fields[1] == nil && fields[3] == nil)
96 s = ezalloc(sizeof *s);
98 s->suffix = estrdup(fields[0]);
100 s->generic = estrdup(fields[1]);
101 s->specific = estrdup(fields[2]);
104 s->encoding = estrdup(fields[3]);
109 * classify by file name extensions
112 uriclass(HConnect *hc, char *name)
116 HContent *type, *enc;
121 if((p = strrchr(name, '/')) != nil)
123 buf = hstrdup(hc, name);
124 while((p = strrchr(buf, '.')) != nil){
125 for(s = suffixes; s; s = s->next){
126 if(strcmp(p, s->suffix) == 0){
127 if(s->generic != nil && type == nil)
128 type = hmkcontent(hc, s->generic, s->specific, nil);
129 if(s->encoding != nil && enc == nil)
130 enc = hmkcontent(hc, s->encoding, nil, nil);
136 conts.encoding = enc;
141 * classify by initial contents of file
144 dataclass(HConnect *hc, char *buf, int n)
150 for(; n > 0; n -= m){
153 if(c < 32 && c != '\n' && c != '\r' && c != '\t' && c != '\v'){
155 conts.encoding = nil;
160 m = chartorune(&r, buf);
163 conts.encoding = nil;
169 conts.type = hmkcontent(hc, "text", "plain", nil);
170 conts.encoding = nil;