summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore1
-rw-r--r--Makefile14
-rw-r--r--levd/Makefile13
-rw-r--r--levd/levd.c77
-rw-r--r--newpass/.gitignore1
-rw-r--r--newpass/Makefile13
-rw-r--r--newpass/core/newpass.c132
7 files changed, 251 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..5e56e04
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+/bin
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..f6bba39
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,14 @@
+.PHONY: all
+all: bin levd newpass
+
+.PHONY: bin
+bin:
+ mkdir -p bin/
+
+.PHONY: newpass
+newpass:
+ cd newpass/; $(MAKE)
+
+.PHONY: levd
+levd:
+ cd levd/; $(MAKE)
diff --git a/levd/Makefile b/levd/Makefile
new file mode 100644
index 0000000..9088129
--- /dev/null
+++ b/levd/Makefile
@@ -0,0 +1,13 @@
+CFILES = \
+ levd.c
+
+OUT_FILE = ../bin/levd
+CFLAGS = -Wall -pedantic
+CC = clang
+
+.PHONY: all
+all: $(OUT_FILE)
+
+.PHONY: $(OUT_FILE)
+$(OUT_FILE):
+ $(CC) $(CFLAGS) $(CFILES) -o $@
diff --git a/levd/levd.c b/levd/levd.c
new file mode 100644
index 0000000..16da6d5
--- /dev/null
+++ b/levd/levd.c
@@ -0,0 +1,77 @@
+/*
+ * Copyright (c) 2026, Chloe M., et al.
+ * Provided under the BSD-3 clause.
+ */
+
+#include <stdio.h>
+#include <string.h>
+
+/*
+ * Compute the levenshtein distance between two strings
+ *
+ * @s1: First string to compare
+ * @s2: Second string to compare
+ * @l1: Length of first string to compare
+ * @l2: Length of second string to compare
+ */
+static int
+lev_distance(const char *s1, const char *s2, size_t l1, size_t l2)
+{
+ int matrix[l1 + 1][l2 + 1];
+ int i, j, c1, c2;
+ int delete, insert;
+ int subst, min;
+
+ for (i = 0; i <= l1; ++i) {
+ matrix[i][0] = i;
+ }
+
+ for (i = 0; i < l2; ++i) {
+ matrix[0][i] = i;
+ }
+
+ for (i = 1; i <= l1; ++i) {
+ c1 = s1[i - 1];
+ for (j = 1; j <= l2; ++j) {
+ c2 = s2[j - 1];
+ if (c1 == c2) {
+ matrix[i][j] = matrix[i-1][j-1];
+ } else {
+ delete = matrix[i-1][j] + 1;
+ insert = matrix[i][j-1] + 1;
+ subst = matrix[i-1][j-1] + 1;
+ min = delete;
+
+ if (insert < min)
+ min = insert;
+ if (subst < min)
+ min = subst;
+
+ matrix[i][j] = min;
+ }
+ }
+ }
+
+ return matrix[l1][l2];
+}
+
+int
+main(int argc, char **argv)
+{
+ char *s1, *s2;
+ size_t l1, l2;
+
+ if (argc < 3) {
+ printf("fatal: expected s1 and s2\n");
+ return -1;
+ }
+
+ s1 = argv[1];
+ s2 = argv[2];
+
+ l1 = strlen(s1);
+ l2 = strlen(s2);
+
+ printf("%d\n", lev_distance(s1, s2, l1, l2));
+ return 0;
+}
diff --git a/newpass/.gitignore b/newpass/.gitignore
new file mode 100644
index 0000000..345c0c3
--- /dev/null
+++ b/newpass/.gitignore
@@ -0,0 +1 @@
+/newpass
diff --git a/newpass/Makefile b/newpass/Makefile
new file mode 100644
index 0000000..8d28a99
--- /dev/null
+++ b/newpass/Makefile
@@ -0,0 +1,13 @@
+CFILES = \
+ core/newpass.c
+
+OUT_FILE = ../bin/newpass
+CFLAGS = -Wall -pedantic
+CC = clang
+
+.PHONY: all
+all: $(OUT_FILE)
+
+.PHONY: $(OUT_FILE)
+$(OUT_FILE):
+ $(CC) $(CFLAGS) $(CFILES) -o $@
diff --git a/newpass/core/newpass.c b/newpass/core/newpass.c
new file mode 100644
index 0000000..93b481f
--- /dev/null
+++ b/newpass/core/newpass.c
@@ -0,0 +1,132 @@
+/*
+ * Copyright (c) 2026, Chloe M., et al.
+ * Provided under the BSD-3 clause.
+ */
+
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <fcntl.h>
+#include <assert.h>
+#include <unistd.h>
+
+static const char chrtab[] = {
+ "ab123cde987654321=0fghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWZYZ"
+ "!@#$%^&*()[]><?"
+};
+
+static void
+help(void)
+{
+ printf("usage: ./newpass [flags]\n");
+ printf("[-h] Display this help menu\n");
+ printf("[-l] Length of password to create\n");
+ printf("[-c] Number of passwords to generate\n");
+}
+
+static void *
+xmalloc(size_t sz)
+{
+ void *buf;
+
+#ifdef __OpenBSD__
+ buf = malloc_conceal(sz);
+#else
+ buf = malloc(sz);
+#endif
+
+ assert(buf != NULL && "allocation failure");
+ return buf;
+}
+
+static uint8_t *
+randbytes(size_t count)
+{
+ int fd;
+ uint8_t *buf;
+ ssize_t nbyte;
+
+ buf = xmalloc(count);
+ fd = open("/dev/urandom", O_RDONLY);
+
+ if (fd < 0) {
+ perror("open");
+ free(buf);
+ return NULL;
+ }
+
+ if ((nbyte = read(fd, buf, count)) < 0) {
+ perror("read");
+ free(buf);
+ close(fd);
+ return NULL;
+ }
+
+ return buf;
+}
+
+static void
+genpass(size_t passlen)
+{
+ uint8_t ind, *bytes;
+ size_t i;
+
+ if ((bytes = randbytes(passlen)) == NULL) {
+ printf("fatal: unable to generate random bytes\n");
+ return;
+ }
+
+ for (i = 0; i < passlen; ++i) {
+ ind = bytes[i] % sizeof(chrtab);
+ printf("%c", chrtab[ind]);
+ }
+
+ printf("\n");
+ free(bytes);
+}
+
+int
+main(int argc, char **argv)
+{
+ int opt;
+ size_t passlen = 0;
+ size_t i, passcount = 0;
+
+ while ((opt = getopt(argc, argv, "hl:c:")) != -1) {
+ switch (opt) {
+ case 'h':
+ help();
+ return -1;
+ case 'l':
+ if ((passlen = atoi(optarg)) == 0) {
+ printf("fatal: bad length given\n");
+ return -1;
+ }
+
+ break;
+ case 'c':
+ if ((passcount = atoi(optarg)) == 0) {
+ printf("fatal: bad count given\n");
+ return -1;
+ }
+
+ break;
+ }
+ }
+
+ if (passcount == 0) {
+ ++passcount;
+ }
+
+ if (passlen == 0) {
+ printf("fatal: please specify a length with '-l'\n");
+ help();
+ return -1;
+ }
+
+ for (i = 0; i < passcount; ++i) {
+ genpass(passlen);
+ }
+
+ return 0;
+}