[PATCH 9/9] dtput: Support adding strings with spaces

Grant Likely grant.likely at secretlab.ca
Thu Jul 7 04:47:45 EST 2011


On Tue, Jul 05, 2011 at 12:02:57PM -0700, Simon Glass wrote:
> Previously dtput would use sscanf() to read the string from the command line,
> even if no format string was provided. Thus it would split up words at
> whitespace boundaries, and put a \0 between each.
> 
> This changes that behavior. Previously:
> 
> $ ../dtput dtget-test.dtb -ts /compatible this is a test
> 
> $ ../dtget dtget-test.dtb /compatible
> 116 104 105 115 0 105 115 0 97 110 111 116 104 101 114 0 116 101 115 116 0
> 
> and you have to use the -ts flag to see this as a list of strings
> 
> $ ../dtget dtget-test.dtb -ts /compatible
> this is a test
> 
> Now:
> 
> $ ../dtput dtget-test.dtb -ts  /compatible "this is another test"
> 
> $ ../dtget dtget-test.dtb /compatible this is another test
> this is another test
> 
> $ ../dtget dtget-test.dtb /compatible -tb
> 116 104 105 115 32 105 115 32 97 110 111 116 104 101 114 32 116 101 115 116 0
> 
> Note that the \0 terminators are gone.
> 
> Signed-off-by: Simon Glass <sjg at chromium.org>

This would be squashed in with the dtput patch too.

g.

> ---
>  dtput.c            |    7 ++++++-
>  tests/run_tests.sh |    4 +++-
>  2 files changed, 9 insertions(+), 2 deletions(-)
> 
> diff --git a/dtput.c b/dtput.c
> index 48e3c75..d02e6ab 100644
> --- a/dtput.c
> +++ b/dtput.c
> @@ -55,7 +55,12 @@ static int encode_value(char **arg, int arg_count, const char *format,
>  	for (; arg_count > 0; arg++, arg_count--, upto += len) {
>  		/* assume integer unless told otherwise */
>  		if (type == 's') {
> -			sscanf(*arg, format ? format : "%s", value);
> +			if (format)
> +				sscanf(*arg, format, value);
> +			else {
> +				strncpy(value, *arg, MAX_VALUE_SIZE - 1);
> +				value[MAX_VALUE_SIZE - 1] = '\0';
> +			}
>  			len = strlen(value) + 1;
>  			if (verbose)
>  				fprintf(stderr, "\tstring: '%s'\n", value);
> diff --git a/tests/run_tests.sh b/tests/run_tests.sh
> index d95d1fe..71cfab3 100755
> --- a/tests/run_tests.sh
> +++ b/tests/run_tests.sh
> @@ -434,10 +434,12 @@ dtput_tests () {
>      file=dtget-test.dtb
>      $DTC -O dtb -o $file ${file%.dtb}.dts 2>/dev/null
>  
> -    # run_dtput_test <test-name> <expected-result> <file> <key> <value>
> +    # run_dtput_test <test-name> <expected-result> <file> <key> <value> <flags>
>      run_dtput_test "Simple string" "a_model" $file "/model" "a_model" -ts
>  
>      run_dtput_test "Multiple string s" "board1 board2" \
> +	$file "/compatible" "board1 board2" -ts -f%s
> +    run_dtput_test "Single string with spaces" "board1 board2" \
>  	$file "/compatible" "board1 board2" -ts
>      run_dtput_test "Integer" "32768" $file "/cpus/PowerPC,970 at 1/d-cache-size" \
>  	"32768"
> -- 
> 1.7.3.1
> 


More information about the devicetree-discuss mailing list