From 948c5f7e693f50e49a38e0d809098a2b2c08d54a 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 "/" Keep "." and ".." 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 | 18 ++++++++++++++---- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/rpmio/macro.c b/rpmio/macro.c index 88546e7d02..b1462c73d6 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 if (strcmp(buf, ".") && strcmp(buf, "..") && strcmp(buf, "/")) { + /* strip trailing / */ + size_t l = strlen(buf); + if (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..dd17d43bc2 100644 --- a/tests/rpmmacro.at +++ b/tests/rpmmacro.at @@ -364,6 +364,11 @@ 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 /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 +386,15 @@ runroot rpm --eval "%shrink %%%%" runroot rpm --eval "%verbose foo" ], [0], -[ -dir -dir -dir +[. +. +. +. +/ +/ +/ +/foo +/foo /hello/%% bar