From 02e6003fc87ca7ace27beef200813426dd954852 Mon Sep 17 00:00:00 2001 From: Ori Bernstein Date: Sun, 8 Dec 2019 11:54:59 -0800 Subject: fix filetype detecton by suffix so that multiple dots dont confuse it. (thanks kvik) --- sys/src/cmd/upas/marshal/marshal.c | 49 +++++++++++++++++++------------------- 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/sys/src/cmd/upas/marshal/marshal.c b/sys/src/cmd/upas/marshal/marshal.c index c4c8eb921..dc891f7ae 100644 --- a/sys/src/cmd/upas/marshal/marshal.c +++ b/sys/src/cmd/upas/marshal/marshal.c @@ -871,11 +871,21 @@ printinreplyto(Biobuf *out, char *dir) return Bprint(out, "In-Reply-To: <%s>\n", buf); } +int +hassuffix(char *a, char *b) +{ + int na, nb; + + na = strlen(a), nb = strlen(b); + if(na <= nb + 1 || a[na - nb - 1] != '.') + return 0; + return strcmp(a + (na - nb), b) == 0; +} + Attach* mkattach(char *file, char *type, int ainline) { int n, pfd[2]; - char *p; char ftype[64]; Attach *a; Ctype *c; @@ -902,31 +912,22 @@ mkattach(char *file, char *type, int ainline) } /* pick a type depending on extension */ - p = strchr(file, '.'); - if(p != nil) - p++; - - /* check the builtin extensions */ - if(p != nil){ - for(c = ctype; c->ext != nil; c++) - if(strcmp(p, c->ext) == 0){ - a->type = c->type; - a->ctype = c; - return a; - } - } + for(c = ctype; c->ext != nil; c++) + if(hassuffix(file, c->ext)){ + a->type = c->type; + a->ctype = c; + return a; + } /* try the mime types file */ - if(p != nil){ - if(mimetypes == nil) - readmimetypes(); - for(c = mimetypes; c != nil && c->ext != nil; c++) - if(strcmp(p, c->ext) == 0){ - a->type = c->type; - a->ctype = c; - return a; - } - } + if(mimetypes == nil) + readmimetypes(); + for(c = mimetypes; c != nil && c->ext != nil; c++) + if(hassuffix(file, c->ext)){ + a->type = c->type; + a->ctype = c; + return a; + } /* run file to figure out the type */ a->type = "application/octet-stream"; /* safest default */ -- cgit v1.2.3