You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/headers/ti/sprintf.rst
+12-12Lines changed: 12 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,24 +1,24 @@
1
1
.. _sprintf_h:
2
2
3
3
ti/sprintf.h
4
-
======================
4
+
============
5
5
6
6
.. code-block:: c
7
7
8
8
#include <ti/sprintf.h>
9
9
10
-
The OS comes with an implementation of ANSI C89 `sprintf`, which can reduce the size of a program by ~8 KiB. However, it is very limited in functionality. It does not support width specifiers, and it won't accept :code:`long` or :code:`float` arguments.
10
+
The OS comes with an implementation of ANSI C89 `sprintf`, which can reduce the size of a program by ~8 KiB. However, it is very limited in functionality. It does not support length modifiers, and it won't accept :code:`long` or :code:`float` arguments.
11
11
12
12
boot_sprintf
13
-
----------------------------
13
+
------------
14
14
15
-
The following type specifiers are supported :code:`%s %c %d %i %u %o %x %X %p %n`, alongside the flags :code:`-+#0*` in addition to the space flag and width field.
15
+
The following type specifiers are supported :code:`%s %c %d %i %u %o %x %X %p %n`, alongside the flags :code:`-+#0*` in addition to the space flag and minimum field width.
16
16
17
17
All length modifiers :code:`hh h l ll j z t L` and floating point specifiers :code:`%f %g %e %a` are **not** supported.
18
18
19
19
Additionally, each individual argument will write no more than 255 characters each. This means that any strings written with :code:`%s` will be truncated after 255 characters.
20
20
21
-
The :code:`<ti/sprintf.h>` provides `boot_sprintf`, in addition to `boot_snprintf` and `boot_asprintf` as macros.
21
+
:code:`<ti/sprintf.h>` provides `boot_sprintf`, in addition to `boot_snprintf` and `boot_asprintf` as macros.
22
22
23
23
.. code-block:: c
24
24
@@ -30,8 +30,8 @@ The :code:`<ti/sprintf.h>` provides `boot_sprintf`, in addition to `boot_snprint
30
30
31
31
Because the OS does not provide `vsprintf`, `boot_snprintf` and `boot_asprintf` are implemented as macros. This means that :code:`...` or :code:`__VA_ARGS__` will be evaluated twice when the macro is expanded. `sprintf` is traditionally "unsafe" because a maximum output length cannot be specified, which can cause buffer overflows. By writing to an unmapped memory address :code:`0xE40000`, `boot_sprintf` can safely write up to ~786000 bytes which can then be used to determine the length of the output.
32
32
33
-
replacing printf functions
34
-
----------------------------
33
+
Replacing printf functions
34
+
--------------------------
35
35
36
36
To disable all other printf functions with `boot_sprintf`, `boot_snprintf`, and `boot_asprintf`, add the following line to the Makefile. More information :ref:`here <printf>`.
37
37
@@ -44,12 +44,12 @@ To disable all other printf functions with `boot_sprintf`, `boot_snprintf`, and
44
44
:code:`std::snprintf` and :code:`std::asprintf` will cause errors if :code:`HAS_PRINTF = NO`
45
45
46
46
boot_asprintf
47
-
----------------------------
47
+
-------------
48
48
49
49
`boot_asprintf` functions similarly to `asprintf <https://www.man7.org/linux/man-pages/man3/asprintf.3.html>`_. It will automatically allocate a buffer containing the output from `boot_sprintf`. The buffer shall be deallocated with :code:`free`. The returned buffer will be :code:`NULL` if an allocation or formatting error occurs.
50
50
51
51
boot_snprintf
52
-
----------------------------
52
+
-------------
53
53
54
54
`boot_snprintf` is similar to `snprintf`, except that it returns an empty string if the result of `boot_sprintf` won't fit inside the buffer. Otherwise passing in :code:`boot_snprintf(NULL, 0, format, args)` can be used to query the length of the output without writing to a buffer.
55
55
@@ -58,7 +58,7 @@ The truncating behavior of C99 `snprintf` can be replicated with `boot_asprintf`
58
58
.. code-block:: c
59
59
60
60
char buf[20];
61
-
char* temp;
61
+
char *temp;
62
62
boot_asprintf(&temp, format, ...);
63
63
if (temp != NULL) {
64
64
strncpy(buf, temp, sizeof(buf));
@@ -67,12 +67,12 @@ The truncating behavior of C99 `snprintf` can be replicated with `boot_asprintf`
67
67
}
68
68
69
69
printf and fprintf
70
-
----------------------------
70
+
------------------
71
71
`printf` and `fprintf` can be replicated by using `boot_asprintf` and `fputs`
0 commit comments