summaryrefslogtreecommitdiff
path: root/src/ignore.c
diff options
context:
space:
mode:
authorAargh Rai <shubh2007rai@gmail.com>2026-03-28 19:03:45 +0530
committerAargh Rai <shubh2007rai@gmail.com>2026-03-28 19:03:45 +0530
commitc7194b9b07a9c80344b52e002d0863fd258f0b38 (patch)
tree258e0aae8fdbdce64d8df36e75138a186358cd6b /src/ignore.c
basic backup system, which utlizies regex for ignore files
Diffstat (limited to 'src/ignore.c')
-rw-r--r--src/ignore.c66
1 files changed, 66 insertions, 0 deletions
diff --git a/src/ignore.c b/src/ignore.c
new file mode 100644
index 0000000..fbfb8ed
--- /dev/null
+++ b/src/ignore.c
@@ -0,0 +1,66 @@
+#include "lib.h"
+
+int _;
+
+void mkdir_if_not_exist(char* path, int VERBOSE) {
+ if (mkdir(path, S_IRWXU) < 0) return;
+ if (VERBOSE) {
+ printf("(Created to create directory %s)", path);
+ }
+}
+
+void verify_parent_dirs_exist(char* file_path, int VERBOSE) {
+ for (int i = 0; file_path[i] != 0; i++) {
+ if (file_path[i] == '/') file_path[i] = 0;
+ if (file_path[i] != 0) continue;
+ mkdir_if_not_exist(file_path, VERBOSE);
+ file_path[i] = '/';
+ }
+}
+
+char* get_ignore_file(char* ignore_file, int VERBOSE) {
+ verify_parent_dirs_exist(ignore_file, VERBOSE);
+ FILE* file = fopen(ignore_file, "r");
+ char* default_config;
+
+ if (file != 0) {
+ float current_pointer = ftell(file);
+ fseek(file, 0, SEEK_END);
+ long int file_size = ftell(file);
+ fseek(file, current_pointer, SEEK_SET);
+
+ char* contents = malloc(file_size * sizeof(*contents));
+ _ = fread(contents, 1, file_size, file);
+ fclose(file);
+
+ return contents;
+ }
+
+ if (strcmp(ignore_file, (default_config = get_default_config())) != 0) {
+ return NULL;
+ }
+
+ // mainly used for first time running
+ if (default_config == NULL) {
+ return NULL;
+ }
+
+ file = fopen(default_config, "w");
+ if (file == 0) {
+ return NULL;
+ }
+
+ int content_length = strlen(DEFAULT_CONFIG_CONTENTS);
+ _ = fwrite(DEFAULT_CONFIG_CONTENTS, 1, content_length, file);
+ fclose(file);
+ if (VERBOSE) {
+ printf("Creating %s\n", default_config);
+ }
+
+ char* contents = malloc(content_length * sizeof(char));
+ for (int i = 0; i < content_length; i++) {
+ contents[i] = DEFAULT_CONFIG_CONTENTS[i];
+ }
+ return contents;
+}
+