-/* Calculate and print the rotated checksum and the size in 1K blocks
- of file FILE, or of the standard input if FILE is "-".
- If PRINT_NAME is >1, print FILE next to the checksum and size.
- The checksum varies depending on sizeof (int).
- Return 1 if successful. */
-static int bsd_sum_file(const char *file, int print_name)
-{
- FILE *fp;
- int checksum = 0; /* The checksum mod 2^16. */
- uintmax_t total_bytes = 0; /* The number of bytes. */
- int ch; /* Each character read. */
-
- if (IS_STDIN(file)) {
- fp = stdin;
- have_read_stdin = 1;
- } else {
- fp = bb_wfopen(file, "r");
- if (fp == NULL)
- return 0;
- }
-
- while ((ch = getc(fp)) != EOF) {
- ++total_bytes;
- checksum = (checksum >> 1) + ((checksum & 1) << 15);
- checksum += ch;
- checksum &= 0xffff; /* Keep it within bounds. */
- }
-
- if (ferror(fp)) {
- bb_perror_msg(file);
- bb_fclose_nonstdin(fp);
- return 0;
- }
-
- if (bb_fclose_nonstdin(fp) == EOF) {
- bb_perror_msg(file);
- return 0;
- }
-
- printf("%05d %5ju ", checksum, (total_bytes+1023)/1024);
- if (print_name > 1)
- puts(file);
- else
- printf("\n");