shithub: riscv

Download patch

ref: 02e6003fc87ca7ace27beef200813426dd954852
parent: 480d7b8f5f0ccb52391c41ffa58c196618827346
author: Ori Bernstein <ori@eigenstate.org>
date: Sun Dec 8 06:54:59 EST 2019

fix filetype detecton by suffix so that multiple dots dont confuse it. (thanks kvik)

--- a/sys/src/cmd/upas/marshal/marshal.c
+++ b/sys/src/cmd/upas/marshal/marshal.c
@@ -871,11 +871,21 @@
 	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 @@
 	}
 
 	/* pick a type depending on extension */
-	p = strchr(file, '.');
-	if(p != nil)
-		p++;
+	for(c = ctype; c->ext != nil; c++)
+		if(hassuffix(file, c->ext)){
+			a->type = c->type;
+			a->ctype = c;
+			return a;
+		}
 
-	/* 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;
-			}
-	}
-
 	/* 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 */