aboutsummaryrefslogtreecommitdiff
path: root/src/fileManager.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/fileManager.c')
-rw-r--r--src/fileManager.c59
1 files changed, 59 insertions, 0 deletions
diff --git a/src/fileManager.c b/src/fileManager.c
new file mode 100644
index 0000000..56e52cc
--- /dev/null
+++ b/src/fileManager.c
@@ -0,0 +1,59 @@
+/* .
+ * L__ Procedures
+ * | L__ 1h_workout.you86
+ * | L__ 15m_workout.you86
+ * | L__ math_homework.you86
+ * | L__ grocery_shopping.you86
+ * |
+ * L__ DailyStack
+ * L__ 02.09.2021
+ * L__ ParentProcedures.you86
+ * L__ result-1h_workout.png
+ * L__ result-math_homework.docx
+ */
+#include <stdbool.h>
+#include <stdlib.h>
+#include <sys/stat.h>
+#include <errno.h>
+#include <stdio.h>
+#include <string.h>
+
+#define ROOT_DIR "/Documents/You86/"
+#define PROCEDURES_DIR ROOT_DIR"Procedures/"
+#define DAILY_STACK_DIR ROOT_DIR"DailyStack/"
+
+void createDir(char *);
+
+void initFiles() {
+ createDir(ROOT_DIR);
+ createDir(PROCEDURES_DIR);
+ createDir(DAILY_STACK_DIR);
+}
+
+void createDir(char *p_path) {
+ char *p_tilde = getenv("HOME");
+ int tildeLen = strlen(p_tilde), pathLen = strlen(p_path);
+
+ char fullPath[tildeLen + pathLen];
+ for (int i = 0; i < tildeLen + pathLen; i++)
+ fullPath[i] = (i < tildeLen) ? p_tilde[i] : p_path[i - tildeLen];
+
+ int ret = mkdir(fullPath, S_IRWXU);
+ if (ret == -1) {
+ switch (errno) {
+ case EEXIST:
+ return;
+ case EACCES :
+ printf("The parent directory does not allow write for %s", fullPath);
+ break;
+ case ENAMETOOLONG:
+ printf("Pathname \"%s\" is too long", fullPath);
+ break;
+ default:
+ perror("mkdir");
+ printf("Path: \"%s\"\n", fullPath);
+ break;
+ }
+ exit(EXIT_FAILURE);
+ }
+}