-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_handler.c
64 lines (56 loc) · 1.36 KB
/
file_handler.c
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
#include "file_handler.h"
#include <stdio.h>
#include <string.h>
#include <dirent.h>
#include <unistd.h>
#include <sys/stat.h>
int list_directory(char* path){
DIR *d; // a directory object
struct dirent *dir; // used to navigate directories
d = opendir(path);
printf("STARTING DIRECTORY\n\n---");
if (d) {
printf("---\n");
while ((dir = readdir(d)) != NULL) {
if (strcmp(dir->d_name, ".") != 0 && strcmp(dir->d_name, "..") != 0) {
printf("%s\n", dir->d_name);
}
}
printf("---\n");
closedir(d);
}
printf("\n\nENDING DIRECTORY\n");
return 0;
}
int make_file(char* name) {
FILE *fptr;
fptr = fopen(name, "w");
fclose(fptr);
return 1;
}
int make_dir(char* name) {
if (!name){
return -1;
}
int result = mkdir(name, 0777);
return result;
}
int enter(char* path, char* directory) {
if (!directory) {
printf("Error: Directory name is NULL.\n");
return 1;
}
size_t path_len = strlen(path);
if (path_len == 0 || path[path_len - 1] != '/') {
strcat(path, "/");
}
strcat(path, directory);
if (chdir(path) != 0) {
perror("Failed to change directory");
return 1;
}
return 0;
}
void get_current_path(char* path, size_t path_size){
getcwd(path, path_size);
}