Skip to content

Commit 9e2a1a5

Browse files
committed
Added documentation for boot_sprintf
1 parent 456c232 commit 9e2a1a5

File tree

2 files changed

+77
-2
lines changed

2 files changed

+77
-2
lines changed

docs/headers/boot_sprintf.rst

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
.. _boot_sprintf:
2+
3+
boot_sprintf
4+
======================
5+
6+
.. code-block:: c
7+
8+
#include <boot_sprintf.h>
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.
11+
12+
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 width field.
13+
14+
All length modifiers :code:`hh h l ll j z t L` and floating point specifiers :code:`%f %g %e %a` are **not** supported.
15+
16+
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.
17+
18+
The :code:`<boot_sprintf.h>` header provides `boot_sprintf`. `boot_snprintf` and `boot_asprintf` are also provided as macros
19+
20+
.. code-block:: c
21+
22+
int boot_sprintf(char *restrict buffer, const char *restrict format, ...)
23+
24+
int boot_snprintf(char *buffer, size_t count, const char *restrict format, ...)
25+
26+
int boot_asprintf(char **restrict p_buffer, const char *restrict format, ...)
27+
28+
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.
29+
30+
replacing printf functions
31+
----------------------------
32+
33+
To disable all other printf functions with `boot_sprintf`, `boot_snprintf`, and `boot_asprintf`, add the following line to the Makefile. More information `here <https://ce-programming.github.io/toolchain/static/printf.html>`
34+
35+
.. code-block:: makefile
36+
37+
HAS_PRINTF = NO
38+
39+
.. warning::
40+
41+
:code:`std::snprintf` and :code:`std::asprintf` will cause errors if :code:`HAS_PRINTF = NO`
42+
43+
boot_asprintf
44+
----------------------------
45+
46+
`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.
47+
48+
boot_snprintf
49+
----------------------------
50+
51+
`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.
52+
53+
The truncating behavior of C99 `snprintf` can be replicated with `boot_asprintf`
54+
55+
.. code-block:: c
56+
char buf[20];
57+
char* temp;
58+
boot_asprintf(&temp, format, ...);
59+
if (temp != NULL) {
60+
strncpy(buf, temp, sizeof(buf));
61+
buf[sizeof(buf) - 1] = '\0'; // null terminate the end of the buffer
62+
free(temp);
63+
}
64+
65+
printf and fprintf
66+
----------------------------
67+
`printf` and `fprintf` can be replicated by using `boot_asprintf` and `fputs`
68+
69+
.. code-block:: c
70+
char* output;
71+
boot_asprintf(&output, format, ...);
72+
if (output != NULL) {
73+
// fprintf(stdout, ...) == printf(...)
74+
fputs(stdout, output);
75+
free(output);
76+
}

docs/static/printf.rst

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ However, they contribute around 8 KiB to the resultant program.
1111
It is highly recommended to not use `printf` and related functions at all because of this.
1212
If you insist on using these functions, this page details how to do so in the next section.
1313

14-
Alternatively, a limited `sprintf` implementation is baked into the OS which doesn't add any extra size to the resultant program.
15-
Only the 'c', 'd', 'u', 'x', and 's' format specifiers will probably work.
14+
Alternatively, a limited `sprintf` (no `long` or `float` support) implementation is baked into the OS which doesn't add any extra size to the resultant program. See the dedicated page for more information `<boot_sprintf.h> <https://ce-programming.github.io/toolchain/headers/boot_sprintf.html>`_.
1615
To disable all other printf functions and use this `sprintf` implementation, add the following line to the Makefile:
1716

1817
.. code-block:: makefile

0 commit comments

Comments
 (0)