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
7+
68use clap:: Command ;
79use coreutils:: validation;
810use itertools:: Itertools as _;
@@ -45,8 +47,19 @@ fn main() {
4547 let utils = util_map ( ) ;
4648 let mut args = uucore:: args_os ( ) ;
4749
50+ #[ cfg( target_os = "linux" ) ]
51+ // protect against env -a
52+ let binary = {
53+ use std:: ffi:: { CStr , OsString } ;
54+ use std:: os:: unix:: ffi:: OsStringExt ;
55+ let _ = args. next ( ) ;
56+ let n = unsafe { CStr :: from_ptr ( libc:: getauxval ( libc:: AT_EXECFN ) as _ ) } ;
57+ OsString :: from_vec ( n. to_bytes ( ) . to_vec ( ) )
58+ } ;
59+ #[ cfg( not( target_os = "linux" ) ) ]
4860 let binary = validation:: binary_path ( & mut args) ;
49- let binary_as_util = validation:: name ( & binary) . unwrap_or_else ( || {
61+
62+ let binary_as_util = validation:: name ( binary. as_ref ( ) ) . unwrap_or_else ( || {
5063 usage ( & utils, "<unknown binary name>" ) ;
5164 process:: exit ( 0 ) ;
5265 } ) ;
@@ -55,8 +68,12 @@ fn main() {
5568 let is_coreutils = binary_as_util. ends_with ( "utils" ) ;
5669 let matched_util = utils
5770 . keys ( )
71+ //*utils is not ls
5872 . 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
73+ //Prefer stty more than tty
74+ . max_by_key ( |u| u. len ( ) )
75+ // todo: with coreutils -> ls -> blah symlink chain, blah calls ls
76+ ;
6077
6178 let util_name = if let Some ( & util) = matched_util {
6279 Some ( OsString :: from ( util) )
0 commit comments