strcmp(1): re-style, tweak exits

This commit is contained in:
dtb 2024-07-15 04:21:50 -06:00
parent 5caefbb465
commit d87c278be5
Signed by: trinity
GPG Key ID: 34C0543BBB6AF81B

View File

@ -17,26 +17,26 @@
*/ */
#include <stdio.h> /* fprintf(3), stderr */ #include <stdio.h> /* fprintf(3), stderr */
#include <stdlib.h> /* EXIT_FAILURE */ #include <stdlib.h> /* size_t */
#include <sysexits.h> /* EX_USAGE */ #include <sysexits.h> /* EX_USAGE */
static char *program_name = "strcmp"; static *program_name = "strcmp";
int main(int argc, char *argv[]){ int main(int argc, char *argv[]){
int i; if (argc < 3) {
if(argc < 3){
fprintf(stderr, "Usage: %s string string...\n", fprintf(stderr, "Usage: %s string string...\n",
argv[0] == NULL ? program_name : argv[0]); argv[0] == NULL ? program_name : argv[0]
);
return EX_USAGE; return EX_USAGE;
} }
for(; *argv[1] != '\0'; ++argv[1]) for (; *argv[1] != '\0'; ++argv[1]) { /* iterate chars in ref */
for(i = 2; i < argc; ++i) /* iterate argc */
if(*argv[i-1] > *argv[i]) for (size_t i = 2 /* ref cmp */; i < argc; ++argv[i], ++i) {
return 1; /* this doesn't overrun because of nul termination */
else if(*argv[i-1] < *argv[i]++) if (*argv[i-1] != *argv[i]) { return *argv[i-1] - *argv[i]; }
return -1; /* actually 255 */ }
}
return 0; return 0;
} }