>From 4f72a9c9b5222ace172a00f898b6fcc96743c252 Mon Sep 17 00:00:00 2001 From: Assaf Gordon Date: Thu, 6 Jan 2022 12:35:58 -0700 Subject: [PATCH 5/9] cut: implement -D with -b --- src/cut.c | 38 +++++++++++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/src/cut.c b/src/cut.c index 369c47856..ed2e903ab 100644 --- a/src/cut.c +++ b/src/cut.c @@ -525,6 +525,36 @@ cut_adv_fields (char* linebuf, size_t len) return output || fld>1; } + +static bool +cut_adv_bytes (char* linebuf, size_t len) +{ + bool output = false; + + /* Iterate the requested field LIST, and print accordingly */ + for (struct field_range_pair* r = frp; r != last_frp ; ++r) + { + /* If open-ended range, print up to the available fields */ + uintmax_t hi = (r->hi == UINTMAX_MAX) ? len : r->hi; + + if (output_delimiter_specified && output) + fwrite (output_delimiter_string, sizeof (char), + output_delimiter_length, stdout); + + for (uintmax_t i = r->lo - 1 ; i < hi ; ++i ) + { + if (i >=len) + break; + + putchar (linebuf[i]); + + output = true; + } + } + + return true; +} + static void cut_adv (FILE *stream) { @@ -555,12 +585,14 @@ cut_adv (FILE *stream) } - bool output = cut_adv_fields (linebuf, len); + bool output ; + if (operating_mode == byte_mode) + output = cut_adv_bytes (linebuf, len); + else + output = cut_adv_fields (linebuf, len); - //fprintf(stderr,"end of line\n"); if (output) putchar (line_delim); - } free (linebuf); -- 2.20.1