From 8d743dab7a2dc32f47c50e7a64e3b93a47730001 Mon Sep 17 00:00:00 2001 From: DTB Date: Mon, 15 Jul 2024 03:43:25 -0600 Subject: [PATCH 1/6] strcmp(1): add copyright header I could trace strcmp(1) as far back as in my repo. --- src/strcmp.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/strcmp.c b/src/strcmp.c index 33b73c2..bd42054 100644 --- a/src/strcmp.c +++ b/src/strcmp.c @@ -1,3 +1,21 @@ +/* + * Copyright (c) 2022–2024 DTB + * SPDX-License-Identifier: AGPL-3.0-or-later + * + * This program is free software: you can redistribute it and/or modify it under + * the terms of the GNU Affero General Public License as published by the Free + * Software Foundation, either version 3 of the License, or (at your option) any + * later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more + * details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see https://www.gnu.org/licenses/. + */ + #include /* fprintf(3), stderr */ #include /* EXIT_FAILURE */ #include From 5caefbb465eccdf1062fb6f01cbeecaf31d86d5d Mon Sep 17 00:00:00 2001 From: DTB Date: Mon, 15 Jul 2024 03:45:36 -0600 Subject: [PATCH 2/6] strcmp(1): note used sysexit --- src/strcmp.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/strcmp.c b/src/strcmp.c index bd42054..f2b86bb 100644 --- a/src/strcmp.c +++ b/src/strcmp.c @@ -18,7 +18,7 @@ #include /* fprintf(3), stderr */ #include /* EXIT_FAILURE */ -#include +#include /* EX_USAGE */ static char *program_name = "strcmp"; From d87c278be5f071f01c3611411c665b92d35ad914 Mon Sep 17 00:00:00 2001 From: DTB Date: Mon, 15 Jul 2024 04:21:50 -0600 Subject: [PATCH 3/6] strcmp(1): re-style, tweak exits --- src/strcmp.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/strcmp.c b/src/strcmp.c index f2b86bb..7de36a5 100644 --- a/src/strcmp.c +++ b/src/strcmp.c @@ -17,26 +17,26 @@ */ #include /* fprintf(3), stderr */ -#include /* EXIT_FAILURE */ +#include /* size_t */ #include /* EX_USAGE */ -static char *program_name = "strcmp"; +static *program_name = "strcmp"; int main(int argc, char *argv[]){ - int i; - - if(argc < 3){ + if (argc < 3) { fprintf(stderr, "Usage: %s string string...\n", - argv[0] == NULL ? program_name : argv[0]); + argv[0] == NULL ? program_name : argv[0] + ); return EX_USAGE; } - for(; *argv[1] != '\0'; ++argv[1]) - for(i = 2; i < argc; ++i) - if(*argv[i-1] > *argv[i]) - return 1; - else if(*argv[i-1] < *argv[i]++) - return -1; /* actually 255 */ + for (; *argv[1] != '\0'; ++argv[1]) { /* iterate chars in ref */ + /* iterate argc */ + for (size_t i = 2 /* ref cmp */; i < argc; ++argv[i], ++i) { + /* this doesn't overrun because of nul termination */ + if (*argv[i-1] != *argv[i]) { return *argv[i-1] - *argv[i]; } + } + } return 0; } From 16f23e11c0dde95690ed1a2ec969170073493f24 Mon Sep 17 00:00:00 2001 From: DTB Date: Mon, 15 Jul 2024 04:26:57 -0600 Subject: [PATCH 4/6] strcmp.1: update docs to match utility --- docs/strcmp.1 | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/docs/strcmp.1 b/docs/strcmp.1 index c99c8c8..db6d3e8 100644 --- a/docs/strcmp.1 +++ b/docs/strcmp.1 @@ -4,7 +4,7 @@ .\" This work is licensed under CC BY-SA 4.0. To see a copy of this license, .\" visit . .\" -.TH STRCMP 1 2024-06-17 "Harakit X.X.X" +.TH STRCMP 1 2024-07-15 "Harakit X.X.X" .SH NAME strcmp \(en compare strings .\" @@ -20,15 +20,15 @@ Check whether string arguments are the same. .SH DIAGNOSTICS The program will exit successfully if the strings are identical. Otherwise, it -will exit with an error code of 1 if a string passed has a lesser byte value -than one of the prior strings: +will exit with an error code less than 128 if a string passed has a lesser byte +value than one of the prior strings: .RS strcmp b a .RE -or with an error code of 255 if it has a greater byte value than one of the -prior strings: +or with an error code greater than 128 if it has a greater byte value than one +of the prior strings: .RS strcmp a b From efb3ce626d28f3ae8dea04f22cf54adc713f03a7 Mon Sep 17 00:00:00 2001 From: DTB Date: Mon, 15 Jul 2024 04:29:43 -0600 Subject: [PATCH 5/6] strcmp(1): fix program_name type --- src/strcmp.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/strcmp.c b/src/strcmp.c index 7de36a5..784029c 100644 --- a/src/strcmp.c +++ b/src/strcmp.c @@ -20,7 +20,7 @@ #include /* size_t */ #include /* EX_USAGE */ -static *program_name = "strcmp"; +char *program_name = "strcmp"; int main(int argc, char *argv[]){ if (argc < 3) { From becb3bac4eb308584ab13f558fe17e692cc8f8bb Mon Sep 17 00:00:00 2001 From: DTB Date: Mon, 15 Jul 2024 14:40:26 -0600 Subject: [PATCH 6/6] strcmp(1): code clarification --- src/strcmp.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/strcmp.c b/src/strcmp.c index 784029c..33eab10 100644 --- a/src/strcmp.c +++ b/src/strcmp.c @@ -30,10 +30,11 @@ int main(int argc, char *argv[]){ return EX_USAGE; } - for (; *argv[1] != '\0'; ++argv[1]) { /* iterate chars in ref */ - /* iterate argc */ - for (size_t i = 2 /* ref cmp */; i < argc; ++argv[i], ++i) { - /* this doesn't overrun because of nul termination */ + /* This compares the Nth character of arg[2] onward with argv[1]'s Nth + * character, rather than comparing each arg with argv[1] sequentially. */ + for (; *argv[1] != '\0'; ++argv[1]) { /* iterate chars in argv[1] */ + for (size_t i = 2; i < argc; ++argv[i], ++i) { /* iterate &argv[2] */ + /* this never overruns because of nul termination */ if (*argv[i-1] != *argv[i]) { return *argv[i-1] - *argv[i]; } } }