-
Notifications
You must be signed in to change notification settings - Fork 1
/
temp.c
61 lines (51 loc) · 1.64 KB
/
temp.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
#include <unistd.h>
#include <sys/types.h>
#include <dirent.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char** add_file(char** arr, int* size, int* capacity, char* path){
if((*size) >= (*capacity)){
arr = (char**) realloc(arr, 2 * (*capacity) * sizeof(char*));
(*capacity) *= 2;
}
int len = strlen(path);
arr[*size] = (char*) malloc((len + 1) * sizeof(char));
memset(arr[*size], '\0', (len + 1) * sizeof(char));
memcpy(arr[*size], path, len * sizeof(char));
(*size) += 1;
return arr;
}
char** listdir(const char *name, char** arr, int* size, int* capacity)
{
DIR *dir;
struct dirent *entry;
if (!(dir = opendir(name)))
return arr;
while ((entry = readdir(dir)) != NULL) {
if (entry->d_type == DT_DIR) {
char path[1024];
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
continue;
snprintf(path, sizeof(path), "%s/%s", name, entry->d_name);
arr = listdir(path, arr, size, capacity);
} else {
char file_path[1024];
snprintf(file_path, sizeof(file_path), "%s/%s", name, entry->d_name);
// printf("%s\n",file_path);
arr = add_file(arr, size, capacity, file_path);
}
}
closedir(dir);
return arr;
}
// int main(void) {
// int size = 0, capacity = 2;
// char** arr = (char**) malloc(capacity * sizeof(char*));
// arr = listdir("/home/shyamal/Personal/GitHub", arr, &size, &capacity);
// int i;
// for(i = 0; i < size; i ++){
// printf("%s\n", arr[i]);
// }
// return 0;
// }