From c20f3d08d346c0b9e5dd1b6f40788ec784956e3c Mon Sep 17 00:00:00 2001 From: dtb Date: Tue, 1 Nov 2022 16:18:04 -0400 Subject: [PATCH] fix ASCII issues, not a great fix but a fix --- str/str.1 | 2 +- str/str.c | 23 +++++++++++++++++++---- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/str/str.1 b/str/str.1 index 717ab6a..d6dd5ad 100644 --- a/str/str.1 +++ b/str/str.1 @@ -26,7 +26,7 @@ Str will print a message to standard error and exit unsuccessfully if used impro There's no way of knowing which argument failed the test without re-testing arguments individually. .PP -If a character in a string isn't valid ASCII the behavior of this program is undefined. +If a character in a string isn't valid ASCII str will exit unsuccessfully. .SH SEE ALSO diff --git a/str/str.c b/str/str.c index a99d970..3273a5a 100644 --- a/str/str.c +++ b/str/str.c @@ -1,8 +1,7 @@ -#include #include /* NULL */ #include /* fprintf(3) */ #include /* strcmp(3) */ -#ifdef DONT_USE_SYSTEM_SYSEXITS +#if defined DONT_USE_SYSTEM_SYSEXITS # include "../include/sysexits.h" /* EX_USAGE */ #else # include /* EX_USAGE */ @@ -11,9 +10,22 @@ static char *program_name = "str"; /* don't use this */ -#include "isempty.c" +/* #include "isempty.c" */ +/* An isempty() won't work; see implementation - the default behavior of str(1) + * is to return 1, so it would return 1 (false) no matter what. I'm not adding + * a special case, just do something like `! str isvalue "$input"` in the + * higher level. + * However, if you wanna make it work, the file is included, just hack on this + * source file after removing the comment on the include. */ +#include #include "isvalue.c" +/* This is a special addition to the command that lets `str isvalue "$input"` + * function as a lightweight replacement to the common `test -n "$input"` or + * `[ -n "$input" ]`. This will speed up your shellscript execution by a tad + * ONLY if test(1) isn't already built into your shell (most of the time, it + * is, and saves you the overhead of spawning a new process, which is greater + * than the savings of switching from test(1) to this program). */ static struct { char *name; @@ -58,7 +70,10 @@ usage: fprintf(stderr, "Usage: %s [type] [string...]\n", pass: for(argv += 2, r = 1; *argv != NULL; ++argv) for(i = 0; argv[0][i] != '\0'; ++i) - if(!ctypes[ctype].f(argv[0][i])) + /* First checks if argv[0][i] is valid ASCII; ctypes(3) + * don't handle non-ASCII. + * This is bad. */ + if(argv[0][i] < 0x80 && !ctypes[ctype].f(argv[0][i])) return 1; else r = 0;