commit eb63651200bb9dc67b704fda62fca8dbad467991
parent 51e7473b527dfa379c06ff0dc3e2358857a498a4
Author: aabacchus <ben@bvnf.space>
Date: Sun, 23 Apr 2023 22:50:21 +0100
separate an actions.h; distinguish checksums action from functions
Diffstat:
15 files changed, 178 insertions(+), 177 deletions(-)
diff --git a/src/Makefile b/src/Makefile
@@ -2,7 +2,7 @@
XCFLAGS = $(CFLAGS) -Wall -Wextra -Wshadow -pedantic -D_XOPEN_SOURCE=700 -Og -g
-OBJS = utils.o list.o search.o array.o checksum.o pkg.o
+OBJS = utils.o list.o search.o array.o checksum.o checksums.o pkg.o
LIBS = -lblake3
all: ckiss
@@ -27,11 +27,12 @@ clean:
.PHONY: clean test all
-utils.o: ckiss.h array.h utils.h
-test.o: ckiss.h
-list.o: ckiss.h utils.h
-search.o: ckiss.h array.h utils.h
-array.o: ckiss.h array.h utils.h
-checksum.o: ckiss.h pkg.h utils.h
-pkg.o: ckiss.h pkg.h utils.h
-main.o: ckiss.h utils.h
+utils.o: array.h utils.h
+test.o: utils.h
+list.o: actions.h utils.h
+search.o: actions.h array.h utils.h
+array.o: array.h utils.h
+checksum.o: actions.h pkg.h utils.h checksums.h
+checksums.o: utils.h checksums.h
+pkg.o: pkg.h utils.h
+main.o: actions.h utils.h
diff --git a/src/actions.h b/src/actions.h
@@ -0,0 +1,8 @@
+#ifndef _CKISS_ACTIONS_H
+#define _CKISS_ACTIONS_H
+#include "utils.h"
+
+int list(int argc, char **argv, struct env *e);
+int search(int argc, char **argv, struct env *e);
+int checksum(int argc, char **argv, struct env *e);
+#endif
diff --git a/src/array.c b/src/array.c
@@ -1,7 +1,6 @@
#include <stdlib.h>
#include <string.h>
-#include "ckiss.h"
#include "array.h"
#include "utils.h"
diff --git a/src/checksum.c b/src/checksum.c
@@ -1,116 +1,11 @@
-#include <assert.h>
-#include <blake3.h>
#include <stdio.h>
#include <stdlib.h>
-#include <string.h>
-#include "ckiss.h"
+#include "actions.h"
+#include "checksums.h"
#include "pkg.h"
#include "utils.h"
-/* reads f and returns the checksum */
-static char *
-file_checksum(FILE *f) {
- /* This function is essentially copied from b3sum:
- * https://git.sr.ht/~mcf/b3sum/tree/cb4111ccc8061039b014fbb657c72f78984f1069/item/b3sum.c#L19
- * which is used under CC0. */
-
- size_t outlen = 33, len;
- unsigned char *out = malloc(outlen);
- if (out == NULL)
- die_perror("malloc");
-
- size_t buflen = 0x4000;
- char *buf = malloc(buflen);
- if (buf == NULL)
- die_perror("malloc");
-
- blake3_hasher ctx;
- blake3_hasher_init(&ctx);
-
- do {
- len = fread(buf, 1, buflen, f);
- if (len > 0)
- blake3_hasher_update(&ctx, buf, len);
- } while (len == buflen);
- if (ferror(f))
- die_perror("fread");
-
- blake3_hasher_finalize(&ctx, out, outlen);
- free(buf);
-
- size_t hexlen = 2 * outlen;
- char *hex = malloc(hexlen + 1);
- if (hex == NULL)
- die_perror("malloc");
- for (size_t i = 0; i < outlen; i++)
- sprintf(hex + 2*i, "%02x", out[i]);
- hex[hexlen] = '\0';
-
- free(out);
- return hex;
-}
-
-/* returns the checksum of the file specified by s, if needed and if the cache
- * is present (must download first) */
-char *
-source_generate_checksum(struct source *s) {
- assert(s);
- if (s->type != SRC_HTTP && s->type != SRC_FILE)
- return NULL; /* checksum not needed */
-
- FILE *f = fopen(s->cachefile, "rb");
- if (f == NULL)
- die_perror(s->cachefile);
-
- char *c = file_checksum(f);
- fclose(f);
- return c;
-}
-
-/* dies if there is a checksum mismatch. */
-void
-verify_checksums(struct pkg *p) {
- assert(p);
- FILE *f = pkg_open_file(p->pkg_path, "checksums", "r");
- if (f == NULL) {
- if (p->n_need_checksums == 0)
- return;
- else
- die2(p->pkg, "checksums needed but no checksum file");
- }
-
- assert(p->s);
-
- char *buf = NULL;
- size_t bufn = 0;
- ssize_t n;
- for (size_t i = 0; i < p->n; i++) {
- if (p->s[i]->type != SRC_HTTP && p->s[i]->type != SRC_FILE)
- continue;
-
- if ((n = getline(&buf, &bufn, f)) == -1) {
- free(buf);
- fclose(f);
- perror(NULL);
- die2(p->s[i]->remote, "checksums missing");
- }
- if (buf[n - 1] == '\n')
- buf[--n] = '\0';
-
- char *sum = source_generate_checksum(p->s[i]);
- if (strcmp(buf, sum) != 0) {
- free(sum);
- free(buf);
- fclose(f);
- die2(p->s[i]->cachefile, "checksum mismatch");
- }
- free(sum);
- }
- free(buf);
- fclose(f);
-}
-
int
checksum(int argc, char **argv, struct env *e) {
int ret = 0;
diff --git a/src/checksums.c b/src/checksums.c
@@ -0,0 +1,112 @@
+#include <assert.h>
+#include <blake3.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "utils.h"
+#include "checksums.h"
+
+/* reads f and returns the checksum */
+static char *
+file_checksum(FILE *f) {
+ /* This function is essentially copied from b3sum:
+ * https://git.sr.ht/~mcf/b3sum/tree/cb4111ccc8061039b014fbb657c72f78984f1069/item/b3sum.c#L19
+ * which is used under CC0. */
+
+ size_t outlen = 33, len;
+ unsigned char *out = malloc(outlen);
+ if (out == NULL)
+ die_perror("malloc");
+
+ size_t buflen = 0x4000;
+ char *buf = malloc(buflen);
+ if (buf == NULL)
+ die_perror("malloc");
+
+ blake3_hasher ctx;
+ blake3_hasher_init(&ctx);
+
+ do {
+ len = fread(buf, 1, buflen, f);
+ if (len > 0)
+ blake3_hasher_update(&ctx, buf, len);
+ } while (len == buflen);
+ if (ferror(f))
+ die_perror("fread");
+
+ blake3_hasher_finalize(&ctx, out, outlen);
+ free(buf);
+
+ size_t hexlen = 2 * outlen;
+ char *hex = malloc(hexlen + 1);
+ if (hex == NULL)
+ die_perror("malloc");
+ for (size_t i = 0; i < outlen; i++)
+ sprintf(hex + 2*i, "%02x", out[i]);
+ hex[hexlen] = '\0';
+
+ free(out);
+ return hex;
+}
+
+/* returns the checksum of the file specified by s, if needed and if the cache
+ * is present (must download first) */
+char *
+source_generate_checksum(struct source *s) {
+ assert(s);
+ if (s->type != SRC_HTTP && s->type != SRC_FILE)
+ return NULL; /* checksum not needed */
+
+ FILE *f = fopen(s->cachefile, "rb");
+ if (f == NULL)
+ die_perror(s->cachefile);
+
+ char *c = file_checksum(f);
+ fclose(f);
+ return c;
+}
+
+/* dies if there is a checksum mismatch. */
+void
+verify_checksums(struct pkg *p) {
+ assert(p);
+ FILE *f = pkg_open_file(p->pkg_path, "checksums", "r");
+ if (f == NULL) {
+ if (p->n_need_checksums == 0)
+ return;
+ else
+ die2(p->pkg, "checksums needed but no checksum file");
+ }
+
+ assert(p->s);
+
+ char *buf = NULL;
+ size_t bufn = 0;
+ ssize_t n;
+ for (size_t i = 0; i < p->n; i++) {
+ if (p->s[i]->type != SRC_HTTP && p->s[i]->type != SRC_FILE)
+ continue;
+
+ if ((n = getline(&buf, &bufn, f)) == -1) {
+ free(buf);
+ fclose(f);
+ perror(NULL);
+ die2(p->s[i]->remote, "checksums missing");
+ }
+ if (buf[n - 1] == '\n')
+ buf[--n] = '\0';
+
+ char *sum = source_generate_checksum(p->s[i]);
+ if (strcmp(buf, sum) != 0) {
+ free(sum);
+ free(buf);
+ fclose(f);
+ die2(p->s[i]->cachefile, "checksum mismatch");
+ }
+ free(sum);
+ }
+ free(buf);
+ fclose(f);
+}
+
diff --git a/src/checksums.h b/src/checksums.h
@@ -0,0 +1,11 @@
+#ifndef _CKISS_CHECKSUMS_H
+#define _CKISS_CHECKSUMS_H
+#include "pkg.h"
+
+/* returns the checksum of the file specified by s, if needed and if the cache
+ * is present (must download first) */
+char *source_generate_checksum(struct source *s);
+
+/* dies if there is a checksum mismatch. */
+void verify_checksums(struct pkg *p);
+#endif
diff --git a/src/ckiss.h b/src/ckiss.h
@@ -1,53 +0,0 @@
-#ifndef _CKISS_H
-#define _CKISS_H
-
-#include <limits.h>
-#include <stdbool.h>
-#include <stdio.h>
-#include <stdnoreturn.h>
-#include <sys/stat.h>
-#include <unistd.h>
-
-#ifndef noreturn
-#define noreturn
-#endif
-
-#define KISS_VERSION "0.1.0 (compat 5.6.4)"
-
-struct env {
- bool color;
- bool debug;
- bool force;
- bool keeplog;
- bool prompt;
- char **hooks;
- char **kiss_path;
- char **path;
- char *cac_dir;
- char *compress;
- char *elf;
- char *get[7];
- char *pid;
- char *pwd;
- char *root;
- char *su;
- char *sys_db;
- char *tmpdir;
- char date[17]; /* YYYY-MM-DD-HH:MM + '\0' */
-};
-
-/* include these now in case they need struct env */
-#include "pkg.h"
-
-/* returns the checksum of the file specified by s, if needed and if the cache
- * is present (must download first) */
-char *source_generate_checksum(struct source *s);
-
-/* dies if there is a checksum mismatch. */
-void verify_checksums(struct pkg *p);
-
-int list(int argc, char **argv, struct env *e);
-int search(int argc, char **argv, struct env *e);
-int checksum(int argc, char **argv, struct env *e);
-
-#endif
diff --git a/src/list.c b/src/list.c
@@ -3,7 +3,8 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#include "ckiss.h"
+
+#include "actions.h"
#include "utils.h"
void
diff --git a/src/main.c b/src/main.c
@@ -1,8 +1,11 @@
#include <stdio.h>
#include <stdlib.h>
-#include "ckiss.h"
+
+#include "actions.h"
#include "utils.h"
+#define KISS_VERSION "0.1.0 (compat 5.6.4)"
+
noreturn void
usage(int r) {
mylog("ckiss [c|l|s|v] [pkg]...");
diff --git a/src/pkg.c b/src/pkg.c
@@ -3,8 +3,8 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
+#include <unistd.h>
-#include "ckiss.h"
#include "pkg.h"
#include "utils.h"
diff --git a/src/pkg.h b/src/pkg.h
@@ -1,7 +1,7 @@
#ifndef _CKISS_SOURCE_H
#define _CKISS_SOURCE_H
-#include "ckiss.h"
+#include "utils.h"
enum pkg_source_types {
SRC_HTTP,
diff --git a/src/search.c b/src/search.c
@@ -1,7 +1,7 @@
#include <stdio.h>
#include <stdlib.h>
-#include "ckiss.h"
+#include "actions.h"
#include "array.h"
#include "utils.h"
diff --git a/src/test.c b/src/test.c
@@ -1,5 +1,5 @@
#include <stdio.h>
-#include "ckiss.h"
+#include "utils.h"
int
main(int argc, char **argv) {
diff --git a/src/utils.c b/src/utils.c
@@ -6,9 +6,9 @@
#include <string.h>
#include <sys/stat.h>
#include <time.h>
+#include <unistd.h>
#include "array.h"
-#include "ckiss.h"
#include "utils.h"
static char *c1 = "", *c2 = "", *c3 = "";
diff --git a/src/utils.h b/src/utils.h
@@ -3,9 +3,33 @@
#include <stdbool.h>
#include <stdnoreturn.h>
+#include <sys/stat.h>
#include "array.h"
+struct env {
+ bool color;
+ bool debug;
+ bool force;
+ bool keeplog;
+ bool prompt;
+ char **hooks;
+ char **kiss_path;
+ char **path;
+ char *cac_dir;
+ char *compress;
+ char *elf;
+ char *get[7];
+ char *pid;
+ char *pwd;
+ char *root;
+ char *su;
+ char *sys_db;
+ char *tmpdir;
+ char date[17]; /* YYYY-MM-DD-HH:MM + '\0' */
+};
+
+
/* called "mylog" to avoid collision with math.h log function. */
void mylog(const char *s);
void mylog2(const char *name, const char *s);