aboutsummaryrefslogtreecommitdiff
path: root/fileutils.c
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2020-07-13 15:59:30 +0200
committerdec05eba <dec05eba@protonmail.com>2020-07-13 15:59:30 +0200
commitae0520e57267dbd866fc8cd25f66f4e6af2ac118 (patch)
tree22788688f1b588c3ad00c1ce3fe13da68b3a9382 /fileutils.c
parenta1ca82847eb356c6b85ada2ac11f38d98f6e085e (diff)
Move c files into src directory
Diffstat (limited to 'fileutils.c')
-rw-r--r--fileutils.c82
1 files changed, 0 insertions, 82 deletions
diff --git a/fileutils.c b/fileutils.c
deleted file mode 100644
index ea57550..0000000
--- a/fileutils.c
+++ /dev/null
@@ -1,82 +0,0 @@
-#include "fileutils.h"
-#include "alloc.h"
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <errno.h>
-#include <pwd.h>
-#include <unistd.h>
-#include <fcntl.h>
-#include <sys/stat.h>
-
-const char* get_home_dir() {
- const char *home_dir = getenv("HOME");
- if(!home_dir) {
- struct passwd *pw = getpwuid(getuid());
- home_dir = pw->pw_dir;
- }
- return home_dir;
-}
-
-int file_get_content(const char *filepath, char **data, long *size) {
- int result = 0;
- FILE *file = fopen(filepath, "rb");
- if(!file) {
- int err = -errno;
- perror(filepath);
- return err;
- }
-
- fseek(file, 0, SEEK_END);
- *size = ftell(file);
- if(*size == -1) {
- fprintf(stderr, "Failed to tell the size of file %s, is it not a file?\n", filepath);
- result = -1;
- goto cleanup;
- }
- fseek(file, 0, SEEK_SET);
-
- *data = alloc_or_crash(*size);
- if((long)fread(*data, 1, *size, file) != *size) {
- fprintf(stderr, "Failed to read all bytes in file %s\n", filepath);
- result = -1;
- goto cleanup;
- }
-
- cleanup:
- fclose(file);
- return result;
-}
-
-int create_directory_recursive(char *path) {
- int path_len = strlen(path);
- char *p = path;
- char *end = path + path_len;
- for(;;) {
- char *slash_p = strchr(p, '/');
-
- // Skips first '/', we don't want to try and create the root directory
- if(slash_p == path) {
- ++p;
- continue;
- }
-
- if(!slash_p)
- slash_p = end;
-
- char prev_char = *slash_p;
- *slash_p = '\0';
- int err = mkdir(path, S_IRWXU);
- *slash_p = prev_char;
-
- if(err == -1 && errno != EEXIST)
- return err;
-
- if(slash_p == end)
- break;
- else
- p = slash_p + 1;
- }
- return 0;
-}