From f1f76c44e6cd8c6d0a05c26812faa86edf223b12 Mon Sep 17 00:00:00 2001 From: Florian Festi Date: Tue, 5 Mar 2024 11:40:19 +0100 Subject: [PATCH] Make %dirname behave like dirname (3) Strip trailing '/' unless "/" itself Keep "." untouched Keep root dir for files in the root dir Return "." for files without directory and empty strings See man 3 dirname Resolves: 2928 --- rpmio/macro.c | 18 ++++++++++++++++-- tests/rpmmacro.at | 26 ++++++++++++++++++++++---- 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/rpmio/macro.c b/rpmio/macro.c index 88546e7d02..b81b03c860 100644 --- a/rpmio/macro.c +++ b/rpmio/macro.c @@ -1306,8 +1306,22 @@ static void doFoo(rpmMacroBuf mb, rpmMacroEntry me, ARGV_t argv, size_t *parsed) b++; } else if (rstreq("dirname", me->name)) { buf = xstrdup(argv[1]); - if ((b = strrchr(buf, '/')) != NULL) - *b = '\0'; + if (!strcmp(buf, "")) { + buf = rstrcat(&buf, "."); + } else { + /* strip trailing / */ + size_t l = strlen(buf); + if (l > 1 && buf[l-1] == '/') + buf[l-1] = '\0'; + if ((b = strrchr(buf, '/')) != NULL) + if (b == buf) + /* keep root dir */ + buf[1] = '\0'; + else + *b = '\0'; + else + strcpy(buf, "."); + } b = buf; } else if (rstreq("shrink", me->name)) { /* diff --git a/tests/rpmmacro.at b/tests/rpmmacro.at index 47a13ef2b8..6d460dd7e2 100644 --- a/tests/rpmmacro.at +++ b/tests/rpmmacro.at @@ -364,6 +364,15 @@ runroot rpm --eval "%{dirname:dir}" runroot rpm --eval "%{dirname dir}" runroot rpm --eval "%dirname" runroot rpm --eval "%dirname dir" +runroot rpm --eval "%dirname /" +runroot rpm --eval "%dirname ./" +runroot rpm --eval "%dirname .." +runroot rpm --eval "%dirname ../" +runroot rpm --eval "%dirname ../foo" +runroot rpm --eval "%dirname /foo" +runroot rpm --eval "%dirname /foo/" +runroot rpm --eval "%dirname /foo/foobar" +runroot rpm --eval "%dirname /foo/foobar/" runroot rpm --define '%xxx /hello/%%%%/world' --eval '%{dirname:%xxx}' runroot rpm --eval "%{uncompress}" runroot rpm --eval "%{uncompress:}" @@ -381,10 +390,19 @@ runroot rpm --eval "%shrink %%%%" runroot rpm --eval "%verbose foo" ], [0], -[ -dir -dir -dir +[. +. +. +. +/ +. +. +. +.. +/ +/ +/foo +/foo /hello/%% bar