From 3a9241add947cb6d24b5de7a8927517426a78795 Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Sat, 25 Aug 2012 14:25:22 -0500 Subject: Move commands into "posix", "lsb", and "other" menus/directories. --- toys/posix/cksum.c | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 toys/posix/cksum.c (limited to 'toys/posix/cksum.c') diff --git a/toys/posix/cksum.c b/toys/posix/cksum.c new file mode 100644 index 00000000..213bc74a --- /dev/null +++ b/toys/posix/cksum.c @@ -0,0 +1,87 @@ +/* vi: set sw=4 ts=4: + * + * cksum.c - produce crc32 checksum value for each input + * + * Copyright 2008 Rob Landley + * + * See http://www.opengroup.org/onlinepubs/009695399/utilities/cksum.html + +USE_CKSUM(NEWTOY(cksum, "IPLN", TOYFLAG_BIN)) + +config CKSUM + bool "cksum" + default y + help + usage: cksum [-IPLN] [file...] + + For each file, output crc32 checksum value, length and name of file. + If no files listed, copy from stdin. Filename "-" is a synonym for stdin. + + -L Little endian (defaults to big endian) + -P Pre-inversion + -I Skip post-inversion + -N Do not include length in CRC calculation +*/ + +#include "toys.h" + +DEFINE_GLOBALS( + unsigned crc_table[256]; +) + +#define TT this.cksum + +static unsigned cksum_be(unsigned crc, unsigned char c) +{ + return (crc<<8)^TT.crc_table[(crc>>24)^c]; +} + +static unsigned cksum_le(unsigned crc, unsigned char c) +{ + return TT.crc_table[(crc^c)&0xff] ^ (crc>>8); +} + +static void do_cksum(int fd, char *name) +{ + unsigned crc = (toys.optflags&4) ? 0xffffffff : 0; + uint64_t llen = 0, llen2; + unsigned (*cksum)(unsigned crc, unsigned char c); + + + cksum = (toys.optflags&2) ? cksum_le : cksum_be; + // CRC the data + + for (;;) { + int len, i; + + len = read(fd, toybuf, sizeof(toybuf)); + if (len<0) { + perror_msg("%s",name); + toys.exitval = EXIT_FAILURE; + } + if (len<1) break; + + llen += len; + for (i=0; i>= 8; + } + } + + printf("%u %"PRIu64, (toys.optflags&8) ? crc : ~crc, llen2); + if (strcmp("-", name)) printf(" %s", name); + xputc('\n'); +} + +void cksum_main(void) +{ + crc_init(TT.crc_table, toys.optflags&2); + loopfiles(toys.optargs, do_cksum); +} -- cgit v1.2.3