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 <stdlib.h> /* EXIT_FAILURE */
#include <stdlib.h> /* size_t */
#include <sysexits.h> /* 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;
}