@@ -25,6 +25,8 @@ use std::{
2525 path:: PathBuf ,
2626} ;
2727
28+ const DEFAULT_TMP_DIR : & str = "../target/tmp" ;
29+
2830static ERE : Mutex < bool > = Mutex :: new ( false ) ;
2931
3032#[ derive( Parser , Debug , Clone ) ]
@@ -1346,7 +1348,7 @@ fn print_multiline_binary(line: &str) {
13461348 }
13471349 }
13481350 }
1349- } else if let Some ( line) = line. strip_suffix ( & [ '\n' ] ) {
1351+ } else if let Some ( line) = line. strip_suffix ( [ '\n' ] ) {
13501352 println ! ( "{}$" , line) ;
13511353 } else {
13521354 print ! ( "{}$" , line) ;
@@ -1416,6 +1418,16 @@ fn filter_comments(raw_script: impl AsRef<str>) -> String {
14161418 raw_script_without_comments
14171419}
14181420
1421+ /// Get path to [`wfile`] in tmp dir
1422+ fn get_tmp_path ( wfile : PathBuf ) -> PathBuf {
1423+ let mut tmp_path =
1424+ PathBuf :: from ( std:: env:: var ( "CARGO_TARGET_TMPDIR" ) . unwrap_or ( DEFAULT_TMP_DIR . to_string ( ) ) ) ;
1425+
1426+ tmp_path. extend ( & wfile) ;
1427+
1428+ tmp_path
1429+ }
1430+
14191431/// Contains [`Command`] sequence of all [`Sed`] session
14201432/// that applied all to every line of input files
14211433#[ derive( Debug ) ]
@@ -1821,6 +1833,15 @@ fn execute_replace(
18211833 Some ( wfile)
18221834 } ) {
18231835 if replace && wfile. components ( ) . next ( ) . is_some ( ) {
1836+ let mut wfile = wfile. clone ( ) ;
1837+ if !( wfile. is_absolute ( )
1838+ || wfile. starts_with ( "./" )
1839+ || wfile. starts_with ( "../" )
1840+ || wfile. exists ( ) )
1841+ {
1842+ wfile = get_tmp_path ( wfile) ;
1843+ }
1844+
18241845 if let Ok ( mut file) = std:: fs:: OpenOptions :: new ( )
18251846 . append ( true )
18261847 . create ( true )
@@ -2213,6 +2234,15 @@ impl Sed {
22132234 }
22142235
22152236 fn execute_w ( & mut self , wfile : PathBuf ) -> Result < ( ) , SedError > {
2237+ let mut wfile = wfile. clone ( ) ;
2238+ if !( wfile. is_absolute ( )
2239+ || wfile. starts_with ( "./" )
2240+ || wfile. starts_with ( "../" )
2241+ || wfile. exists ( ) )
2242+ {
2243+ wfile = get_tmp_path ( wfile) ;
2244+ }
2245+
22162246 let _ = match std:: fs:: OpenOptions :: new ( )
22172247 . append ( true )
22182248 . create ( true )
0 commit comments