"GOT", but the "O" is a cute, smiling pufferfish. Index | Thread | Search

From:
Christian Weisgerber <naddy@mips.inka.de>
Subject:
Re: Got relies on non-POSIX basename(3), dirname(3)
To:
gameoftrees@openbsd.org
Date:
Sat, 19 Sep 2020 22:10:38 +0200

Download raw body.

Thread
I don't know the purpose of got_path_{base,dir}name(), but since
they're basically strdup(fooname(path)) we can switch that to
fooname(strdup(path)) without semantic change.

Does this look right?

diff ebc5bf649f20688294a309f00a46e9e326fea671 /home/naddy/got
blob - 549cac5f15819dd3e37527467f02c394f51d5006
file + lib/path.c
--- lib/path.c
+++ lib/path.c
@@ -358,18 +358,20 @@ got_path_dir_is_empty(const char *dir)
 const struct got_error *
 got_path_dirname(char **parent, const char *path)
 {
-	char *p;
+	char *p, *dir;
 
-	p = dirname(path);
+	p = strdup(path);
 	if (p == NULL)
+		return got_error_from_errno("strdup");
+
+	dir = dirname(p);
+	if (dir == NULL)
 		return got_error_from_errno2("dirname", path);
 
-	if (p[0] == '.' && p[1] == '\0')
+	if (dir[0] == '.' && dir[1] == '\0')
 		return got_error_path(path, GOT_ERR_BAD_PATH);
 
-	*parent = strdup(p);
-	if (*parent == NULL)
-		return got_error_from_errno("strdup");
+	*parent = dir;
 
 	return NULL;
 }
@@ -424,15 +426,17 @@ done:
 const struct got_error *
 got_path_basename(char **s, const char *path)
 {
-	char *base;
+	char *p, *base;
 
-	base = basename(path);
+	p = strdup(path);
+	if (p == NULL)
+		return got_error_from_errno("strdup");
+
+	base = basename(p);
 	if (base == NULL)
 		return got_error_from_errno2("basename", path);
 
-	*s = strdup(base);
-	if (*s == NULL)
-		return got_error_from_errno("strdup");
+	*s = base;
 
 	return NULL;
 }
-- 
Christian "naddy" Weisgerber                          naddy@mips.inka.de