Okay, so I tried to come up with something, but I didn't get it to work properly, I think. Sometimes the output was unexpected, or maybe I just expected the wrong things to happen.
My little test program is supposed to print the latest version of those entered by the user as parameters to the program. Currently I let it print the result for every value of i in the for loop.
Code:
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, const char *argv[])
{
if (argc<2) {
fprintf(stderr, "Arguments missing.\n");
exit (1);
}
const char *max;
max=argv[1];
printf("1 – %6s (Max: %s)\n", argv[1], max); // Remove this line
for (int i=2; i<argc; i++) {
if (strverscmp(argv[i], max)==1)
max=argv[i];
printf("%d – %6s (Max: %s)\n", i, argv[i], max); // Remove this line
}
// printf("%s\n", max);
return 0;
}
Now, compiling and a few test runs:
Code:
$ gcc -std=gnu99 -o LatestVersion LatestVersion.c
$ ./LatestVersion 2.10.0 2.30.0
1 – 2.10.0 (Max: 2.10.0)
2 – 2.30.0 (Max: 2.10.0)
$ ./LatestVersion 2.19.0 2.30.0
1 – 2.19.0 (Max: 2.19.0)
2 – 2.30.0 (Max: 2.19.0)
$ ./LatestVersion 2.20.0 2.30.0
1 – 2.20.0 (Max: 2.20.0)
2 – 2.30.0 (Max: 2.30.0)
$ ./LatestVersion 1.20.0 1.30.0
1 – 1.20.0 (Max: 1.20.0)
2 – 1.30.0 (Max: 1.30.0)
$ ./LatestVersion 1.10.0 1.30.0
1 – 1.10.0 (Max: 1.10.0)
2 – 1.30.0 (Max: 1.10.0)
$ ./LatestVersion 1.10.0 1.90.0
1 – 1.10.0 (Max: 1.10.0)
2 – 1.90.0 (Max: 1.10.0)
$ ./LatestVersion 1.10.0 1.99.0
1 – 1.10.0 (Max: 1.10.0)
2 – 1.99.0 (Max: 1.10.0)
$ ./LatestVersion 1.20.0 1.99.0
1 – 1.20.0 (Max: 1.20.0)
2 – 1.99.0 (Max: 1.20.0)
$ ./LatestVersion 1.30.0 1.99.0
1 – 1.30.0 (Max: 1.30.0)
2 – 1.99.0 (Max: 1.30.0)
$ ./LatestVersion 1.80.0 1.99.0
1 – 1.80.0 (Max: 1.80.0)
2 – 1.99.0 (Max: 1.99.0)
$ ./LatestVersion 1.70.0 1.99.0
1 – 1.70.0 (Max: 1.70.0)
2 – 1.99.0 (Max: 1.70.0)
$ ./LatestVersion 1.79.0 1.99.0
1 – 1.79.0 (Max: 1.79.0)
2 – 1.99.0 (Max: 1.79.0)
$
Some of the results are unexpected and some are expected.
Since I'm not very good at this, I suspect that I didn't do this quite right… But what? Or maybe I just misunderstood how strverscmp works.
I expected this output:
Code:
$ ./LatestVersion 2.10.0 2.30.0
1 – 2.10.0 (Max: 2.10.0)
2 – 2.30.0 (Max: 2.30.0)
$ ./LatestVersion 2.19.0 2.30.0
1 – 2.19.0 (Max: 2.19.0)
2 – 2.30.0 (Max: 2.30.0)
$ ./LatestVersion 2.20.0 2.30.0
1 – 2.20.0 (Max: 2.20.0)
2 – 2.30.0 (Max: 2.30.0)
$ ./LatestVersion 1.20.0 1.30.0
1 – 1.20.0 (Max: 1.20.0)
2 – 1.30.0 (Max: 1.30.0)
$ ./LatestVersion 1.10.0 1.30.0
1 – 1.10.0 (Max: 1.10.0)
2 – 1.30.0 (Max: 1.30.0)
$ ./LatestVersion 1.10.0 1.90.0
1 – 1.10.0 (Max: 1.10.0)
2 – 1.90.0 (Max: 1.90.0)
$ ./LatestVersion 1.10.0 1.99.0
1 – 1.10.0 (Max: 1.10.0)
2 – 1.99.0 (Max: 1.99.0)
$ ./LatestVersion 1.20.0 1.99.0
1 – 1.20.0 (Max: 1.20.0)
2 – 1.99.0 (Max: 1.99.0)
$ ./LatestVersion 1.30.0 1.99.0
1 – 1.30.0 (Max: 1.30.0)
2 – 1.99.0 (Max: 1.99.0)
$ ./LatestVersion 1.80.0 1.99.0
1 – 1.80.0 (Max: 1.80.0)
2 – 1.99.0 (Max: 1.99.0)
$ ./LatestVersion 1.70.0 1.99.0
1 – 1.70.0 (Max: 1.70.0)
2 – 1.99.0 (Max: 1.99.0)
$ ./LatestVersion 1.79.0 1.99.0
1 – 1.79.0 (Max: 1.79.0)
2 – 1.99.0 (Max: 1.99.0)
$