Skip to content

Commit 54dfd18

Browse files
committed
feat(printf): added PRINTF_OVERRIDE_LIBC support
Fixes #16
1 parent cc8f3bc commit 54dfd18

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,8 @@ int length = sprintf(NULL, "Hello, world"); // length is set to 12
160160

161161
| Name | Default value | Description |
162162
|------|---------------|-------------|
163+
| PRINTF_INCLUDE_CONFIG_H | undefined | Define this as compiler switch (e.g. `gcc -DPRINTF_INCLUDE_CONFIG_H`) to include a "printf_config.h" definition file |
164+
| PRINTF_OVERRIDE_LIBC | undefined | Define this to override the LIBC function declarations by using `printf_()` instead of `printf()` directly |
163165
| PRINTF_NTOA_BUFFER_SIZE | 32 | ntoa (integer) conversion buffer size. This must be big enough to hold one converted numeric number _including_ leading zeros, normally 32 is a sufficient value. Created on the stack |
164166
| PRINTF_FTOA_BUFFER_SIZE | 32 | ftoa (float) conversion buffer size. This must be big enough to hold one converted float number _including_ leading zeros, normally 32 is a sufficient value. Created on the stack |
165167
| PRINTF_SUPPORT_FLOAT | defined | Define this to enable floating point (%f) support |

printf.c

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -699,8 +699,11 @@ static int _vsnprintf(out_fct_type out, char* buffer, const size_t maxlen, const
699699

700700
///////////////////////////////////////////////////////////////////////////////
701701

702-
702+
#ifndef PRINTF_OVERRIDE_LIBC
703703
int printf(const char* format, ...)
704+
#else
705+
int printf_(const char* format, ...)
706+
#endif
704707
{
705708
va_list va;
706709
va_start(va, format);
@@ -711,7 +714,11 @@ int printf(const char* format, ...)
711714
}
712715

713716

717+
#ifndef PRINTF_OVERRIDE_LIBC
714718
int sprintf(char* buffer, const char* format, ...)
719+
#else
720+
int sprintf_(char* buffer, const char* format, ...)
721+
#endif
715722
{
716723
va_list va;
717724
va_start(va, format);
@@ -721,7 +728,11 @@ int sprintf(char* buffer, const char* format, ...)
721728
}
722729

723730

731+
#ifndef PRINTF_OVERRIDE_LIBC
724732
int snprintf(char* buffer, size_t count, const char* format, ...)
733+
#else
734+
int snprintf_(char* buffer, size_t count, const char* format, ...)
735+
#endif
725736
{
726737
va_list va;
727738
va_start(va, format);
@@ -731,13 +742,21 @@ int snprintf(char* buffer, size_t count, const char* format, ...)
731742
}
732743

733744

745+
#ifndef PRINTF_OVERRIDE_LIBC
734746
int vsnprintf(char* buffer, size_t count, const char* format, va_list va)
747+
#else
748+
int vsnprintf_(char* buffer, size_t count, const char* format, va_list va)
749+
#endif
735750
{
736751
return _vsnprintf(_out_buffer, buffer, count, format, va);
737752
}
738753

739754

755+
#ifndef PRINTF_OVERRIDE_LIBC
740756
int fctprintf(void (*out)(char character, void* arg), void* arg, const char* format, ...)
757+
#else
758+
int fctprintf_(void (*out)(char character, void* arg), void* arg, const char* format, ...)
759+
#endif
741760
{
742761
va_list va;
743762
va_start(va, format);

printf.h

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
// \brief Tiny printf, sprintf and snprintf implementation, optimized for speed on
2626
// embedded systems with a very limited resources.
2727
// Use this instead of bloated standard/newlib printf.
28-
// These routines are thread safe and reentrant!
28+
// These routines are thread safe and reentrant.
2929
//
3030
///////////////////////////////////////////////////////////////////////////////
3131

@@ -56,6 +56,12 @@ extern "C" {
5656
void _putchar(char character);
5757

5858

59+
// if PRINTF_OVERRIDE_LIBC is is defined, the regular printf() API is overridden by macro defines
60+
// and internal underscore-appended functions like printf_() are used to avoid conflicts with
61+
// LIBC defined printf() functions.
62+
// default: undefined
63+
#ifndef PRINTF_OVERRIDE_LIBC
64+
5965
/**
6066
* Tiny printf implementation
6167
* You have to implement _putchar if you use printf()
@@ -97,6 +103,23 @@ int vsnprintf(char* buffer, size_t count, const char* format, va_list va);
97103
*/
98104
int fctprintf(void (*out)(char character, void* arg), void* arg, const char* format, ...);
99105

106+
#else // PRINTF_OVERRIDE_LIBC
107+
108+
// override the LIBC defined function names
109+
#define printf printf_
110+
#define sprintf sprintf_
111+
#define snprintf snprintf_
112+
#define vsnprintf vsnprintf_
113+
#define fctprintf fctprintf_
114+
115+
int printf_(const char* format, ...);
116+
int sprintf_(char* buffer, const char* format, ...);
117+
int snprintf_(char* buffer, size_t count, const char* format, ...);
118+
int vsnprintf_(char* buffer, size_t count, const char* format, va_list va);
119+
int fctprintf_(void (*out)(char character, void* arg), void* arg, const char* format, ...);
120+
121+
#endif // PRINTF_OVERRIDE_LIBC
122+
100123

101124
#ifdef __cplusplus
102125
}

0 commit comments

Comments
 (0)