-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnulibc.c
466 lines (416 loc) · 12.8 KB
/
nulibc.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
#include <stdarg.h>
#if defined(UNIX) || defined(__linux__)
#include <unistd.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if defined(_WIN32) || defined(_WIN64)
#include <windows.h>
#else
#include <sys/syscall.h>
#endif
#define STDOUT 1
#define STDERR 2
#define STDIN 0
// Custom definition for size_t (unsigned long) to avoid conflict with mingw-w64's typedefs.
typedef unsigned long custom_size_t;
// Custom definition for pid_t (int) to avoid conflict with mingw-w64's typedefs.
typedef int custom_pid_t;
typedef struct {
char *str;
custom_size_t len;
} nstring;
custom_size_t nstrlen(nstring *str) {
return str->len;
}
nstring nstr_new(const char *str) {
nstring s;
s.len = strlen(str);
s.str = (char *)malloc(s.len + 1);
strcpy(s.str, str);
return s;
}
nstring nstrncpy(const nstring *s, custom_size_t start, custom_size_t length) {
nstring result;
if (!s || !s->str || start >= s->len) {
result.str = NULL;
result.len = 0;
return result;
}
custom_size_t max_len = (start + length) > s->len ? s->len - start : length;
result.str = (char *)malloc(max_len + 1);
memcpy(result.str, s->str + start, max_len);
result.str[max_len] = '\0';
result.len = max_len;
return result;
}
int strcmp(const char *str1, const char *str2) {
while (*str1 && (*str1 == *str2)) {
str1++;
str2++;
}
return (unsigned char)*str1 - (unsigned char)*str2;
}
void write_char(int fd, char c) {
#if defined(_WIN32) || defined(_WIN64)
fwrite(&c, 1, 1, stdout);
#endif
#if defined(__unix__) || defined(__unix) || defined(__linux__) || defined(__APPLE__) || defined(__MACH__)
write(fd, &c, 1);
#endif
}
void write_str(int fd, const char *str) {
while (*str) {
#if defined(_WIN32) || defined(_WIN64)
fwrite(str, 1, 1, stdout);
#endif
#if defined(__unix__) || defined(__unix) || defined(__linux__) || defined(__APPLE__) || defined(__MACH__)
write(fd, str, 1);
#endif
str++;
}
}
void write_num(int fd, int num) {
char buffer[20];
int i = 0;
if (num == 0) {
#if defined(_WIN32) || defined(_WIN64)
fwrite("0", 1, 1, stdout);
#endif
#if defined(__unix__) || defined(__unix) || defined(__linux__) || defined(__APPLE__) || defined(__MACH__)
write(fd, "0", 1);
#endif
return;
}
if (num < 0) {
#if defined(_WIN32) || defined(_WIN64)
fwrite("-", 1, 1, stdout);
#endif
#if defined(__unix__) || defined(__unix) || defined(__linux__) || defined(__APPLE__) || defined(__MACH__)
write(fd, "-", 1);
#endif
num = -num;
}
while (num > 0) {
buffer[i++] = (num % 10) + '0';
num /= 10;
}
for (int j = i - 1; j >= 0; j--) {
#if defined(_WIN32) || defined(_WIN64)
fwrite(&buffer[j], 1, 1, stdout);
#endif
#if defined(__unix__) || defined(__unix) || defined(__linux__) || defined(__APPLE__) || defined(__MACH__)
write(fd, &buffer[j], 1);
#endif
}
}
void write_unsigned(int fd, unsigned int num) {
char buffer[20];
int i = 0;
if (num == 0) {
#if defined(_WIN32) || defined(_WIN64)
fwrite("0", 1, 1, stdout);
#endif
#if defined(__unix__) || defined(__unix) || defined(__linux__) || defined(__APPLE__) || defined(__MACH__)
write(fd, "0", 1);
#endif
return;
}
while (num > 0) {
buffer[i++] = (num % 10) + '0';
num /= 10;
}
for (int j = i - 1; j >= 0; j--) {
#if defined(_WIN32) || defined(_WIN64)
fwrite(&buffer[j], 1, 1, stdout);
#endif
#if defined(__unix__) || defined(__unix) || defined(__linux__) || defined(__APPLE__) || defined(__MACH__)
write(fd, &buffer[j], 1);
#endif
}
}
void write_hex(int fd, unsigned int num) {
const char hex_chars[] = "0123456789abcdef";
char buffer[10];
int i = 0;
if (num == 0) {
#if defined(_WIN32) || defined(_WIN64)
fwrite("0", 1, 1, stdout);
#endif
#if defined(__unix__) || defined(__unix) || defined(__linux__) || defined(__APPLE__) || defined(__MACH__)
write(fd, "0", 1);
#endif
return;
}
while (num > 0) {
buffer[i++] = hex_chars[num % 16];
num /= 16;
}
for (int j = i - 1; j >= 0; j--) {
#if defined(_WIN32) || defined(_WIN64)
fwrite(&buffer[j], 1, 1, stdout);
#endif
#if defined(__unix__) || defined(__unix) || defined(__linux__) || defined(__APPLE__) || defined(__MACH__)
write(fd, &buffer[j], 1);
#endif
}
}
void write_ptr(int fd, void *ptr) {
// Write "0x" before the pointer value.
#if defined(_WIN32) || defined(_WIN64)
fwrite("0x", 2, 1, stdout);
#endif
#if defined(__unix__) || defined(__unix) || defined(__linux__) || defined(__APPLE__) || defined(__MACH__)
write(fd, "0x", 2);
#endif
// Casting pointer to unsigned long long for 64-bit safety.
write_hex(fd, (unsigned int)((unsigned long long)ptr));
}
void write_float(int fd, double num) {
char buffer[50];
int int_part = (int)num;
double frac_part = num - int_part;
write_num(fd, int_part);
// Write decimal point.
#if defined(_WIN32) || defined(_WIN64)
fwrite(".", 1, 1, stdout);
#endif
#if defined(__unix__) || defined(__unix) || defined(__linux__) || defined(__APPLE__) || defined(__MACH__)
write(fd, ".", 1);
#endif
// Multiply the fractional part to get 6 decimal places.
frac_part *= 1000000;
int frac_int = (int)(frac_part + 0.5); // rounding
// Ensure leading zeros if necessary.
char frac_buffer[10];
snprintf(frac_buffer, sizeof(frac_buffer), "%06d", frac_int);
write_str(fd, frac_buffer);
}
void write_long(int fd, long int num) {
char buffer[32];
int len = snprintf(buffer, sizeof(buffer), "%ld", num);
if(len > 0) {
#if defined(_WIN32) || defined(_WIN64)
fwrite(buffer, 1, len, stdout);
#endif
#if defined(__unix__) || defined(__unix) || defined(__linux__) || defined(__APPLE__) || defined(__MACH__)
write(fd, buffer, len);
#endif
}
}
void write_double(int fd, double num) {
char buffer[64];
int len = snprintf(buffer, sizeof(buffer), "%lf", num);
if(len > 0) {
#if defined(_WIN32) || defined(_WIN64)
fwrite(buffer, 1, len, stdout);
#endif
#if defined(__unix__) || defined(__unix) || defined(__linux__) || defined(__APPLE__) || defined(__MACH__)
write(fd, buffer, len);
#endif
}
}
void nprintf(int fd, const char *format, ...) {
va_list args;
va_start(args, format);
const char *ptr = format;
while (*ptr) {
if (*ptr == '%' && *(ptr + 1) == 'd') {
int num = va_arg(args, int);
write_num(fd, num);
ptr += 2;
} else if (*ptr == '%' && *(ptr + 1) == 'u') {
unsigned int num = va_arg(args, unsigned int);
write_unsigned(fd, num);
ptr += 2;
} else if (*ptr == '%' && *(ptr + 1) == 's') {
char *str = va_arg(args, char*);
write_str(fd, str);
ptr += 2;
} else if (*ptr == '%' && *(ptr + 1) == 'c') {
char c = (char)va_arg(args, int);
write_char(fd, c);
ptr += 2;
} else if (*ptr == '%' && *(ptr + 1) == 'x') {
unsigned int hex = va_arg(args, unsigned int);
write_hex(fd, hex);
ptr += 2;
} else if (*ptr == '%' && *(ptr + 1) == 'p') {
void *p = va_arg(args, void*);
write_ptr(fd, p);
ptr += 2;
} else if (*ptr == '%' && *(ptr + 1) == 'f') {
double num = va_arg(args, double);
write_float(fd, num);
ptr += 2;
} else if (*ptr == '%' && *(ptr + 1) == '%' ) {
write_char(fd, '%');
ptr += 2;
} else if (*ptr == '%' && *(ptr + 1) == 'l' && *(ptr + 2) == 'd') {
long int num = va_arg(args, long int);
write_long(fd, num);
ptr += 3;
} else if (*ptr == '%' && *(ptr + 1) == 'l' && *(ptr + 2) == 'f') {
double num = va_arg(args, double);
write_double(fd, num);
ptr += 3;
} else {
write_char(fd, *ptr);
ptr++;
}
}
va_end(args);
}
typedef struct {
int SUCCESS;
int FAILURE;
int INVALID_ARGUMENT;
int COMMAND_NOT_FOUND;
int PERMISSION_DENIED;
int SIGNAL_TERMINATED;
int SIGNAL_INT;
int SEGFAULT;
int OUT_OF_RANGE;
} ExitCode;
static const ExitCode ExitStatus = {
.SUCCESS = 0,
.FAILURE = 1,
.INVALID_ARGUMENT = 128,
.COMMAND_NOT_FOUND = 127,
.PERMISSION_DENIED = 126,
.SIGNAL_TERMINATED = 137,
.SIGNAL_INT = 130,
.SEGFAULT = 11,
.OUT_OF_RANGE = 255
};
void nexit(int status) {
exit(status);
}
int nsys(const char *command) {
return system(command);
}
void __NCLRSCRN__() {
#if defined(_WIN32) || defined(_WIN64)
if (nsys("cls") == -1) {
return;
}
#elif defined(__unix__) || defined(__unix) || defined(__linux__) || defined(__APPLE__) || defined(__MACH__)
if (system("clear") == -1) {
return;
}
#else
return;
#endif
}
typedef struct MemBlock {
char* data;
custom_size_t size;
int free;
struct MemBlock* next;
char name[64];
} MemBlock;
typedef struct MemManager {
custom_size_t total;
custom_size_t used;
MemBlock* blocks;
} MemManager;
MemManager* mem_init(custom_size_t total) {
MemManager* mgr = (MemManager*)malloc(sizeof(MemManager));
if (!mgr) return NULL;
mgr->total = total;
mgr->used = 0;
mgr->blocks = NULL;
return mgr;
}
MemBlock* mem_alloc(MemManager* mgr, custom_size_t size, const char* name) {
if (!mgr || size == 0 || mgr->used + size > mgr->total || !name) return NULL;
MemBlock* blk = (MemBlock*)malloc(sizeof(MemBlock));
if (!blk) return NULL;
blk->data = (char*)malloc(size);
if (!blk->data) {
free(blk);
return NULL;
}
blk->size = size;
blk->free = 0;
blk->next = mgr->blocks;
mgr->blocks = blk;
mgr->used += size;
strncpy(blk->name, name, sizeof(blk->name) - 1);
blk->name[sizeof(blk->name) - 1] = '\0';
return blk;
}
void mem_free(MemManager* mgr, MemBlock* blk) {
if (!mgr || !blk || blk->free) return;
blk->free = 1;
mgr->used -= blk->size;
free(blk->data);
blk->data = NULL;
}
void mem_cleanup(MemManager* mgr) {
if (!mgr) return;
MemBlock* current = mgr->blocks;
while (current) {
MemBlock* next = current->next;
free(current->data);
free(current);
current = next;
}
free(mgr);
}
nstring nstrcpy(const nstring *src) {
if (!src || !src->str) return (nstring){.str = NULL, .len = 0};
char *new_str = (char *)malloc(src->len + 1);
for (custom_size_t i = 0; i < src->len; i++) {
new_str[i] = src->str[i];
}
new_str[src->len] = '\0';
return (nstring){.str = new_str, .len = src->len};
}
int nstr_cmp(const nstring *s1, const nstring *s2) {
if (!s1 || !s2 || !s1->str || !s2->str) return 0;
custom_size_t min_len = s1->len < s2->len ? s1->len : s2->len;
for (custom_size_t i = 0; i < min_len; i++) {
if (s1->str[i] != s2->str[i]) return s1->str[i] - s2->str[i];
}
return s1->len - s2->len;
}
nstring nstrcat(const nstring *s1, const nstring *s2) {
if (!s1 || !s2 || !s1->str || !s2->str) return (nstring){.str = NULL, .len = 0};
custom_size_t new_len = s1->len + s2->len;
char *new_str = (char *)malloc(new_len + 1);
memcpy(new_str, s1->str, s1->len);
memcpy(new_str + s1->len, s2->str, s2->len);
new_str[new_len] = '\0';
return (nstring){.str = new_str, .len = new_len};
}
void ninput(nstring *ns) {
size_t buffer_size = 64; // Start with an initial buffer size
size_t len = 0; // Length of the string input
char *buffer = (char *)malloc(buffer_size);
int ch;
if (!buffer) {
fprintf(stderr, "Memory allocation failed.\n");
return;
}
// Read input character by character
while ((ch = getchar()) != '\n' && ch != EOF) {
// Check if we need to expand the buffer
if (len + 1 >= buffer_size) {
buffer_size *= 2;
char *new_buffer = (char *)realloc(buffer, buffer_size);
if (!new_buffer) {
free(buffer);
fprintf(stderr, "Memory reallocation failed.\n");
return;
}
buffer = new_buffer;
}
buffer[len++] = (char)ch; // Store the character in the buffer
}
buffer[len] = '\0'; // Null-terminate the string
// Assign the string to nstring
ns->len = len;
ns->str = buffer;
}