From 3e3b212b397e92af8efcbbf29b52269df53c4661 Mon Sep 17 00:00:00 2001 From: Ray Gardner Date: Fri, 12 Apr 2024 17:12:33 -0600 Subject: [PATCH] Add -b option (use bytes, not characters) All work done by @oliverkwebb, thanks Oliver Co-Authored-By: Oliver Webb --- toys/pending/awk.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/toys/pending/awk.c b/toys/pending/awk.c index def9edb0..ee6d220c 100644 --- a/toys/pending/awk.c +++ b/toys/pending/awk.c @@ -5,7 +5,7 @@ * * See https://pubs.opengroup.org/onlinepubs/9699919799/utilities/awk.html -USE_AWK(NEWTOY(awk, "F:v*f*c", TOYFLAG_USR|TOYFLAG_BIN)) +USE_AWK(NEWTOY(awk, "F:v*f*bc", TOYFLAG_USR|TOYFLAG_BIN)) config AWK bool "awk" @@ -16,6 +16,7 @@ config AWK awk [-F sepstring] -f progfile [-f progfile]... [-v assignment]... [argument...] also: + -b : use bytes, not characters -c : compile only, do not run */ @@ -383,7 +384,8 @@ static unsigned *strtowc(char *str, size_t len, int *newlen) { size_t ai = 0, ui = 0; unsigned *ret = xzalloc(sizeof(int) * len); - while (ai < len) { + while (ai < len) if (FLAG(b)) ret[ui++] = str[ai++]; + else { int isvalid = utf8towc(ret+(ui++), str+ai, len-ai); if (!isvalid) ai++; // Null byte else if (isvalid < 0) ret[ui] = '?', ai+=(+isvalid); -- 2.39.2