LCOV - code coverage report
Current view: top level - shared/libc - printf.c (source / functions) Hit Total Coverage
Test: unix_coverage_v1.22.0-343-g7b050b366.info Lines: 24 38 63.2 %
Date: 2024-04-25 17:00:48 Functions: 1 2 50.0 %
Branches: 3 6 50.0 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * This file is part of the MicroPython project, http://micropython.org/
       3                 :            :  *
       4                 :            :  * The MIT License (MIT)
       5                 :            :  *
       6                 :            :  * Copyright (c) 2013, 2014 Damien P. George
       7                 :            :  *
       8                 :            :  * Permission is hereby granted, free of charge, to any person obtaining a copy
       9                 :            :  * of this software and associated documentation files (the "Software"), to deal
      10                 :            :  * in the Software without restriction, including without limitation the rights
      11                 :            :  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
      12                 :            :  * copies of the Software, and to permit persons to whom the Software is
      13                 :            :  * furnished to do so, subject to the following conditions:
      14                 :            :  *
      15                 :            :  * The above copyright notice and this permission notice shall be included in
      16                 :            :  * all copies or substantial portions of the Software.
      17                 :            :  *
      18                 :            :  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
      19                 :            :  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
      20                 :            :  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
      21                 :            :  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
      22                 :            :  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
      23                 :            :  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
      24                 :            :  * THE SOFTWARE.
      25                 :            :  */
      26                 :            : 
      27                 :            : #include "py/mpconfig.h"
      28                 :            : 
      29                 :            : #include <stdint.h>
      30                 :            : #include <string.h>
      31                 :            : #include <stdarg.h>
      32                 :            : 
      33                 :            : #include "py/obj.h"
      34                 :            : #include "py/mphal.h"
      35                 :            : 
      36                 :            : #if MICROPY_PY_BUILTINS_FLOAT
      37                 :            : #include "py/formatfloat.h"
      38                 :            : #endif
      39                 :            : 
      40                 :            : #if MICROPY_DEBUG_PRINTERS
      41                 :          0 : int DEBUG_printf(const char *fmt, ...) {
      42                 :          0 :     va_list ap;
      43                 :          0 :     va_start(ap, fmt);
      44                 :          0 :     int ret = mp_vprintf(MICROPY_DEBUG_PRINTER, fmt, ap);
      45                 :          0 :     va_end(ap);
      46                 :          0 :     return ret;
      47                 :            : }
      48                 :            : #endif
      49                 :            : 
      50                 :            : #if MICROPY_USE_INTERNAL_PRINTF
      51                 :            : 
      52                 :            : #undef putchar  // Some stdlibs have a #define for putchar
      53                 :            : int printf(const char *fmt, ...);
      54                 :            : int vprintf(const char *fmt, va_list ap);
      55                 :            : int putchar(int c);
      56                 :            : int puts(const char *s);
      57                 :            : int vsnprintf(char *str, size_t size, const char *fmt, va_list ap);
      58                 :            : int snprintf(char *str, size_t size, const char *fmt, ...);
      59                 :            : 
      60                 :            : int printf(const char *fmt, ...) {
      61                 :          0 :     va_list ap;
      62                 :          0 :     va_start(ap, fmt);
      63                 :          0 :     int ret = mp_vprintf(MICROPY_INTERNAL_PRINTF_PRINTER, fmt, ap);
      64                 :          0 :     va_end(ap);
      65                 :          0 :     return ret;
      66                 :            : }
      67                 :            : 
      68                 :            : int vprintf(const char *fmt, va_list ap) {
      69                 :          0 :     return mp_vprintf(MICROPY_INTERNAL_PRINTF_PRINTER, fmt, ap);
      70                 :            : }
      71                 :            : 
      72                 :            : // need this because gcc optimises printf("%c", c) -> putchar(c), and printf("a") -> putchar('a')
      73                 :            : int putchar(int c) {
      74                 :         32 :     char chr = c;
      75                 :         32 :     MICROPY_INTERNAL_PRINTF_PRINTER->print_strn(MICROPY_INTERNAL_PRINTF_PRINTER->data, &chr, 1);
      76                 :         32 :     return chr;
      77                 :            : }
      78                 :            : 
      79                 :            : // need this because gcc optimises printf("string\n") -> puts("string")
      80                 :            : int puts(const char *s) {
      81                 :          4 :     MICROPY_INTERNAL_PRINTF_PRINTER->print_strn(MICROPY_INTERNAL_PRINTF_PRINTER->data, s, strlen(s));
      82                 :          4 :     return putchar('\n'); // will return 10, which is >0 per specs of puts
      83                 :            : }
      84                 :            : 
      85                 :            : typedef struct _strn_print_env_t {
      86                 :            :     char *cur;
      87                 :            :     size_t remain;
      88                 :            : } strn_print_env_t;
      89                 :            : 
      90                 :         36 : static void strn_print_strn(void *data, const char *str, size_t len) {
      91                 :         36 :     strn_print_env_t *strn_print_env = data;
      92         [ -  + ]:         36 :     if (len > strn_print_env->remain) {
      93                 :          0 :         len = strn_print_env->remain;
      94                 :            :     }
      95                 :         36 :     memcpy(strn_print_env->cur, str, len);
      96                 :         36 :     strn_print_env->cur += len;
      97                 :         36 :     strn_print_env->remain -= len;
      98                 :         36 : }
      99                 :            : 
     100                 :            : #if defined(__GNUC__) && !defined(__clang__) && __GNUC__ < 9
     101                 :            : // uClibc requires this alias to be defined, or there may be link errors
     102                 :            : // when linkings against it statically.
     103                 :            : // GCC 9 gives a warning about missing attributes so it's excluded until
     104                 :            : // uClibc+GCC9 support is needed.
     105                 :            : int __GI_vsnprintf(char *str, size_t size, const char *fmt, va_list ap) __attribute__((weak, alias("vsnprintf")));
     106                 :            : #endif
     107                 :            : 
     108                 :            : int vsnprintf(char *str, size_t size, const char *fmt, va_list ap) {
     109                 :         24 :     strn_print_env_t strn_print_env = {str, size};
     110                 :         24 :     mp_print_t print = {&strn_print_env, strn_print_strn};
     111                 :         24 :     int len = mp_vprintf(&print, fmt, ap);
     112                 :            :     // add terminating null byte
     113         [ +  - ]:         24 :     if (size > 0) {
     114         [ -  + ]:         24 :         if (strn_print_env.remain == 0) {
     115                 :          0 :             strn_print_env.cur[-1] = 0;
     116                 :            :         } else {
     117                 :         24 :             strn_print_env.cur[0] = 0;
     118                 :            :         }
     119                 :            :     }
     120                 :         24 :     return len;
     121                 :            : }
     122                 :            : 
     123                 :            : int snprintf(char *str, size_t size, const char *fmt, ...) {
     124                 :         24 :     va_list ap;
     125                 :         24 :     va_start(ap, fmt);
     126                 :         24 :     int ret = vsnprintf(str, size, fmt, ap);
     127                 :         24 :     va_end(ap);
     128                 :         24 :     return ret;
     129                 :            : }
     130                 :            : 
     131                 :            : #endif // MICROPY_USE_INTERNAL_PRINTF

Generated by: LCOV version 1.15-5-g462f71d