diff options
author | Anton Kling <anton@kling.gg> | 2024-12-09 21:01:08 +0100 |
---|---|---|
committer | Anton Kling <anton@kling.gg> | 2024-12-09 21:01:08 +0100 |
commit | 77be4a54bcc65b74c3b10f60a936a71117c3c274 (patch) | |
tree | c56159c89379cbcc1f3a6146be507e7cc25132d0 | |
parent | 0d2f1ff5ca366f9ba18a349f5e06295f264c5da4 (diff) |
lock: Add lock utility
-rw-r--r-- | userland/minibox/Makefile | 2 | ||||
-rw-r--r-- | userland/minibox/minibox.c | 2 | ||||
-rw-r--r-- | userland/minibox/utilities/include.h | 1 | ||||
-rw-r--r-- | userland/minibox/utilities/lock.c | 44 |
4 files changed, 47 insertions, 2 deletions
diff --git a/userland/minibox/Makefile b/userland/minibox/Makefile index 3301186..a9e7234 100644 --- a/userland/minibox/Makefile +++ b/userland/minibox/Makefile @@ -1,6 +1,6 @@ CC="i686-sb-gcc" CFLAGS=-Wall -Wextra -pedantic -Wimplicit-fallthrough -g -O0 -OBJ=minibox.o utilities/cat.o utilities/echo.o utilities/yes.o utilities/minibox.o utilities/ascii.o utilities/wc.o utilities/init.o utilities/ls.o utilities/touch.o utilities/ed.o utilities/sh/sh.o utilities/sh/lexer.o utilities/sh/ast.o utilities/kill.o utilities/sha1sum.o utilities/rdate.o utilities/true.o utilities/false.o +OBJ=minibox.o utilities/cat.o utilities/echo.o utilities/yes.o utilities/minibox.o utilities/ascii.o utilities/wc.o utilities/init.o utilities/ls.o utilities/touch.o utilities/ed.o utilities/sh/sh.o utilities/sh/lexer.o utilities/sh/ast.o utilities/kill.o utilities/sha1sum.o utilities/rdate.o utilities/true.o utilities/false.o utilities/lock.o %.o: %.c $(CC) $(CFLAGS) $(INCLUDE) $(LIBS) -c $< -o $@ diff --git a/userland/minibox/minibox.c b/userland/minibox/minibox.c index c010f37..9d7ea0c 100644 --- a/userland/minibox/minibox.c +++ b/userland/minibox/minibox.c @@ -22,7 +22,7 @@ Command utilities[] = {COMMAND(minibox), COMMAND(ascii), COMMAND(echo), COMMAND(cat), COMMAND(yes), COMMAND(wc), COMMAND(init), COMMAND(ls), COMMAND(touch), COMMAND(ed), COMMAND(sh), COMMAND(kill), - COMMAND(sha1sum), COMMAND(rdate), COMMAND(true), COMMAND(false)}; + COMMAND(sha1sum), COMMAND(rdate), COMMAND(true), COMMAND(false), COMMAND(lock)}; char *parse_filename(char *str) { char *tmp = NULL, *is = str; diff --git a/userland/minibox/utilities/include.h b/userland/minibox/utilities/include.h index 844559a..cc2990d 100644 --- a/userland/minibox/utilities/include.h +++ b/userland/minibox/utilities/include.h @@ -44,3 +44,4 @@ int sha1sum_main(int argc, char **argv); int rdate_main(int argc, char **argv); int true_main(int argc, char **argv); int false_main(int argc, char **argv); +int lock_main(int argc, char **argv); diff --git a/userland/minibox/utilities/lock.c b/userland/minibox/utilities/lock.c new file mode 100644 index 0000000..027038d --- /dev/null +++ b/userland/minibox/utilities/lock.c @@ -0,0 +1,44 @@ +#include "include.h" +#include <dirent.h> +#include <fcntl.h> +#include <stdio.h> +#include <string.h> +#include <unistd.h> + +int lock_main(int argc, char **argv) { + if (argc < 2) { + fprintf("Usage: %s <password>", argv[0] ? argv[0] : "lock"); + return 1; + } + char *password = argv[1]; + size_t buffer_length = strlen(password) + 1; + char *buffer = malloc(buffer_length); + + int should_exit = 0; + size_t i = 0; + for (;;) { + char c; + if (0 == read(STDIN_FILENO, &c, 1)) { + break; + } + if ('\n' == c) { + break; + } + if (i > buffer_length) { + should_exit = 1; + continue; + } + buffer[i] = c; + i++; + } + buffer[i] = '\0'; + + if (should_exit) { + return 1; + } + + if (0 != strcmp(password, buffer)) { + return 1; + } + return 0; +} |