sum.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * sum -- checksum and count the blocks in a file
  4. * Like BSD sum or SysV sum -r, except like SysV sum if -s option is given.
  5. *
  6. * Copyright (C) 86, 89, 91, 1995-2002, 2004 Free Software Foundation, Inc.
  7. * Copyright (C) 2005 by Erik Andersen <andersen@codepoet.org>
  8. * Copyright (C) 2005 by Mike Frysinger <vapier@gentoo.org>
  9. *
  10. * Written by Kayvan Aghaiepour and David MacKenzie
  11. * Taken from coreutils and turned into a busybox applet by Mike Frysinger
  12. *
  13. * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
  14. */
  15. #include "libbb.h"
  16. enum { SUM_BSD, PRINT_NAME, SUM_SYSV };
  17. /* BSD: calculate and print the rotated checksum and the size in 1K blocks
  18. The checksum varies depending on sizeof (int). */
  19. /* SYSV: calculate and print the checksum and the size in 512-byte blocks */
  20. /* Return 1 if successful. */
  21. static unsigned sum_file(const char *file, const unsigned type)
  22. {
  23. #define buf bb_common_bufsiz1
  24. uintmax_t total_bytes = 0;
  25. int fd = 0, r;
  26. /* The sum of all the input bytes, modulo (UINT_MAX + 1). */
  27. unsigned s = 0;
  28. if (NOT_LONE_DASH(file)) {
  29. fd = open(file, O_RDONLY);
  30. if (fd == -1)
  31. goto ret_bad;
  32. }
  33. while (1) {
  34. size_t bytes_read = safe_read(fd, buf, BUFSIZ);
  35. if ((ssize_t)bytes_read <= 0) {
  36. r = (fd && close(fd) != 0);
  37. if (!bytes_read && !r)
  38. /* no error */
  39. break;
  40. ret_bad:
  41. bb_perror_msg(file);
  42. return 0;
  43. }
  44. total_bytes += bytes_read;
  45. if (type >= SUM_SYSV) {
  46. do s += buf[--bytes_read]; while (bytes_read);
  47. } else {
  48. r = 0;
  49. do {
  50. s = (s >> 1) + ((s & 1) << 15);
  51. s += buf[r++];
  52. s &= 0xffff; /* Keep it within bounds. */
  53. } while (--bytes_read);
  54. }
  55. }
  56. if (type < PRINT_NAME)
  57. file = "";
  58. if (type >= SUM_SYSV) {
  59. r = (s & 0xffff) + ((s & 0xffffffff) >> 16);
  60. s = (r & 0xffff) + (r >> 16);
  61. printf("%d %ju %s\n", s, (total_bytes+511)/512, file);
  62. } else
  63. printf("%05d %5ju %s\n", s, (total_bytes+1023)/1024, file);
  64. return 1;
  65. #undef buf
  66. }
  67. int sum_main(int argc, char **argv);
  68. int sum_main(int argc, char **argv)
  69. {
  70. unsigned n;
  71. unsigned type = SUM_BSD;
  72. n = getopt32(argv, "sr");
  73. if (n & 1) type = SUM_SYSV;
  74. /* give the bsd priority over sysv func */
  75. if (n & 2) type = SUM_BSD;
  76. if (argc == optind) {
  77. /* Do not print the name */
  78. n = sum_file("-", type);
  79. } else {
  80. /* Need to print the name if either
  81. - more than one file given
  82. - doing sysv */
  83. type += argc - 1 > optind || type == SUM_SYSV;
  84. for (n = 1; optind < argc; optind++)
  85. n &= sum_file(argv[optind], type);
  86. }
  87. return !n;
  88. }