-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.c
executable file
·109 lines (85 loc) · 2.39 KB
/
test.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
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
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <dirent.h>
typedef struct counter {
size_t dirs;
size_t files;
} counter_t;
typedef struct entry {
char *name;
int is_dir;
struct entry *next;
} entry_t;
int walk(const char* directory, const char* prefix, counter_t *counter) {
entry_t *head = NULL, *current, *iter;
size_t size = 0, index;
struct dirent *file_dirent;
DIR *dir_handle;
char *full_path, *segment, *pointer, *next_prefix;
dir_handle = opendir(directory);
if (!dir_handle) {
fprintf(stderr, "Cannot open directory \"%s\"\n", directory);
return -1;
}
counter->dirs++;
while ((file_dirent = readdir(dir_handle)) != NULL) {
if (file_dirent->d_name[0] == '.') {
continue;
}
current = malloc(sizeof(entry_t));
current->name = strcpy(malloc(strlen(file_dirent->d_name) + 1), file_dirent->d_name);
current->is_dir = file_dirent->d_type == DT_DIR;
current->next = NULL;
if (head == NULL) {
head = current;
} else if (strcmp(current->name, head->name) < 0) {
current->next = head;
head = current;
} else {
for (iter = head; iter->next && strcmp(current->name, iter->next->name) > 0; iter = iter->next);
current->next = iter->next;
iter->next = current;
}
size++;
}
closedir(dir_handle);
if (!head) {
return 0;
}
for (index = 0; index < size; index++) {
if (index == size - 1) {
pointer = "└── ";
segment = " ";
} else {
pointer = "├── ";
segment = "│ ";
}
printf("%s%s%s\n", prefix, pointer, head->name);
if (head->is_dir) {
full_path = malloc(strlen(directory) + strlen(head->name) + 2);
sprintf(full_path, "%s/%s", directory, head->name);
next_prefix = malloc(strlen(prefix) + strlen(segment) + 1);
sprintf(next_prefix, "%s%s", prefix, segment);
walk(full_path, next_prefix, counter);
free(full_path);
free(next_prefix);
} else {
counter->files++;
}
current = head;
head = head->next;
free(current->name);
free(current);
}
return 0;
}
int main(int argc, char *argv[]) {
char* directory = argc > 1 ? argv[1] : ".";
printf("%s\n", directory);
counter_t counter = {0, 0};
walk(directory, "", &counter);
printf("\n%zu directories, %zu files\n",
counter.dirs ? counter.dirs - 1 : 0, counter.files);
return 0;
}