Last active
June 14, 2019 16:44
-
-
Save glukianets/a6aff6f8e6676a8790e7adee782ce5a5 to your computer and use it in GitHub Desktop.
A simple c function proxying standard printf slightly modifying contents on the way.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdarg.h> | |
#include <assert.h> | |
int prontf(const char *fmt, ...) { | |
va_list varargs; | |
va_start(varargs, fmt); | |
int bufsize = vsnprintf(NULL, 0, fmt, varargs) + 1; | |
va_end(varargs); | |
if (bufsize <= 0) | |
return bufsize; | |
char buf[bufsize]; | |
buf[bufsize - 1] = '\0'; | |
va_start(varargs, fmt); | |
int cwr = vsnprintf(buf, bufsize, fmt, varargs); | |
va_end(varargs); | |
assert(cwr <= bufsize); | |
for (char *c = buf; *c != '\0'; ++c) | |
*c = *c == 'i' ? 'o' : *c; | |
return printf("%s", buf); | |
} | |
int main() { | |
prontf("Think different"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment