logo

utils

~/.local/bin tools and git-hooks git clone https://hacktivis.me/git/utils.git
commit: 767057730d30be5c4ecf6a2e0d8f9c6934071e10
parent 1f61e0de672bc0843afd346bed8e371d298a2568
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Thu,  8 Jul 2021 02:26:54 +0200

bin/{dir,base}name: Ignore -- as argument

Diffstat:

Mbin/basename.c6++++++
Mbin/dirname.c13+++++++++++--
Mtest-bin/basename18++++++++++++------
Mtest-bin/dirname18++++++++++++------
4 files changed, 41 insertions(+), 14 deletions(-)

diff --git a/bin/basename.c b/bin/basename.c @@ -25,6 +25,12 @@ suffix_basename(char *name, char *suffix) int main(int argc, char *argv[]) { + if((argc > 1) && (strcmp(argv[1], "--") == 0)) + { + argv++; + argc--; + } + switch(argc) { case 1: puts("."); break; diff --git a/bin/dirname.c b/bin/dirname.c @@ -4,14 +4,23 @@ #include <libgen.h> // dirname() #include <stdio.h> // puts() +#include <string.h> // strcmp() int main(int argc, char *argv[]) { if(argc != 2) { - fputs("usage: dirname string\n", stderr); - return 1; + if((argc == 3) && (strcmp(argv[1], "--") == 0)) + { + argv++; + argc--; + } + else + { + fputs("usage: dirname string\n", stderr); + return 1; + } } puts(dirname(argv[1])); diff --git a/test-bin/basename b/test-bin/basename @@ -5,16 +5,21 @@ noargs_body() { atf_check -o "inline:.\n" ../bin/basename } -atf_test_case singleslash -singleslash_body() { +atf_test_case 1slash +1slash_body() { atf_check -o "inline:bin\n" ../bin/basename "/usr/bin" } -atf_test_case doubleslash -doubleslash_body() { +atf_test_case 2slash +2slash_body() { atf_check -o "inline:bin\n" ../bin/basename "/usr//bin" } +atf_test_case 2dash +2dash_body() { + atf_check -o "inline:bin\n" ../bin/basename -- "/usr//bin" +} + atf_test_case testopt testopt_body() { atf_check -o "inline:bin\n" ../bin/basename "/usr//bin-test" "-test" @@ -23,7 +28,8 @@ testopt_body() { atf_init_test_cases() { cd "$(atf_get_srcdir)" atf_add_test_case noargs - atf_add_test_case singleslash - atf_add_test_case doubleslash + atf_add_test_case 1slash + atf_add_test_case 2slash + atf_add_test_case 2dash atf_add_test_case testopt } diff --git a/test-bin/dirname b/test-bin/dirname @@ -5,19 +5,25 @@ noargs_body() { atf_check -e "inline:usage: dirname string\n" -s exit:1 ../bin/dirname } -atf_test_case singleslash -singleslash_body() { +atf_test_case 1slash +1slash_body() { atf_check -o "inline:/usr\n" ../bin/dirname "/usr/bin" } -atf_test_case doubleslash -doubleslash_body() { +atf_test_case 2slash +2slash_body() { atf_check -o "inline:/usr\n" ../bin/dirname "/usr//bin" } +atf_test_case 2dash +2dash_body() { + atf_check -o "inline:/usr\n" ../bin/dirname -- "/usr//bin" +} + atf_init_test_cases() { cd "$(atf_get_srcdir)" atf_add_test_case noargs - atf_add_test_case singleslash - atf_add_test_case doubleslash + atf_add_test_case 1slash + atf_add_test_case 2slash + atf_add_test_case 2dash }