From d2ae35eac9eca030d9b96252706da8494b7f5b18 Mon Sep 17 00:00:00 2001 From: emma Date: Sun, 28 Jul 2024 01:15:08 -0600 Subject: [PATCH] fop(1): fixes glaring issue with newline handling --- src/fop.rs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/fop.rs b/src/fop.rs index fc45248..06024d5 100644 --- a/src/fop.rs +++ b/src/fop.rs @@ -71,7 +71,7 @@ fn main() { let mut buf = String::new(); if let Err(e) = stdin().read_to_string(&mut buf) { - eprintln!("{}: {}", argv[0], e.strerror); + eprintln!("{}: {}", argv[0], e.strerror()); exit(EX_IOERR); }; @@ -120,8 +120,13 @@ fn main() { /* get the output with which the original field will be replaced */ let mut replace = output.stdout.clone(); - /* as long as it’s not a newline, set the replacement to the output */ - if replace.pop() != Some(b'\n') { replace = output.stdout; } + /* pop trailing newline out if the input did not contain it */ + if fields[index].chars().last() != Some('\n') /* no newline */ + && replace.pop() != Some(b'\n') { /* pop last char of replacement */ + /* restore replacement to original command output if popped char was not + * a newline */ + replace = output.stdout; + } /* convert the output of the program to UTF-8 */ let new_field = String::from_utf8(replace).unwrap_or_else(|e| {