summaryrefslogtreecommitdiff
path: root/src/main.rs
blob: b2ee6f00b4b9dc973198bebab17fcf55ddd42d35 (plain)
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
use std::env;
use std::fs::File;
use std::io;
use std::io::prelude::*;

const BUFSIZE: usize = 17;

fn main() {
    let args: Vec<String> = env::args().collect();
    for arg in &args[1..] {
        cat(arg, BUFSIZE);
    }
}


fn cat(filename: &str, bufsize: usize) {
    // The following gives fugly error message if there's a problem.
    // Need to find a better way to report errors to normal people.
    let mut f = File::open(filename).unwrap();

    let mut buffer = vec![0; bufsize];
    loop {
        match f.read(&mut buffer).unwrap() {
            0 => break,
            n => write(&buffer[..n]),
        };
    }
}


fn write(buffer: &[u8]) {
    // Again, the error message is fugly if there's a problem. Also,
    // ideally this wouldn't hardcode the output stream, but it turns
    // out that io::stdout() doesn't return a File, so passing in an
    // open file is tricky.
    io::stdout().write(&buffer).unwrap();
}