@@ -13,10 +13,12 @@ use operation::{
13
13
} ;
14
14
use std:: collections:: HashMap ;
15
15
use std:: ffi:: OsString ;
16
- use std:: io:: { BufWriter , ErrorKind , Write , stdin, stdout} ;
16
+ use std:: io:: { BufWriter , Write , stdin, stdout} ;
17
17
use uucore:: display:: Quotable ;
18
18
use uucore:: error:: { FromIo , UResult , USimpleError , UUsageError } ;
19
19
use uucore:: fs:: is_stdin_directory;
20
+ #[ cfg( not( target_os = "windows" ) ) ]
21
+ use uucore:: libc;
20
22
use uucore:: { format_usage, os_str_as_bytes, show} ;
21
23
22
24
use uucore:: locale:: { get_message, get_message_with_args} ;
@@ -31,6 +33,15 @@ mod options {
31
33
32
34
#[ uucore:: main]
33
35
pub fn uumain ( args : impl uucore:: Args ) -> UResult < ( ) > {
36
+ // When we receive a SIGPIPE signal, we want to terminate the process so
37
+ // that we don't print any error messages to stderr. Rust ignores SIGPIPE
38
+ // (see https://github.com/rust-lang/rust/issues/62569), so we restore it's
39
+ // default action here.
40
+ #[ cfg( not( target_os = "windows" ) ) ]
41
+ unsafe {
42
+ libc:: signal ( libc:: SIGPIPE , libc:: SIG_DFL ) ;
43
+ }
44
+
34
45
let matches = uu_app ( ) . try_get_matches_from ( args) ?;
35
46
36
47
let delete_flag = matches. get_flag ( options:: DELETE ) ;
@@ -157,10 +168,16 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
157
168
translate_input ( & mut locked_stdin, & mut buffered_stdout, op) ?;
158
169
}
159
170
160
- // Handle broken pipe errors gracefully during flush.
171
+ #[ cfg( not( target_os = "windows" ) ) ]
172
+ buffered_stdout
173
+ . flush ( )
174
+ . map_err_context ( || get_message ( "tr-error-write-error" ) ) ?;
175
+
176
+ // SIGPIPE is not available on Windows.
177
+ #[ cfg( target_os = "windows" ) ]
161
178
match buffered_stdout. flush ( ) {
162
179
Ok ( ( ) ) => { }
163
- Err ( err) if err. kind ( ) == ErrorKind :: BrokenPipe => { }
180
+ Err ( err) if err. kind ( ) == std :: io :: ErrorKind :: BrokenPipe => std :: process :: exit ( 13 ) ,
164
181
Err ( err) => return Err ( err. map_err_context ( || get_message ( "tr-error-write-error" ) ) ) ,
165
182
}
166
183
0 commit comments