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
|
#include "io.h"
#include "../cstr/cstr.h"
#include "../aec/aec.h"
#include "../sys/types.h"
void __wstdn(const char *buf, u64 n)
{
write(STDOUT_FD, buf, n);
}
void wstd(const char *buf)
{
__wstdn(buf, cstr_length(buf));
}
unsigned long rstd(char *buf, unsigned long count)
{
return read(STDIN_FD, buf, count);
}
void wf(unsigned int fd, char *buf)
{
write(fd, buf, cstr_length(buf));
}
void putchar(const char c)
{
__wstdn(&c, 1);
}
void wstdf__(const char *buf, const void **args)
{
wff__(STDOUT_FD, buf, args);
}
void wff__(unsigned int fd, const char *buf, const void **args)
{
const char *start = buf;
char stoi_buf[32] = "";
int i;
for (; *buf; ++buf) {
if (*buf == '%') {
if (buf - start > 0)
write(fd, start, buf - start);
++buf;
switch (*buf) {
case '%': __wstdn("%", 1); break;
case 's':
write(fd, ((const char **)args)[0], cstr_length(((const char**)args)[0]));
++args;
break;
case 'i':
for (i = 0; i < 32; ++i)
stoi_buf[i] = 0;
i64_to_cstr(((u64 *)args)[0], stoi_buf, 32);
write(fd, stoi_buf, cstr_length(stoi_buf));
++args;
break;
case 'u':
for (i = 0; i < 32; ++i)
stoi_buf[i] = 0;
u64_to_cstr(((u64 *)args)[0], stoi_buf, 32);
write(fd, stoi_buf, cstr_length(stoi_buf));
++args;
break;
case 'S':
sgr(((const char **)args)[0]);
++args;
break;
case 'F':
foreground(((const char **)args)[0]);
++args;
break;
case 'B':
background(((const char **)args)[0]);
++args;
break;
}
start = buf + 1;
}
}
if (buf - start > 0)
write(fd, start, buf - start);
}
char __buf[BUFSIZ] = { 0 };
int get_next_line_from_fd(unsigned int fd, char *linebuf)
{
u64 size = 0;
char *line = __buf;
while (*line && *line != '\n') line++;
if (!*line) {
size = cstr_length(__buf);
i64 s = read(fd, __buf + size, BUFSIZ - size - 1);
if (s == 0 && size == 0) {
return -1;
} else if (s == 0) {
for (int i = 0; i < size; ++i) linebuf[i] = __buf[i];
__buf[0] = 0;
return size;
}
__buf[s] = 0;
while (*line && *line != '\n') line++;
if (!*line) {
return -2;
}
}
*line = 0;
++line;
size = cstr_length(__buf);
for (int i = 0; __buf[i]; ++i) linebuf[i] = __buf[i];
linebuf[size] = 0;
int i = 0;
for (; *line; ++i) {
__buf[i] = *line;
++line;
}
__buf[i] = 0;
return size;
}
#ifdef IO_LIB_UNIT_TEST
int main() {
wstdf("%s %S%F%s%S\n", "hallo", SGR_UNDERLINE, COLOR_RED, "welt", SGR_RESET);
}
#endif
|