@@ -119,8 +119,14 @@ fn filter_flags(args: impl Iterator<Item = OsString>) -> (impl Iterator<Item = O
119119 ( args, options)
120120}
121121
122+ static mut IS_POSIXLY_CORRECT : bool = false ;
123+
122124#[ uucore:: main]
123125pub fn uumain ( args : impl uucore:: Args ) -> UResult < ( ) > {
126+ unsafe {
127+ IS_POSIXLY_CORRECT = env:: var_os ( "POSIXLY_CORRECT" ) . is_some ( ) ;
128+ }
129+
124130 // args[0] is the name of the binary.
125131 let mut args = args. skip ( 1 ) . peekable ( ) ;
126132
@@ -135,47 +141,47 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
135141 // > escapes are always enabled. To echo the string ‘-n’, one of the
136142 // > characters can be escaped in either octal or hexadecimal
137143 // > representation. For example, echo -e '\x2dn'.
138- let is_posixly_correct = env:: var_os ( "POSIXLY_CORRECT" ) . is_some ( ) ;
139144
140- let ( args, options) : ( Box < dyn Iterator < Item = OsString > > , Options ) = if is_posixly_correct {
141- if args. peek ( ) . is_some_and ( |arg| arg == "-n" ) {
142- // if POSIXLY_CORRECT is set and the first argument is the "-n" flag
143- // we filter flags normally but 'escaped' is activated nonetheless.
144- let ( args, _) = filter_flags ( args) ;
145- (
146- Box :: new ( args) ,
147- Options {
148- trailing_newline : false ,
149- ..Options :: posixly_correct_default ( )
150- } ,
151- )
152- } else {
153- // if POSIXLY_CORRECT is set and the first argument is not the "-n" flag
154- // we just collect all arguments as no arguments are interpreted as flags.
155- ( Box :: new ( args) , Options :: posixly_correct_default ( ) )
156- }
157- } else if let Some ( first_arg) = args. next ( ) {
158- if first_arg == "--help" && args. peek ( ) . is_none ( ) {
159- // If POSIXLY_CORRECT is not set and the first argument
160- // is `--help`, GNU coreutils prints the help message.
161- //
162- // Verify this using:
163- //
164- // POSIXLY_CORRECT=1 echo --help
165- // echo --help
166- uu_app ( ) . print_help ( ) ?;
167- return Ok ( ( ) ) ;
168- } else if first_arg == "--version" && args. peek ( ) . is_none ( ) {
169- writeln ! ( stdout( ) , "echo {}" , crate_version!( ) ) ?;
170- return Ok ( ( ) ) ;
171- }
145+ let ( args, options) : ( Box < dyn Iterator < Item = OsString > > , Options ) =
146+ if unsafe { IS_POSIXLY_CORRECT } {
147+ if args. peek ( ) . is_some_and ( |arg| arg == "-n" ) {
148+ // if POSIXLY_CORRECT is set and the first argument is the "-n" flag
149+ // we filter flags normally but 'escaped' is activated nonetheless.
150+ let ( args, _) = filter_flags ( args) ;
151+ (
152+ Box :: new ( args) ,
153+ Options {
154+ trailing_newline : false ,
155+ ..Options :: posixly_correct_default ( )
156+ } ,
157+ )
158+ } else {
159+ // if POSIXLY_CORRECT is set and the first argument is not the "-n" flag
160+ // we just collect all arguments as no arguments are interpreted as flags.
161+ ( Box :: new ( args) , Options :: posixly_correct_default ( ) )
162+ }
163+ } else if let Some ( first_arg) = args. next ( ) {
164+ if first_arg == "--help" && args. peek ( ) . is_none ( ) {
165+ // If POSIXLY_CORRECT is not set and the first argument
166+ // is `--help`, GNU coreutils prints the help message.
167+ //
168+ // Verify this using:
169+ //
170+ // POSIXLY_CORRECT=1 echo --help
171+ // echo --help
172+ uu_app ( ) . print_help ( ) ?;
173+ return Ok ( ( ) ) ;
174+ } else if first_arg == "--version" && args. peek ( ) . is_none ( ) {
175+ writeln ! ( stdout( ) , "echo {}" , crate_version!( ) ) ?;
176+ return Ok ( ( ) ) ;
177+ }
172178
173- // if POSIXLY_CORRECT is not set we filter the flags normally
174- let ( args, options) = filter_flags ( std:: iter:: once ( first_arg) . chain ( args) ) ;
175- ( Box :: new ( args) , options)
176- } else {
177- ( Box :: new ( args) , Options :: default ( ) )
178- } ;
179+ // if POSIXLY_CORRECT is not set we filter the flags normally
180+ let ( args, options) = filter_flags ( std:: iter:: once ( first_arg) . chain ( args) ) ;
181+ ( Box :: new ( args) , options)
182+ } else {
183+ ( Box :: new ( args) , Options :: default ( ) )
184+ } ;
179185
180186 execute ( & mut stdout ( ) . lock ( ) , args, options) ?;
181187
0 commit comments