33// For the full copyright and license information, please view the LICENSE
44// file that was distributed with this source code.
55
6+ // spell-checker:ignore getauxval EXECFN
7+
68use clap:: Command ;
79use coreutils:: validation;
810use itertools:: Itertools as _;
@@ -45,8 +47,24 @@ fn main() {
4547 let utils = util_map ( ) ;
4648 let mut args = uucore:: args_os ( ) ;
4749
50+ #[ cfg( any( target_os = "linux" , target_os = "android" ) ) ]
51+ // protect against env -a
52+ let binary = {
53+ use std:: ffi:: { CStr , OsString } ;
54+ use std:: os:: unix:: ffi:: OsStringExt ;
55+ let p: * const libc:: c_char = unsafe { libc:: getauxval ( libc:: AT_EXECFN ) as _ } ;
56+ if p. is_null ( ) {
57+ let _ = writeln ! ( io:: stderr( ) , "getauxval failed" ) ;
58+ process:: exit ( 1 ) ;
59+ }
60+ let _ = args. next ( ) ;
61+ let n = unsafe { CStr :: from_ptr ( p) } ;
62+ OsString :: from_vec ( n. to_bytes ( ) . to_vec ( ) )
63+ } ;
64+ #[ cfg( not( any( target_os = "linux" , target_os = "android" ) ) ) ]
4865 let binary = validation:: binary_path ( & mut args) ;
49- let binary_as_util = validation:: name ( & binary) . unwrap_or_else ( || {
66+
67+ let binary_as_util = validation:: name ( binary. as_ref ( ) ) . unwrap_or_else ( || {
5068 usage ( & utils, "<unknown binary name>" ) ;
5169 process:: exit ( 0 ) ;
5270 } ) ;
@@ -55,8 +73,12 @@ fn main() {
5573 let is_coreutils = binary_as_util. ends_with ( "utils" ) ;
5674 let matched_util = utils
5775 . keys ( )
76+ //*utils is not ls
5877 . filter ( |& & u| binary_as_util. ends_with ( u) && !is_coreutils)
59- . max_by_key ( |u| u. len ( ) ) ; //Prefer stty more than tty. *utils is not ls
78+ //Prefer stty more than tty
79+ . max_by_key ( |u| u. len ( ) )
80+ // todo: with coreutils -> ls -> blah symlink chain, blah calls ls
81+ ;
6082
6183 let util_name = if let Some ( & util) = matched_util {
6284 Some ( OsString :: from ( util) )
0 commit comments