shithub: riscv

Download patch

ref: d73c67660bd2573787e99c300806c2f1394a8b52
parent: 026c679f49ca89b7585d3ee3364d154c2b00cc3a
author: mischief <[email protected]>
date: Tue Sep 18 11:58:22 EDT 2018

sum, md5sum, sha1sum: set exit status properly on open/read errors

--- a/sys/src/cmd/md5sum.c
+++ b/sys/src/cmd/md5sum.c
@@ -4,6 +4,8 @@
 
 #pragma	varargck	type	"M"	uchar*
 
+static char exitstr[ERRMAX];
+
 static int
 digestfmt(Fmt *fmt)
 {
@@ -28,7 +30,8 @@
 	while((n = read(fd, buf, sizeof buf)) > 0)
 		md5(buf, n, nil, s);
 	if(n < 0){
-		fprint(2, "reading %s: %r\n", name ? name : "stdin");
+		snprint(exitstr, sizeof(exitstr), "reading %s: %r\n", name ? name : "stdin");
+		fprint(2, "%s", exitstr);
 		return;
 	}
 	md5(nil, 0, digest, s);
@@ -56,11 +59,12 @@
 	else for(i = 0; i < argc; i++){
 		fd = open(argv[i], OREAD);
 		if(fd < 0){
-			fprint(2, "md5sum: can't open %s: %r\n", argv[i]);
+			snprint(exitstr, sizeof(exitstr), "can't open %s: %r", argv[i]);
+			fprint(2, "%s: %s\n", argv0, exitstr);
 			continue;
 		}
 		sum(fd, argv[i]);
 		close(fd);
 	}
-	exits(nil);
+	exits(exitstr);
 }
--- a/sys/src/cmd/sha1sum.c
+++ b/sys/src/cmd/sha1sum.c
@@ -7,6 +7,8 @@
 
 #pragma	varargck	type	"M"	uchar*
 
+static char exitstr[ERRMAX];
+
 typedef struct Sha2 Sha2;
 struct Sha2 {
 	int	bits;
@@ -48,7 +50,8 @@
 	while((n = read(fd, buf, sizeof buf)) > 0)
 		(*shafunc)(buf, n, nil, s);
 	if(n < 0){
-		fprint(2, "reading %s: %r\n", name? name: "stdin");
+		snprint(exitstr, sizeof(exitstr), "reading %s: %r\n", name? name: "stdin");
+		fprint(2, "%s", exitstr);
 		return;
 	}
 	(*shafunc)(nil, 0, digest, s);
@@ -96,11 +99,12 @@
 		for(i = 0; i < argc; i++){
 			fd = open(argv[i], OREAD);
 			if(fd < 0){
-				fprint(2, "%s: can't open %s: %r\n", argv0, argv[i]);
+				snprint(exitstr, sizeof(exitstr), "can't open %s: %r", argv[i]);
+				fprint(2, "%s: %s\n", argv0, exitstr);
 				continue;
 			}
 			sum(fd, argv[i]);
 			close(fd);
 		}
-	exits(nil);
+	exits(exitstr);
 }
--- a/sys/src/cmd/sum.c
+++ b/sys/src/cmd/sum.c
@@ -3,8 +3,10 @@
 
 typedef ulong	Sumfn(ulong, void*, uvlong);
 extern Sumfn	sumr, sum5, sum32;
-char		*sumfile(char*, Sumfn*);
+void		sumfile(char*, Sumfn*);
 
+static char exitstr[ERRMAX];
+
 void
 usage(void)
 {
@@ -16,7 +18,6 @@
 main(int argc, char **argv)
 {
 	Sumfn *fn = sum32;
-	char *exitstr=0, *s;
 
 	ARGBEGIN{
 	case 'r':
@@ -31,14 +32,13 @@
 	}ARGEND
 	if(*argv){
 		while(*argv)
-			if(s = sumfile(*argv++, fn))	/* assign = */
-				exitstr = s;
+			sumfile(*argv++, fn);
 	}else
-		exitstr = sumfile(0, fn);
+		sumfile(0, fn);
 	exits(exitstr);
 }
 
-char*
+void
 sumfile(char *file, Sumfn *fn)
 {
 	int fd;
@@ -49,9 +49,9 @@
 
 	if(file){
 		if((fd = open(file, OREAD)) < 0){
-			errstr(buf, sizeof buf);
-			fprint(2, "%s: %s: %s\n", argv0, file, buf);
-			return "can't open";
+			snprint(exitstr, sizeof(exitstr), "can't open %s: %r", file);
+			fprint(2, "%s: %s\n", argv0, exitstr);
+			return;
 		}
 	}else
 		fd = 0;
@@ -62,11 +62,12 @@
 		sum = (*fn)(sum, buf, n);
 	}
 	if(n < 0){
-		errstr(buf, sizeof buf);
-		fprint(2, "%s: %s: read error: %s\n", argv0, file? file:"<stdin>", buf);
+		snprint(exitstr, sizeof(exitstr), "reading %s: %r", file? file:"<stdin>");
+		fprint(2, "%s: %s\n", argv0, exitstr);
+
 		if(file)
 			close(fd);
-		return "read error";
+		return;
 	}
 	if(file)
 		close(fd);
@@ -74,7 +75,6 @@
 	if(file)
 		print(" %s", file);
 	print("\n");
-	return 0;
 }
 
 #define	VBSIZE		512		/* system v */