-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_bonus.c
144 lines (129 loc) · 3.5 KB
/
test_bonus.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* test_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fdi-cecc <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/04 18:35:10 by fdi-cecc #+# #+# */
/* Updated: 2024/06/07 09:43:19 by fdi-cecc ### ########.fr */
/* */
/* ************************************************************************** */
#include "get_next_line_bonus.h"
#include "get_next_line_bonus.h"
#include <fcntl.h> // For open
#include <stdio.h> // For printf, perror
#include <stdlib.h> // For free
int main(int argc, char **argv)
{
int i;
int *fd;
char **lines;
int *file_done;
int files_remaining;
if (argc < 2)
{
printf("Specify the files please: %s file1 file2 ...\n", argv[0]);
return 1;
}
// Allocate memory for file descriptors, lines, and file status
fd = (int *)malloc((argc - 1) * sizeof(int));
lines = (char **)malloc((argc - 1) * sizeof(char *));
file_done = (int *)malloc((argc - 1) * sizeof(int));
i = 0;
while (i < argc - 1)
{
fd[i] = open(argv[i + 1], O_RDONLY);
if (fd[i] < 0)
{
perror("Error opening file");
free(fd);
free(lines);
free(file_done);
return 1;
}
lines[i] = NULL;
file_done[i] = 0; // Initialize file status as not done
i++;
}
files_remaining = argc - 1;
while (files_remaining > 0)
{
i = 0;
while (i < argc - 1)
{
if (!file_done[i])
{
if (lines[i] != NULL)
{
free(lines[i]);
}
lines[i] = get_next_line(fd[i]);
if (lines[i] != NULL)
{
printf("%s", lines[i]);
}
else
{
file_done[i] = 1; // Mark this file as done
files_remaining--;
}
}
i++;
}
}
i = 0;
while (i < argc - 1)
{
close(fd[i]);
if (lines[i] != NULL)
{
free(lines[i]);
}
i++;
}
free(fd);
free(lines);
free(file_done);
return 0;
}
// cc -Wall -Werror -Wextra test_bonus.c get_next_line_bonus.c get_next_line_utils_bonus.c
/* int main(int argc, char **argv)
{
int i;
int fd[argc - 1];
char *line;
i = 1;
if (argc < 2)
{
printf("Specify the files please: %s file1 file2 ...\n", argv[0]);
return (1);
}
while (i < argc)
{
fd[i - 1] = open(argv[i], O_RDONLY);
if (fd[i - 1] < 0)
{
perror("Check your FDs");
return (1);
}
i++;
}
i = 0;
while (i < argc - 1)
{
while ((line = get_next_line(fd[i])) != NULL)
{
if (line[i])
{
printf("%s", line);
free(line);
}
printf("\n");
}
close(fd[i]);
i++;
}
return (0);
} */
//cc -Wall -Werror -Wextra test_bonus.c get_next_line_bonus.c get_next_line_utils_bonus.c -o test_bonus