-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdir.c
More file actions
197 lines (162 loc) · 4.77 KB
/
dir.c
File metadata and controls
197 lines (162 loc) · 4.77 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
188
189
190
191
192
193
194
195
196
197
#include <unistd.h>
#include <string.h>
#include <dirent.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include "dir.h"
filenames_t * getFilenames(char *dirname) {
if (dirname == NULL) {
perror("Invalid directory name");
return NULL;
}
filenames_t *filenames = malloc(sizeof(filenames_t));
if (filenames == NULL) {
perror("Error on filenames_t struct allocation");
return NULL;
}
dirlistcount_t *listingCount = countOfListing(dirname);
if (listingCount == NULL) {
return NULL;
}
size_t count = listingCount->files;
free(listingCount);
filenames->count = count;
filenames->f_names = (char **) malloc(count * sizeof(char *));
if (filenames->f_names == NULL) {
perror("Error on filenames_t f_names struct allocation");
return NULL;
}
DIR *dirstream = opendir(dirname);
if (dirstream == NULL) {
perror("Invalid directorystream pointer to stream");
return NULL;
}
struct dirent *entry;
for (size_t i = 0; (entry = readdir(dirstream)) && i != count; ) {
if (entry == NULL) {
perror("Error on entry allocation");
return NULL;
}
if(!isdir(entry)) {
filenames->f_names[i] = (char *) malloc(strlen(entry->d_name) + 1);
if (filenames->f_names[i] == NULL) {
perror("Error on f_names allocation");
return NULL;
}
strcpy(filenames->f_names[i], entry->d_name);
i++;
}
}
closedir(dirstream);
return filenames;
}
/* adds filenames that is it get from getFilenames function of a dir */
filenames_t * addFilenames(filenames_t *filenames, char *dirname) {
if (!filenames || !dirname) {
return NULL;
}
filenames_t *temp = getFilenames(dirname);
if (temp == NULL) {
perror("Error on filenames_t allocation in addFilenames>getFilenames dir.c");
return NULL;
}
temp->count++;
size_t count = temp->count;
temp->f_names = realloc(temp->f_names, count * sizeof(char *));
if (temp->f_names == NULL) {
perror("Error on filenames_t f_names allocation in addFilenames dir.c");
return NULL;
}
temp->f_names[count - 1] = (char *) malloc(strlen(temp->f_names[0]) + 1);
if (temp->f_names[count - 1] == NULL) {
perror("Error on filenames_t f_names[] allocation");
return NULL;
}
strcpy(temp->f_names[count - 1], temp->f_names[0]);
temp->f_names[0] = realloc(temp->f_names[0], strlen(filenames->f_names[0]) + 1);
if (temp->f_names[0] == NULL) {
perror("Error on f_names[0] reallocation");
return NULL;
}
strcpy(temp->f_names[0], filenames->f_names[0]);
freeEntries(filenames);
return temp;
}
dirlistcount_t * countOfListing(char *dirname) {
DIR *dirstream = opendir(dirname);
if (dirstream == NULL) {
perror("Invalid directorystream pointer to stream");
return 0;
}
size_t count = 0;
size_t dircount = 0;
struct dirent *entry = NULL;
while ((entry = readdir(dirstream))) {
if (entry == NULL) {
perror("Error on dirent struct allocation");
return 0;
}
char *name = entry->d_name;
count++;
if (isdir(entry)) {
dircount++;
if (isSymbolicDirStructure(name)) {
count--;
}
}
}
closedir(dirstream);
dircount -= 2; // subracting . and .. entries.
dirlistcount_t *cntOfList = (dirlistcount_t *) malloc(sizeof(dirlistcount_t));
if (cntOfList == NULL) {
perror("Error on dirlistcount_t allocation");
return NULL;
}
cntOfList->total = count;
cntOfList->files = count - dircount;
cntOfList->dir = dircount;
return cntOfList;
}
bool isdir(struct dirent *entry) {
if (entry->d_type == DT_DIR) {
return true;
}
return false;
}
/* returns true if dir is . or .. */
bool isSymbolicDirStructure(char *name) {
if (name[0] == '.') {
if (name[1] == '\0') {
return true;
}
else if (name[1] == '.' && name[2] == '\0') {
return true;
}
}
return false;
}
void printFilenames(const filenames_t *filenames) {
if (filenames) {
size_t count = filenames->count;
for (int i = 0; i != count; i++) {
printf("%d. %s\n", i + 1, filenames->f_names[i]);
}
}
}
bool freeEntries(filenames_t *list) {
if (list == NULL) {
return false;
}
size_t count = list->count;
if (count == 0) {
free(list);
return true;
}
for (size_t i = 0; i != count; i++) {
free(list->f_names[i]);
}
free(list->f_names);
free(list);
return true;
}