diff options
author | Ori Bernstein <ori@eigenstate.org> | 2019-12-08 11:54:59 -0800 |
---|---|---|
committer | Ori Bernstein <ori@eigenstate.org> | 2019-12-08 11:54:59 -0800 |
commit | 02e6003fc87ca7ace27beef200813426dd954852 (patch) | |
tree | 1a7095d470ab01983f0a7a582670fef4e3e3833a | |
parent | 480d7b8f5f0ccb52391c41ffa58c196618827346 (diff) |
fix filetype detecton by suffix so that multiple dots dont confuse it. (thanks kvik)
-rw-r--r-- | sys/src/cmd/upas/marshal/marshal.c | 49 |
1 files 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 */ |