Thread (24 messages) flat view 24 messages, 4 authors, 2016-06-15

Re: [PATCH v3 09/10] abspath: convert absolute_path() to strbuf

From: René Scharfe <hidden>
Date: 2016-06-15 23:02:03

Am 28.07.2014 um 21:15 schrieb Jeff King:
On Mon, Jul 28, 2014 at 08:33:55PM +0200, René Scharfe wrote:
quoted
  const char *absolute_path(const char *path)
  {
-	static char buf[PATH_MAX + 1];
-
-	if (!*path) {
-		die("The empty string is not a valid path");
-	} else if (is_absolute_path(path)) {
-		if (strlcpy(buf, path, PATH_MAX) >= PATH_MAX)
-			die("Too long path: %.*s", 60, path);
-	} else {
-		size_t len;
-		const char *fmt;
-		const char *cwd = get_pwd_cwd();
-		if (!cwd)
-			die_errno("Cannot determine the current working directory");
-		len = strlen(cwd);
-		fmt = (len > 0 && is_dir_sep(cwd[len - 1])) ? "%s%s" : "%s/%s";
-		if (snprintf(buf, PATH_MAX, fmt, cwd, path) >= PATH_MAX)
-			die("Too long path: %.*s", 60, path);
-	}
-	return buf;
+	static struct strbuf sb;
+	strbuf_init(&sb, 0);
+	strbuf_add_absolute_path(&sb, path);
+	return sb.buf;
  }
Is it right to strbuf_init here? That means that we are throwing away
the old buffer for each call. I would think you want instead:

   static struct strbuf sb = STRBUF_INIT;
   strbuf_reset(&sb);
   ...
I changed it from _reset to _init, but I can't remember why. :(  Perhaps 
it's the summer heat.  Your version makes more sense to me now.

René
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help