From 779fc0a3e637fe8b339c04b7a79984a324edfcf8 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Sat, 11 Aug 2018 19:33:38 -0400 Subject: fastsum --- src/fastsum.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 src/fastsum.c (limited to 'src/fastsum.c') diff --git a/src/fastsum.c b/src/fastsum.c new file mode 100644 index 0000000..4578ff2 --- /dev/null +++ b/src/fastsum.c @@ -0,0 +1,53 @@ +#include /* for errno */ +#include /* for error(3gnu) */ +#include /* for O_RDONLY */ +#include /* for PRI*, uint*_t, int*_t */ +#include /* for bool */ +#include /* for printf */ +#include /* for open(2), fstat(2) and struct stat */ +#include /* for close(2), read(2), lseek(2), SEEK_SET */ + +int main(int argc, char *argv[]) { + bool err = false; + for (int i = 1; i < argc; i++) { + const char *filename = argv[i]; + + int fd = open(filename, O_RDONLY); + if (fd < 0) { + err = true; + error(0, errno, "open %s", filename); + continue; + } + + struct stat st; + if (fstat(fd, &st) < 0) { + err = true; + error(0, errno, "fstat %s", filename); + close(fd); + continue; + } + + if (lseek(fd, st.st_size/2, SEEK_SET) < 0) { + err = true; + error(0, errno, "lseek %s", filename); + close(fd); + continue; + } + + uint8_t b; + int n = read(fd, &b, sizeof(b)); + if (n <= 0) { + if (n == 0) + error(0, 0, "read %s: unexpected end of file", filename); + else + error(0, errno, "read %s", filename); + err = true; + close(fd); + continue; + } + + printf("%"PRId64":%"PRIx8" %s\n", (int64_t)st.st_size, b, filename); + close(fd); + } + return err ? 1 : 0; +} -- cgit v1.2.3