-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
91 lines (83 loc) · 2.62 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: elmaksim <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/24 13:23:36 by elmaksim #+# #+# */
/* Updated: 2024/01/25 13:34:15 by elmaksim ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line.h"
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#define RESET "\x1B[0m"
#define RED "\x1B[31m"
#define GREEN "\x1B[32m"
#define BLUE "\x1B[34m"
void print_colored_line(const char *line, const char *color)
{
printf("%s%s%s", color, line, RESET);
}
void check_leaks(void)
{
FILE *leaksFile;
char buffer[1024];
system("leaks a.out > leaks_output");
leaksFile = fopen("leaks_output", "r");
print_colored_line("Checking for leaks:\n", BLUE);
if (leaksFile != NULL)
{
while (fgets(buffer, sizeof(buffer), leaksFile) != NULL)
printf("%s", buffer);
fclose(leaksFile);
}
remove("leaks_output");
print_colored_line("Finished checking leaks!\n", GREEN);
}
void process_file(const char *file_path, int buffer_size)
{
int fd = open(file_path, O_RDWR);
if (fd < 0)
{
perror("Error opening file");
exit(1);
}
print_colored_line("\n\nReading lines from the file ", BLUE);
print_colored_line(file_path, RED);
print_colored_line(" with ", BLUE);
print_colored_line("BUFFER_SIZE=", RED);
printf("%d", buffer_size);
print_colored_line(":\n", BLUE);
char *str = get_next_line(fd);
while (str)
{
printf("%s", str);
free(str);
str = get_next_line(fd);
}
free(str);
close(fd);
}
int main(int argc, char *argv[])
{
if (argc < 2)
{
fprintf(stderr, "\nUsage: %s <file_path1> [buffer_size1]\
<file_path2> [buffer_size2] ...\n", argv[0]);
return (1);
}
// Iterate over file paths and buffer sizes
for (int i = 1; i < argc; i += 2)
{
char *file_path = argv[i];
int buffer_size = (i + 1 < argc) ? atoi(argv[i + 1]) : BUFFER_SIZE;
process_file(file_path, buffer_size);
}
// Check for memory leaks
atexit(check_leaks);
print_colored_line("\n\nAll tests passed successfully!\n\n", GREEN);
return (0);
}