shithub: zuke

Download patch

ref: 9830943f0ead458292de34c3fe4877966c3dbdba
parent: 108260c329f31c919d3ce9f4eb7a51dd88a32919
author: Sigrid Haflínudóttir <ftrvxmtrx@gmail.com>
date: Mon Jun 15 09:52:52 EDT 2020

use readn and correct loop break conditions

--- a/icy.c
+++ b/icy.c
@@ -2,7 +2,7 @@
 icyfill(Meta *m)
 {
 	char *s, *e, *p, *path, *d;
-	int f, r, n;
+	int f, n;
 
 	path = strdup(m->path);
 	s = strchr(path, ':')+3;
@@ -18,11 +18,8 @@
 		return -1;
 	fprint(f, "GET /%s HTTP/0.9\r\nIcy-MetaData: 1\r\n\r\n", e ? e : "");
 	s = malloc(4096);
-	for(r = 0; r < 4096; r += n){
-		if((n = read(f, s+r, 4096-r)) < 1)
-			break;
-	}
-	s[r] = 0;
+	n = readn(f, s, 4095);
+	s[n] = 0;
 	for(; s = strchr(s, '\n');){
 		s++;
 		if(strncmp(s, "icy-name:", 9) == 0 && (e = strchr(s, '\r')) != nil){
--- a/zuke.c
+++ b/zuke.c
@@ -430,7 +430,7 @@
 	Ioproc *io;
 	Image *thiscover;
 	ulong c;
-	int p[2], fd, pid, n, got, noinit, trycoverload;
+	int p[2], fd, pid, n, noinit, trycoverload;
 	u64int bytesfrom, bf;
 	Meta *cur;
 
@@ -481,19 +481,11 @@
 		sendul(player->ev, Evready);
 		buf = malloc(Relbufsz);
 		io = ioproc();
-		for(c = 0, got = 0; got < Relbufsz; got += n){
-			if((c = nbrecvul(player->ctl)) != 0)
-				break;
-			n = ioread(io, p[1], buf+got, Relbufsz-got);
-			if(n < 1)
-				break;
-		}
-		if(c == 0)
-			c = recvul(player->ctl);
+		n = ioreadn(io, p[1], buf, Relbufsz);
+		c = recvul(player->ctl);
 		if(c != Cstart)
 			goto freeplayer;
-		iowrite(io, audio, buf, got);
-		byteswritten = got;
+		byteswritten = iowrite(io, audio, buf, n);
 		bytesfrom = 0;
 		c = 0;
 		noinit = 1;
@@ -507,8 +499,8 @@
 		n = Relbufsz;
 		if(bytesfrom > byteswritten && n > bytesfrom-byteswritten)
 			n = bytesfrom-byteswritten;
-		n = ioread(io, p[1], buf, n);
-		if(n < 1)
+		n = ioreadn(io, p[1], buf, n);
+		if(n < 0)
 			break;
 
 		thiscover = nil;
@@ -639,7 +631,7 @@
 		if((s = realloc(s, alloc)) == nil)
 			sysfatal("no memory");
 		for(n = 0; sz < alloc; sz += n){
-			n = read(0, s+sz, alloc-sz);
+			n = readn(0, s+sz, alloc-sz);
 			if(n < 0)
 				sysfatal("%r");
 			if(n == 0)