[PATCH v2 6/6] cmd: printf: forward '%p' format string specifier

Heinrich Schuchardt xypron.glpk at gmx.de
Tue Dec 12 16:24:17 CET 2023


On 12.12.23 09:52, lukas.funke-oss at weidmueller.com wrote:
> From: Lukas Funke <lukas.funke at weidmueller.com>
>
> Forward '%p' format specifier to the underlying format logic in order
> to print pointers, especially bitmaps.
>
> Signed-off-by: Lukas Funke <lukas.funke at weidmueller.com>
> ---
>
> (no changes since v1)
>
>   cmd/printf.c | 29 +++++++++++++++++++++++++++++
>   1 file changed, 29 insertions(+)
>
> diff --git a/cmd/printf.c b/cmd/printf.c
> index 0c6887e0d6..a90c923871 100644
> --- a/cmd/printf.c
> +++ b/cmd/printf.c
> @@ -90,6 +90,7 @@
>   #include <stddef.h>
>   #include <stdio.h>
>   #include <stdlib.h>
> +#include <linux/bitmap.h>
>
>   #define WANT_HEX_ESCAPES 0
>   #define PRINT_CONVERSION_ERROR 1
> @@ -476,6 +477,16 @@ static int get_width_prec(const char *str)
>   	return (int)v;
>   }
>
> +static int print_pointer(struct print_inf *inf, char *format,
> +			 unsigned int fmt_length, const char *argument)
> +{
> +	u64 value = simple_strtoull(argument, NULL, 0);
> +
> +	printf_str(inf, format, &value);
> +
> +	return inf->error;
> +}
> +
>   /* Print the text in FORMAT, using ARGV for arguments to any '%' directives.
>    * Return advanced ARGV.
>    */
> @@ -536,6 +547,24 @@ static char **print_formatted(struct print_inf *inf, char *f, char **argv, int *
>   					}
>   				}
>   			}
> +			if (*f == 'p') {
> +				static const char ptr_format_chars[] = "bl";
> +				++f;
> +				++direc_length;
> +				char *p = strchr(ptr_format_chars, *f);
> +				/* consume whole format token */
> +				while (*f != '\0' && *(p++) == *f) {
> +					++f;
> +					++direc_length;
> +				}
> +				if (print_pointer(inf, direc_start, direc_length, *argv++)) {
> +					printf("`%s': invalid format\n", direc_start);

There are different error codes. PRINT_TRUNCATED_ERROR does not indicate
an invalid format.

Best regards

Heinrich

> +					/* causes main() to exit with error */
> +					return saved_argv - 1;
> +				}
> +				f--;
> +				break;
> +			}
>
>   			/* Remove "lLhz" size modifiers, repeatedly.
>   			 * bash does not like "%lld", but coreutils



More information about the U-Boot mailing list