forked from mpaland/printf
-
Notifications
You must be signed in to change notification settings - Fork 54
/
aliasing.c
52 lines (47 loc) · 1.17 KB
/
aliasing.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
#include <printf_config.h>
#include <printf/printf.h>
int strcmp_(const char* lhs, const char* rhs)
{
while (1) {
char lhs_c = *lhs++;
char rhs_c = *rhs++;
if (lhs_c != rhs_c) { return lhs_c - rhs_c; }
if (lhs_c == '\0') { return 0; }
}
}
enum { BufferSize = 100 };
char buffer[BufferSize];
void putchar_(char c)
{
for(char* ptr = buffer; ptr - buffer < BufferSize; ptr++) {
if (*ptr == '\0') {
*ptr++ = c;
*ptr++ = '\0';
return;
}
}
}
void clear_buffer(void)
{
for(int i = 0; i < BufferSize; i++) {
buffer[i] = '\0';
}
}
int main(void)
{
#if PRINTF_ALIAS_STANDARD_FUNCTION_NAMES_SOFT || PRINTF_ALIAS_STANDARD_FUNCTION_NAMES_HARD
clear_buffer();
printf("printf'ing an integer: %d and a string: %s\n", 12, "Hello world");
const char* expected = "printf'ing an integer: 12 and a string: Hello world\n";
if (strcmp_(buffer, expected) != 0) {
return(-1);
}
clear_buffer();
sprintf(buffer, "sprintf'ing an integer: %d and a string: %s\n", 34, "quick brown fox");
expected = "sprintf'ing an integer: 34 and a string: quick brown fox\n";
if (strcmp_(buffer, expected) != 0) {
return(-1);
}
#endif
return 0;
}