From 3e240741f127e39520089b48ed77c9b9c56b0b9e Mon Sep 17 00:00:00 2001 From: Ainar Garipov Date: Fri, 11 Feb 2022 17:41:36 +0300 Subject: [PATCH] cherry-pick: scripts: imp mips compat Updates #4269. Squashed commit of the following: commit f633e875f4f0ab767a0537d9bfe95734823f8a51 Author: Ainar Garipov Date: Fri Feb 11 17:33:53 2022 +0300 scripts: imp mips compat --- scripts/install.sh | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/scripts/install.sh b/scripts/install.sh index 3b1777ed..47aeeff4 100644 --- a/scripts/install.sh +++ b/scripts/install.sh @@ -39,8 +39,25 @@ is_command() { } # Function is_little_endian checks if the CPU is little-endian. +# +# See https://serverfault.com/a/163493/267530. is_little_endian() { - [ "$( head -c 6 /bin/sh | tail -c 1 )" = "$( printf '\001' )" ] + # The ASCII character "I" has the octal code of 111. In the two-byte octal + # display mode (-o), hexdump will print it either as "000111" on a little + # endian system or as a "111000" on a big endian one. Return the sixth + # character to compare it against the number '1'. + # + # Do not use echo -n, because its behavior in the presence of the -n flag is + # explicitly implementation-defined in POSIX. Use hexdump instead of od, + # because OpenWrt and its derivatives have the former but not the latter. + is_little_endian_result="$( + printf 'I'\ + | hexdump -o\ + | awk '{ print substr($2, 6, 1); exit; }' + )" + readonly is_little_endian_result + + [ "$is_little_endian_result" -eq '1' ] } # Function check_required checks if the required software is available on the