# Patch for portSPRINTF

**URL:** <https://forums.freertos.org/t/patch-for-portsprintf/4483>\
**Category:** Kernel\
**Created:** [May 30, 2011, 8:39am UTC](https://forums.freertos.org/t/patch-for-portsprintf/4483 "2011-05-30T08:39:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [May 30, 2011, 8:39am UTC](https://forums.freertos.org/t/patch-for-portsprintf/4483/1 "2011-05-30T08:39:35Z")

</div>

**[pmunts](https://sourceforge.net/u/pmunts/)** wrote on [Monday, May 30, 2011](https://sourceforge.net/p/freertos/discussion/382005/thread/c762f451/#ad52):

Using sprintf() in tasks.c causes floating point libraries to be linked when using ARM gcc/newlib toolchains.&nbsp; The following patch to tasks.c allows substituting the integer only siprintf() if the following macro is defined in FreeRTOSConfig.h:

#define portSPRINTF siprintf

– tasks.c.original 2011-05-30 10:25:51.000000000 +0200  
+++ tasks.c.new 2011-05-30 10:28:53.000000000 +0200  
@@ -74,6 +74,10 @@

#undef MPU\_WRAPPERS\_INCLUDED\_FROM\_API\_FILE

+#ifndef portSPRINTF  
+#define portSPRINTF sprintf  
+#endif  
+  
/\*  
&nbsp; \* Macro to define the amount of stack available to the idle task.  
&nbsp; \*/  
@@ -2232,7 +2236,7 @@  
}  
#endif

- sprintf( pcStatusString, ( char \* ) “%s\t\t%c\t%u\t%u\t%u\r\n”, pxNextTCB-\>pcTaskName, cStatus, ( unsigned int ) pxNextTCB-\>uxPriority, usStackRemaining, ( unsigned int ) pxNextTCB-\>uxTCBNumber );  
+ portSPRINTF( pcStatusString, ( char \* ) “%s\t\t%c\t%u\t%u\t%u\r\n”, pxNextTCB-\>pcTaskName, cStatus, ( unsigned int ) pxNextTCB-\>uxPriority, usStackRemaining, ( unsigned int ) pxNextTCB-\>uxTCBNumber );  
strcat( ( char \* ) pcWriteBuffer, ( char \* ) pcStatusString );

} while( pxNextTCB != pxFirstTCB );  
@@ -2262,7 +2266,7 @@  
if( pxNextTCB-\>ulRunTimeCounter == 0 )  
{  
/\* The task has used no CPU time at all. \*/  
- sprintf( pcStatsString, ( char \* ) “%s\t\t0\t\t0%%\r\n”, pxNextTCB-\>pcTaskName );  
+ portSPRINTF( pcStatsString, ( char \* ) “%s\t\t0\t\t0%%\r\n”, pxNextTCB-\>pcTaskName );  
}  
else  
{  
@@ -2275,13 +2279,13 @@  
{  
#ifdef portLU\_PRINTF\_SPECIFIER\_REQUIRED  
{  
- sprintf( pcStatsString, ( char \* ) “%s\t\t%lu\t\t%lu%%\r\n”, pxNextTCB-\>pcTaskName, pxNextTCB-\>ulRunTimeCounter, ulStatsAsPercentage );  
+ portSPRINTF( pcStatsString, ( char \* ) “%s\t\t%lu\t\t%lu%%\r\n”, pxNextTCB-\>pcTaskName, pxNextTCB-\>ulRunTimeCounter, ulStatsAsPercentage );  
}  
#else  
{  
/\* sizeof( int ) == sizeof( long ) so a smaller  
printf() library can be used. \*/  
- sprintf( pcStatsString, ( char \* ) “%s\t\t%u\t\t%u%%\r\n”, pxNextTCB-\>pcTaskName, ( unsigned int ) pxNextTCB-\>ulRunTimeCounter, ( unsigned int ) ulStatsAsPercentage );  
+ portSPRINTF( pcStatsString, ( char \* ) “%s\t\t%u\t\t%u%%\r\n”, pxNextTCB-\>pcTaskName, ( unsigned int ) pxNextTCB-\>ulRunTimeCounter, ( unsigned int ) ulStatsAsPercentage );  
}  
#endif  
}  
@@ -2291,13 +2295,13 @@  
consumed less than 1% of the total run time. \*/  
#ifdef portLU\_PRINTF\_SPECIFIER\_REQUIRED  
{  
- sprintf( pcStatsString, ( char \* ) “%s\t\t%lu\t\t\<1%%\r\n”, pxNextTCB-\>pcTaskName, pxNextTCB-\>ulRunTimeCounter );  
+ portSPRINTF( pcStatsString, ( char \* ) “%s\t\t%lu\t\t\<1%%\r\n”, pxNextTCB-\>pcTaskName, pxNextTCB-\>ulRunTimeCounter );  
}  
#else  
{  
/\* sizeof( int ) == sizeof( long ) so a smaller  
printf() library can be used. \*/  
- sprintf( pcStatsString, ( char \* ) “%s\t\t%u\t\t\<1%%\r\n”, pxNextTCB-\>pcTaskName, ( unsigned int ) pxNextTCB-\>ulRunTimeCounter );  
+ portSPRINTF( pcStatsString, ( char \* ) “%s\t\t%u\t\t\<1%%\r\n”, pxNextTCB-\>pcTaskName, ( unsigned int ) pxNextTCB-\>ulRunTimeCounter );  
}  
#endif  
}

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [May 30, 2011, 8:59am UTC](https://forums.freertos.org/t/patch-for-portsprintf/4483/2 "2011-05-30T08:59:20Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Monday, May 30, 2011](https://sourceforge.net/p/freertos/discussion/382005/thread/c762f451/#8914):

Thanks for the suggestions.&nbsp; These lines of code are very tricky to get universally acceptable because the code is (officially) built with 17 different compilers.

If you don’t want floating point support in your sprintf() calls then include the file called printf-stdarg.c in your project.&nbsp; That contains an extremely small implementation that uses very little stack.&nbsp; The file is included in lots of the FreeRTOS/Demo subdirectories.

Regards.

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [May 30, 2011, 9:08am UTC](https://forums.freertos.org/t/patch-for-portsprintf/4483/3 "2011-05-30T09:08:02Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Monday, May 30, 2011](https://sourceforge.net/p/freertos/discussion/382005/thread/c762f451/#f86e):

…not to mention the 8, 16, 20, and 32 bit architectures supported.

Having a port specific macro is probably a better solution that the current code, but will take some effort to refit to the exiting ports.

Regards.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [May 30, 2011, 11:02am UTC](https://forums.freertos.org/t/patch-for-portsprintf/4483/4 "2011-05-30T11:02:38Z")

</div>

**[pmunts](https://sourceforge.net/u/pmunts/)** wrote on [Monday, May 30, 2011](https://sourceforge.net/p/freertos/discussion/382005/thread/c762f451/#4815):

The

+#ifndef portSPRINTF  
+#define portSPRINTF sprintf  
+#endif

at the top of my patch ensures that if you don’t define portSPRINTF somewhere it will execute exactly as before the patch, with or without printf-stdarg.c.

The thing is, I don’t want to replace the sprintf() in newlib with the one in printf-strarg.c.&nbsp; I do want to able to use the integer- only siprintf() in tasks.c to avoid linking in floating point libraries.&nbsp; This patch allows me to do that without any other side effects and I thought I would share it.&nbsp; Interestingly, newlib itself has the same issue, calling sprintf() instead of siprintf in ctime() for instance.&nbsp; I 've submitted a patch to them, too.

I do appreciate the testing effort.&nbsp; I am only supporting 5 boards with 2 toolchains and it is almost too much.
