-
Notifications
You must be signed in to change notification settings - Fork 0
/
myFunctionalLib.c
executable file
·281 lines (215 loc) · 5.97 KB
/
myFunctionalLib.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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
#include "myFunctionalLib.h"
#include <time.h>
#include <locale.h>
//========== Structural Functions ==========
/*
* Create pointer to the read file.
*/
MyFile* create_File(char* name, char* path, int myIsItAFile){
MyFile* myFileCreated = malloc(sizeof(MyFile));
myFileCreated -> myName = malloc(sizeof(char) * (strlen(name)+1));
strcpy(myFileCreated -> myName,name);
myFileCreated -> myPath = malloc(sizeof(char) * (strlen(path)+1));
strcpy(myFileCreated -> myPath, path);
myFileCreated -> myPrint = malloc( sizeof(char) * (strlen(name) + strlen(path)+3) ); //carac de fin, \n et le / entre les deux
strcpy(myFileCreated -> myPrint, path);
slashItCorrectly(&(myFileCreated->myPrint));
myStrCat(&(myFileCreated->myPrint), name);
myStrCat(&(myFileCreated->myPrint), "\n");
myFileCreated -> isItAFile = myIsItAFile;
return myFileCreated;
}
/*
* Add pointer of read file at the end of the table of the pointer to the files.
*/
listOfFiles* insertFile(MyFile* newFile, listOfFiles* list){
listOfFiles* newList = malloc(sizeof(listOfFiles));
newList -> myFile = newFile;
newList -> next = NULL;
listOfFiles* listParcours = list;
while(listParcours -> next != NULL)
listParcours = listParcours -> next;
listParcours -> next = newList;
return list;
}
/*
* Print the list of read files.
*/
void printListOfFiles(listOfFiles* list){
while(list != NULL){
printf("%s", list -> myFile -> myPrint);
list = list -> next;
}
}
/*
* Copie a pointer from listOfFiles.
*/
void myListOfFilesPtrCpy(listOfFiles** dest, listOfFiles* ptrToCpy){
*dest = ptrToCpy;
}
/*
* Set the list pointer to the next file pointer.
*/
int nextFile(listOfFiles** list){
listOfFiles* temp = malloc(sizeof(listOfFiles));
temp = *list;
if(temp -> next != NULL){// Un Next existe, la liste continue
myListOfFilesPtrCpy(list, temp -> next);
return 1;
}else{ // Il n'y a plus d'élément après celui là
return 0;
}
}
/*
* Change the pointer reference to the file next to the next file, equivalent to delete pointer to the file in list: it won't be reacheable anymore
* when browsing the list.
*/
int supprNextFileOf(listOfFiles** list){
listOfFiles* temp = malloc(sizeof(listOfFiles));
temp = *list;
if (temp->next == NULL){
return 0;
}else{
//Next existe si le code ci dessous s'exécute
temp -> next = temp -> next -> next;
}
return 1;
}
//========== Operational Functions ==========
/*
* Translate the mode_t value into printable string.
*/
char* fonction_permission(struct stat s)
{
char* string = malloc(12);
strcpy(string,(S_ISDIR(s.st_mode)) ? "d" : "-");
strcat(string,(s.st_mode & S_IRUSR) ? "r" : "-");
strcat(string,(s.st_mode & S_IWUSR) ? "w" : "-");
strcat(string,(s.st_mode & S_IXUSR) ? "x" : "-");
strcat(string,(s.st_mode & S_IRGRP) ? "r" : "-");
strcat(string,(s.st_mode & S_IWGRP) ? "w" : "-");
strcat(string,(s.st_mode & S_IXGRP) ? "x" : "-");
strcat(string,(s.st_mode & S_IROTH) ? "r" : "-");
strcat(string,(s.st_mode & S_IWOTH) ? "w" : "-");
strcat(string,(s.st_mode & S_IXOTH) ? "x" : "-");
/*
char* localeT = setlocale(LC_TIME,NULL);
if(strcmp(localeT,"C")==0){
strcat(string,".");
}
*/
//AS SEEN with Nicolas, no need to implement this format.
return string;
}
/*
* Set locale to the french format.
*/
char* dateFormater(time_t st_mdate)
{
char* localeT = setlocale(LC_TIME,NULL);
char *result;
if(strcmp(localeT,"fr_FR.UTF-8")==0){
result = malloc(20);
strftime(result, 20, "%b %d %H:%M", localtime(&st_mdate));
}else{
result = malloc(20);
strftime(result, 20, "%b %d %H:%M", localtime(&st_mdate));
}
return result;
}
/*
* Return pointer of string of the complete path of the file from concatenation of its name with the complete path of its owner.
*/
char* completePathBuilder(listOfFiles* list){
char* pathToBuild = malloc(sizeof(char) * (strlen(list -> myFile -> myPath) + 1 + strlen(list -> myFile -> myName) +1) );
strcpy(pathToBuild, list -> myFile -> myPath);
slashItCorrectly(&pathToBuild);
myStrCat(&pathToBuild, list-> myFile -> myName);
return pathToBuild;
}
/*
* Copy refence of a pointer into another.
*/
void myStrPtrCpy(char** dest, char* ptrToCpy){
*dest = ptrToCpy;
}
/*
* Homemade function equivalent to strcat.
*/
void myStrCat(char** dest, char*c){
char* temp = malloc(sizeof(char) * ( strlen(*dest) + strlen(c) + 1 ) );
strcpy(temp, *dest);
strcat(temp, c);
myStrPtrCpy(dest, temp);
}
/*
* Return length of the given string.
*/
int myStrLen(char* strToSize){
int taille=0;
while (strToSize[taille]!='\0')
taille++;
return taille;
}
/*
* Add slash to the non void path of the file if it doesn't have already.
*/
void slashItCorrectly(char** str){
char c;
int boolean =0, i=0;
if(strlen(*str) > 0){
c = (*str)[i];
while(c != '\0'){
if (c=='/')
boolean = 1;
else
boolean = 0;
i++;
c = (*str)[i];
}
if(boolean == 0){myStrCat(str,"/");}
}
}
/*
* Change bracket, if there is one, into path, else do nothing.
*/
int changeMyBrackets(char** tabOfString, char* thePath){
int i = 0;
int boolean= 0;
while(tabOfString[i]!=NULL){
if(strcmp(tabOfString[i],"{}")==0){
boolean=1;
tabOfString[i]=thePath;
}
i++;
}
return boolean;
}
char** splitMyString (char* givenStr) {
char* myStr = malloc(sizeof(char)* (strlen(givenStr)+1) );
strcpy(myStr, givenStr);
const char s[2] = " ";
char *TempoEltInMyStr;
char** myStrSplitted = malloc(sizeof(char*)* (countItems(myStr) + 1) );
int i = 0;
TempoEltInMyStr = strtok(myStr, s);
while( TempoEltInMyStr != NULL ) {
myStrSplitted[i] = malloc(sizeof(char)* (strlen(TempoEltInMyStr)+1) );
strcpy(myStrSplitted[i], TempoEltInMyStr);
i++;
TempoEltInMyStr = strtok(NULL, s);
}
myStrSplitted[i] = NULL;
return myStrSplitted;
}
int countItems(char* myStr){
int i =0, cnt = 1;
while(myStr[i] != '\0'){
if (myStr[i]==' '){ cnt++;}
i++;
}
return cnt;
}