wc

OS in Rust

Announcements

  • Lab Day
    • Set up environment
    • Building a command line program

Homework

  • “CLI” is just this lab but more complete.
    • Windows officially no longer supported.
  • Due Friday, 23 Jan. at 1440 ET.
    • Plan to hang out as a class 1310-1440 on Fridays, probably.

Setup

  • If you are stuck setting up Rust, following this
  • At the end you should have:
    • cargo, git, and at least one of gcc or clang on your system.
    • A repository named 371os on Github that is either (1) public or (2) shared with [cd-public](https://github.com/cd-public

Requirements

    • That is, you must support wc src/main.rs but not wc --files0-from=F src/main.rs
    • Check out wc and wc --help (which you don’t need to provide) to get a sense of the task.

Exercise

Repository

  • Create a repository for this class.
  • Requirements
    • Name is 371os
    • May be public or private to you and me (cd-public).
    • Must email me a link to your repository from your @willamette.edu email, e.g.:
  • If you’re stuck, read more here

Crate

  • To complete the lab today, create a crate named my_wc in a folder named 01 in your 371os repository.
cargo new 01 --name my_wc --vcs none
  • If you’re stuck, read more here

wc

  • Implement the minimal base functionality of wc using this crate.

Full functionality

  • Here is wc --help
  • You do not need to do all of this!
Usage: wc [OPTION]... [FILE]...
  or:  wc [OPTION]... --files0-from=F
Print newline, word, and byte counts for each FILE, and a total line if
more than one FILE is specified.  A word is a non-zero-length sequence of
characters delimited by white space.

With no FILE, or when FILE is -, read standard input.

The options below may be used to select which counts are printed, always in
the following order: newline, word, character, byte, maximum line length.
  -c, --bytes            print the byte counts
  -m, --chars            print the character counts
  -l, --lines            print the newline counts
      --files0-from=F    read input from the files specified by
                           NUL-terminated names in file F;
                           If F is - then read names from standard input
  -L, --max-line-length  print the maximum display width
  -w, --words            print the word counts
      --help     display this help and exit
      --version  output version information and exit

GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
Report any translation bugs to <https://translationproject.org/team/>
Full documentation <https://www.gnu.org/software/coreutils/wc>
or available locally via: info '(coreutils) wc invocation'

Required functionality

  • That is more than required, you need only implement:
Usage: wc [FILE]
Print newline, word, and byte counts for each FILE. A word is a 
non-zero-length sequence of characters delimited by white space.
  • This can be trivially tested with some of the following:
$ wc src/main.rs
 12  30 229 src/main.rs
$ cargo build release
$ ./target/release/my_wc src/main.rs
 12  30 229 src/main.rs
  • Your src/main.rs need not be of any particular size, and you needn’t implement all code within src/main.rs, but you should get the same counts from wc and from your release binary.

Helpful Reference


fn read_lines(filename: &str) -> Vec<String> {
    let mut result = Vec::new();

    for line in std::fs::read_to_string(filename).unwrap().lines() {
        result.push(line.to_string())
    }

    result
}

Fin