@@ -25,6 +25,8 @@ use std::{
25
25
path:: PathBuf ,
26
26
} ;
27
27
28
+ const DEFAULT_TMP_DIR : & str = "../target/tmp" ;
29
+
28
30
static ERE : Mutex < bool > = Mutex :: new ( false ) ;
29
31
30
32
#[ derive( Parser , Debug , Clone ) ]
@@ -1346,7 +1348,7 @@ fn print_multiline_binary(line: &str) {
1346
1348
}
1347
1349
}
1348
1350
}
1349
- } else if let Some ( line) = line. strip_suffix ( & [ '\n' ] ) {
1351
+ } else if let Some ( line) = line. strip_suffix ( [ '\n' ] ) {
1350
1352
println ! ( "{}$" , line) ;
1351
1353
} else {
1352
1354
print ! ( "{}$" , line) ;
@@ -1416,6 +1418,16 @@ fn filter_comments(raw_script: impl AsRef<str>) -> String {
1416
1418
raw_script_without_comments
1417
1419
}
1418
1420
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
+
1419
1431
/// Contains [`Command`] sequence of all [`Sed`] session
1420
1432
/// that applied all to every line of input files
1421
1433
#[ derive( Debug ) ]
@@ -1821,6 +1833,15 @@ fn execute_replace(
1821
1833
Some ( wfile)
1822
1834
} ) {
1823
1835
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
+
1824
1845
if let Ok ( mut file) = std:: fs:: OpenOptions :: new ( )
1825
1846
. append ( true )
1826
1847
. create ( true )
@@ -2213,6 +2234,15 @@ impl Sed {
2213
2234
}
2214
2235
2215
2236
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
+
2216
2246
let _ = match std:: fs:: OpenOptions :: new ( )
2217
2247
. append ( true )
2218
2248
. create ( true )
0 commit comments