diff options
| author | @syxhe <https://t.me/syxhe> | 2025-01-21 17:01:03 -0600 |
|---|---|---|
| committer | @syxhe <https://t.me/syxhe> | 2025-01-21 17:01:03 -0600 |
| commit | 5d3068832c6094cf3b3ffce89d2398134e939b1f (patch) | |
| tree | f23dc5597a1bfe76b2748f6762d09ff97f05141f /src/shared.c | |
| parent | b15ffbc1e51f913d6ab1cfa345a06eecfc098405 (diff) | |
Write some sample code to encrypt a file via password
Diffstat (limited to 'src/shared.c')
| -rw-r--r-- | src/shared.c | 22 |
1 files changed, 21 insertions, 1 deletions
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) { | |||
| 81 | lstr = tmp; | 81 | lstr = tmp; |
| 82 | } | 82 | } |
| 83 | 83 | ||
| 84 | if(lstr) { | 84 | if(lstr && fd == STDIN_FILENO) { |
| 85 | lstr[csize - ccap - 1] = '\0'; // Don't include the newline | 85 | lstr[csize - ccap - 1] = '\0'; // Don't include the newline |
| 86 | } | 86 | } |
| 87 | 87 | ||
| 88 | *str = lstr; | 88 | *str = lstr; |
| 89 | return ((bytesread == 0) ? (csize - ccap) : -1); | 89 | return ((bytesread == 0) ? (csize - ccap) : -1); |
| 90 | } | 90 | } |
| 91 | |||
| 92 | |||
| 93 | int writewholebuffer(int fd, const unsigned char *buf, int len) { | ||
| 94 | int total = 0; | ||
| 95 | int left = len; | ||
| 96 | int n; | ||
| 97 | |||
| 98 | while(total < len) { | ||
| 99 | if((n = write(fd, buf + total, left)) < 0) | ||
| 100 | break; | ||
| 101 | |||
| 102 | total += n; | ||
| 103 | left -= n; | ||
| 104 | } | ||
| 105 | |||
| 106 | return (n < 0) ? -1 : 0; | ||
| 107 | } | ||
| 108 | // Adapted from Beej's `sendall()` function | ||
| 109 | // https://beej.us/guide/bgnet/html/split/slightly-advanced-techniques.html#sendall | ||
| 110 | // Thanks Beej! \ No newline at end of file | ||
