-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
396 lines (319 loc) · 11.8 KB
/
main.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
#include <dirent.h>
#include <sys/wait.h>
#include <string.h>
#include <regex.h>
#include <errno.h>
#define BUFSIZE 512
#define MAX_PROCESS 32
#pragma pack(1)
typedef struct bmpHeader {
char signature[2];
int fileSize;
int reserved;
int dataOffset;
int size;
int width;
int height;
char planes[2];
char bitCount[2];
int compression;
int xPixelPerM;
int yPixelPerM;
int colorsUsed;
int colorsImportant;
} bmpHeader;
#pragma pack()
void usage(char *name) {
printf("Usage: %s <entry_dir> <output_dir> <c>\n", name);
}
char *userPermission(mode_t userPerm) {
static char buf[4];
buf[0] = (userPerm & S_IRUSR) ? 'R' : '-';
buf[1] = (userPerm & S_IWUSR) ? 'W' : '-';
buf[2] = (userPerm & S_IXUSR) ? 'X' : '-';
buf[3] = '\0';
return buf;
}
char *groupPermission(mode_t groupPerm) {
static char buf[4];
buf[0] = (groupPerm & S_IRGRP) ? 'R' : '-';
buf[1] = (groupPerm & S_IWGRP) ? 'W' : '-';
buf[2] = (groupPerm & S_IXGRP) ? 'X' : '-';
buf[3] = '\0';
return buf;
}
char *otherPermission(mode_t otherPerm) {
static char buf[4];
buf[0] = (otherPerm & S_IROTH) ? 'R' : '-';
buf[1] = (otherPerm & S_IWOTH) ? 'W' : '-';
buf[2] = (otherPerm & S_IXOTH) ? 'X' : '-';
buf[3] = '\0';
return buf;
}
char *writePermission(struct stat stats) {
static char buf[BUFSIZE];
sprintf(buf, "drepturi de acces user: %s\ndrepturi de acces grup: %s\ndrepturi de acces altii: %s\n",
userPermission(stats.st_mode & S_IRWXU),
groupPermission(stats.st_mode & S_IRWXG), otherPermission(stats.st_mode & S_IRWXO));
return buf;
}
int openStatFile(char *filePath) {
static int fd;
char fileToOpen[BUFSIZE] = "data_out/";
strcat(fileToOpen, filePath);
strcat(fileToOpen, "_statistica.txt");
if ((fd = open(fileToOpen, O_WRONLY | O_CREAT, S_IRWXU)) < 0) {
perror("Error opening output file\n");
exit(1);
}
return fd;
}
int processDirectoryStats(struct dirent *dirent, struct stat stats) {
int fd = openStatFile(dirent->d_name);
char output[BUFSIZE * 2];
char buf[BUFSIZE];
strftime(buf, sizeof(buf), "%d.%m.%Y", gmtime(&stats.st_mtime));
int outputSize = sprintf(output,
"nume director: %s\nidentificatorul utilizatorului: %d\n%s\n",
dirent->d_name, stats.st_uid, writePermission(stats));
if (write(fd, output, outputSize) < 0) {
perror("Error writing to file\n");
exit(1);
}
return 5;
}
int processImageStats(struct dirent *dirent, struct stat stats, char *filePath) {
int fd = openStatFile(dirent->d_name), outputSize;
char output[BUFSIZE * 2];
char buf[BUFSIZE];
strftime(buf, sizeof(buf), "%d.%m.%Y", gmtime(&stats.st_mtime));
int f1;
struct bmpHeader statsBmpHeader;
if ((f1 = open(filePath, O_RDONLY)) < 0) {
perror("Error opening input file\n");
exit(1);
}
if (read(f1, &statsBmpHeader, sizeof(bmpHeader)) < 0) {
perror("Error reading input file\n");
exit(1);
}
outputSize = sprintf(output,
"nume fisier: %s\ninaltime: %d\nlungime: %d\ndimensiune: %ld\nidentificatorul utilizatorului: %d\ntimpul ultimei modificari: %s\ncontorul de legaturi: %ld\n%s\n",
dirent->d_name, statsBmpHeader.height, statsBmpHeader.width, stats.st_size, stats.st_uid,
buf,
stats.st_nlink, writePermission(stats));
if (close(f1) < 0) {
perror("Error closing file\n");
exit(1);
}
if (write(fd, output, outputSize) < 0) {
perror("Error writing to file\n");
exit(1);
}
return 10;
}
void processImageConversion(char *filePath) {
int f1, bytesRead;
char colors[3];
struct bmpHeader statsBmpHeader;
if ((f1 = open(filePath, O_RDWR)) < 0) {
perror("Error opening input file\n");
exit(1);
}
if (read(f1, &statsBmpHeader, sizeof(bmpHeader)) < 0) {
perror("Error reading input file\n");
exit(1);
}
// Set the offset bytes
lseek(f1, statsBmpHeader.dataOffset, SEEK_SET);
while ((bytesRead = read(f1, colors, sizeof(colors))) > 0) {
char grayscale = 0.299 * colors[0] + 0.587 * colors[1] + 0.114 * colors[2];
memset(colors, grayscale, sizeof(colors));
// Move to the position we started the read operation
lseek(f1, -bytesRead, SEEK_CUR);
write(f1, colors, sizeof(colors));
}
if (close(f1) < 0) {
perror("Error closing file\n");
exit(1);
}
}
int processLinkFileStats(struct dirent *dirent, struct stat stats, struct stat statsFile) {
int fd = openStatFile(dirent->d_name), outputSize;
char output[BUFSIZE * 2];
outputSize = sprintf(output,
"nume legatura: %s\ndimensiune: %ld\ndimensiune fisier: %ld\n%s\n",
dirent->d_name, stats.st_size, statsFile.st_size, writePermission(stats));
if (write(fd, output, outputSize) < 0) {
perror("Error writing to file\n");
exit(1);
}
return 6;
}
int processFileStats(struct dirent *dirent, struct stat stats) {
int fd = openStatFile(dirent->d_name), outputSize;
char output[BUFSIZE * 2];
char buf[BUFSIZE];
strftime(buf, sizeof(buf), "%d.%m.%Y", gmtime(&stats.st_mtime));
outputSize = sprintf(output,
"nume fisier: %s\ndimensiune: %ld\nidentificatorul utilizatorului: %d\ntimpul ultimei modificari: %s\ncontorul de legaturi: %ld\n%s\n",
dirent->d_name, stats.st_size, stats.st_uid,
buf,
stats.st_nlink, writePermission(stats));
if (write(fd, output, outputSize) < 0) {
perror("Error writing to file\n");
exit(1);
}
return 8;
}
void processDirectory(char *directoryPath, char *outDirectory, char *character) {
DIR *dir = NULL;
struct dirent *dirent = NULL;
if ((dir = opendir(directoryPath)) == NULL) {
perror("Error opening directory\n");
exit(1);
}
if ((opendir(outDirectory)) == NULL) {
perror("Error opening output directory\n");
exit(1);
}
pid_t pid[MAX_PROCESS];
int totalProcessesRunning = 0;
regex_t regexBMP;
if (regcomp(®exBMP, ".bmp$", 0) != 0) {
perror("Regex could not compile\n");
exit(1);
}
while ((dirent = readdir(dir)) != NULL) {
if (strcmp(dirent->d_name, ".") != 0 &&
strcmp(dirent->d_name, "..") != 0) { // treat current directory and parent directory
struct stat stats;
char filePath[BUFSIZE];
strcpy(filePath, directoryPath);
strcat(filePath, "/");
strcat(filePath, dirent->d_name);
if (lstat(filePath, &stats) < 0) {
perror("Error getting stats from input file\n");
exit(1);
}
int isValidRegex = REG_NOMATCH;
if (S_ISREG(stats.st_mode)) {
isValidRegex = regexec(®exBMP, dirent->d_name, 0, NULL, 0);
}
if (S_ISREG(stats.st_mode) && isValidRegex == 0) {
// Is bmp file
// First process for stats
if (totalProcessesRunning < MAX_PROCESS && (pid[totalProcessesRunning] = fork()) < 0) {
perror("Error creating a new process\n");
exit(1);
}
totalProcessesRunning++;
if (pid[totalProcessesRunning - 1] == 0) {
int exitProcess = processImageStats(dirent, stats, filePath);
exit(exitProcess);
}
// Second process for color changes
if (totalProcessesRunning < MAX_PROCESS && (pid[totalProcessesRunning] = fork()) < 0) {
perror("Error creating a new process\n");
exit(1);
}
totalProcessesRunning++;
if (pid[totalProcessesRunning - 1] == 0) {
processImageConversion(filePath);
exit(0);
}
} else if (S_ISREG(stats.st_mode)) {
// Is regulat file and not .bmp
// First process for stats
if (totalProcessesRunning < MAX_PROCESS && (pid[totalProcessesRunning] = fork()) < 0) {
perror("Error creating a new process\n");
exit(1);
}
totalProcessesRunning++;
if (pid[totalProcessesRunning - 1] == 0) {
int exitProcess = 0;
exitProcess = processFileStats(dirent, stats);
exit(exitProcess);
}
// Second process for script
if (totalProcessesRunning < MAX_PROCESS && (pid[totalProcessesRunning] = fork()) < 0) {
perror("Error creating a new process\n");
exit(1);
}
totalProcessesRunning++;
if (pid[totalProcessesRunning - 1] == 0) {
// execlp("bash", "bash", "./script", character, (char *)NULL);
// perror("Error executin bash script\n");
// exit(1);
exit(0);
}
} else {
// Is dir or sym link
if (totalProcessesRunning < MAX_PROCESS && (pid[totalProcessesRunning] = fork()) < 0) {
perror("Error creating a new process\n");
exit(1);
}
totalProcessesRunning++;
if (pid[totalProcessesRunning - 1] == 0) {
int exitProcess = 0;
if (S_ISDIR(stats.st_mode)) {
exitProcess = processDirectoryStats(dirent, stats);
} else if (S_ISLNK(stats.st_mode)) {
struct stat statsFile;
if (stat(filePath, &stats) < 0) {
perror("Error getting stats from input file\n");
exit(1);
}
exitProcess = processLinkFileStats(dirent, stats, statsFile);
}
exit(exitProcess);
}
}
}
}
pid_t waitProcessID;
int processStatus;
int totalValidSentences = 0;
int fStats;
if ((fStats = open("statistica.txt", O_APPEND | O_WRONLY | O_CREAT, S_IRWXU)) < 0) {
perror("Error opening file for stats\n");
exit(1);
}
for (int i = 0; i < totalProcessesRunning; i++) {
waitProcessID = wait(&processStatus);
if (WIFEXITED(processStatus)) {
int exitStatus = WEXITSTATUS(processStatus);
printf("S-a încheiat procesul cu pid-ul %d și codul %d\n", waitProcessID, exitStatus);
if (exitStatus > 0) {
char output[BUFSIZE];
int outputSize = sprintf(output, "%d\n", exitStatus);
if (write(fStats, output, outputSize) < 0) {
perror("Error writing to file\n");
exit(1);
}
}
} else {
printf("The child process %d finished abnormally", waitProcessID);
}
}
printf("Au fost identificate in total %d propozitii corecte care contin caracterul %s\n", totalValidSentences, character);
if (closedir(dir) < 0) {
perror("Error closing dir\n");
exit(1);
}
}
int main(int argc, char *argv[]) {
if (argc != 4) {
usage(argv[0]);
exit(1);
}
processDirectory(argv[1], argv[2], argv[3]);
return 0;
}