sum.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 "busybox.h"
  16. /* 1 if any of the files read were the standard input */
  17. static int have_read_stdin;
  18. /* make a little more readable and avoid using strcmp for just 2 bytes */
  19. #define IS_STDIN(s) (s[0] == '-' && s[1] == '\0')
  20. /* Calculate and print the rotated checksum and the size in 1K blocks
  21. of file FILE, or of the standard input if FILE is "-".
  22. If PRINT_NAME is >1, print FILE next to the checksum and size.
  23. The checksum varies depending on sizeof (int).
  24. Return 1 if successful. */
  25. static int bsd_sum_file(const char *file, int print_name)
  26. {
  27. FILE *fp;
  28. int checksum = 0; /* The checksum mod 2^16. */
  29. uintmax_t total_bytes = 0; /* The number of bytes. */
  30. int ch; /* Each character read. */
  31. int ret = 0;
  32. if (IS_STDIN(file)) {
  33. fp = stdin;
  34. have_read_stdin++;
  35. } else {
  36. fp = fopen_or_warn(file, "r");
  37. if (fp == NULL)
  38. goto out;
  39. }
  40. while ((ch = getc(fp)) != EOF) {
  41. ++total_bytes;
  42. checksum = (checksum >> 1) + ((checksum & 1) << 15);
  43. checksum += ch;
  44. checksum &= 0xffff; /* Keep it within bounds. */
  45. }
  46. if (ferror(fp)) {
  47. bb_perror_msg(file);
  48. fclose_if_not_stdin(fp);
  49. goto out;
  50. }
  51. if (fclose_if_not_stdin(fp) == EOF) {
  52. bb_perror_msg(file);
  53. goto out;
  54. }
  55. ret++;
  56. printf("%05d %5ju ", checksum, (total_bytes+1023)/1024);
  57. if (print_name > 1)
  58. puts(file);
  59. else
  60. puts("");
  61. out:
  62. return ret;
  63. }
  64. /* Calculate and print the checksum and the size in 512-byte blocks
  65. of file FILE, or of the standard input if FILE is "-".
  66. If PRINT_NAME is >0, print FILE next to the checksum and size.
  67. Return 1 if successful. */
  68. #define MY_BUF_SIZE 8192
  69. static int sysv_sum_file(const char *file, int print_name)
  70. {
  71. RESERVE_CONFIG_BUFFER(buf, MY_BUF_SIZE);
  72. int fd;
  73. uintmax_t total_bytes = 0;
  74. /* The sum of all the input bytes, modulo (UINT_MAX + 1). */
  75. unsigned int s = 0;
  76. if (IS_STDIN(file)) {
  77. fd = 0;
  78. have_read_stdin = 1;
  79. } else {
  80. fd = open(file, O_RDONLY);
  81. if (fd == -1)
  82. goto release_and_ret;
  83. }
  84. while (1) {
  85. size_t bytes_read = safe_read(fd, buf, MY_BUF_SIZE);
  86. if (bytes_read == 0)
  87. break;
  88. if (bytes_read == -1) {
  89. release_and_ret:
  90. bb_perror_msg(file);
  91. RELEASE_CONFIG_BUFFER(buf);
  92. if (!IS_STDIN(file))
  93. close(fd);
  94. return 0;
  95. }
  96. total_bytes += bytes_read;
  97. while (bytes_read--)
  98. s += buf[bytes_read];
  99. }
  100. if (!IS_STDIN(file) && close(fd) == -1)
  101. goto release_and_ret;
  102. else
  103. RELEASE_CONFIG_BUFFER(buf);
  104. {
  105. int r = (s & 0xffff) + ((s & 0xffffffff) >> 16);
  106. s = (r & 0xffff) + (r >> 16);
  107. printf("%d %ju ", s, (total_bytes+511)/512);
  108. }
  109. puts(print_name ? file : "");
  110. return 1;
  111. }
  112. int sum_main(int argc, char **argv)
  113. {
  114. int flags;
  115. int ok;
  116. int (*sum_func)(const char *, int) = bsd_sum_file;
  117. /* give the bsd func priority over sysv func */
  118. flags = getopt32(argc, argv, "sr");
  119. if (flags & 1)
  120. sum_func = sysv_sum_file;
  121. if (flags & 2)
  122. sum_func = bsd_sum_file;
  123. have_read_stdin = 0;
  124. if ((argc - optind) == 0)
  125. ok = sum_func("-", 0);
  126. else
  127. for (ok = 1; optind < argc; optind++)
  128. ok &= sum_func(argv[optind], 1);
  129. if (have_read_stdin && fclose(stdin) == EOF)
  130. bb_perror_msg_and_die("-");
  131. exit(ok ? EXIT_SUCCESS : EXIT_FAILURE);
  132. }