diff options
author | Andrew Gregory <andrew.gregory.8@gmail.com> | 2014-06-26 17:52:05 +0200 |
---|---|---|
committer | Allan McRae <allan@archlinux.org> | 2014-08-03 10:46:32 +0200 |
commit | bbeced26f6ce167ad93817ebe7180cd22ef33c86 (patch) | |
tree | db4bcc3bf951847ba0a4617e6f083c7aa104ced3 | |
parent | e8de265f8039165dc32ffb78f6a6a5eb0c1514ad (diff) | |
download | pacman-bbeced26f6ce167ad93817ebe7180cd22ef33c86.tar.gz pacman-bbeced26f6ce167ad93817ebe7180cd22ef33c86.tar.xz |
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>
-rw-r--r-- | src/common/util-common.c | 18 | ||||
-rw-r--r-- | src/common/util-common.h | 2 |
2 files changed, 12 insertions, 8 deletions
diff --git a/src/common/util-common.c b/src/common/util-common.c index e6f95194..3316eaec 100644 --- a/src/common/util-common.c +++ b/src/common/util-common.c @@ -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); } diff --git a/src/common/util-common.h b/src/common/util-common.h index 5f04b00c..576702fa 100644 --- a/src/common/util-common.h +++ b/src/common/util-common.h @@ -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); |