#include #include #include #include FILE term = 0; void print(char *s) { fprint(term, s); } void printf(char *format, ...) { va_list ap; va_start(ap, format); vfprintf(term, format, ap); va_end(ap); } char* readln() { return freadln(term); } void fprintf(FILE f, char* format, ...) { va_list ap; va_start(ap, format); vfprintf(f, format, ap); va_end(ap); } // FUNCTIONS void fprint(FILE f, char *s) { write(f, 0, strlen(s), s); } void fprint_int(FILE f, int number) { char s[32]; char *v = format_int(s, number); *v = 0; fprint(f, s); } void fprint_hex(FILE f, unsigned v) { char s[11]; char *e = format_hex(s, v); *e = 0; fprint(f, s); } void vfprintf(FILE f, char *format, va_list ap) { char bb[256]; int bufl = 256; char *buf = bb; char *end = buf; while (*format) { if (*format == '%') { // ASSUMPTION : (TODO) WE HAVE ENOUGH SPACE - NOT THE CASE!!! format++; if (*format == 'd' || *format == 'i') { end = format_int(end, va_arg(ap, int)); } else if (*format == 'p') { end = format_hex(end, va_arg(ap, uint32_t)); } else if (*format == 's') { char *s = va_arg(ap, char*); strcpy(end, s); end += strlen(s); } format++; } else { *(end++) = *(format++); } if (end - buf > bufl - 2) { bufl *= 2; char *nbuf = (char*)malloc(bufl); memcpy(nbuf, buf, end - buf); end = nbuf + (end - buf); if (buf != bb) free(buf); buf = nbuf; } } *end = 0; fprint(f, buf); if (buf != bb) free(buf); }