[pacman-dev] [PATCH 1/3] vercmp: fail when the wrong number of arguments are provided
Fixes FS#49093 Signed-off-by: Eli Schwartz <eschwartz@archlinux.org> --- src/util/vercmp.c | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/util/vercmp.c b/src/util/vercmp.c index 3521a96a..7b34b30a 100644 --- a/src/util/vercmp.c +++ b/src/util/vercmp.c @@ -40,8 +40,6 @@ static void usage(void) int main(int argc, char *argv[]) { - const char *s1 = ""; - const char *s2 = ""; int ret; if(argc == 1) { @@ -54,14 +52,12 @@ int main(int argc, char *argv[]) usage(); return 0; } - if(argc > 2) { - s2 = argv[2]; - } - if(argc > 1) { - s1 = argv[1]; + if(argc != 3) { + printf("error: %d arguments specified but vercmp needs 2\n", argc-1); + return EXIT_FAILURE; } - ret = alpm_pkg_vercmp(s1, s2); + ret = alpm_pkg_vercmp(argv[1], argv[2]); printf("%d\n", ret); return EXIT_SUCCESS; } -- 2.15.1
I think two ways to ask for this are enough for everyone, and we have never documented this anyway. Signed-off-by: Eli Schwartz <eschwartz@archlinux.org> --- src/util/vercmp.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/util/vercmp.c b/src/util/vercmp.c index 7b34b30a..42639bec 100644 --- a/src/util/vercmp.c +++ b/src/util/vercmp.c @@ -47,8 +47,7 @@ int main(int argc, char *argv[]) return 2; } if(argc > 1 && - (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0 - || strcmp(argv[1], "--usage") == 0)) { + (strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0)) { usage(); return 0; } -- 2.15.1
On 18/12/17 05:28, Eli Schwartz wrote:
I think two ways to ask for this are enough for everyone, and we have never documented this anyway.
Thanks, A
Signed-off-by: Eli Schwartz <eschwartz@archlinux.org> --- doc/vercmp.8.txt | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/doc/vercmp.8.txt b/doc/vercmp.8.txt index 5316b3cb..18819953 100644 --- a/doc/vercmp.8.txt +++ b/doc/vercmp.8.txt @@ -11,7 +11,7 @@ vercmp - version comparison utility Synopsis -------- -'vercmp' <version1> <version2> +'vercmp' [-h] [--help] <version1> <version2> Description @@ -44,8 +44,9 @@ mainly for supporting versioned dependencies that do not include the 'pkgrel'. Options ------- *-h, \--help*:: - Display syntax for the given operation. If no operation was supplied, - then the general syntax is shown. + + Display summary of the available return codes. Must be the first option + specified. Examples @@ -67,11 +68,6 @@ Examples -1 -Configuration -------------- -There is none. - - See Also -------- linkman:pacman[8], linkman:makepkg[8], linkman:libalpm[3] -- 2.15.1
On 12/17/17 at 02:28pm, Eli Schwartz wrote:
Fixes FS#49093
Signed-off-by: Eli Schwartz <eschwartz@archlinux.org> --- src/util/vercmp.c | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-)
diff --git a/src/util/vercmp.c b/src/util/vercmp.c index 3521a96a..7b34b30a 100644 --- a/src/util/vercmp.c +++ b/src/util/vercmp.c @@ -40,8 +40,6 @@ static void usage(void)
int main(int argc, char *argv[]) { - const char *s1 = ""; - const char *s2 = ""; int ret;
if(argc == 1) { @@ -54,14 +52,12 @@ int main(int argc, char *argv[]) usage(); return 0; } - if(argc > 2) { - s2 = argv[2]; - } - if(argc > 1) { - s1 = argv[1]; + if(argc != 3) { + printf("error: %d arguments specified but vercmp needs 2\n", argc-1);
Errors should go to stderr, not stdout, and our style guidelines require spaces around arithmetic operators.
+ return EXIT_FAILURE; }
- ret = alpm_pkg_vercmp(s1, s2); + ret = alpm_pkg_vercmp(argv[1], argv[2]); printf("%d\n", ret); return EXIT_SUCCESS; } -- 2.15.1
Fixes FS#49093 Signed-off-by: Eli Schwartz <eschwartz@archlinux.org> --- v2: errors go to stderr follow style guidelines for arithmetic minor grammatical correction for error message src/util/vercmp.c | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/util/vercmp.c b/src/util/vercmp.c index 3521a96a..6298ba79 100644 --- a/src/util/vercmp.c +++ b/src/util/vercmp.c @@ -40,8 +40,6 @@ static void usage(void) int main(int argc, char *argv[]) { - const char *s1 = ""; - const char *s2 = ""; int ret; if(argc == 1) { @@ -54,14 +52,12 @@ int main(int argc, char *argv[]) usage(); return 0; } - if(argc > 2) { - s2 = argv[2]; - } - if(argc > 1) { - s1 = argv[1]; + if(argc != 3) { + fprintf(stderr, "error: %d argument(s) specified but vercmp needs 2\n", argc - 1); + return EXIT_FAILURE; } - ret = alpm_pkg_vercmp(s1, s2); + ret = alpm_pkg_vercmp(argv[1], argv[2]); printf("%d\n", ret); return EXIT_SUCCESS; } -- 2.15.1
On 18/12/17 13:57, Eli Schwartz wrote:
Fixes FS#49093
Signed-off-by: Eli Schwartz <eschwartz@archlinux.org> ---
v2: errors go to stderr follow style guidelines for arithmetic minor grammatical correction for error message
src/util/vercmp.c | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-)
diff --git a/src/util/vercmp.c b/src/util/vercmp.c index 3521a96a..6298ba79 100644 --- a/src/util/vercmp.c +++ b/src/util/vercmp.c @@ -40,8 +40,6 @@ static void usage(void)
int main(int argc, char *argv[]) { - const char *s1 = ""; - const char *s2 = ""; int ret;
if(argc == 1) { @@ -54,14 +52,12 @@ int main(int argc, char *argv[]) usage(); return 0; } - if(argc > 2) { - s2 = argv[2]; - } - if(argc > 1) { - s1 = argv[1]; + if(argc != 3) { + fprintf(stderr, "error: %d argument(s) specified but vercmp needs 2\n", argc - 1);
This sentence sounds awkwark to me. Can we just go: fprintf(stderr, "error: %d argument(s) specified\n\n "Usage: vercmp <ver1> <ver2>\n") ?
+ return EXIT_FAILURE; }
- ret = alpm_pkg_vercmp(s1, s2); + ret = alpm_pkg_vercmp(argv[1], argv[2]); printf("%d\n", ret); return EXIT_SUCCESS; }
Fixes FS#49093 Signed-off-by: Eli Schwartz <eschwartz@archlinux.org> --- v3: update error message to be less awkward, just provide usage. I agree this sounds much better. src/util/vercmp.c | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/util/vercmp.c b/src/util/vercmp.c index 0b5d4508..1b2f979e 100644 --- a/src/util/vercmp.c +++ b/src/util/vercmp.c @@ -40,8 +40,6 @@ static void usage(void) int main(int argc, char *argv[]) { - const char *s1 = ""; - const char *s2 = ""; int ret; if(argc == 1) { @@ -53,14 +51,13 @@ int main(int argc, char *argv[]) usage(); return 0; } - if(argc > 2) { - s2 = argv[2]; - } - if(argc > 1) { - s1 = argv[1]; + if(argc != 3) { + fprintf(stderr, "error: %d argument(s) specified\n\n" + "Usage: vercmp <ver1> <ver2>\n", argc - 1); + return EXIT_FAILURE; } - ret = alpm_pkg_vercmp(s1, s2); + ret = alpm_pkg_vercmp(argv[1], argv[2]); printf("%d\n", ret); return EXIT_SUCCESS; } -- 2.15.1
participants (3)
-
Allan McRae
-
Andrew Gregory
-
Eli Schwartz