diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..8ef3544 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "wasi-tutorial" +version = "0.1.0" + diff --git a/input.txt b/input.txt new file mode 100644 index 0000000..e965047 --- /dev/null +++ b/input.txt @@ -0,0 +1 @@ +Hello diff --git a/output.txt b/output.txt new file mode 100644 index 0000000..e965047 --- /dev/null +++ b/output.txt @@ -0,0 +1 @@ +Hello diff --git a/src/main.rs b/src/main.rs index e7a11a9..19a2b7f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,32 @@ -fn main() { - println!("Hello, world!"); +use std::env; +use std::fs; +use std::io::{Read, Write}; + +fn process(input_fname: &str, output_fname: &str) -> Result<(), String> { + let mut input_file = + fs::File::open(input_fname).map_err(|err| format!("error opening input: {}", err))?; + let mut contents = Vec::new(); + input_file + .read_to_end(&mut contents) + .map_err(|err| format!("read error: {}", err))?; + + let mut output_file = fs::File::create(output_fname) + .map_err(|err| format!("error opening output '{}': {}", output_fname, err))?; + output_file + .write_all(&contents) + .map_err(|err| format!("write error: {}", err)) } + +fn main() { + let args: Vec = env::args().collect(); + let program = args[0].clone(); + + if args.len() < 3 { + eprintln!("{} ", program); + return; + } + + if let Err(err) = process(&args[1], &args[2]) { + eprintln!("{}", err) + } +} \ No newline at end of file