annotate toys/posix/cksum.c @ 1207:22739b6d5a0e draft

Add -H option to cksum (hex output), fix to use FLAG macros.
author Rob Landley <rob@landley.net>
date Sat, 22 Feb 2014 08:02:09 -0600
parents 6cc69be43c42
children
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
1207
22739b6d5a0e Add -H option to cksum (hex output), fix to use FLAG macros.
Rob Landley <rob@landley.net>
parents: 780
diff changeset
7 USE_CKSUM(NEWTOY(cksum, "HIPLN", 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
1207
22739b6d5a0e Add -H option to cksum (hex output), fix to use FLAG macros.
Rob Landley <rob@landley.net>
parents: 780
diff changeset
18 -H Hexadecimal checksum (defaults to decimal)
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
19 -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
20 -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
21 -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
22 -N Do not include length in CRC calculation
333
d5d8f9a6e649 Add cksum.
Rob Landley <rob@landley.net>
parents:
diff changeset
23 */
d5d8f9a6e649 Add cksum.
Rob Landley <rob@landley.net>
parents:
diff changeset
24
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
25 #define FOR_cksum
333
d5d8f9a6e649 Add cksum.
Rob Landley <rob@landley.net>
parents:
diff changeset
26 #include "toys.h"
d5d8f9a6e649 Add cksum.
Rob Landley <rob@landley.net>
parents:
diff changeset
27
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
28 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
29 unsigned crc_table[256];
333
d5d8f9a6e649 Add cksum.
Rob Landley <rob@landley.net>
parents:
diff changeset
30 )
d5d8f9a6e649 Add cksum.
Rob Landley <rob@landley.net>
parents:
diff changeset
31
337
aaafa1ceaa91 Add -N, -I, -L, and -P options to cksum.
Rob Landley <rob@landley.net>
parents: 336
diff changeset
32 static unsigned cksum_be(unsigned crc, unsigned char c)
333
d5d8f9a6e649 Add cksum.
Rob Landley <rob@landley.net>
parents:
diff changeset
33 {
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
34 return (crc<<8)^TT.crc_table[(crc>>24)^c];
333
d5d8f9a6e649 Add cksum.
Rob Landley <rob@landley.net>
parents:
diff changeset
35 }
d5d8f9a6e649 Add cksum.
Rob Landley <rob@landley.net>
parents:
diff changeset
36
337
aaafa1ceaa91 Add -N, -I, -L, and -P options to cksum.
Rob Landley <rob@landley.net>
parents: 336
diff changeset
37 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
38 {
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
39 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
40 }
aaafa1ceaa91 Add -N, -I, -L, and -P options to cksum.
Rob Landley <rob@landley.net>
parents: 336
diff changeset
41
333
d5d8f9a6e649 Add cksum.
Rob Landley <rob@landley.net>
parents:
diff changeset
42 static void do_cksum(int fd, char *name)
d5d8f9a6e649 Add cksum.
Rob Landley <rob@landley.net>
parents:
diff changeset
43 {
1207
22739b6d5a0e Add -H option to cksum (hex output), fix to use FLAG macros.
Rob Landley <rob@landley.net>
parents: 780
diff changeset
44 unsigned crc = (toys.optflags & FLAG_P) ? 0xffffffff : 0;
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
45 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
46 unsigned (*cksum)(unsigned crc, unsigned char c);
333
d5d8f9a6e649 Add cksum.
Rob Landley <rob@landley.net>
parents:
diff changeset
47
1207
22739b6d5a0e Add -H option to cksum (hex output), fix to use FLAG macros.
Rob Landley <rob@landley.net>
parents: 780
diff changeset
48 cksum = (toys.optflags & FLAG_L) ? cksum_le : cksum_be;
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
49 // CRC the data
333
d5d8f9a6e649 Add cksum.
Rob Landley <rob@landley.net>
parents:
diff changeset
50
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
51 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
52 int len, i;
333
d5d8f9a6e649 Add cksum.
Rob Landley <rob@landley.net>
parents:
diff changeset
53
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
54 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
55 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
56 if (len<1) break;
333
d5d8f9a6e649 Add cksum.
Rob Landley <rob@landley.net>
parents:
diff changeset
57
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
58 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
59 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
60 }
333
d5d8f9a6e649 Add cksum.
Rob Landley <rob@landley.net>
parents:
diff changeset
61
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
62 // CRC the length
333
d5d8f9a6e649 Add cksum.
Rob Landley <rob@landley.net>
parents:
diff changeset
63
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
64 llen2 = llen;
1207
22739b6d5a0e Add -H option to cksum (hex output), fix to use FLAG macros.
Rob Landley <rob@landley.net>
parents: 780
diff changeset
65 if (!(toys.optflags & FLAG_N)) {
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
66 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
67 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
68 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
69 }
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
70 }
333
d5d8f9a6e649 Add cksum.
Rob Landley <rob@landley.net>
parents:
diff changeset
71
1207
22739b6d5a0e Add -H option to cksum (hex output), fix to use FLAG macros.
Rob Landley <rob@landley.net>
parents: 780
diff changeset
72 printf((toys.optflags & FLAG_H) ? "%x" : "%u",
22739b6d5a0e Add -H option to cksum (hex output), fix to use FLAG macros.
Rob Landley <rob@landley.net>
parents: 780
diff changeset
73 (toys.optflags & FLAG_I) ? crc : ~crc);
22739b6d5a0e Add -H option to cksum (hex output), fix to use FLAG macros.
Rob Landley <rob@landley.net>
parents: 780
diff changeset
74 printf(" %"PRIu64, llen2);
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
75 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
76 xputc('\n');
333
d5d8f9a6e649 Add cksum.
Rob Landley <rob@landley.net>
parents:
diff changeset
77 }
d5d8f9a6e649 Add cksum.
Rob Landley <rob@landley.net>
parents:
diff changeset
78
d5d8f9a6e649 Add cksum.
Rob Landley <rob@landley.net>
parents:
diff changeset
79 void cksum_main(void)
d5d8f9a6e649 Add cksum.
Rob Landley <rob@landley.net>
parents:
diff changeset
80 {
1207
22739b6d5a0e Add -H option to cksum (hex output), fix to use FLAG macros.
Rob Landley <rob@landley.net>
parents: 780
diff changeset
81 crc_init(TT.crc_table, toys.optflags & FLAG_L);
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
82 loopfiles(toys.optargs, do_cksum);
333
d5d8f9a6e649 Add cksum.
Rob Landley <rob@landley.net>
parents:
diff changeset
83 }