diff options
author | rsc <devnull@localhost> | 2003-11-23 18:04:47 +0000 |
---|---|---|
committer | rsc <devnull@localhost> | 2003-11-23 18:04:47 +0000 |
commit | bc7cb1a15a67c859c8c71c4b52bb35fe9425a63d (patch) | |
tree | 8ca0fe4e2418e6aa18dc74a236c577a719f6c6ed /src/cmd/sha1sum.c | |
parent | f08fdedcee12c06e3ce9ac9bec363915978e8289 (diff) | |
download | plan9port-bc7cb1a15a67c859c8c71c4b52bb35fe9425a63d.tar.gz plan9port-bc7cb1a15a67c859c8c71c4b52bb35fe9425a63d.tar.bz2 plan9port-bc7cb1a15a67c859c8c71c4b52bb35fe9425a63d.zip |
new utilities.
the .C files compile but are renamed to avoid building automatically.
Diffstat (limited to 'src/cmd/sha1sum.c')
-rw-r--r-- | src/cmd/sha1sum.c | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/src/cmd/sha1sum.c b/src/cmd/sha1sum.c new file mode 100644 index 00000000..d8c449fa --- /dev/null +++ b/src/cmd/sha1sum.c @@ -0,0 +1,61 @@ +#include <u.h> +#include <libc.h> +#include <bio.h> +#include <libsec.h> + +static int +digestfmt(Fmt *fmt) +{ + char buf[SHA1dlen*2+1]; + uchar *p; + int i; + + p = va_arg(fmt->args, uchar*); + for(i=0; i<SHA1dlen; i++) + sprint(buf+2*i, "%.2ux", p[i]); + return fmtstrcpy(fmt, buf); +} + +static void +sum(int fd, char *name) +{ + int n; + uchar buf[8192], digest[SHA1dlen]; + DigestState *s; + + s = sha1(nil, 0, nil, nil); + while((n = read(fd, buf, sizeof buf)) > 0) + sha1(buf, n, nil, s); + sha1(nil, 0, digest, s); + if(name == nil) + print("%M\n", digest); + else + print("%M\t%s\n", digest, name); +} + +void +main(int argc, char *argv[]) +{ + int i, fd; + + ARGBEGIN{ + default: + fprint(2, "usage: sha1sum [file...]\n"); + exits("usage"); + }ARGEND + + fmtinstall('M', digestfmt); + + if(argc == 0) + sum(0, nil); + else for(i = 0; i < argc; i++){ + fd = open(argv[i], OREAD); + if(fd < 0){ + fprint(2, "sha1sum: can't open %s: %r\n", argv[i]); + continue; + } + sum(fd, argv[i]); + close(fd); + } + exits(nil); +} |