From 2be40dd8d9930ad66b26cfaa42d1176dd55b42e8 Mon Sep 17 00:00:00 2001 From: "@syxhe" Date: Tue, 7 Jan 2025 17:27:53 -0600 Subject: For posterity --- src/encryption.c | 50 +++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 39 insertions(+), 11 deletions(-) (limited to 'src/encryption.c') diff --git a/src/encryption.c b/src/encryption.c index f7fdcd4..c7928d8 100644 --- a/src/encryption.c +++ b/src/encryption.c @@ -1,11 +1,17 @@ +#define _GNU_SOURCE + #include "encryption.h" #include "shared.h" #include +#include +#include #include +#include #include #include +#include #include int checkSodium(void) { @@ -30,25 +36,47 @@ int checkSodium(void) { int maketmp(const char *dest, const char *format, ...) { + char *filename = NULL, *fullpath = NULL; + struct stat fb; + int fd = -1; va_list ap; - va_start(ap, format); - + // Make sure the destination exists and is a directory + if(stat(dest, &fb) < 0) + return -1; + if(!S_ISDIR(fb.st_mode)) + return -2; + // Get the first half of the filename + va_start(ap, format); + if(vasprintf(&filename, format, ap) < 0) + return -3; va_end(ap); - return 0; + + // Get the second half of the filename + int fps = asprintf(&fullpath, "%s%s", dest, filename); // Hack to not duplicate `free(filename)` + free(filename); + if(fps < 0) + return -4; + + // Open the temp file + if((fd = open(dest, (O_WRONLY | O_CLOEXEC | O_CREAT | O_TMPFILE), S_IWUSR)) < 0) + fd = -5; + + free(fullpath); + return fd; } -int main() { - char *test = NULL; - - // Turns out GNU did this for me, and I trust their code more than my own, so I'm using this now - if(asprintf(&test, "We do a little trolling %d", 900) < 0) - error(1, ENOMEM, "asprintf call failed"); +int main(int argc, char *argv[]) { + if(argc != 3) + error(1, 0, "USAGE: "); + + + int fd = maketmp(argv[1], "%s.test", argv[2]); + if(fd < 0) + error(1, errno, "Couldn't open temp file"); - printf("%s\n", test); - return 0; } \ No newline at end of file -- cgit v1.2.3