md5sum.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <bio.h>
  12. #include <mp.h>
  13. #include <libsec.h>
  14. static int
  15. digestfmt(Fmt *fmt)
  16. {
  17. char buf[MD5dlen*2+1];
  18. uint8_t *p;
  19. int i;
  20. p = va_arg(fmt->args, uint8_t*);
  21. for(i=0; i<MD5dlen; i++)
  22. sprint(buf+2*i, "%.2x", p[i]);
  23. return fmtstrcpy(fmt, buf);
  24. }
  25. static void
  26. sum(int fd, char *name)
  27. {
  28. int n;
  29. uint8_t buf[8192], digest[MD5dlen];
  30. DigestState *s;
  31. s = md5(nil, 0, nil, nil);
  32. while((n = read(fd, buf, sizeof buf)) > 0)
  33. md5(buf, n, nil, s);
  34. if(n < 0){
  35. fprint(2, "reading %s: %r\n", name ? name : "stdin");
  36. return;
  37. }
  38. md5(nil, 0, digest, s);
  39. if(name == nil)
  40. print("%M\n", digest);
  41. else
  42. print("%M\t%s\n", digest, name);
  43. }
  44. void
  45. main(int argc, char *argv[])
  46. {
  47. int i, fd;
  48. ARGBEGIN{
  49. default:
  50. fprint(2, "usage: md5sum [file...]\n");
  51. exits("usage");
  52. }ARGEND
  53. fmtinstall('M', digestfmt);
  54. if(argc == 0)
  55. sum(0, nil);
  56. else for(i = 0; i < argc; i++){
  57. fd = open(argv[i], OREAD);
  58. if(fd < 0){
  59. fprint(2, "md5sum: can't open %s: %r\n", argv[i]);
  60. continue;
  61. }
  62. sum(fd, argv[i]);
  63. close(fd);
  64. }
  65. exits(nil);
  66. }