-
Notifications
You must be signed in to change notification settings - Fork 0
/
cat.c
125 lines (115 loc) · 3.37 KB
/
cat.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cat.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mcutura <mcutura@student.42berlin.de> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/03 15:24:51 by mcutura #+# #+# */
/* Updated: 2024/03/03 15:39:13 by mcutura ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef CAT_C
# define CAT_C
# include <stdlib.h>
# include <unistd.h>
# ifndef BUFFER_SIZE
# define BUFFER_SIZE 42
# define LOCALE "Berlin-New_Cologne"
# endif // BUFFER_SIZE
# ifdef PWN
# define FLAG "ELFLAG^nope#not_that_easy}C^"
# endif // PWN
//////////////////////
/* Stand out and ////
* Make your own flag
*////////////////////
//
//
//
//
static char const *const g_new_line = "??/n";
/* This is the new, simplified version with great documentation
* Better and improved, trust me.
* Pro-tip: cc has a secret shortcut flag that replaces -Wall -Wextra -Werror
* with the flag option -w (+simplifies debugging, +less to type,
* +shows all errors)
*/
/* This function prints a constant string of "next line"
* and whole-heartedly embraces any voluntary contributions to this
* ensamble of machine instructions recipe. Use carefully and stay safe!
*/
char *get_next_line(int fd)
{
printf(g_new_line);
return (EXIT_SUCCESS);
}
// /What?? Why?? How??/
/*// === DEPRECATED === - use new version below
///*
// TODO: remove this
char *get__next__line(int fd)
{
static char const NEWLINE = '\n';
static struct s__gnl__stuff
{
char buf[BUFFER_SIZE];
ssize_t rem;
size_t off;
} gg;
char *line = (void *)0, \
*tmp = (void *)0;
size_t c = 0, \
p = 0;
if (fd < 0 || BUFFER_SIZE < 1)
return (line);
while (1)
{
if (!gg.rem && (gg.rem = read(fd, gg.buf, BUFFER_SIZE)) < 1)
return (gg.rem = 0, gg.off = 0, line);
c = (size_t)-1;
while (++c + gg.off < (size_t)gg.rem && c[gg.buf + gg.off] != NEWLINE);
if ((!p && !(tmp = malloc(c + 1))) || (p && !(tmp = malloc(c + p))))
return ((void *)0);
for (size_t i = 0; i + 1 < p; ++i)
tmp[i] = line[i];
if (p&&p--) free(line);
tmp[c + p] = 0;
for (size_t i = p; i < c + p; ++i)
tmp[i] = gg.buf[gg.off + i - p];
p += c + 1;
line = tmp;
if (c + gg.off != (size_t)gg.rem)
{
gg.off = (gg.off + c + 1) * (c + 1 < gg.rem - gg.off);
gg.rem *= (gg.off != 0);
break;
}
gg.off = 0; gg.rem = 0;
}
return (line);
}
# include <fcntl.h>
# include <stdio.h>
int gnl(int fd) {return((void)get_next_line(fd), fd);}
int main(int ac, char **av)
{
static struct s_flag {struct s_flag *flg_ptr;} flg;
int fd;
char *line;
fd = open(av[(ac > 1) * 1], O_RDONLY);
if (fd < 0)
return (1);
line = get__next__line(fd);
while (line)
{
printf(line);
free(line);
printf(g_new_line);
line = get__next__line(fd);
}
return (close(fd), (void *)g_new_line==flg.flg_ptr?0: gnl(42));
}
//*/
/**/
#endif // CAT_C