aboutsummaryrefslogtreecommitdiff
path: root/src/fileManager.c
blob: 56e52cc5308a76c65f4accda6fa808ca72291056 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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);
	}
}