shithub: riscv

Download patch

ref: 064b082ae09420b8b1de820fd03094f9b36f3146
parent: bd6a86a52b4618dcac1119b447ade78dcbd2d690
author: cinap_lenrek <[email protected]>
date: Tue Jun 19 06:01:05 EDT 2012

mothra: cleanup

--- a/sys/src/cmd/mothra/forms.c
+++ b/sys/src/cmd/mothra/forms.c
@@ -496,10 +496,10 @@
 
 void mencodeform(Form *form, int fd){
 	char *b, *p, *sep;
+	int ifd, n, nb;
 	Option *o;
 	Field *f;
 	Rune *rp;
-	int n;
 
 	sep = "--" BOUNDARY;
 	for(f=form->fields;f;f=f->next)switch(f->type){
@@ -549,11 +549,7 @@
 		sep = "\r\n--" BOUNDARY;
 		free(b);
 		break;
-	}
-	for(f=form->fields;f;f=f->next)if(f->type == FILE){
-		char buf[1024];
-		int ifd;
-
+	case FILE:
 		if(f->name==0 || f->value[0]==0)
 			continue;
 		if(p = strrchr(f->value, '/'))
@@ -560,16 +556,21 @@
 			p++;
 		if(p == 0 || *p == 0)
 			p = f->value;
-		if((ifd = open(f->value, OREAD)) < 0)
+		if((b = malloc(nb = 8192)) == nil)
 			continue;
-		if(filetype(ifd, buf, sizeof(buf)) < 0)
-			strcpy(buf, "application/octet-stream");
-		fprint(fd, "%s\r\nContent-Disposition: form-data; name=\"%s\"; filename=\"%s\""
-			"\r\nContent-Type: %s\r\n\r\n", sep, f->name, p, buf);
-		while((n = read(ifd, buf, sizeof(buf))) > 0)
-			write(fd, buf, n);
-		close(ifd);
-		sep = "\r\n--" BOUNDARY;
+		if((ifd = open(f->value, OREAD)) >= 0){
+			if(filetype(ifd, b, nb) < 0)
+				strcpy(b, "application/octet-stream");
+			fprint(fd, "%s\r\nContent-Disposition: form-data; name=\"%s\"; filename=\"%s\""
+				"\r\nContent-Type: %s\r\n\r\n", sep, f->name, p, b);
+			sep = "\r\n--" BOUNDARY;
+			while((n = read(ifd, b, nb)) > 0)
+				if(write(fd, b, n) != n)
+					break;
+			close(ifd);
+		}
+		free(b);
+		break;
 	}
 	fprint(fd, "%s--\r\n", sep);
 }