-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.c
More file actions
187 lines (157 loc) · 4.01 KB
/
utils.c
File metadata and controls
187 lines (157 loc) · 4.01 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include "utils.h"
char *notes_path = NULL;
size_t sizeCheck(char *category, char *name)
{
if (category == NULL)
{
printf("ERROR: NULL category\n");
return 0;
}
if (name == NULL || *name == '\0')
{
return snprintf(NULL, 0, "%s/%s/", notes_path, category);
}
return snprintf(NULL, 0, "%s/%s/%s.md", notes_path, category, name);
}
char *pathlloc(char *category, char *name)
{
if (category == NULL)
{
printf("ERROR\n");
return notes_path;
}
size_t size = sizeCheck(category, name);
char *memlloc = malloc(size + 1);
if (memlloc == NULL)
{
printf("Was not possible to allocate memory\n");
return notes_path;
}
if (name == NULL)
snprintf(memlloc, size + 1, "%s/%s", notes_path, category);
else
snprintf(memlloc, size + 1, "%s/%s/%s.md", notes_path, category, name);
return memlloc;
}
// This is for checking if a folder or a file is created
int path_validation(char* path)
{
if (path == NULL)
{
printf("No path was given\n");
return -1;
}
size_t size = strlen(path) + 1;
char *dir = malloc(size);
if (dir == NULL)
{
printf("Was not possible reach a path\n");
free(dir);
return -1;
}
snprintf(dir, size, "%s", path);
struct stat st = {0};
// If directory does not exist.
if (stat(dir, &st) == -1)
{
free(dir);
return -1;
}
free(dir);
return 0;
}
int init_home_path()
{
const char *home = getenv("HOME");
if (home == NULL)
{
printf("ERROR: Was not possible to reach %s\n", home);
return -1;
}
size_t len = strlen(home) + strlen("/notes") + 1;
notes_path = malloc(len);
if (notes_path == NULL)
{
printf("ERROR: Failed to check home path\n");
free(notes_path);
return -1;
}
snprintf(notes_path, len,"%s/notes", home);
return 0;
}
int init_notes_directory()
{
if (path_validation(notes_path) != 0)
{
if (mkdir(notes_path, 0777) != 0)
{
printf("ERROR: Was not possible reach notes directory\n");
return -1;
}
}
return 0;
}
void create_category(char *category)
{
if (category == NULL)
{
printf("ERROR: NULL Category\n");
return;
}
char *category_path = pathlloc(category, NULL);
// If folder already exists just return.
if (path_validation(category_path) == 0)
{
free(category_path);
return;
}
if (mkdir(category_path, 0777) != 0)
{
printf("ERROR: Was not possible to reach file\n");
}
free(category_path);
}
const char* get_editor()
{
// Had to do change this because was
// actually '\0', and i forgor that
// and was doing NULL
const char* editor = getenv("EDITOR");
const char* visual = getenv("VISUAL");
if ((editor == NULL || *editor == '\0') && (visual == NULL || *visual == '\0'))
{
return "nano";
}
if (editor == NULL || *editor == '\0')
{
return visual;
}
return editor;
}
void open_editor(const char* filepath)
{
size_t command_size = snprintf(NULL, 0,"%s \"%s\"", get_editor(), filepath);
char *command = malloc(command_size + 1);
snprintf(command, command_size + 1,"%s \"%s\"", get_editor(), filepath);
if (system(command) != 0)
printf("Editor was not possible to open.\n");
}
int confirm_removal(char *category, char *name)
{
char response;
while (1)
{
printf("Do you want to remove %s '%s'? y/n\n", name ? "file" : "directory",
name ? name : category
);
response = fgetc(stdin);
response = tolower(response);
if (response == 'y') return 0;
if (response == 'n')
{
printf("Operation canceled.\n");
return -1;
}
printf("Invalid input. Please enter 'y' or 'n'.\n\n");
}
}