blob: 790240c21e24e2dc6e91d5dad0c56c9d2b48c12f (
plain)
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
|
#include "my_c_tail.h"
#include <stdlib.h>
#include <unistd.h>
void stdintail(unsigned int n)
{
char **lines = calloc(2000, sizeof(char *));
lines[0] = malloc(350 * sizeof(char));
size_t m = 0;
char c;
size_t i = 0;
while (read(STDIN_FILENO, &c, 1))
{
if (c == '\n')
{
lines[m][i] = '\0';
lines[++m] = malloc(350 * sizeof(char));
i = 0;
}
else
{
lines[m][i++] = c;
}
}
size_t j;
if (m > n)
{
for (size_t i = 0; i < m - n; i++)
free(lines[i]);
j = m - n;
}
else
j = 0;
for (; j < m; j++)
{
for (size_t i = 0; lines[j][i]; i++)
write(STDOUT_FILENO, &(lines[j][i]), 1);
write(STDOUT_FILENO, "\n", 1);
free(lines[j]);
}
free(lines[m]);
free(lines);
}
|