From 5d3068832c6094cf3b3ffce89d2398134e939b1f Mon Sep 17 00:00:00 2001 From: "@syxhe" Date: Tue, 21 Jan 2025 17:01:03 -0600 Subject: Write some sample code to encrypt a file via password --- src/shared.c | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) (limited to 'src/shared.c') diff --git a/src/shared.c b/src/shared.c index f3e98ee..40240d5 100644 --- a/src/shared.c +++ b/src/shared.c @@ -81,10 +81,30 @@ int readwholebuffer(char **str, unsigned long int initsize, int fd) { lstr = tmp; } - if(lstr) { + if(lstr && fd == STDIN_FILENO) { lstr[csize - ccap - 1] = '\0'; // Don't include the newline } *str = lstr; return ((bytesread == 0) ? (csize - ccap) : -1); } + + +int writewholebuffer(int fd, const unsigned char *buf, int len) { + int total = 0; + int left = len; + int n; + + while(total < len) { + if((n = write(fd, buf + total, left)) < 0) + break; + + total += n; + left -= n; + } + + return (n < 0) ? -1 : 0; +} +// Adapted from Beej's `sendall()` function +// https://beej.us/guide/bgnet/html/split/slightly-advanced-techniques.html#sendall + // Thanks Beej! \ No newline at end of file -- cgit v1.2.3