-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprogram.c
682 lines (554 loc) · 16.6 KB
/
program.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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <stdint.h>
#include <dirent.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include <ctype.h>
#define FILE_OUT "statistica.txt"
#define BUFF_SIZE 256
#define PATH_SIZE 256
//file descritptors for pipes
int ChildIn[2], OutParent[2];
//character to be searched for
char character;
DIR* openDir(char* path)
{
DIR* dIn = opendir(path);
if (dIn == NULL)
{
char* errstr = "\0";
sprintf(errstr, "Could not open directory %s\n", path);
perror(errstr); //prints an error message stating what file cannot be opened
exit(errno); //stops the program
}
return dIn;
}
int openFileReadWrite(char* path)
{
int fd = 0;
if((fd = open(path, O_RDWR)) < 0)
{
char* errstr = "\0";
sprintf(errstr, "Could not open file %s\n", path);
perror(errstr); //prints an error message stating what file cannot be opened
exit(errno); //stops the program
}
return fd;
}
int createFile(char* path)
{
int out = creat(path, S_IRWXU); //creates output file
if(out == -1) //if output file cannot be created
{
perror("Could not create output file\n"); //prints an error message
exit(errno); //stops the program
}
return out;
}
void closeDir(DIR* dir)
{
if(closedir(dir) != 0) //closes directory
{
perror("Could not close directory\n");
exit(errno); //stops program if directory cannot be closed
}
}
void closeFile(int fd)
{
if(close(fd) != 0)
{
perror("Could not close file\n");
exit(errno);
}
}
void writeInFile(int file, void* buffer, size_t nbytes)
{
if(write(file, buffer, nbytes) <= 0)
{
perror("Eroare la scrierea in fisier\n");
exit(errno);
}
}
char* generateRelativePath(char* parentPath, char* fileName)
{
char* path = malloc(sizeof(char) * PATH_SIZE);
if(path == NULL)
{
perror("Error at allocating memory\n");
exit(errno);
}
sprintf(path, "%s/%s", parentPath, fileName);
return path;
}
void getStat(char* path, struct stat* infop)
{
if(lstat(path, infop) != 0)
{
perror("Something went wrong getting the stats for a file...\n");
exit(errno);
}
}
int getEntryType(mode_t mod)
{
if(S_ISDIR(mod))
{
return 0;
}
if(S_ISLNK(mod))
{
return 1;
}
if(S_ISREG(mod))
{
return 2;
}
return -1;
}
char* getUserRights(mode_t mod)
{
char* rights = malloc(sizeof(char) * 4);
if(rights == NULL)
{
perror("Error at allocating memory\n");
exit(errno);
}
sprintf(rights, "---"); //no permissions
if(mod & S_IRUSR)
{
rights[0] = 'R';
}
if(mod & S_IWUSR)
{
rights[1] = 'W';
}
if(mod & S_IXUSR)
{
rights[2] = 'X';
}
return rights;
}
char* getGroupRights(mode_t mod)
{
char* rights = malloc(sizeof(char) * 4);
if(rights == NULL)
{
perror("Error at allocating memory\n");
exit(errno);
}
sprintf(rights, "---"); //no permissions
if(mod & S_IRGRP)
{
rights[0] = 'R';
}
if(mod & S_IWGRP)
{
rights[1] = 'W';
}
if(mod & S_IXGRP)
{
rights[2] = 'X';
}
return rights;
}
char* getOtherRights(mode_t mod)
{
char* rights = malloc(sizeof(char) * 4);
if(rights == NULL)
{
perror("Error at allocating memory\n");
exit(errno);
}
sprintf(rights, "---"); //no permissions
if(mod & S_IROTH)
{
rights[0] = 'R';
}
if(mod & S_IWOTH)
{
rights[1] = 'W';
}
if(mod & S_IXOTH)
{
rights[2] = 'X';
}
return rights;
}
char* getLastModification(time_t time)
{
struct tm* timeptr = gmtime(&time);
return asctime(timeptr);
}
void processDir(char* path, struct stat* inf, int fout)
{
char buffer[BUFF_SIZE];
//prints dir name
sprintf(buffer, "nume director: %s\n", path);
writeInFile(fout, buffer, strlen(buffer));
//prints user id
sprintf(buffer, "identificatorul utilizatorului: %u\n", inf->st_uid);
writeInFile(fout, buffer, strlen(buffer));
//prints user permissions
sprintf(buffer, "drepturi de acces user: %s\n", getUserRights(inf->st_mode));
writeInFile(fout, buffer, strlen(buffer));
//prints group permissions
sprintf(buffer, "drepturi de acces grup: %s\n", getGroupRights(inf->st_mode));
writeInFile(fout, buffer, strlen(buffer));
//prints others permissions
sprintf(buffer, "drepturi de acces altii: %s\n", getOtherRights(inf->st_mode));
writeInFile(fout, buffer, strlen(buffer));
writeInFile(fout, "\n", 1);
}
void processLink(char* path, struct stat* inf, int fout)
{
char buffer[BUFF_SIZE];
struct stat target;
if(stat(path, &target) != 0)
{
perror("Something went wrong getting the stats for a file...\n");
exit(errno);
}
//prints link name
sprintf(buffer, "nume legatura: %s\n", path);
writeInFile(fout, buffer, strlen(buffer));
//prints link size
sprintf(buffer, "dimensiune legatura: %ld\n", inf->st_size);
writeInFile(fout, buffer, strlen(buffer));
//prints target size
sprintf(buffer, "dimensiune fisier target: %ld\n", target.st_size);
writeInFile(fout, buffer, strlen(buffer));
//prints user permissions
sprintf(buffer, "drepturi de acces user: %s\n", getUserRights(inf->st_mode));
writeInFile(fout, buffer, strlen(buffer));
//prints group permissions
sprintf(buffer, "drepturi de acces grup: %s\n", getGroupRights(inf->st_mode));
writeInFile(fout, buffer, strlen(buffer));
//prints others permissions
sprintf(buffer, "drepturi de acces altii: %s\n", getOtherRights(inf->st_mode));
writeInFile(fout, buffer, strlen(buffer));
writeInFile(fout, "\n", 1);
}
void readFromFile(int file, void* buffer, size_t nbytes)
{
int rd = read(file, buffer, nbytes);
if(rd < 0)
{
perror("Error at reading from file\n");
exit(errno);
}
}
uint32_t bytesToNumber(const uint8_t bytes[4]) {
// Assuming little-endian byte order
return (uint32_t)(
(bytes[0] << 0) |
(bytes[1] << 8) |
(bytes[2] << 16) |
(bytes[3] << 24)
);
}
void bmpToGrayScale(int file, int width, int height)
{
int p = -1, s = -1;
if((p = fork()) < 0)
{
perror("Error at forking\n");
exit(errno);
}
if(p == 0)
{
unsigned char* data = (unsigned char*)malloc(width * height * 3);
if(data == NULL)
{
perror("Error at allocating memory\n");
exit(errno);
}
int rd = read(file, data, width * height * 3);
if(rd < 0)
{
perror("Error at reading from BMP file\n");
exit(errno);
}
for(int i = 0; i < width * height; i++)
{
unsigned char r = data[i * 3 + 2];
unsigned char g = data[i * 3 + 1];
unsigned char b = data[i * 3];
unsigned char gray = 0.299 * r + 0.587 * g + 0.114 * b;
data[i * 3] = gray;
data[i * 3 + 1] = gray;
data[i * 3 + 2] = gray;
}
if(lseek(file, 54, SEEK_SET) < 0)
{
perror("Error at lseek\n");
exit(errno);
}
int wr = write(file, data, width * height * 3);
if(wr < 0)
{
perror("Error at writing to BMP file\n");
exit(errno);
}
free(data);
exit(0);
}
else
{
wait(&s);
if(!WIFEXITED(s))
{
perror("Process finished anormally\n");
exit(s);
}
printf("Process with PID %d finished with status %d\n", p, WEXITSTATUS(s));
}
}
void processFile(char* path, struct stat* inf, int fout, int* childIn, int* outParent, int* lines)
{
char sign[] = "AA\0";
uint8_t metabuffer[4];
uint8_t heightB[4];
uint8_t widthB[4];
int file = openFileReadWrite(path);
//reads signature
readFromFile(file, sign, 2);
//if file is BMP
if((strcmp(sign, "BM")) == 0)
{
for(int i = 0; i < 4; i++)
{
//reads unimportant data from header
readFromFile(file, metabuffer, 4);
}
//reads image width
readFromFile(file, widthB, 4);
//reads image length
readFromFile(file, heightB, 4);
}
char buffer[BUFF_SIZE];
//prints file name
sprintf(buffer, "numele fisierului: %s\n", path);
writeInFile(fout, buffer, strlen(buffer));
//if file is BMP
if(strcmp(sign, "BM") == 0)
{
//transforms bytes arrays into numbers
u_int32_t width = bytesToNumber(widthB);
uint32_t height = bytesToNumber(heightB);
//turns BMP into grayscale
bmpToGrayScale(file, width, height);
//prints file name
sprintf(buffer, "inaltime: %u\n", height);
writeInFile(fout, buffer, strlen(buffer));
//prints file name
sprintf(buffer, "lungime: %u\n", width);
writeInFile(fout, buffer, strlen(buffer));
}
//prints file size
sprintf(buffer, "dimensiune fisier: %ld\n", inf->st_size);
writeInFile(fout, buffer, strlen(buffer));
//prints user id
sprintf(buffer, "identificatorul utilizatorului: %u\n", inf->st_uid);
writeInFile(fout, buffer, strlen(buffer));
//prints last modification
sprintf(buffer, "timpuul ultimei modificari: %s", getLastModification(inf->st_atime));
writeInFile(fout, buffer, strlen(buffer));
//prints number of links
sprintf(buffer, "contorul de legaturi: %lu\n", inf->st_nlink);
writeInFile(fout, buffer, strlen(buffer));
//prints user permissions
sprintf(buffer, "drepturi de acces user: %s\n", getUserRights(inf->st_mode));
writeInFile(fout, buffer, strlen(buffer));
//prints group permissions
sprintf(buffer, "drepturi de acces grup: %s\n", getGroupRights(inf->st_mode));
writeInFile(fout, buffer, strlen(buffer));
//prints others permissions
sprintf(buffer, "drepturi de acces altii: %s\n", getOtherRights(inf->st_mode));
writeInFile(fout, buffer, strlen(buffer));
writeInFile(fout, "\n", 1);
//Urmatoarele linii de cod au fost comentate pentru ca se blocheaza la citirea din pipe, dar le puteti decomenta pentru a vedea ca scriptul functioneaza
//creates a new process for counting the number of lines in the file
// if(strcmp(sign, "BM") != 0)
// {
// if(pipe(childIn) < 0)
// {
// perror("Error at creating pipe\n");
// exit(errno);
// }
// int p = -1, s = -1;
// if((p = fork()) < 0)
// {
// perror("Error at forking\n");
// exit(errno);
// }
// if(p == 0)
// {
// //closes write end of the pipe
// close(childIn[1]);
// //redirects standard input to read from the pipe
// dup2(childIn[0], STDIN_FILENO);
// close(childIn[0]);
// //executes script.sh
// execlp("./script.sh", "./script.sh", &character, (char *)NULL);
// //checks for errors
// perror("Error executing script.sh\n");
// exit(EXIT_FAILURE);
// }
// else
// {
// //reads contents of file
// char buffer[inf->st_size + 1];
// int bytesRead = 0;
// if((bytesRead = read(file, buffer, inf->st_size)) < 0)
// {
// perror("Error at reading from file\n");
// exit(errno);
// }
// //writes contents of file in pipe
// if(write(childIn[1], buffer, bytesRead) < 0)
// {
// perror("Error at writing to pipe\n");
// exit(errno);
// }
// //wair for child process to finish
// wait(&s);
// if(!WIFEXITED(s))
// {
// perror("Process finished anormally\n");
// exit(s);
// }
// printf("Process with PID %d finished with status %d\n", p, WEXITSTATUS(s));
// //reads number of lines returned by script.sh
// char outputBuffer[10];
// int bytesReadFromPipe = read(childIn[0], outputBuffer, sizeof(outputBuffer));
// if(bytesReadFromPipe < 0)
// {
// perror("Error at reading from pipe\n");
// exit(errno);
// }
// //closes read end of the pipe
// close(childIn[0]);
// *lines = atoi(outputBuffer);
// }
// }
closeFile(file);
}
void processEntry(char* path, int type, struct stat* inf, int fout, int* childIn, int* outParent, int* lines)
{
switch (type)
{
case 0: //directory
processDir(path, inf, fout);
break;
case 1: //symlink
processLink(path, inf, fout);
break;
case 2: //regular file
processFile(path, inf, fout, childIn, outParent, lines);
break;
default:
printf("File %s is of unknown type\n", path);
break;
}
}
char* generateOutputPath(char* dirpath, char* filename)
{
char* path = malloc(sizeof(char) * PATH_SIZE);
if(path == NULL)
{
perror("Error at allocating memory\n");
exit(errno);
}
sprintf(path, "%s/%s_statistica.txt", dirpath, filename);
return path;
}
int main(int argc, char** argv)
{
if(argc != 4 )
{
perror("Usage ./program <input_dir> <output_dir> <character>");
exit(errno);
}
if(isalnum(argv[3][0]) == 0)
{
perror("Third argument must be an alphanumeric character\n");
exit(errno);
}
character = argv[3][0];
//open directory from path received in command line arguments aray
DIR* dirIn = openDir(argv[1]);
//initializes pid variable and status variable to be used when creating processes
int pid = -1, status = -1;
//counts number of processes spawned
int count = 0;
//count number of lines returned by the shell script in each child process
int lines = 0;
//reads directory entries until the end
struct dirent* entry;
while((entry = readdir(dirIn)) != NULL)
{
//generates relative path of current entry to program.c
char* relpath = generateRelativePath(argv[1], entry->d_name);
//generates path of the _statistics.txt file
char* outpath = generateOutputPath(argv[2], entry->d_name);
// //gets info on current entry
struct stat info;
getStat(relpath, &info);
//gets the type of the current entry: 0 - dir, 1 - symlink, 2 - regular file, -1 - unknown
int type = getEntryType(info.st_mode);
if((pid = fork()) < 0) //creates new child process for writing in file
{
perror("Error at forking\n");
exit(errno);
}
if(pid == 0) //is a child process
{
//creates statistics file in output directory
int fileout = createFile(outpath);
//number of correct lines for current entry, zero if not a regular file or has no correct lines
int lines = 0;
//processes entry and writes in output file according to entry type
processEntry(relpath, type, &info, fileout, ChildIn, OutParent, &lines);
//close output file
closeFile(fileout);
//exits current process
exit(count);
}
//increments number of processes
count++;
//frees memory allocated with functions
free(relpath); free(outpath);
//adds a waiting time of 1 second between processes
sleep(1);
}
//goes through all child processes and waits for them to be completed
for(int i = 0; i < count; i++)
{
//gets pid of the last child process completed
int p = wait(&status);
//checks for errors
if(p < 0)
{
perror("Error at finishing process\n");
exit(status);
}
//prints the status of each process
if(!WIFEXITED(status))
{
perror("Process finished anormally\n");
exit(status);
}
//prints process and status
printf("Process with PID %d finished with status %d\n", p, WEXITSTATUS(status));
}
printf("Au fost identificate in total %d propozitii corecte care contin caracterul %c\n", lines, character);
//closes input directory
closeDir(dirIn);
return 0;
}