Rewrite last_path_elm() to take a strbuf parameter and to trim off the
last path name element in place rather than returning a pointer to the
beginning of the last path name element.  This simplifies the function
a bit and makes it integrate better with its caller, which is also
strbuf-based.  Rename the function accordingly.

Signed-off-by: Michael Haggerty <mhag...@alum.mit.edu>
---
 lockfile.c | 38 ++++++++++++++++----------------------
 1 file changed, 16 insertions(+), 22 deletions(-)

diff --git a/lockfile.c b/lockfile.c
index 8e7bcda..f9c8515 100644
--- a/lockfile.c
+++ b/lockfile.c
@@ -100,32 +100,28 @@ static void remove_lock_file_on_signal(int signo)
 }
 
 /*
- * p = absolute or relative path name
+ * path = absolute or relative path name
  *
- * Return a pointer into p showing the beginning of the last path name
- * element.  If p is empty or the root directory ("/"), just return p.
+ * Remove the last path name element from path (leaving the preceding
+ * "/", if any).  If path is empty or the root directory ("/"), set
+ * path to the empty string.
  */
-static char *last_path_elm(char *p)
+static void trim_last_path_elm(struct strbuf *path)
 {
-       /* r starts pointing to null at the end of the string */
-       char *r = strchr(p, '\0');
-
-       if (r == p)
-               return p; /* just return empty string */
-
-       r--; /* back up to last non-null character */
+       int i = path->len;
 
        /* back up past trailing slashes, if any */
-       while (r > p && *r == '/')
-               r--;
+       while (i && path->buf[i - 1] == '/')
+               i--;
 
        /*
-        * then go backwards until I hit a slash, or the beginning of
-        * the string
+        * then go backwards until a slash, or the beginning of the
+        * string
         */
-       while (r > p && *(r-1) != '/')
-               r--;
-       return r;
+       while (i && path->buf[i - 1] != '/')
+               i--;
+
+       strbuf_setlen(path, i);
 }
 
 
@@ -155,14 +151,12 @@ static void resolve_symlink(struct strbuf *path)
                if (is_absolute_path(link.buf))
                        /* absolute path simply replaces p */
                        strbuf_reset(path);
-               else {
+               else
                        /*
                         * link is a relative path, so replace the
                         * last element of p with it.
                         */
-                       char *r = last_path_elm(path->buf);
-                       strbuf_setlen(path, r - path->buf);
-               }
+                       trim_last_path_elm(path);
 
                strbuf_addbuf(path, &link);
        }
-- 
1.9.1

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to