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

This commit is contained in:
mischief 2018-09-18 15:58:22 -07:00
parent 026c679f49
commit d73c67660b
3 changed files with 27 additions and 19 deletions

View file

@ -4,6 +4,8 @@
#pragma varargck type "M" uchar*
static char exitstr[ERRMAX];
static int
digestfmt(Fmt *fmt)
{
@ -28,7 +30,8 @@ sum(int fd, char *name)
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 @@ main(int argc, char *argv[])
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);
}

View file

@ -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 @@ sum(int fd, char *name)
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 @@ main(int argc, char *argv[])
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);
}

View file

@ -3,7 +3,9 @@
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 @@ void
main(int argc, char **argv)
{
Sumfn *fn = sum32;
char *exitstr=0, *s;
ARGBEGIN{
case 'r':
@ -31,14 +32,13 @@ main(int argc, char **argv)
}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 @@ sumfile(char *file, Sumfn *fn)
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 @@ sumfile(char *file, Sumfn *fn)
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 @@ sumfile(char *file, Sumfn *fn)
if(file)
print(" %s", file);
print("\n");
return 0;
}
#define VBSIZE 512 /* system v */