1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
use std::borrow::Cow;
use std::io::{self, Result, Write};
use clap::{Arg, ArgAction, Command};
use uucore::error::{UResult, USimpleError};
use uucore::format_usage;
#[cfg(any(target_os = "linux", target_os = "android"))]
mod splice;
const ABOUT: &str = "repeatedly display a line with STRING (or 'y')";
const USAGE: &str = "{} [STRING]...";
const BUF_SIZE: usize = 16 * 1024;
#[uucore::main]
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let matches = uu_app().try_get_matches_from(args)?;
let string = if let Some(values) = matches.get_many::<String>("STRING") {
let mut result = values.fold(String::new(), |res, s| res + s + " ");
result.pop();
result.push('\n');
Cow::from(result)
} else {
Cow::from("y\n")
};
let mut buffer = [0; BUF_SIZE];
let bytes = prepare_buffer(&string, &mut buffer);
match exec(bytes) {
Ok(()) => Ok(()),
Err(err) if err.kind() == io::ErrorKind::BrokenPipe => Ok(()),
Err(err) => Err(USimpleError::new(1, format!("standard output: {}", err))),
}
}
pub fn uu_app() -> Command {
Command::new(uucore::util_name())
.about(ABOUT)
.override_usage(format_usage(USAGE))
.arg(Arg::new("STRING").action(ArgAction::Append))
.infer_long_args(true)
}
fn prepare_buffer<'a>(input: &'a str, buffer: &'a mut [u8; BUF_SIZE]) -> &'a [u8] {
if input.len() < BUF_SIZE / 2 {
let mut size = 0;
while size < BUF_SIZE - input.len() {
let (_, right) = buffer.split_at_mut(size);
right[..input.len()].copy_from_slice(input.as_bytes());
size += input.len();
}
&buffer[..size]
} else {
input.as_bytes()
}
}
#[cfg(unix)]
fn enable_pipe_errors() -> Result<()> {
let ret = unsafe { libc::signal(libc::SIGPIPE, libc::SIG_DFL) };
if ret == libc::SIG_ERR {
return Err(io::Error::new(io::ErrorKind::Other, ""));
}
Ok(())
}
#[cfg(not(unix))]
fn enable_pipe_errors() -> Result<()> {
Ok(())
}
pub fn exec(bytes: &[u8]) -> io::Result<()> {
let stdout = io::stdout();
let mut stdout = stdout.lock();
enable_pipe_errors()?;
#[cfg(any(target_os = "linux", target_os = "android"))]
{
match splice::splice_data(bytes, &stdout) {
Ok(_) => return Ok(()),
Err(splice::Error::Io(err)) => return Err(err),
Err(splice::Error::Unsupported) => (),
}
}
loop {
stdout.write_all(bytes)?;
}
}