trim_last_path_component(): replace last_path_elm()
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 now also strbuf-based. Rename the function accordingly and a bit less tersely. Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
parent
6cad805332
commit
0c0d6e8601
38
lockfile.c
38
lockfile.c
@ -76,32 +76,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
|
* Remove the last path name element from path (leaving the preceding
|
||||||
* element. If p is empty or the root directory ("/"), just return p.
|
* "/", 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_component(struct strbuf *path)
|
||||||
{
|
{
|
||||||
/* r starts pointing to null at the end of the string */
|
int i = path->len;
|
||||||
char *r = strchr(p, '\0');
|
|
||||||
|
|
||||||
if (r == p)
|
|
||||||
return p; /* just return empty string */
|
|
||||||
|
|
||||||
r--; /* back up to last non-null character */
|
|
||||||
|
|
||||||
/* back up past trailing slashes, if any */
|
/* back up past trailing slashes, if any */
|
||||||
while (r > p && *r == '/')
|
while (i && path->buf[i - 1] == '/')
|
||||||
r--;
|
i--;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* then go backwards until I hit a slash, or the beginning of
|
* then go backwards until a slash, or the beginning of the
|
||||||
* the string
|
* string
|
||||||
*/
|
*/
|
||||||
while (r > p && *(r-1) != '/')
|
while (i && path->buf[i - 1] != '/')
|
||||||
r--;
|
i--;
|
||||||
return r;
|
|
||||||
|
strbuf_setlen(path, i);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -131,14 +127,12 @@ static void resolve_symlink(struct strbuf *path)
|
|||||||
if (is_absolute_path(link.buf))
|
if (is_absolute_path(link.buf))
|
||||||
/* absolute path simply replaces p */
|
/* absolute path simply replaces p */
|
||||||
strbuf_reset(path);
|
strbuf_reset(path);
|
||||||
else {
|
else
|
||||||
/*
|
/*
|
||||||
* link is a relative path, so replace the
|
* link is a relative path, so replace the
|
||||||
* last element of p with it.
|
* last element of p with it.
|
||||||
*/
|
*/
|
||||||
char *r = last_path_elm(path->buf);
|
trim_last_path_component(path);
|
||||||
strbuf_setlen(path, r - path->buf);
|
|
||||||
}
|
|
||||||
|
|
||||||
strbuf_addbuf(path, &link);
|
strbuf_addbuf(path, &link);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user