logo

utils

~/.local/bin tools and git-hooks git clone https://hacktivis.me/git/utils.git
commit: 543c0646c04a081878dea32303d447c8db865ffb
parent 3deb1abcaf37d703d1fa3512014bb6987685ac12
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Sun, 30 Jan 2022 14:33:00 +0100

bin/strings: Fix reading from read_buf

Diffstat:

Mbin/strings.c22++++++----------------
1 file changed, 6 insertions(+), 16 deletions(-)

diff --git a/bin/strings.c b/bin/strings.c @@ -1,5 +1,5 @@ // Collection of Unix tools, comparable to coreutils -// Copyright 2017-2021 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me> +// Copyright 2017-2022 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me> // SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only #define _POSIX_C_SOURCE 200809L @@ -7,7 +7,7 @@ #include <errno.h> /* errno */ #include <fcntl.h> /* open(), O_RDONLY */ #include <stdio.h> /* fprintf(), BUFSIZ */ -#include <stdlib.h> /* strtonum() */ +#include <stdlib.h> /* (BSD) strtonum() */ #include <string.h> /* strerror(), strncmp(), memset() */ #include <unistd.h> /* read(), write(), close(), getopt(), optarg, optind */ @@ -27,21 +27,18 @@ concat(int fd, const char *fdname) while((c = read(fd, read_buf, sizeof(read_buf))) > 0) { - int s; + int read_pos; char b = 0; - for(s = 0; s < c; offset++, s++, b = read_buf[s]) + for(read_pos = 0; read_pos < c; offset++, read_pos++) { + b = read_buf[read_pos]; + if(isprint(b) && write_pos < 4096) { write_buf[write_pos++] = b; } -#define END_NONPRINT -#ifdef END_NONPRINT else -#else - else if(b == '\n' || b == 0) -#endif { if(write_pos >= opt_min_strlen) { @@ -57,13 +54,6 @@ concat(int fd, const char *fdname) write_pos = 0; memset(write_buf, 0, sizeof(write_buf)); } -#ifndef END_NONPRINT - else - { - write_pos = 0; - memset(write_buf, 0, sizeof(write_buf)); - } -#endif } }