From 4cdae6d621190b46d9f86fede3eefa47ff1070c9 Mon Sep 17 00:00:00 2001 From: Ruben Carlo Benante Date: Fri, 22 May 2026 19:31:51 -0300 Subject: [PATCH] IMPROVES-i11 sequal now uses errno and returns -1,0,+1 --- cmp11sht.c | 37 +++++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/cmp11sht.c b/cmp11sht.c index 33cadca..2375ea3 100644 --- a/cmp11sht.c +++ b/cmp11sht.c @@ -34,6 +34,7 @@ #include #include #include +#include #define SBUFF 256 /* Max string size */ @@ -121,7 +122,15 @@ cmp11sht v20251221.0718 (C) 2025 by Ruben C. Benante (MIT Lic)\n\n" if(fenda == argv[1] || fendb == argv[2]) // string { if(opt==3) printf("cmp11sht: string\n"); + errno = 0; res = sequal(argv[1], argv[2], delta, &ratio, s1, s2); + if(errno == EINVAL) + res = 3; /* error → CLI exit 3 */ + else if(res > 0) + res = 1; /* a > b → CLI exit 1 */ + else if(res < 0) + res = 2; /* a < b → CLI exit 2 */ + /* res == 0 stays 0 (equal) */ if(opt==3) printf("result: "); if(opt) printf("%d\n", res); if(opt==3) printf("ratio: "); @@ -132,6 +141,11 @@ cmp11sht v20251221.0718 (C) 2025 by Ruben C. Benante (MIT Lic)\n\n" { if(opt==3) printf("cmp11sht: float\n"); res = fequal(a, b, delta); + if(res > 0) + res = 1; /* a > b → CLI exit 1 */ + else if(res < 0) + res = 2; /* a < b → CLI exit 2 */ + /* res == 0 stays 0 (equal) */ if(opt==3) printf("result: "); if(opt) printf("%d\n", res); if(opt==3) printf("diff: "); @@ -151,25 +165,32 @@ cmp11sht v20251221.0718 (C) 2025 by Ruben C. Benante (MIT Lic)\n\n" int fequal(float a, float b, float delta) { if(a < b - delta) - return 2; + return -1; if(a > b + delta) - return 1; + return 1; /* b-delta <= a <= b+delta */ return 0; } /* ---------------------------------------------------------------------- */ -/* compare similarity between two strings - * Return 0 if similar above given threshold - * -1 if a < b alphabetically - * +1 if a > b alphabetically +/* compare similarity between two strings. + * Return: + * 0 if equal or similar above given threshold + * -1 if a < b alphabetically (after normalization) + * +1 if a > b alphabetically (after normalization) + * On error: sets errno = EINVAL and returns 0; result is undefined. + * Caller must reset errno = 0 before the call to detect errors. */ int sequal(char *a, char *b, float thr, float *ratio, char *s1, char *s2) { int i; - if(!a || !b || !s1 || !s2 || !ratio) return 3; /* error sentinel */ + if(!a || !b || !s1 || !s2 || !ratio) + { + errno = EINVAL; + return 0; + } // remove accents asciify(a, s1); @@ -194,7 +215,7 @@ int sequal(char *a, char *b, float thr, float *ratio, char *s1, char *s2) *ratio = shit11(s1, s2); if(*ratio > thr) return 0; - return (i < 0)? 2 : 1; + return (i < 0)? -1 : 1; } /* ---------------------------------------------------------------------- */