-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathio.c
303 lines (262 loc) · 6.24 KB
/
io.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/* Routines for character input and output to the BBS */
#define _GNU_SOURCE
#include "io.h"
#include <regex.h>
#include <stdarg.h>
#include <stdio.h>
#include "defs.h"
#include "ext.h"
#ifndef BBS
int ansi = 0;
#endif
static int my_cputs(const char* s);
static int my_puts(const char* s);
/* fix % signs and emails and whatever else */
/* This allows user entered text to be sent to colorize()*/
void cleanup_strings(char* string) {
if (strstr(string, "%")) {
replace_substring(string, "%", "%%");
}
}
void replace_substring(char* str, const char* oldSubstr,
const char* newSubstr) {
int oldLen = strlen(oldSubstr);
int newLen = strlen(newSubstr);
int strLen = strlen(str);
char result[strlen(str) * 2]; // Adjust the size as per your requirement
int i, count = 0;
for (i = 0; i < strLen; i++) {
if (strstr(&str[i], oldSubstr) == &str[i]) {
strcpy(&result[count], newSubstr);
count += newLen;
i += oldLen - 1;
} else {
result[count] = str[i];
count++;
}
}
result[count] = '\0';
strcpy(str, result);
}
/* A standard printf -- no color code recognition. */
/* works best if output is not fflushed */
int my_printf(const char* fmt, ...) {
// delegate to my_vsprintf
va_list ap;
va_start(ap, fmt);
char* buf = my_vsprintf(NULL, fmt, ap);
va_end(ap);
// output, then discard the buffer
int r = my_puts(buf);
free(buf);
return r;
}
char* my_vsprintf(char* prefix, const char* fmt, va_list ap) {
// let vasprintf do all the work for us.
char* buf = NULL;
int n = vasprintf(&buf, fmt, ap);
if (n < 0 || buf == NULL) {
return strdup("");
}
if (prefix == NULL) {
return buf;
}
// concat the prefix and buf.
char* result = NULL;
n = asprintf(&result, "%s%s", prefix, buf);
free(prefix);
if (n < 0 || result == NULL) {
return strdup("");
}
return result;
}
char* my_sprintf(char* prefix, const char* fmt, ...) {
// delegate to my_vsprintf
va_list ap;
va_start(ap, fmt);
char* buf = my_vsprintf(prefix, fmt, ap);
va_end(ap);
return buf;
}
int checked_snprintf_with_traceinfo(const char* file, int line, char* out,
size_t len, const char* fmt, ...) {
va_list ap;
va_start(ap, fmt);
errno = 0;
const int n = vsnprintf(out, len, fmt, ap);
const int saved_errno = errno;
va_end(ap);
if (n < 0) {
// error: I/O or system error.
char* emsg = my_sprintf(NULL,
"FATAL: vsnprintf system or I/O error. "
"Called at file '%s':%d "
" errno said: %s",
file, line, strerror(saved_errno));
logfatal(emsg);
} else if ((size_t)n >= len) {
// error: buffer overflow
char* emsg =
my_sprintf(NULL, "FATAL: buffer overflow at file '%s':%d", file, line);
logfatal(emsg);
}
return n;
}
char* checked_strcat_with_traceinfo(const char* file, int line, char* dest,
size_t max_dest_size, const char* src) {
// we need room for both strings and the null terminator.
size_t src_len = strlen(src);
size_t dest_len = strlen(dest);
size_t need_size = dest_len + src_len + 1;
if (need_size <= max_dest_size) {
memcpy(dest + dest_len, src, src_len + 1);
} else {
// error: buffer overflow
char* emsg =
my_sprintf(NULL, "FATAL: buffer overflow at file '%s':%d", file, line);
logfatal(emsg);
}
return dest;
}
static int my_puts(const char* s) {
int count = 0;
while (*s) {
count += (my_putchar(*s) != EOF);
++s;
}
return count;
}
/* A simple printf that recognizes color codes */
int colorize(const char* fmt, ...) {
// delegate to my_vsprintf
va_list ap;
va_start(ap, fmt);
char* buf = my_vsprintf(NULL, fmt, ap);
va_end(ap);
// write it
int rc = my_cputs(buf);
free(buf);
return rc;
}
void output_color(char c) {
switch (c) {
case '!':
output(ANSI_RESET);
break;
case 'r':
output(ANSI_FG_RED);
break;
case 'R':
output(ANSI_FG_BOLD_RED);
break;
case 'g':
output(ANSI_FG_GREEN);
break;
case 'G':
output(ANSI_FG_BOLD_GREEN);
break;
case 'y':
output(ANSI_FG_YELLOW);
break;
case 'Y':
output(ANSI_FG_BOLD_YELLOW);
break;
case 'b':
output(ANSI_FG_BLUE);
break;
case 'B':
output(ANSI_FG_BOLD_BLUE);
break;
case 'm':
case 'p':
output(ANSI_FG_MAGENTA);
break;
case 'M':
case 'P':
output(ANSI_FG_BOLD_MAGENTA);
break;
case 'c':
output(ANSI_FG_CYAN);
break;
case 'C':
output(ANSI_FG_BOLD_CYAN);
break;
case 'w':
output(ANSI_FG_WHITE);
break;
case 'W':
output(ANSI_FG_BOLD_WHITE);
break;
case 'd':
case 'D':
output(ANSI_FG_BOLD_BLACK);
break;
case 'U':
break;
default:
break;
}
}
/* check for color codes and \r\n translation. Return the number of characters
printed. */
static int my_cputs(const char* s) {
int count = 0;
bool underline = false;
while (*s) {
if (ansi) {
if (*s == ' ' || *s == '\n' || *s == '\r') {
if (underline) {
underline = false;
output(ANSI_RESET_UNDERSCORE);
}
}
}
if (*s == '@') {
s++;
if (*s == '@') {
s++;
if (strchr("!rRgGyYbBcCmMpPwWDU", *s)) {
if (ansi) {
output_color(*s);
if (*s == 'U') {
underline = true;
output(ANSI_UNDERSCORE);
}
}
} else {
s--;
count += (my_putchar('@') != EOF);
count += (my_putchar('@') != EOF);
}
} else {
s--;
count += (my_putchar(*s) != EOF);
}
} else {
count += (my_putchar(*s) != EOF);
}
++s;
}
return count;
}
int output(const char* s) {
while (*s) {
my_putchar(*s);
++s;
}
return 0;
}
int my_putchar(int c) {
#ifdef _SSL
char newline = '\r';
if (c == '\n') SSL_write(1, &newline, sizeof(newline));
SSL_write(1, &c, sizeof(c));
return (c);
#else
if (c == '\n') {
putchar('\r');
}
return (putchar(c));
#endif
}
int my_putc(int c, FILE* stream) { return putc(c, stream); }