github.com/filecoin-project/bacalhau@v0.3.23-0.20230228154132-45c989550ace/testdata/wasm/cat/src/main.rs (about)

     1  use std::env;
     2  use std::process;
     3  use std::io;
     4  use std::io::Read;
     5  use std::io::Write;
     6  use std::fs::File;
     7  use std::error::Error;
     8  
     9  fn cat(path: &String) -> Result<(), Box<dyn Error>> {
    10      let mut input = File::open(path)?;
    11      let mut buffer = [0; 256];
    12      let mut output = io::stdout();
    13      
    14      loop {
    15          let read = input.read(&mut buffer)?;
    16          let mut written = 0;
    17  
    18          if read <= 0 { 
    19              return Ok(());
    20          }
    21  
    22          loop {
    23              written = output.write(&mut buffer[written..read])?;
    24              if written >= read { break; }
    25          }
    26      }
    27  }
    28  
    29  fn main() {
    30      let args: Vec<String> = env::args().collect();
    31      for arg in args.iter().skip(1) {
    32          if let Err(err) = cat(arg) {
    33              eprintln!("{}", err);
    34              process::exit(1);
    35          }
    36      }
    37      
    38      process::exit(0);
    39  }