From 20e9c19ccd6b1153724d159af2e9378059a25402 Mon Sep 17 00:00:00 2001 From: emma Date: Sat, 17 Dec 2022 17:48:07 -0500 Subject: [PATCH] cat(1): -u works, kinda, but requires 3 ctrl+D presses --- src/cat.rs | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/src/cat.rs b/src/cat.rs index cb81066..e3b09a1 100644 --- a/src/cat.rs +++ b/src/cat.rs @@ -18,10 +18,9 @@ */ use std::env; -use std::fs::{File, read, read_to_string}; +use std::fs::{File, read_to_string}; use std::io; -use std::io::{ Read, Write }; -use std::os::unix::io::FromRawFd; +use std::io::{ BufRead, Read }; use std::str; use arg::Args; @@ -50,6 +49,33 @@ fn main() -> ExitCode { if args.paths.is_empty() { args.paths.push("-".to_string()); } + while args.u { + for path in &args.paths { + if path == "-" { + loop { + let mut buf = String::new(); + match io::stdin().lock().read_line(&mut buf) { + Ok(bytes) => { + if bytes == 0 { break }; + print!("{}", buf); + }, + Err(err) => println!("{}", err), + }; + } + } else { + let mut input = String::new(); + match File::open(path) { + Ok(input) => {}, + Err(_) => { + println!("{}: {}: No such file or directory.", &argv0, path); + return ExitCode::NoInput; + }, + }; + } + } + return ExitCode::Ok; + } + for path in args.paths { if path == "-" { let mut bytes: Vec = Vec::new();