From 5d99c1208905e1c1b42f2491483a37ae99345f4f Mon Sep 17 00:00:00 2001 From: Richard Alpe Date: Mon, 10 Mar 2025 16:16:52 +0100 Subject: [PATCH] bin: copy: use dirname() to derive parent directory Cosmetic change intended to improve code readability. Signed-off-by: Richard Alpe --- src/bin/copy.c | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/src/bin/copy.c b/src/bin/copy.c index 6e54b079..fa20b99b 100644 --- a/src/bin/copy.c +++ b/src/bin/copy.c @@ -6,6 +6,7 @@ #include #include #include +#include #include #include @@ -85,7 +86,7 @@ static char *getuser(void) /* * If UNIX user is in UNIX group of directory containing file, - * return gid, otherwise -1 + * return 1, otherwise 0. * * E.g., writing to /cfg/foo, where /cfg is owned by root:wheel, * should result in the file being owned by $LOGNAME:wheel with @@ -93,23 +94,19 @@ static char *getuser(void) */ static gid_t in_group(const char *user, const char *fn, gid_t *gid) { - char dir[strlen(fn) + 2]; + char path[PATH_MAX]; const struct passwd *pw; int i, num = 0, rc = 0; struct stat st; gid_t *groups; - char *ptr; + char *dir; pw = getpwnam(user); if (!pw) return 0; - strlcpy(dir, fn, sizeof(dir)); - ptr = strrchr(dir, '/'); - if (ptr) - *ptr = 0; - else - strlcpy(dir, ".", sizeof(dir)); + strlcpy(path, fn, sizeof(path)); + dir = dirname(path); if (stat(dir, &st)) return 0;