From 0a0a32c35b33a73bf6354e4085d24244abfcd857 Mon Sep 17 00:00:00 2001 From: Martijn Dekker Date: Wed, 20 Mar 2024 18:54:38 +0000 Subject: [PATCH] Add support for Android/Termux As of this commit, ksh supports the Termux environment on Android. There were several challenges to solve to make it build. Termux is based on the typical Linux environment but with important changes, and there is no attempt at POSIX compliance. This commit adds the necessary workarounds to deal with this system's quirks. This commit was tested on Android 14.0 arm64 with Termux 0.118.0 on the Android emulator provided by Android Studio on macOS. To build correctly, you need the clang, binutils, and getconf packages. At runtime, the getconf package is needed for AST getconf(1) fallbacks to function, and ncurses-utils packages (specifically, tput(1)) is needed to enable multiline editing. bin/package: - Since Termux does not have 'getconf PATH' (since it lacks POSIX confstr(3)): move the DEFPATH/PATH code and add two fallbacks for 'getconf PATH' that require a compiler: one that tries confstr(), currently expected to fail on Termux, and another one that tries the non-standard _PATH_DEFPATH macro, which exist on most (all?) Linux and BSD systems including Android. (re: 78b1a845) - Export DEFPATH to the environment so we can eliminate repetitive code to determine it in iffe and mamprobe. - hostinfo(): Use 'android' and not 'linux' as the host type base name on Android/Termux; this will allow us to implement necessary special-casing in make.probe (see below). src/cmd/INIT/iffe.sh, src/cmd/INIT/mamprobe.sh, src/lib/libast/comp/conf.sh, src/lib/libast/comp/conf.tab: - Require DEFPATH in the environment (see above). - Use it instead of the code to determine default system path. src/cmd/INIT/make.probe, **/Mamfile: - On Termux we need API version 26 for the system headers to declare a couple of functions we depend on, including catopen(3). This is done by adding --target=aarch64-linux-android26 to the compiler command line. Add a CC.TARGET variable (which becomes mam_cc_TARGET in the Mamfiles) to contain this flag. src/lib/libast/features/standards: - Add support for Android. After the make.probe change above we can treat it the same as GNU, as the Android headers also recognise _GNU_SOURCE for some things. src/lib/libast/comp/omitted.c: - On Android, bzero(3) is a macro that expands to __bionic_bzero(), so iffe doesn't detect bzero() as a library function. Check for both the iffe result _lib_bzero and the presence of bzero as a macro to avoid a build error. src/lib/libast/comp/strtold.c: - Avoid another build failure: work around Android system header incompatibilities with the AST headers by not including stdlib.h from ast_sys.h in this case. This is done by temporarily defining _STDLIB_H so the Android stdlib.h code gets skipped. src/lib/libast/sfio/sfcvt.c: - Work around another build failure by not relying on the _lib_isnan iffe test result on Android. src/lib/libast/vmalloc/vmhdr.h: - Include here to avoid yet another build failure on Android (undeclared function '_ast_signal'). src/lib/libcmd/fds.c: - Since Android declares ntohs(3) in sys/endian.h and not in the POSIX standard location arpa/inet.h, include the former on Android. src/cmd/ksh93/tests/*.sh: - Since Android does not have /tmp, avoid using it. For testing cd, /dev is a good alternative. - Android don't allow inheriting stdout in closed state. Disable the tests that rely on this on systems that don't allow this. --- NEWS | 8 ++ bin/package | 154 ++++++++++++++++++++++-------- src/cmd/INIT/iffe.sh | 48 ++-------- src/cmd/INIT/make.probe | 24 ++++- src/cmd/INIT/mamprobe.sh | 8 +- src/cmd/builtin/Mamfile | 2 +- src/cmd/ksh93/Mamfile | 2 +- src/cmd/ksh93/include/version.h | 2 +- src/cmd/ksh93/tests/basic.sh | 9 +- src/cmd/ksh93/tests/builtins.sh | 14 +-- src/cmd/ksh93/tests/io.sh | 13 ++- src/cmd/ksh93/tests/leaks.sh | 6 +- src/cmd/ksh93/tests/pty.sh | 4 +- src/cmd/ksh93/tests/variables.sh | 2 +- src/lib/libast/Mamfile | 3 +- src/lib/libast/comp/conf.sh | 9 +- src/lib/libast/comp/conf.tab | 43 +-------- src/lib/libast/comp/omitted.c | 2 +- src/lib/libast/comp/strtold.c | 9 ++ src/lib/libast/features/standards | 6 +- src/lib/libast/sfio/sfcvt.c | 2 +- src/lib/libast/vmalloc/vmhdr.h | 3 +- src/lib/libcmd/Mamfile | 2 +- src/lib/libcmd/fds.c | 4 + src/lib/libdll/Mamfile | 2 +- src/lib/libsum/Mamfile | 2 +- 26 files changed, 216 insertions(+), 167 deletions(-) diff --git a/NEWS b/NEWS index e310546cc133..a941a4ba2dcb 100644 --- a/NEWS +++ b/NEWS @@ -2,6 +2,14 @@ This documents significant changes in the dev branch of ksh 93u+m. For full details, see the git log at: https://github.com/ksh93/ksh Uppercase BUG_* IDs are shell bug IDs as used by the Modernish shell library. +2024-03-20: + +- Android with Termux is now a supported environment for ksh 93u+m. Testing + was done on Android 14.0 with Termux 0.118.0. Please report any breakage. + Build dependencies (Termux packages): clang, binutils, getconf. Runtime + depenencies: ncurses-utils (for --multiline), getconf (fallback for the + /opt/ast/bin/getconf builtin). + 2024-03-12: - Fixed a regression in the 'printf' built-in, introduced on 2023-05-17, diff --git a/bin/package b/bin/package index 98b989bbbae5..3354e72f1b29 100755 --- a/bin/package +++ b/bin/package @@ -82,42 +82,6 @@ case $PWD in exit 1 ;; esac || exit -# Outputs sanitized system $PATH, eliminating duplicate and nonexistent dirs, '..', etc. -sanitize_PATH() ( - set -fu +e - IFS=':' - unset -v CDPATH - sPATH='' - for dir in $1; do - # Sanitize this path, resolving symlinks, - # with special-casing of ksh's virtual built-ins directory - case $dir in - /opt/ast/bin) - test ! -d "$dir" && sdir=$dir ;; - */* | [!+-]* | [+-]*[!0123456789]*) - sdir=$(cd -- $dir 2>/dev/null && pwd -P && echo X) ;; - *) sdir=$(cd ./$dir 2>/dev/null && pwd -P && echo X) ;; - esac || continue - sdir=${sdir%?X} - # Skip duplicates - case :$sPATH: in - *:"$sdir":*) - continue ;; - esac - # Found one, add it - sPATH=${sPATH:+$sPATH:}$sdir - done - printf '%s\n' "${sPATH#:}" -) - -# Ensure a sane $PATH beginning with standard utilities. -# Find preferred 'getconf' on NixOS and Solaris/illumos. -DEFPATH=$( - PATH=/run/current-system/sw/bin:/usr/xpg7/bin:/usr/xpg6/bin:/usr/xpg4/bin:/bin:/usr/bin:$PATH - getconf PATH 2>/dev/null -) || DEFPATH=/bin:/usr/bin:/sbin:/usr/sbin -PATH=$(sanitize_PATH "/opt/ast/bin:$DEFPATH:$PATH") - # shell checks checksh() { @@ -137,7 +101,7 @@ lib="" # need /usr/local/lib /usr/local/shlib ccs="/usr/kvm /usr/ccs/bin" org="gnu GNU" makefiles="Mamfile" # ksh 93u+m no longer uses these: Nmakefile nmakefile Makefile makefile -env="HOSTTYPE PACKAGEROOT INSTALLROOT PATH FPATH MANPATH" +env="HOSTTYPE PACKAGEROOT INSTALLROOT PATH DEFPATH FPATH MANPATH" package_use='=$HOSTTYPE=$PACKAGEROOT=$INSTALLROOT=$EXECROOT=$CC=' @@ -151,7 +115,7 @@ command=${0##*/} case $(getopts '[-][123:xyz]' opt --xyz 2>/dev/null; echo 0$opt) in 0123) USAGE=$' [-? -@(#)$Id: '$command$' (ksh 93u+m) 2024-03-07 $ +@(#)$Id: '$command$' (ksh 93u+m) 2024-03-20 $ ] [-author?Glenn Fowler ] [-author?Contributors to https://github.com/ksh93/ksh] @@ -579,7 +543,7 @@ SEE ALSO pkgadd(1), pkgmk(1), rpm(1), sh(1), tar(1), optget(3) IMPLEMENTATION - version package (ksh 93u+m) 2024-03-07 + version package (ksh 93u+m) 2024-03-20 author Glenn Fowler author Contributors to https://github.com/ksh93/ksh copyright (c) 1994-2012 AT&T Intellectual Property @@ -1069,6 +1033,9 @@ int main(void) *-linux-gnu* | *-linux-musl*) # fix missing machine field, e.g. aarch64-linux-gnu => aarch64-unknown-linux-gnu canon=${canon%%-*}-unknown-${canon#*-} ;; + *-linux-android*) + # Android/Termux is very much its own thing, so identify it as 'android', not 'linux' + canon=${canon%-linux-*}-android ;; esac case -${canon}- in --|*-powerpc-*) @@ -1674,6 +1641,115 @@ checkcc() esac } +# output sanitized system $PATH, eliminating duplicate and nonexistent dirs, '..', etc. + +sanitize_PATH() +( + set -fu +e + IFS=':' + unset -v CDPATH + sPATH='' + for dir in $1; do + # Sanitize this path, resolving symlinks, + # with special-casing of ksh's virtual built-ins directory + case $dir in + /opt/ast/bin) + test ! -d "$dir" && sdir=$dir ;; + */* | [!+-]* | [+-]*[!0123456789]*) + sdir=$(cd -- $dir 2>/dev/null && pwd -P && echo X) ;; + *) sdir=$(cd ./$dir 2>/dev/null && pwd -P && echo X) ;; + esac || continue + sdir=${sdir%?X} + # Skip duplicates + case :$sPATH: in + *:"$sdir":*) + continue ;; + esac + # Found one, add it + sPATH=${sPATH:+$sPATH:}$sdir + done + printf '%s\n' "${sPATH#:}" +) + +# Ensure a sane $PATH beginning with standard utilities. +# POSIXly, a simple 'getconf PATH' should do it, but reality is otherwise. +# Find preferred getconf(1) on NixOS and Solaris/illumos. +# Compile fallback programs on systems without getconf(1). + +DEFPATH=$( + # support Android/Termux, NixOS, Solaris/illumos, generic /bin:/usr/bin + PATH=/data/data/com.termux/files/usr/bin:/run/current-system/sw/bin:/usr/xpg7/bin:/usr/xpg6/bin:/usr/xpg4/bin:/bin:/usr/bin:$PATH + getconf PATH 2>/dev/null || { + # no getconf(1) -- try confstr(3) + checkcc + t=${TMPDIR:-/tmp}/path_test.$$ + trap 'set +f; rm -rf "$t."*' 0 + cat > $t.c <<-EOF + #include + #include + int main(void) + { + char buf[8192]; + size_t len = confstr(_CS_PATH, &buf, sizeof(buf)); + if (len > 0 && len < sizeof(buf)) + { + printf("%s\n", buf); + return 0; + } + return 1; + } + EOF + $CC $CCFLAGS $LDFLAGS -o "$t.exe" "$t.c" 2>/dev/null && "$t.exe" + } || { + # no confstr(3) either -- try non-standard _PATH_DEFPATH + cat > $t.c <<-EOF + #include + #include + int main(void) + { + printf("%s\n", _PATH_DEFPATH); + return 0; + } + EOF + $CC $CCFLAGS $LDFLAGS -o "$t.exe" "$t.c" 2>/dev/null && "$t.exe" + } +) || DEFPATH=/bin:/usr/bin:/sbin:/usr/sbin +# Fix for NixOS. Not all POSIX standard utilities come with the default system, +# e.g. 'bc', 'file', 'vi'. The command that NixOS recommends to get missing +# utilities, e.g. 'nix-env -iA nixos.bc', installs them in a default profile +# directory that is not in $(getconf PATH). So add this path to the standard path. +# See: https://github.com/NixOS/nixpkgs/issues/65512 +if test -e /etc/NIXOS && + nix_profile_dir=/nix/var/nix/profiles/default/bin && + test -d "$nix_profile_dir" +then case ":$DEFPATH:" in + *:"$nix_profile_dir":* ) + # nothing to do + ;; + * ) # insert the default profile directory as the second entry + DEFPATH=$( + set -f + IFS=: + set -- $DEFPATH + one=$1 + shift + echo "$one:$nix_profile_dir${1+:}$*" + ) ;; + esac +fi +# Fix for AIX. At least as of version 7.1, the system default 'find', 'diff -u' and 'patch' utilities +# are broken and/or non-compliant in ways that make them incompatible with POSIX 2018. However, GNU +# utilities are commonly installed in /opt/freeware/bin, and under standard names (no g- prefix). +if test -d /opt/freeware/bin +then case $(uname) in + AIX ) DEFPATH="/opt/freeware/bin:$DEFPATH" ;; + esac +fi + +export DEFPATH # for iffe, etc. + +PATH=$(sanitize_PATH "/opt/ast/bin:$DEFPATH:$PATH") + # some actions have their own PACKAGEROOT or kick out early case $action in diff --git a/src/cmd/INIT/iffe.sh b/src/cmd/INIT/iffe.sh index 8002ebf1c909..0b5a7b6b0100 100644 --- a/src/cmd/INIT/iffe.sh +++ b/src/cmd/INIT/iffe.sh @@ -33,7 +33,14 @@ esac set -o noglob command=iffe -version=2023-04-06 +version=2024-03-20 + +# DEFPATH should be inherited from package(1) +case $DEFPATH in +/*) ;; +*) echo "$command: DEFPATH not set" >&2 + exit 1 ;; +esac compile() # $cc ... { @@ -106,44 +113,7 @@ pkg() # package { case $1 in '') # Determine default system path, store in $pth. - pth=$( - PATH=/run/current-system/sw/bin:/usr/xpg7/bin:/usr/xpg6/bin:/usr/xpg4/bin:/bin:/usr/bin:$PATH - exec getconf PATH 2>/dev/null - ) - case $pth in - '' | [!/]* | *:[!/]* | *: ) - pth="/bin /usr/bin /sbin /usr/sbin" ;; - *:*) pth=$(echo "$pth" | sed 's/:/ /g') ;; - esac - # Fix for NixOS. Not all POSIX standard utilities come with the default system, - # e.g. 'bc', 'file', 'vi'. The command that NixOS recommends to get missing - # utilities, e.g. 'nix-env -iA nixos.bc', installs them in a default profile - # directory that is not in $(getconf PATH). So add this path to the standard path. - # See: https://github.com/NixOS/nixpkgs/issues/65512 - if test -e /etc/NIXOS && - nix_profile_dir=/nix/var/nix/profiles/default/bin && - test -d "$nix_profile_dir" - then case " $pth " in - *" $nix_profile_dir "* ) - # nothing to do - ;; - * ) # insert the default profile directory as the second entry - pth=$( - set $pth - one=$1 - shift - echo "$one $nix_profile_dir${1+ }$@" - ) ;; - esac - fi - # Fix for AIX. At least as of version 7.1, the system default 'find', 'diff -u' and 'patch' utilities - # are broken and/or non-compliant in ways that make them incompatible with POSIX 2018. However, GNU - # utilities are commonly installed in /opt/freeware/bin, and under standard names (no g- prefix). - if test -d /opt/freeware/bin - then case $(uname) in - AIX ) pth="/opt/freeware/bin $pth" ;; - esac - fi + pth=$(echo "$DEFPATH" | sed 's/:/ /g') return ;; '<') shift diff --git a/src/cmd/INIT/make.probe b/src/cmd/INIT/make.probe index 2f4f6b82af24..243f21cc7aa6 100644 --- a/src/cmd/INIT/make.probe +++ b/src/cmd/INIT/make.probe @@ -3,7 +3,7 @@ # Glenn Fowler # AT&T Research # -# @(#)make.probe (ksh 93u+m) 2024-01-09 +# @(#)make.probe (ksh 93u+m) 2024-03-20 # # C probe for make # @@ -1903,6 +1903,27 @@ case $keepstdlib in ;; esac +# +# determine the need for any API version targets to add to the compiler flags +# + +CC_TARGET= +case $hosttype in +android.*) + # get current default API level + cat >_android_test.c <<-EOF + #include + int main(void) { printf("%d\n", __ANDROID_API__); return 0; } + EOF + $cc -o _android_test _android_test.c + got=$(./_android_test) + (set +f; exec rm -rf _android_test*) & + # check for minimum required; we need API 26 for catopen(3) et al + if test -n "$got" && test "$got" -lt 26 + then CC_TARGET="--target=$(uname -m)-linux-android26" + fi ;; +esac + # # set up for local override # @@ -2100,6 +2121,7 @@ echo CC.SUFFIX.OBJECT = $CC_SUFFIX_OBJECT echo CC.SUFFIX.SHARED = $CC_SUFFIX_SHARED echo CC.SUFFIX.SOURCE = $CC_SUFFIX_SOURCE echo CC.SUFFIX.STATIC = $CC_SUFFIX_STATIC +echo CC.TARGET = $CC_TARGET echo CC.VERSION = $CC_VERSION case $CC_VERSION_STRING in *\"*) i=`echo " $CC_VERSION_STRING" | sed -e 's,",\\\\",g' -e 's,^ ,,' -e 's,.*,"&",'` ;; diff --git a/src/cmd/INIT/mamprobe.sh b/src/cmd/INIT/mamprobe.sh index 6a4bd3bc9d5e..79be7fad249d 100644 --- a/src/cmd/INIT/mamprobe.sh +++ b/src/cmd/INIT/mamprobe.sh @@ -22,13 +22,7 @@ command=mamprobe -bins=` - ( - userPATH=$PATH - PATH=/run/current-system/sw/bin:/usr/xpg7/bin:/usr/xpg6/bin:/usr/xpg4/bin:/bin:/usr/bin:$PATH - getconf PATH 2>/dev/null && echo "$userPATH" || echo /bin:/usr/bin:/sbin:/usr/sbin:"$userPATH" - ) | sed 's/:/ /g' -` || exit +bins=$(echo "$DEFPATH:$PATH" | sed 's/:/ /g') || exit # check the options diff --git a/src/cmd/builtin/Mamfile b/src/cmd/builtin/Mamfile index fc18b4478e6d..e8f843d55df3 100644 --- a/src/cmd/builtin/Mamfile +++ b/src/cmd/builtin/Mamfile @@ -8,7 +8,7 @@ setv MAMAKE_STRICT 3 setv INSTALLROOT ../../.. setv PACKAGE_ast_INCLUDE ${INSTALLROOT}/include/ast setv CC cc -setv mam_cc_FLAGS ${-debug-symbols?1?${mam_cc_DEBUG} -D_BLD_DEBUG?${mam_cc_OPTIMIZE}?} +setv mam_cc_FLAGS ${mam_cc_TARGET} ${-debug-symbols?1?${mam_cc_DEBUG} -D_BLD_DEBUG?${mam_cc_OPTIMIZE}?} setv CCFLAGS setv CCLDFLAGS ${-strip-symbols?1?${mam_cc_LD_STRIP}??} setv IFFEFLAGS diff --git a/src/cmd/ksh93/Mamfile b/src/cmd/ksh93/Mamfile index 58875a437a75..61dcef19f896 100644 --- a/src/cmd/ksh93/Mamfile +++ b/src/cmd/ksh93/Mamfile @@ -8,7 +8,7 @@ setv INSTALLROOT ../../.. setv PACKAGE_ast_INCLUDE ${INSTALLROOT}/include/ast setv CC cc setv AR ${mam_cc_AR} ${mam_cc_AR_ARFLAGS} -setv mam_cc_FLAGS ${-debug-symbols?1?${mam_cc_DEBUG} -D_BLD_DEBUG?${mam_cc_OPTIMIZE}?} +setv mam_cc_FLAGS ${mam_cc_TARGET} ${-debug-symbols?1?${mam_cc_DEBUG} -D_BLD_DEBUG?${mam_cc_OPTIMIZE}?} setv CCFLAGS setv CCLDFLAGS ${-strip-symbols?1?${mam_cc_LD_STRIP}??} setv IFFEFLAGS diff --git a/src/cmd/ksh93/include/version.h b/src/cmd/ksh93/include/version.h index 1f1c3d88f419..81af66d4b9e8 100644 --- a/src/cmd/ksh93/include/version.h +++ b/src/cmd/ksh93/include/version.h @@ -18,7 +18,7 @@ #define SH_RELEASE_FORK "93u+m" /* only change if you develop a new ksh93 fork */ #define SH_RELEASE_SVER "1.1.0-alpha" /* semantic version number: https://semver.org */ -#define SH_RELEASE_DATE "2024-03-12" /* must be in this format for $((.sh.version)) */ +#define SH_RELEASE_DATE "2024-03-20" /* must be in this format for $((.sh.version)) */ #define SH_RELEASE_CPYR "(c) 2020-2024 Contributors to ksh " SH_RELEASE_FORK /* Scripts sometimes field-split ${.sh.version}, so don't change amount of whitespace. */ diff --git a/src/cmd/ksh93/tests/basic.sh b/src/cmd/ksh93/tests/basic.sh index 130a8a8663c3..5c16bdcda92b 100755 --- a/src/cmd/ksh93/tests/basic.sh +++ b/src/cmd/ksh93/tests/basic.sh @@ -133,10 +133,10 @@ eval $bar if [[ $foo != 'foo bar' ]] then err_exit 'eval foo=\$bar, with bar="foo\ bar" not working' fi -cd /tmp -cd ../../tmp || err_exit "cd ../../tmp failed" -if [[ $PWD != /tmp ]] -then err_exit 'cd ../../tmp is not /tmp' +cd /dev +cd ../../dev || err_exit "cd ../../dev failed" +if [[ $PWD != /dev ]] +then err_exit 'cd ../../dev is not /dev' fi ( sleep .2; cat <script <<-EOF + trap + $sig # unignore trap ":" $sig echo \$\$ sh -c 'echo \$\$' diff --git a/src/cmd/ksh93/tests/builtins.sh b/src/cmd/ksh93/tests/builtins.sh index d1434718c7f8..0ac6bb8b7931 100755 --- a/src/cmd/ksh93/tests/builtins.sh +++ b/src/cmd/ksh93/tests/builtins.sh @@ -1312,10 +1312,10 @@ got=$(OLDPWD=$tmp/oldpwd cd -) function fn { - typeset OLDPWD=/tmp + typeset OLDPWD=/dev cd - } -exp='/tmp' +exp='/dev' got=$(OLDPWD=/bin fn) [[ $got == "$exp" ]] || err_exit "cd - doesn't recognize overridden OLDPWD variable if it is overridden in function scope" \ @@ -1324,10 +1324,10 @@ got=$(OLDPWD=/bin fn) function fn { typeset PWD=bug - cd /tmp + cd /dev echo "$PWD" } -exp='/tmp' +exp='/dev' got=$(fn) [[ $got == "$exp" ]] || err_exit "PWD isn't set after cd if already set in function scope" \ @@ -1336,7 +1336,7 @@ got=$(fn) # $PWD should be set correctly after cd exp="$PWD $PWD" -got=$(echo $PWD; PWD=/tmp cd /dev; echo $PWD) +got=$(echo $PWD; PWD=/bin cd /dev; echo $PWD) [[ $got == "$exp" ]] || err_exit "PWD is incorrect after cd" \ "(expected $(printf %q "$exp"), got $(printf %q "$got"))" @@ -1633,12 +1633,12 @@ HOME=/dev cd function fn { - typeset HOME=/tmp + typeset HOME=/dev cd } fn unset -f fn -[[ $PWD == /tmp ]] || err_exit "'cd' does not chdir to \$HOME (local assignment)" +[[ $PWD == /dev ]] || err_exit "'cd' does not chdir to \$HOME (local assignment)" # ====== # Double evaluation of arithmetic expression passed to float conversion operators in printf diff --git a/src/cmd/ksh93/tests/io.sh b/src/cmd/ksh93/tests/io.sh index f473be54aba3..0a9ca0df066c 100755 --- a/src/cmd/ksh93/tests/io.sh +++ b/src/cmd/ksh93/tests/io.sh @@ -2,7 +2,7 @@ # # # This software is part of the ast package # # Copyright (c) 1982-2012 AT&T Intellectual Property # -# Copyright (c) 2020-2023 Contributors to ksh 93u+m # +# Copyright (c) 2020-2024 Contributors to ksh 93u+m # # and is licensed under the # # Eclipse Public License, Version 2.0 # # # @@ -18,6 +18,11 @@ . "${SHTESTS_COMMON:-${0%/*}/_common}" +# Some systems (at least Android) don't allow inheriting stdout in closed state. +# Disable the tests that rely on this on these systems. +sh -c 'exec 3>&1' 1>&- 2>/dev/null +typeset -si can_close_stdout=$? + unset HISTFILE function fun @@ -276,7 +281,9 @@ fi # !SHOPT_SCRIPTONL~Y $SHELL -c "{ > $tmp/1 ; date;} >&- 2> /dev/null" > $tmp/2 [[ -s $tmp/1 || -s $tmp/2 ]] && err_exit 'commands with standard output closed produce output' -$SHELL -c "$SHELL -c ': 3>&1' 1>&- 2>/dev/null" && err_exit 'closed standard output not passed to subshell' +if ((can_close_stdout)); then +$SHELL -c "$SHELL -c ': 3>&1' 1>&- 2>/dev/null" && err_exit 'closed standard output not passed to child shell' +fi # can_close_stdout [[ $(cat <<- \EOF | $SHELL do_it_all() { @@ -906,6 +913,7 @@ got=$(< dir1/dir2/x) # ====== # ksh misbehaved when stdout is closed # https://github.com/ksh93/ksh/issues/314 +if ((can_close_stdout)); then "$SHELL" -c 'pwd; echo "$?" >&2; echo test; echo "$?" > file' >&- 2>stderr exp='1' [[ $(/dev/full && err_exit "'$cmd' does not detect disk full (inherited FD)" done fi +fi # can_close_stdout # ====== # Command substitution hangs, writing infinite zero bytes, when redirecting standard output on a built-in that forks diff --git a/src/cmd/ksh93/tests/leaks.sh b/src/cmd/ksh93/tests/leaks.sh index c3ede91ce547..fa50ed8770a0 100755 --- a/src/cmd/ksh93/tests/leaks.sh +++ b/src/cmd/ksh93/tests/leaks.sh @@ -2,7 +2,7 @@ # # # This software is part of the ast package # # Copyright (c) 1982-2012 AT&T Intellectual Property # -# Copyright (c) 2020-2022 Contributors to ksh 93u+m # +# Copyright (c) 2020-2024 Contributors to ksh 93u+m # # and is licensed under the # # Eclipse Public License, Version 2.0 # # # @@ -395,12 +395,12 @@ DONE # Test for a memory leak after 'cd' (in relation to $PWD and $OLDPWD) TEST title='PWD and/or OLDPWD changed by cd' DO - cd /tmp + cd /tmp 2>/dev/null || cd / cd - > /dev/null PWD=/foo OLDPWD=/bar cd /bin - cd /usr + cd /usr 2>/dev/null || cd / cd /dev cd /dev cd - > /dev/null diff --git a/src/cmd/ksh93/tests/pty.sh b/src/cmd/ksh93/tests/pty.sh index cc06192067ac..499a5600fea4 100755 --- a/src/cmd/ksh93/tests/pty.sh +++ b/src/cmd/ksh93/tests/pty.sh @@ -466,8 +466,8 @@ u yes-yes disabled # Test file name completion in vi mode -if((SHOPT_VSH)); then -mkdir "/tmp/fakehome_$$" && tst $LINENO </dev/null; then +tst $LINENO < + +#if __ANDROID_API__ +#define _STDLIB_H 1 +#endif + #include +#if __ANDROID_API__ +#undef _STDLIB_H +#endif + #if _ast_fltmax_double #undef strtold #endif diff --git a/src/lib/libast/features/standards b/src/lib/libast/features/standards index 071f85a340c1..44f2a30d1a7c 100644 --- a/src/lib/libast/features/standards +++ b/src/lib/libast/features/standards @@ -148,7 +148,7 @@ elif tst note{ SunOS (Solaris, illumos) }end compile{ #define NULL 0 #endif /* __SUNPRO_C */ } -elif tst note{ GNU (glibc) or Cygwin }end compile{ +elif tst note{ GNU (glibc), Cygwin, or Android }end compile{ /* * On GNU (GNU's Not UNIX) and Cygwin, _GNU_SOURCE is the "everything and the kitchen sink" macro * (see feature_test_macros(7)), but on GNU we also need to define _FILE_OFFSET_BITS to get large @@ -163,8 +163,8 @@ elif tst note{ GNU (glibc) or Cygwin }end compile{ #include #include #include - #if !__GLIBC__ && !__CYGWIN__ - #error not GNU or Cygwin + #if !__GLIBC__ && !__CYGWIN__ && !__ANDROID_API__ + #error not GNU, Cygwin, or Android #endif int _do_these_compile_ = _POSIX_PATH_MAX & _SC_PAGESIZE; #if _typ_u_long diff --git a/src/lib/libast/sfio/sfcvt.c b/src/lib/libast/sfio/sfcvt.c index 70bbf75b1a47..4c8dd946a955 100644 --- a/src/lib/libast/sfio/sfcvt.c +++ b/src/lib/libast/sfio/sfcvt.c @@ -34,7 +34,7 @@ static char *Zero = "0"; #define SFIO_ZERO ((_Sfi = 1), strlcpy(buf, Zero, size), buf) #define SFIO_INTPART (SFIO_IDIGITS/2) -#if !_lib_isnan +#if !_lib_isnan || __ANDROID_API__ #undef isnan #undef isnanl #if _lib_fpclassify diff --git a/src/lib/libast/vmalloc/vmhdr.h b/src/lib/libast/vmalloc/vmhdr.h index 71786b1bd44a..3c096293e8d7 100644 --- a/src/lib/libast/vmalloc/vmhdr.h +++ b/src/lib/libast/vmalloc/vmhdr.h @@ -2,7 +2,7 @@ * * * This software is part of the ast package * * Copyright (c) 1985-2012 AT&T Intellectual Property * -* Copyright (c) 2020-2023 Contributors to ksh 93u+m * +* Copyright (c) 2020-2024 Contributors to ksh 93u+m * * and is licensed under the * * Eclipse Public License, Version 2.0 * * * @@ -35,6 +35,7 @@ #define _npt_sbrk 1 #include +#include #if _npt_getpagesize #undef getpagesize diff --git a/src/lib/libcmd/Mamfile b/src/lib/libcmd/Mamfile index 05db9042e46e..b2ebae88598f 100644 --- a/src/lib/libcmd/Mamfile +++ b/src/lib/libcmd/Mamfile @@ -8,7 +8,7 @@ setv INSTALLROOT ../../.. setv PACKAGE_ast_INCLUDE ${INSTALLROOT}/include/ast setv CC cc setv AR ${mam_cc_AR} ${mam_cc_AR_ARFLAGS} -setv mam_cc_FLAGS ${mam_cc_DLL} ${-debug-symbols?1?${mam_cc_DEBUG} -D_BLD_DEBUG?${mam_cc_OPTIMIZE}?} -DERROR_CATALOG=\""libcmd"\" -DHOSTTYPE=\""${mam_cc_HOSTTYPE}"\" -D_BLD_cmd +setv mam_cc_FLAGS ${mam_cc_TARGET} ${mam_cc_DLL} ${-debug-symbols?1?${mam_cc_DEBUG} -D_BLD_DEBUG?${mam_cc_OPTIMIZE}?} -DERROR_CATALOG=\""libcmd"\" -DHOSTTYPE=\""${mam_cc_HOSTTYPE}"\" -D_BLD_cmd setv CCFLAGS setv IFFEFLAGS setv LDFLAGS diff --git a/src/lib/libcmd/fds.c b/src/lib/libcmd/fds.c index c5346af3d503..b2bcb4b985e9 100644 --- a/src/lib/libcmd/fds.c +++ b/src/lib/libcmd/fds.c @@ -39,6 +39,10 @@ static const char usage[] = #include #include #include +#if __ANDROID_API__ +/* Android declares ntohs(3) here, not in arpa/inet.h as POSIX mandates */ +#include +#endif /* __ANDROID_API__ */ #else #undef S_IFSOCK #endif diff --git a/src/lib/libdll/Mamfile b/src/lib/libdll/Mamfile index e636748baa2f..063cc982cbcb 100644 --- a/src/lib/libdll/Mamfile +++ b/src/lib/libdll/Mamfile @@ -8,7 +8,7 @@ setv INSTALLROOT ../../.. setv PACKAGE_ast_INCLUDE ${INSTALLROOT}/include/ast setv CC cc setv AR ${mam_cc_AR} ${mam_cc_AR_ARFLAGS} -setv mam_cc_FLAGS ${mam_cc_DLL} ${-debug-symbols?1?${mam_cc_DEBUG} -D_BLD_DEBUG?${mam_cc_OPTIMIZE}?} +setv mam_cc_FLAGS ${mam_cc_TARGET} ${mam_cc_DLL} ${-debug-symbols?1?${mam_cc_DEBUG} -D_BLD_DEBUG?${mam_cc_OPTIMIZE}?} setv CCFLAGS setv IFFEFLAGS setv LDFLAGS diff --git a/src/lib/libsum/Mamfile b/src/lib/libsum/Mamfile index 7ad675087aad..c8c6d689e819 100644 --- a/src/lib/libsum/Mamfile +++ b/src/lib/libsum/Mamfile @@ -8,7 +8,7 @@ setv INSTALLROOT ../../.. setv PACKAGE_ast_INCLUDE ${INSTALLROOT}/include/ast setv CC cc setv AR ${mam_cc_AR} ${mam_cc_AR_ARFLAGS} -setv mam_cc_FLAGS ${mam_cc_PIC} ${-debug-symbols?1?${mam_cc_DEBUG} -D_BLD_DEBUG?${mam_cc_OPTIMIZE}?} +setv mam_cc_FLAGS ${mam_cc_TARGET} ${mam_cc_PIC} ${-debug-symbols?1?${mam_cc_DEBUG} -D_BLD_DEBUG?${mam_cc_OPTIMIZE}?} setv CCFLAGS setv IFFEFLAGS setv LDFLAGS