Changeset 2725 in MondoRescue for branches/2.2.9/mindi-busybox/coreutils/cksum.c
- Timestamp:
- Feb 25, 2011, 9:26:54 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2.2.9/mindi-busybox/coreutils/cksum.c
r1765 r2725 5 5 * Copyright (C) 2006 by Rob Sullivan, with ideas from code by Walter Harms 6 6 * 7 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. */8 7 * Licensed under GPLv2 or later, see file LICENSE in this source tree. 8 */ 9 9 #include "libbb.h" 10 10 11 int cksum_main(int argc, char **argv); 12 int cksum_main(int argc, char **argv) 11 /* This is a NOEXEC applet. Be very careful! */ 12 13 int cksum_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; 14 int cksum_main(int argc UNUSED_PARAM, char **argv) 13 15 { 14 16 uint32_t *crc32_table = crc32_filltable(NULL, 1); 17 uint32_t crc; 18 off_t length, filesize; 19 int bytes_read; 20 int exit_code = EXIT_SUCCESS; 15 21 16 FILE *fp; 17 uint32_t crc; 18 long length, filesize; 19 int bytes_read; 20 char *cp; 21 22 int inp_stdin = (argc == optind) ? 1 : 0; 22 #if ENABLE_DESKTOP 23 getopt32(argv, ""); /* coreutils 6.9 compat */ 24 argv += optind; 25 #else 26 argv++; 27 #endif 23 28 24 29 do { 25 fp = fopen_or_warn_stdin((inp_stdin) ? bb_msg_standard_input : *++argv);30 int fd = open_or_warn_stdin(*argv ? *argv : bb_msg_standard_input); 26 31 32 if (fd < 0) { 33 exit_code = EXIT_FAILURE; 34 continue; 35 } 27 36 crc = 0; 28 37 length = 0; 29 38 30 39 #define read_buf bb_common_bufsiz1 31 while ((bytes_read = fread(read_buf, 1, BUFSIZ, fp)) > 0) { 32 cp = read_buf; 33 length += bytes_read; 34 while (bytes_read--) 35 crc = (crc << 8) ^ crc32_table[((crc >> 24) ^ (*cp++)) & 0xffL]; 40 while ((bytes_read = safe_read(fd, read_buf, sizeof(read_buf))) > 0) { 41 crc = crc32_block_endian1(crc, read_buf, bytes_read, crc32_table); 36 42 } 43 close(fd); 37 44 38 45 filesize = length; 39 46 40 for (; length; length >>= 8) 41 crc = (crc << 8) ^ crc32_table[((crc >> 24) ^ length) & 0xffL]; 42 crc ^= 0xffffffffL; 47 while (length) { 48 crc = (crc << 8) ^ crc32_table[(uint8_t)(crc >> 24) ^ (uint8_t)length]; 49 /* must ensure that shift is unsigned! */ 50 if (sizeof(length) <= sizeof(unsigned)) 51 length = (unsigned)length >> 8; 52 else if (sizeof(length) <= sizeof(unsigned long)) 53 length = (unsigned long)length >> 8; 54 else 55 length = (unsigned long long)length >> 8; 56 } 57 crc = ~crc; 43 58 44 if (inp_stdin) { 45 printf("%" PRIu32 " %li\n", crc, filesize); 46 break; 47 } 59 printf((*argv ? "%"PRIu32" %"OFF_FMT"i %s\n" : "%"PRIu32" %"OFF_FMT"i\n"), 60 crc, filesize, *argv); 61 } while (*argv && *++argv); 48 62 49 printf("%" PRIu32 " %li %s\n", crc, filesize, *argv); 50 fclose(fp); 51 } while (*(argv + 1)); 52 53 fflush_stdout_and_exit(EXIT_SUCCESS); 63 fflush_stdout_and_exit(exit_code); 54 64 }
Note:
See TracChangeset
for help on using the changeset viewer.