coreutils

General Software Utilities
git clone http://git.omkov.net/coreutils
Log | Tree | Refs | README | LICENCE | Download

AuthorJamozed <[email protected]>
Date2020-07-26 09:28:20
Commit5e04987bc5a288fcafccfe0fbfe5e8b13c18661b
Parent6351c50493c777f7cc6146e7f060a7a6573867db

sum: Add sum utility

Diffstat

M CMakeLists.txt | 1 +
M README.md | 1 +
A src/sum.c | 139 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

3 files changed, 141 insertions, 0 deletions

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 1676019..004389c 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -34,6 +34,7 @@ ADD_EXECUTABLE(rand     ${PROJECT_SOURCE_DIR}/src/rand.c)
 ADD_EXECUTABLE(realpath ${PROJECT_SOURCE_DIR}/src/realpath.c)
 ADD_EXECUTABLE(rmdir    ${PROJECT_SOURCE_DIR}/src/rmdir.c)
 ADD_EXECUTABLE(sleep    ${PROJECT_SOURCE_DIR}/src/sleep.c)
+ADD_EXECUTABLE(sum      ${PROJECT_SOURCE_DIR}/src/sum.c)
 ADD_EXECUTABLE(sync     ${PROJECT_SOURCE_DIR}/src/sync.c)
 ADD_EXECUTABLE(tee      ${PROJECT_SOURCE_DIR}/src/tee.c)
 ADD_EXECUTABLE(time     ${PROJECT_SOURCE_DIR}/src/time.c)
diff --git a/README.md b/README.md
index d69b0e2..153b3e4 100644
--- a/README.md
+++ b/README.md
@@ -28,6 +28,7 @@ UNIX-like systems.
 | realpath         | Resolve an absolute pathname             |          |
 | rmdir            | Remove directories                       | POSIX    |
 | sleep            | Suspend execution for an interval        | POSIX    |
+| sum              | Write file checksums and block counts    |          |
 | sync             | Synchronise file system caches to disk   |          |
 | tee              | Duplicate standard input                 | POSIX    |
 | time             | Time a simple command                    | POSIX    |
diff --git a/src/sum.c b/src/sum.c
new file mode 100644
index 0000000..5e2ad25
--- /dev/null
+++ b/src/sum.c
@@ -0,0 +1,139 @@
+// sum.c, version 1.0.0
+// OMKOV coreutils sum
+// Copyright (C) 2020, Jakob Wakeling
+// All rights reserved.
+
+/*
+OMKOV Permissive Licence, version 1.0
+
+Copyright (C) 2020, Jakob Wakeling
+All rights reserved.
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of
+this software and associated documentation files (the "Software"), to deal with
+the Software without restriction, including without limitation the rights to
+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
+the Software, and to permit persons to whom the Software is furnished to do so,
+subject to the following conditions:
+
+* Redistributions of source code must retain the above copyright notice, this
+  list of conditions and the following disclaimers.
+* Redistributions in binary form must reproduce the above copyright notice, this
+  list of conditions and the following disclaimers in the documentation and/or
+  other materials provided with the distribution.
+* Neither the names of the copyright holders, nor the names of its contributors
+  may be used to endorse or promote products derived from this Software without
+  specific prior written permission.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
+FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT
+HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE SOFTWARE.
+*/
+
+#include "optget.h"
+
+#include <error.h>
+
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+
+#define VERSION "1.0.0"
+
+static inline int sum(const char *file);
+
+static inline uint32_t bsd(FILE *fi, size_t *fl);
+static inline uint32_t sysv(FILE *fi, size_t *fl);
+
+static uint32_t (*fn)(FILE *, size_t *) = bsd;
+
+static void help(void);
+static void version(void);
+
+int main(int ac, char *av[]) {
+	lop_t lops[] = {
+		{ "help",    ARG_NUL, 256 },
+		{ "version", ARG_NUL, 257 },
+		{ NULL, 0, 0 }
+	};
+	
+	opt_t opt = OPTGET_INIT; opt.str = "rs"; opt.lops = lops; int o;
+	while ((o = optget(&opt, av, 1)) != -1) switch (o) {
+	case 'r': { fn = bsd; break; }
+	case 's': { fn = sysv; break; }
+	case 256: { help(); return 0; }
+	case 257: { version(); return 0; }
+	default: { return 1; }
+	}
+	
+	bool warned = false;
+	
+	if (opt.ind == ac) { sum(NULL); }
+	else for (char **p = &av[opt.ind]; *p; ++p) if (sum(*p)) {
+		warn("%s: %s", *p, serr()); warned = true;
+	}
+	
+	return warned;
+}
+
+static inline int sum(const char *file) {
+	FILE *fi; size_t bl = 0; register uint32_t sum;
+	
+	if (!file || (file[0] == '-' && file[1] == 0)) { fi = stdin; }
+	else if (!(fi = fopen(file, "r"))) { return 1; }
+	
+	sum = fn(fi, &bl);
+	
+	printf("%u %zu", sum, bl);
+	if (file) { printf(" %s", file); } fputc('\n', stdout);
+	
+	if (fi != stdin) { fclose(fi); } return 0;
+}
+
+static inline uint32_t bsd(FILE *fi, size_t *bl) {
+	uint8_t buf[BUFSIZ * 16]; register uint16_t sum = 0;
+	
+	for (size_t c; (c = fread(buf, 1, sizeof (buf), fi)); *bl += c) {
+		for (register size_t i = 0; i != c; ++i) {
+			sum = (sum >> 1) + ((sum & 1) << 15);
+			sum += buf[i]; sum &= 0xFFFF;
+		}
+	}
+	
+	*bl = *bl / 1024 + 1; return sum;
+}
+
+static inline uint32_t sysv(FILE *fi, size_t *bl) {
+	uint8_t buf[BUFSIZ * 16]; register uint32_t sum = 0;
+	
+	for (size_t c; (c = fread(buf, 1, sizeof (buf), fi)); *bl += c) {
+		for (register size_t i = 0; i != c; ++i) { sum += buf[i]; }
+	}
+	
+	sum = (sum & 0xFFFF) + ((sum & 0xFFFFFFFF) >> 16);
+	sum = (sum & 0xFFFF) + (sum >> 16);
+	
+	*bl = *bl / 512 + 1; return sum;
+}
+
+static void help(void) {
+	puts("sum - write file checksums and block counts\n");
+	puts("usage: sum [-rs] [file...]\n");
+	puts("options:");
+	puts("  -r         Use BSD algorithm and 1024 byte blocks");
+	puts("  -s         Use System V algorithm and 512 byte blocks");
+	puts("  --help     Display help information");
+	puts("  --version  Display version information");
+	return;
+}
+
+static void version(void) {
+	puts("OMKOV coreutils sum, version " VERSION);
+	puts("Copyright (C) 2020, Jakob Wakeling");
+	puts("All rights reserved.");
+	puts("OMKOV Permissive Licence (https://www.omkov.net/OLPE)");
+	return;
+}