|
| 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 | + } |
0 commit comments