2019-08-08 12:44:48 +00:00
|
|
|
//! Prints a file given as an argument to stdout.
|
|
|
|
|
|
|
|
use std::env::args;
|
|
|
|
|
2019-08-14 01:47:39 +00:00
|
|
|
use async_std::fs::File;
|
|
|
|
use async_std::io;
|
|
|
|
use async_std::prelude::*;
|
|
|
|
use async_std::task;
|
2019-08-08 12:44:48 +00:00
|
|
|
|
2019-09-04 23:23:27 +00:00
|
|
|
const LEN: usize = 16 * 1024; // 16 Kb
|
2019-08-08 12:44:48 +00:00
|
|
|
|
|
|
|
fn main() -> io::Result<()> {
|
|
|
|
let path = args().nth(1).expect("missing path argument");
|
|
|
|
|
|
|
|
task::block_on(async {
|
2019-08-12 10:44:57 +00:00
|
|
|
let mut file = File::open(&path).await?;
|
2019-08-08 12:44:48 +00:00
|
|
|
let mut stdout = io::stdout();
|
|
|
|
let mut buf = vec![0u8; LEN];
|
|
|
|
|
|
|
|
loop {
|
|
|
|
// Read a buffer from the file.
|
|
|
|
let n = file.read(&mut buf).await?;
|
|
|
|
|
|
|
|
// If this is the end of file, clean up and return.
|
|
|
|
if n == 0 {
|
|
|
|
stdout.flush().await?;
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write the buffer into stdout.
|
|
|
|
stdout.write_all(&buf[..n]).await?;
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|