aboutsummaryrefslogtreecommitdiff
path: root/coreutils
diff options
context:
space:
mode:
Diffstat (limited to 'coreutils')
-rw-r--r--coreutils/Config.in6
-rw-r--r--coreutils/Makefile.in1
-rw-r--r--coreutils/cksum.c56
3 files changed, 63 insertions, 0 deletions
diff --git a/coreutils/Config.in b/coreutils/Config.in
index c77a1c90f..5d2965b47 100644
--- a/coreutils/Config.in
+++ b/coreutils/Config.in
@@ -52,6 +52,12 @@ config CONFIG_CHROOT
52 chroot is used to change the root directory and run a command. 52 chroot is used to change the root directory and run a command.
53 The default command is `/bin/sh'. 53 The default command is `/bin/sh'.
54 54
55config CONFIG_CKSUM
56 bool "cksum"
57 default n
58 help
59 cksum is used to calculate the CRC32 checksum of a file.
60
55config CONFIG_CMP 61config CONFIG_CMP
56 bool "cmp" 62 bool "cmp"
57 default n 63 default n
diff --git a/coreutils/Makefile.in b/coreutils/Makefile.in
index ff0e89b4d..24eee0b91 100644
--- a/coreutils/Makefile.in
+++ b/coreutils/Makefile.in
@@ -18,6 +18,7 @@ COREUTILS-$(CONFIG_CHGRP) += chgrp.o
18COREUTILS-$(CONFIG_CHMOD) += chmod.o 18COREUTILS-$(CONFIG_CHMOD) += chmod.o
19COREUTILS-$(CONFIG_CHOWN) += chown.o 19COREUTILS-$(CONFIG_CHOWN) += chown.o
20COREUTILS-$(CONFIG_CHROOT) += chroot.o 20COREUTILS-$(CONFIG_CHROOT) += chroot.o
21COREUTILS-$(CONFIG_CKSUM) += cksum.o
21COREUTILS-$(CONFIG_CMP) += cmp.o 22COREUTILS-$(CONFIG_CMP) += cmp.o
22COREUTILS-$(CONFIG_COMM) += comm.o 23COREUTILS-$(CONFIG_COMM) += comm.o
23COREUTILS-$(CONFIG_CP) += cp.o 24COREUTILS-$(CONFIG_CP) += cp.o
diff --git a/coreutils/cksum.c b/coreutils/cksum.c
new file mode 100644
index 000000000..194ea7d2a
--- /dev/null
+++ b/coreutils/cksum.c
@@ -0,0 +1,56 @@
1/* vi: set sw=4 ts=4: */
2/*
3 * cksum - calculate the CRC32 checksum of a file
4 *
5 * Copyright (C) 2006 by Rob Sullivan, with ideas from code by Walter Harms
6 *
7 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. */
8
9#include <stdio.h>
10#include <unistd.h>
11#include <fcntl.h>
12#include <inttypes.h>
13#include "busybox.h"
14
15int cksum_main(int argc, char **argv) {
16
17 uint32_t *crc32_table = bb_crc32_filltable(1);
18
19 FILE *fp;
20 uint32_t crc;
21 long length, filesize;
22 int bytes_read;
23 char *cp;
24 RESERVE_CONFIG_BUFFER(buf, BUFSIZ);
25 int inp_stdin = (argc == optind) ? 1 : 0;
26
27 do {
28 fp = bb_wfopen_input((inp_stdin) ? bb_msg_standard_input : *++argv);
29
30 crc = 0;
31 length = 0;
32
33 while ((bytes_read = fread(buf, 1, BUFSIZ, fp)) > 0) {
34 cp = buf;
35 length += bytes_read;
36 while (bytes_read--)
37 crc = (crc << 8) ^ crc32_table[((crc >> 24) ^ (*cp++)) & 0xffL];
38 }
39
40 filesize = length;
41
42 for (; length; length >>= 8)
43 crc = (crc << 8) ^ crc32_table[((crc >> 24) ^ length) & 0xffL];
44 crc ^= 0xffffffffL;
45
46 if (inp_stdin) {
47 printf("%"PRIu32" %li\n", crc, filesize);
48 break;
49 }
50
51 printf("%"PRIu32" %li %s\n", crc, filesize, *argv);
52 fclose(fp);
53 } while (*(argv+1));
54
55 return EXIT_SUCCESS;
56}