mirror of
https://github.com/moparisthebest/pacman
synced 2024-12-22 15:58:50 -05:00
llstat: modify path in place
This makes llstat's signature differ from lstat's, but we never actually use it on a const string and this saves a large number of strdup's. This also allows stripping multiple trailing slashes and corrects a bug where calling llstat on "/" would result in calling lstat on an empty string. Signed-off-by: Andrew Gregory <andrew.gregory.8@gmail.com>
This commit is contained in:
parent
e8de265f80
commit
bbeced26f6
@ -80,17 +80,21 @@ char *mdirname(const char *path)
|
||||
* @param buf structure to fill with stat information
|
||||
* @return the return code from lstat
|
||||
*/
|
||||
int llstat(const char *path, struct stat *buf)
|
||||
int llstat(char *path, struct stat *buf)
|
||||
{
|
||||
int ret;
|
||||
char *c = NULL;
|
||||
size_t len = strlen(path);
|
||||
|
||||
/* strip the trailing slash if one exists */
|
||||
if(len != 0 && path[len - 1] == '/') {
|
||||
char *newpath = strdup(path);
|
||||
newpath[len - 1] = '\0';
|
||||
ret = lstat(newpath, buf);
|
||||
free(newpath);
|
||||
while(len > 1 && path[len - 1] == '/') {
|
||||
--len;
|
||||
c = path + len;
|
||||
}
|
||||
|
||||
if(c) {
|
||||
*c = '\0';
|
||||
ret = lstat(path, buf);
|
||||
*c = '/';
|
||||
} else {
|
||||
ret = lstat(path, buf);
|
||||
}
|
||||
|
@ -25,7 +25,7 @@
|
||||
const char *mbasename(const char *path);
|
||||
char *mdirname(const char *path);
|
||||
|
||||
int llstat(const char *path, struct stat *buf);
|
||||
int llstat(char *path, struct stat *buf);
|
||||
|
||||
#ifndef HAVE_STRNDUP
|
||||
char *strndup(const char *s, size_t n);
|
||||
|
Loading…
Reference in New Issue
Block a user