summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/encryption.c50
-rw-r--r--src/shared.c5
2 files changed, 42 insertions, 13 deletions
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 @@
1#define _GNU_SOURCE
2
1#include "encryption.h" 3#include "encryption.h"
2#include "shared.h" 4#include "shared.h"
3 5
4#include <sodium.h> 6#include <sodium.h>
5 7
8#include <sys/types.h>
9#include <sys/stat.h>
6#include <stdarg.h> 10#include <stdarg.h>
11#include <unistd.h>
7#include <errno.h> 12#include <errno.h>
8#include <error.h> 13#include <error.h>
14#include <fcntl.h>
9#include <stdio.h> 15#include <stdio.h>
10 16
11int checkSodium(void) { 17int checkSodium(void) {
@@ -30,25 +36,47 @@ int checkSodium(void) {
30 36
31 37
32int maketmp(const char *dest, const char *format, ...) { 38int maketmp(const char *dest, const char *format, ...) {
39 char *filename = NULL, *fullpath = NULL;
40 struct stat fb;
41 int fd = -1;
33 va_list ap; 42 va_list ap;
34 va_start(ap, format);
35 43
36 44 // Make sure the destination exists and is a directory
45 if(stat(dest, &fb) < 0)
46 return -1;
47 if(!S_ISDIR(fb.st_mode))
48 return -2;
37 49
50 // Get the first half of the filename
51 va_start(ap, format);
52 if(vasprintf(&filename, format, ap) < 0)
53 return -3;
38 va_end(ap); 54 va_end(ap);
39 return 0; 55
56 // Get the second half of the filename
57 int fps = asprintf(&fullpath, "%s%s", dest, filename); // Hack to not duplicate `free(filename)`
58 free(filename);
59 if(fps < 0)
60 return -4;
61
62 // Open the temp file
63 if((fd = open(dest, (O_WRONLY | O_CLOEXEC | O_CREAT | O_TMPFILE), S_IWUSR)) < 0)
64 fd = -5;
65
66 free(fullpath);
67 return fd;
40} 68}
41 69
42 70
43 71
44int main() { 72int main(int argc, char *argv[]) {
45 char *test = NULL; 73 if(argc != 3)
46 74 error(1, 0, "USAGE: <dest> <filename>");
47 // Turns out GNU did this for me, and I trust their code more than my own, so I'm using this now 75
48 if(asprintf(&test, "We do a little trolling %d", 900) < 0) 76
49 error(1, ENOMEM, "asprintf call failed"); 77 int fd = maketmp(argv[1], "%s.test", argv[2]);
78 if(fd < 0)
79 error(1, errno, "Couldn't open temp file");
50 80
51 printf("%s\n", test);
52
53 return 0; 81 return 0;
54} \ No newline at end of file 82} \ No newline at end of file
diff --git a/src/shared.c b/src/shared.c
index 2f6dd5b..7be3c28 100644
--- a/src/shared.c
+++ b/src/shared.c
@@ -63,11 +63,12 @@ int vasprintf(char **str, const char *format, va_list ap) {
63 63
64int asprintf(char **str, const char *format, ...) { 64int asprintf(char **str, const char *format, ...) {
65 va_list ap; 65 va_list ap;
66 int ret;
66 67
67 va_start(ap, format); 68 va_start(ap, format);
68 int ret = vasprintf(str, format, ap); 69 ret = vasprintf(str, format, ap);
69 va_end(ap); 70 va_end(ap);
70 71
71 return ret; 72 return ret;
72} 73}
73 74