annotate toys/posix/cksum.c @ 943:8cb7d65a40e8

here's a version of paste. It doesn't deal with wide characters yet and likely behaves very badly when given too many files or delimiters.
author Felix Janda <felix.janda@posteo.de>
date Wed, 03 Jul 2013 02:29:24 -0500
parents 6cc69be43c42
children 22739b6d5a0e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
1 /* cksum.c - produce crc32 checksum value for each input
333
d5d8f9a6e649 Add cksum.
Rob Landley <rob@landley.net>
parents:
diff changeset
2 *
d5d8f9a6e649 Add cksum.
Rob Landley <rob@landley.net>
parents:
diff changeset
3 * Copyright 2008 Rob Landley <rob@landley.net>
d5d8f9a6e649 Add cksum.
Rob Landley <rob@landley.net>
parents:
diff changeset
4 *
656
6df4ccc0acbe Regularize command headers, update links to standards documents.
Rob Landley <rob@landley.net>
parents: 653
diff changeset
5 * See http://opengroup.org/onlinepubs/9699919799/utilities/cksum.html
333
d5d8f9a6e649 Add cksum.
Rob Landley <rob@landley.net>
parents:
diff changeset
6
337
aaafa1ceaa91 Add -N, -I, -L, and -P options to cksum.
Rob Landley <rob@landley.net>
parents: 336
diff changeset
7 USE_CKSUM(NEWTOY(cksum, "IPLN", TOYFLAG_BIN))
333
d5d8f9a6e649 Add cksum.
Rob Landley <rob@landley.net>
parents:
diff changeset
8
d5d8f9a6e649 Add cksum.
Rob Landley <rob@landley.net>
parents:
diff changeset
9 config CKSUM
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
10 bool "cksum"
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
11 default y
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
12 help
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
13 usage: cksum [-IPLN] [file...]
336
0c816f5abe22 Add -F option to cksum.
Rob Landley <rob@landley.net>
parents: 333
diff changeset
14
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
15 For each file, output crc32 checksum value, length and name of file.
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
16 If no files listed, copy from stdin. Filename "-" is a synonym for stdin.
336
0c816f5abe22 Add -F option to cksum.
Rob Landley <rob@landley.net>
parents: 333
diff changeset
17
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
18 -L Little endian (defaults to big endian)
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
19 -P Pre-inversion
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
20 -I Skip post-inversion
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
21 -N Do not include length in CRC calculation
333
d5d8f9a6e649 Add cksum.
Rob Landley <rob@landley.net>
parents:
diff changeset
22 */
d5d8f9a6e649 Add cksum.
Rob Landley <rob@landley.net>
parents:
diff changeset
23
674
7e846e281e38 New build infrastructure to generate FLAG_ macros and TT alias, #define FOR_commandname before #including toys.h to trigger it. Rename DEFINE_GLOBALS() to just GLOBALS() (because I could never remember if it was DECLARE_GLOBALS). Convert existing commands to use new infrastructure, and replace optflag constants with FLAG_ macros where appropriate.
Rob Landley <rob@landley.net>
parents: 656
diff changeset
24 #define FOR_cksum
333
d5d8f9a6e649 Add cksum.
Rob Landley <rob@landley.net>
parents:
diff changeset
25 #include "toys.h"
d5d8f9a6e649 Add cksum.
Rob Landley <rob@landley.net>
parents:
diff changeset
26
674
7e846e281e38 New build infrastructure to generate FLAG_ macros and TT alias, #define FOR_commandname before #including toys.h to trigger it. Rename DEFINE_GLOBALS() to just GLOBALS() (because I could never remember if it was DECLARE_GLOBALS). Convert existing commands to use new infrastructure, and replace optflag constants with FLAG_ macros where appropriate.
Rob Landley <rob@landley.net>
parents: 656
diff changeset
27 GLOBALS(
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
28 unsigned crc_table[256];
333
d5d8f9a6e649 Add cksum.
Rob Landley <rob@landley.net>
parents:
diff changeset
29 )
d5d8f9a6e649 Add cksum.
Rob Landley <rob@landley.net>
parents:
diff changeset
30
337
aaafa1ceaa91 Add -N, -I, -L, and -P options to cksum.
Rob Landley <rob@landley.net>
parents: 336
diff changeset
31 static unsigned cksum_be(unsigned crc, unsigned char c)
333
d5d8f9a6e649 Add cksum.
Rob Landley <rob@landley.net>
parents:
diff changeset
32 {
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
33 return (crc<<8)^TT.crc_table[(crc>>24)^c];
333
d5d8f9a6e649 Add cksum.
Rob Landley <rob@landley.net>
parents:
diff changeset
34 }
d5d8f9a6e649 Add cksum.
Rob Landley <rob@landley.net>
parents:
diff changeset
35
337
aaafa1ceaa91 Add -N, -I, -L, and -P options to cksum.
Rob Landley <rob@landley.net>
parents: 336
diff changeset
36 static unsigned cksum_le(unsigned crc, unsigned char c)
aaafa1ceaa91 Add -N, -I, -L, and -P options to cksum.
Rob Landley <rob@landley.net>
parents: 336
diff changeset
37 {
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
38 return TT.crc_table[(crc^c)&0xff] ^ (crc>>8);
337
aaafa1ceaa91 Add -N, -I, -L, and -P options to cksum.
Rob Landley <rob@landley.net>
parents: 336
diff changeset
39 }
aaafa1ceaa91 Add -N, -I, -L, and -P options to cksum.
Rob Landley <rob@landley.net>
parents: 336
diff changeset
40
333
d5d8f9a6e649 Add cksum.
Rob Landley <rob@landley.net>
parents:
diff changeset
41 static void do_cksum(int fd, char *name)
d5d8f9a6e649 Add cksum.
Rob Landley <rob@landley.net>
parents:
diff changeset
42 {
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
43 unsigned crc = (toys.optflags&4) ? 0xffffffff : 0;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
44 uint64_t llen = 0, llen2;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
45 unsigned (*cksum)(unsigned crc, unsigned char c);
333
d5d8f9a6e649 Add cksum.
Rob Landley <rob@landley.net>
parents:
diff changeset
46
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
47 cksum = (toys.optflags&2) ? cksum_le : cksum_be;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
48 // CRC the data
333
d5d8f9a6e649 Add cksum.
Rob Landley <rob@landley.net>
parents:
diff changeset
49
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
50 for (;;) {
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
51 int len, i;
333
d5d8f9a6e649 Add cksum.
Rob Landley <rob@landley.net>
parents:
diff changeset
52
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
53 len = read(fd, toybuf, sizeof(toybuf));
780
6cc69be43c42 Have error_msg() and friends set TT.exitval to 1 if it's still 0, clean out other places that were setting it that no longer need to.
Rob Landley <rob@landley.net>
parents: 694
diff changeset
54 if (len<0) perror_msg("%s", name);
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
55 if (len<1) break;
333
d5d8f9a6e649 Add cksum.
Rob Landley <rob@landley.net>
parents:
diff changeset
56
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
57 llen += len;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
58 for (i=0; i<len; i++) crc=cksum(crc, toybuf[i]);
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
59 }
333
d5d8f9a6e649 Add cksum.
Rob Landley <rob@landley.net>
parents:
diff changeset
60
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
61 // CRC the length
333
d5d8f9a6e649 Add cksum.
Rob Landley <rob@landley.net>
parents:
diff changeset
62
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
63 llen2 = llen;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
64 if (!(toys.optflags&1)) {
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
65 while (llen) {
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
66 crc = cksum(crc, llen);
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
67 llen >>= 8;
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
68 }
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
69 }
333
d5d8f9a6e649 Add cksum.
Rob Landley <rob@landley.net>
parents:
diff changeset
70
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
71 printf("%u %"PRIu64, (toys.optflags&8) ? crc : ~crc, llen2);
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
72 if (strcmp("-", name)) printf(" %s", name);
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
73 xputc('\n');
333
d5d8f9a6e649 Add cksum.
Rob Landley <rob@landley.net>
parents:
diff changeset
74 }
d5d8f9a6e649 Add cksum.
Rob Landley <rob@landley.net>
parents:
diff changeset
75
d5d8f9a6e649 Add cksum.
Rob Landley <rob@landley.net>
parents:
diff changeset
76 void cksum_main(void)
d5d8f9a6e649 Add cksum.
Rob Landley <rob@landley.net>
parents:
diff changeset
77 {
694
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
78 crc_init(TT.crc_table, toys.optflags&2);
786841fdb1e0 Reindent to two spaces per level. Remove vi: directives that haven't worked right in years (ubuntu broke its' vim implementation). Remove trailing spaces. Add/remove blank lines. Re-wordwrap in places. Update documentation with new coding style.
Rob Landley <rob@landley.net>
parents: 674
diff changeset
79 loopfiles(toys.optargs, do_cksum);
333
d5d8f9a6e649 Add cksum.
Rob Landley <rob@landley.net>
parents:
diff changeset
80 }