@@ -5,6 +5,8 @@ use std::{borrow::Cow, mem};
55use crate :: common:: { SudoPath , SudoString } ;
66
77pub mod help;
8+ #[ cfg_attr( not( feature = "sudoedit" ) , allow( unused) ) ]
9+ pub mod help_edit;
810
911#[ cfg( test) ]
1012mod tests;
@@ -21,6 +23,13 @@ pub enum SudoAction {
2123 Version ( SudoVersionOptions ) ,
2224}
2325
26+ pub ( super ) fn is_sudoedit ( command_path : Option < String > ) -> bool {
27+ use std:: os:: unix:: ffi:: OsStrExt ;
28+ std:: path:: Path :: new ( & command_path. unwrap_or_default ( ) )
29+ . file_name ( )
30+ . is_some_and ( |name| name. as_bytes ( ) . starts_with ( b"sudoedit" ) )
31+ }
32+
2433impl SudoAction {
2534 /// try to parse and environment variable assignment
2635 /// parse command line arguments from the environment and handle errors
@@ -107,8 +116,6 @@ impl TryFrom<SudoOptions> for SudoHelpOptions {
107116 let help = mem:: take ( & mut opts. help ) ;
108117 debug_assert ! ( help) ;
109118
110- reject_all ( "--help" , opts) ?;
111-
112119 Ok ( Self { } )
113120 }
114121}
@@ -124,8 +131,6 @@ impl TryFrom<SudoOptions> for SudoVersionOptions {
124131 let version = mem:: take ( & mut opts. version ) ;
125132 debug_assert ! ( version) ;
126133
127- reject_all ( "--version" , opts) ?;
128-
129134 Ok ( Self { } )
130135 }
131136}
@@ -518,12 +523,11 @@ impl SudoArg {
518523 ] ;
519524
520525 /// argument assignments and shorthand options preprocessing
521- fn normalize_arguments < I > ( iter : I ) -> Result < Vec < Self > , String >
526+ /// the iterator should only iterate over the actual arguments
527+ fn normalize_arguments < I > ( mut arg_iter : I ) -> Result < Vec < Self > , String >
522528 where
523- I : IntoIterator < Item = String > ,
529+ I : Iterator < Item = String > ,
524530 {
525- // the first argument is the sudo command - so we can skip it
526- let mut arg_iter = iter. into_iter ( ) . skip ( 1 ) ;
527531 let mut processed = vec ! [ ] ;
528532
529533 while let Some ( arg) = arg_iter. next ( ) {
@@ -628,8 +632,16 @@ impl SudoOptions {
628632 I : IntoIterator < Item = T > ,
629633 T : Into < String > + Clone ,
630634 {
631- let mut options = Self :: default ( ) ;
632- let arg_iter = SudoArg :: normalize_arguments ( iter. into_iter ( ) . map ( Into :: into) ) ?
635+ let mut arg_iter = iter. into_iter ( ) . map ( Into :: into) ;
636+
637+ let invoked_as_sudoedit = is_sudoedit ( arg_iter. next ( ) ) ;
638+
639+ let mut options = Self {
640+ edit : invoked_as_sudoedit,
641+ ..Self :: default ( )
642+ } ;
643+
644+ let arg_iter = SudoArg :: normalize_arguments ( arg_iter) ?
633645 . into_iter ( )
634646 . peekable ( ) ;
635647
@@ -644,7 +656,7 @@ impl SudoOptions {
644656 "warning: preserving the entire environment is not supported, `{flag}` is ignored"
645657 )
646658 }
647- "-e" | "--edit" => {
659+ "-e" | "--edit" if !invoked_as_sudoedit => {
648660 options. edit = true ;
649661 }
650662 "-H" | "--set-home" => {
0 commit comments