shithub: qk2

Download patch

ref: f961e207e8f8a9e544dceb12ca1e554d0bd6ea7e
parent: a4200cb010c09374c6d664cbcc4d88c2e40006e2
author: qwx <qwx@sciops.net>
date: Sat Jul 24 06:21:03 EDT 2021

cd: fixes from qk1

--- a/cd.c
+++ b/cd.c
@@ -11,7 +11,6 @@
 static int ntrk, trk, chtrk;
 static char trtype;
 static int cpid = -1;
-static Reprog *pat;
 static cvar_t *ccdvol;
 
 static int
@@ -19,14 +18,17 @@
 {
 	int fd, i, n;
 	Dir *d;
+	Reprog *pat;
 
 	ntrk = 0;
 
 	if((fd = open("/mnt/cd", OREAD)) < 0)
-		goto err;
-	if((n = dirreadall(fd, &d)) < 0)
-		goto err;
+		return -1;
+	n = dirreadall(fd, &d);
 	close(fd);
+	if(n < 0)
+		return -1;
+	pat = regcomp("[au][0-9][0-9][0-9]");
 	for(i = 0; i < n; i++)
 		if(regexec(pat, d[i].name, nil, 0)){
 			if(!trtype)
@@ -33,15 +35,11 @@
 				trtype = d[i].name[0];
 			ntrk++;
 		}
+	free(pat);
 	free(d);
 	if(ntrk < 1)
 		return -1;
 	return 0;
-
-err:
-	close(fd);
-	fprint(2, "cdinfo: %r\n");
-	return -1;
 }
 
 static void
@@ -52,8 +50,10 @@
 	uchar buf[Nsbuf];
 	short *p, *e;
 
-	if((afd = open("/dev/audio", OWRITE)) < 0)
+	if((afd = open("/dev/audio", OWRITE)) < 0){
+		fprint(2, "cd: open: %r\n");
 		return;
+	}
 	fd = -1;
 	for(;;){
 		if(chtrk > 0){
@@ -61,7 +61,7 @@
 			trk = chtrk;
 			snprint(s, sizeof s, "/mnt/cd/%c%03ud", trtype, trk);
 			if((fd = open(s, OREAD)) < 0)
-				fprint(2, "cproc: %r");
+				fprint(2, "cd: open: %r\n");
 			chtrk = 0;
 		}
 		if(!cdread || fd < 0){
@@ -80,7 +80,7 @@
 			continue;
 		}
 		p = (short *)buf;
-		e = (short *)(buf+sizeof buf);
+		e = (short *)(buf + n);
 		while(p < e){
 			a = *p * cdvol;
 			*p++ = a;
@@ -132,28 +132,30 @@
 {
 	char *cmd;
 
-	if(Cmd_Argc() < 2)
+	if(Cmd_Argc() < 2){
+usage:
+		Com_Printf("cd (play|loop|stop|pause|resume|info) [track]\n");
 		return;
+	}
 	cmd = Cmd_Argv(1);
 	if(cistrcmp(cmd, "play") == 0){
+		if(Cmd_Argc() < 3)
+			goto usage;
 		CDAudio_Play(atoi(Cmd_Argv(2)), false);
-		return;
 	}else if(cistrcmp(cmd, "loop") == 0){
+		if(Cmd_Argc() < 3)
+			goto usage;
 		CDAudio_Play(atoi(Cmd_Argv(2)), true);
-		return;
-	}else if(cistrcmp(cmd, "stop") == 0){
+	}else if(cistrcmp(cmd, "stop") == 0)
 		CDAudio_Stop();
-		return;
-	}else if(cistrcmp(cmd, "pause") == 0){
+	else if(cistrcmp(cmd, "pause") == 0)
 		CDAudio_Pause();
-		return;
-	}else if(cistrcmp(cmd, "resume") == 0){
+	else if(cistrcmp(cmd, "resume") == 0)
 		CDAudio_Resume();
-		return;
-	}else if(cistrcmp(cmd, "info") == 0){
+	else if(cistrcmp(cmd, "info") == 0)
 		Com_Printf("track %d/%d; loop %d; vol %.1f\n", trk, ntrk, cdloop, cdvol);
-		return;
-	}
+	else
+		goto usage;
 }
 
 void
@@ -172,7 +174,6 @@
 {
 	int tid;
 
-	pat = regcomp("[au][0-9][0-9][0-9]");
 	if(cdinfo() < 0)
 		return -1;
 	ccdvol = Cvar_Get("cdvol", "1", CVAR_ARCHIVE);
@@ -195,7 +196,5 @@
 	if(postnote(PNPROC, cpid, "die die die") < 0)
 		fprint(2, "CDAudio_Shutdown: postnote: %r\n");
 	cpid = -1;
-	free(pat);
-	pat = nil;
 	cdread = cdloop = 0;
 }