sum, md5sum, sha1sum: set exit status properly on open/read errors
This commit is contained in:
parent
026c679f49
commit
d73c67660b
3 changed files with 27 additions and 19 deletions
|
@ -4,6 +4,8 @@
|
||||||
|
|
||||||
#pragma varargck type "M" uchar*
|
#pragma varargck type "M" uchar*
|
||||||
|
|
||||||
|
static char exitstr[ERRMAX];
|
||||||
|
|
||||||
static int
|
static int
|
||||||
digestfmt(Fmt *fmt)
|
digestfmt(Fmt *fmt)
|
||||||
{
|
{
|
||||||
|
@ -28,7 +30,8 @@ sum(int fd, char *name)
|
||||||
while((n = read(fd, buf, sizeof buf)) > 0)
|
while((n = read(fd, buf, sizeof buf)) > 0)
|
||||||
md5(buf, n, nil, s);
|
md5(buf, n, nil, s);
|
||||||
if(n < 0){
|
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;
|
return;
|
||||||
}
|
}
|
||||||
md5(nil, 0, digest, s);
|
md5(nil, 0, digest, s);
|
||||||
|
@ -56,11 +59,12 @@ main(int argc, char *argv[])
|
||||||
else for(i = 0; i < argc; i++){
|
else for(i = 0; i < argc; i++){
|
||||||
fd = open(argv[i], OREAD);
|
fd = open(argv[i], OREAD);
|
||||||
if(fd < 0){
|
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;
|
continue;
|
||||||
}
|
}
|
||||||
sum(fd, argv[i]);
|
sum(fd, argv[i]);
|
||||||
close(fd);
|
close(fd);
|
||||||
}
|
}
|
||||||
exits(nil);
|
exits(exitstr);
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,6 +7,8 @@
|
||||||
|
|
||||||
#pragma varargck type "M" uchar*
|
#pragma varargck type "M" uchar*
|
||||||
|
|
||||||
|
static char exitstr[ERRMAX];
|
||||||
|
|
||||||
typedef struct Sha2 Sha2;
|
typedef struct Sha2 Sha2;
|
||||||
struct Sha2 {
|
struct Sha2 {
|
||||||
int bits;
|
int bits;
|
||||||
|
@ -48,7 +50,8 @@ sum(int fd, char *name)
|
||||||
while((n = read(fd, buf, sizeof buf)) > 0)
|
while((n = read(fd, buf, sizeof buf)) > 0)
|
||||||
(*shafunc)(buf, n, nil, s);
|
(*shafunc)(buf, n, nil, s);
|
||||||
if(n < 0){
|
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;
|
return;
|
||||||
}
|
}
|
||||||
(*shafunc)(nil, 0, digest, s);
|
(*shafunc)(nil, 0, digest, s);
|
||||||
|
@ -96,11 +99,12 @@ main(int argc, char *argv[])
|
||||||
for(i = 0; i < argc; i++){
|
for(i = 0; i < argc; i++){
|
||||||
fd = open(argv[i], OREAD);
|
fd = open(argv[i], OREAD);
|
||||||
if(fd < 0){
|
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;
|
continue;
|
||||||
}
|
}
|
||||||
sum(fd, argv[i]);
|
sum(fd, argv[i]);
|
||||||
close(fd);
|
close(fd);
|
||||||
}
|
}
|
||||||
exits(nil);
|
exits(exitstr);
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,9 @@
|
||||||
|
|
||||||
typedef ulong Sumfn(ulong, void*, uvlong);
|
typedef ulong Sumfn(ulong, void*, uvlong);
|
||||||
extern Sumfn sumr, sum5, sum32;
|
extern Sumfn sumr, sum5, sum32;
|
||||||
char *sumfile(char*, Sumfn*);
|
void sumfile(char*, Sumfn*);
|
||||||
|
|
||||||
|
static char exitstr[ERRMAX];
|
||||||
|
|
||||||
void
|
void
|
||||||
usage(void)
|
usage(void)
|
||||||
|
@ -16,7 +18,6 @@ void
|
||||||
main(int argc, char **argv)
|
main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
Sumfn *fn = sum32;
|
Sumfn *fn = sum32;
|
||||||
char *exitstr=0, *s;
|
|
||||||
|
|
||||||
ARGBEGIN{
|
ARGBEGIN{
|
||||||
case 'r':
|
case 'r':
|
||||||
|
@ -31,14 +32,13 @@ main(int argc, char **argv)
|
||||||
}ARGEND
|
}ARGEND
|
||||||
if(*argv){
|
if(*argv){
|
||||||
while(*argv)
|
while(*argv)
|
||||||
if(s = sumfile(*argv++, fn)) /* assign = */
|
sumfile(*argv++, fn);
|
||||||
exitstr = s;
|
|
||||||
}else
|
}else
|
||||||
exitstr = sumfile(0, fn);
|
sumfile(0, fn);
|
||||||
exits(exitstr);
|
exits(exitstr);
|
||||||
}
|
}
|
||||||
|
|
||||||
char*
|
void
|
||||||
sumfile(char *file, Sumfn *fn)
|
sumfile(char *file, Sumfn *fn)
|
||||||
{
|
{
|
||||||
int fd;
|
int fd;
|
||||||
|
@ -49,9 +49,9 @@ sumfile(char *file, Sumfn *fn)
|
||||||
|
|
||||||
if(file){
|
if(file){
|
||||||
if((fd = open(file, OREAD)) < 0){
|
if((fd = open(file, OREAD)) < 0){
|
||||||
errstr(buf, sizeof buf);
|
snprint(exitstr, sizeof(exitstr), "can't open %s: %r", file);
|
||||||
fprint(2, "%s: %s: %s\n", argv0, file, buf);
|
fprint(2, "%s: %s\n", argv0, exitstr);
|
||||||
return "can't open";
|
return;
|
||||||
}
|
}
|
||||||
}else
|
}else
|
||||||
fd = 0;
|
fd = 0;
|
||||||
|
@ -62,11 +62,12 @@ sumfile(char *file, Sumfn *fn)
|
||||||
sum = (*fn)(sum, buf, n);
|
sum = (*fn)(sum, buf, n);
|
||||||
}
|
}
|
||||||
if(n < 0){
|
if(n < 0){
|
||||||
errstr(buf, sizeof buf);
|
snprint(exitstr, sizeof(exitstr), "reading %s: %r", file? file:"<stdin>");
|
||||||
fprint(2, "%s: %s: read error: %s\n", argv0, file? file:"<stdin>", buf);
|
fprint(2, "%s: %s\n", argv0, exitstr);
|
||||||
|
|
||||||
if(file)
|
if(file)
|
||||||
close(fd);
|
close(fd);
|
||||||
return "read error";
|
return;
|
||||||
}
|
}
|
||||||
if(file)
|
if(file)
|
||||||
close(fd);
|
close(fd);
|
||||||
|
@ -74,7 +75,6 @@ sumfile(char *file, Sumfn *fn)
|
||||||
if(file)
|
if(file)
|
||||||
print(" %s", file);
|
print(" %s", file);
|
||||||
print("\n");
|
print("\n");
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#define VBSIZE 512 /* system v */
|
#define VBSIZE 512 /* system v */
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue