sum.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 GPLv2 or later, see file LICENSE in this source tree.
  14. */
  15. //config:config SUM
  16. //config: bool "sum (4.2 kb)"
  17. //config: default y
  18. //config: help
  19. //config: checksum and count the blocks in a file
  20. //applet:IF_SUM(APPLET(sum, BB_DIR_USR_BIN, BB_SUID_DROP))
  21. //kbuild:lib-$(CONFIG_SUM) += sum.o
  22. //usage:#define sum_trivial_usage
  23. //usage: "[-rs] [FILE]..."
  24. //usage:#define sum_full_usage "\n\n"
  25. //usage: "Checksum and count the blocks in a file\n"
  26. //usage: "\n -r Use BSD sum algorithm (1K blocks)"
  27. //usage: "\n -s Use System V sum algorithm (512byte blocks)"
  28. #include "libbb.h"
  29. #include "common_bufsiz.h"
  30. enum { SUM_BSD, PRINT_NAME, SUM_SYSV };
  31. /* BSD: calculate and print the rotated checksum and the size in 1K blocks
  32. The checksum varies depending on sizeof (int). */
  33. /* SYSV: calculate and print the checksum and the size in 512-byte blocks */
  34. /* Return 1 if successful. */
  35. static unsigned sum_file(const char *file, unsigned type)
  36. {
  37. unsigned long long total_bytes = 0;
  38. int fd, r;
  39. /* The sum of all the input bytes, modulo (UINT_MAX + 1). */
  40. unsigned s = 0;
  41. #define buf bb_common_bufsiz1
  42. setup_common_bufsiz();
  43. fd = open_or_warn_stdin(file);
  44. if (fd == -1)
  45. return 0;
  46. while (1) {
  47. size_t bytes_read = safe_read(fd, buf, COMMON_BUFSIZE);
  48. if ((ssize_t)bytes_read <= 0) {
  49. r = (fd && close(fd) != 0);
  50. if (!bytes_read && !r)
  51. /* no error */
  52. break;
  53. bb_simple_perror_msg(file);
  54. return 0;
  55. }
  56. total_bytes += bytes_read;
  57. if (type >= SUM_SYSV) {
  58. do s += buf[--bytes_read]; while (bytes_read);
  59. } else {
  60. r = 0;
  61. do {
  62. s = (s >> 1) + ((s & 1) << 15);
  63. s += buf[r++];
  64. s &= 0xffff; /* Keep it within bounds. */
  65. } while (--bytes_read);
  66. }
  67. }
  68. if (type < PRINT_NAME)
  69. file = "";
  70. if (type >= SUM_SYSV) {
  71. r = (s & 0xffff) + ((s & 0xffffffff) >> 16);
  72. s = (r & 0xffff) + (r >> 16);
  73. printf("%u %llu %s\n", s, (total_bytes + 511) / 512, file);
  74. } else
  75. printf("%05u %5llu %s\n", s, (total_bytes + 1023) / 1024, file);
  76. return 1;
  77. #undef buf
  78. }
  79. int sum_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  80. int sum_main(int argc UNUSED_PARAM, char **argv)
  81. {
  82. unsigned n;
  83. unsigned type = SUM_BSD;
  84. n = getopt32(argv, "sr");
  85. argv += optind;
  86. if (n & 1) type = SUM_SYSV;
  87. /* give the bsd priority over sysv func */
  88. if (n & 2) type = SUM_BSD;
  89. if (!argv[0]) {
  90. /* Do not print the name */
  91. n = sum_file("-", type);
  92. } else {
  93. /* Need to print the name if either
  94. * - more than one file given
  95. * - doing sysv */
  96. type += (argv[1] || type == SUM_SYSV);
  97. n = 1;
  98. do {
  99. n &= sum_file(*argv, type);
  100. } while (*++argv);
  101. }
  102. return !n;
  103. }