1
0
mirror of https://github.com/moparisthebest/pacman synced 2024-12-23 00:08: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:
Andrew Gregory 2014-06-26 11:52:05 -04:00 committed by Allan McRae
parent e8de265f80
commit bbeced26f6
2 changed files with 12 additions and 8 deletions

View File

@ -80,17 +80,21 @@ char *mdirname(const char *path)
* @param buf structure to fill with stat information * @param buf structure to fill with stat information
* @return the return code from lstat * @return the return code from lstat
*/ */
int llstat(const char *path, struct stat *buf) int llstat(char *path, struct stat *buf)
{ {
int ret; int ret;
char *c = NULL;
size_t len = strlen(path); size_t len = strlen(path);
/* strip the trailing slash if one exists */ while(len > 1 && path[len - 1] == '/') {
if(len != 0 && path[len - 1] == '/') { --len;
char *newpath = strdup(path); c = path + len;
newpath[len - 1] = '\0'; }
ret = lstat(newpath, buf);
free(newpath); if(c) {
*c = '\0';
ret = lstat(path, buf);
*c = '/';
} else { } else {
ret = lstat(path, buf); ret = lstat(path, buf);
} }

View File

@ -25,7 +25,7 @@
const char *mbasename(const char *path); const char *mbasename(const char *path);
char *mdirname(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 #ifndef HAVE_STRNDUP
char *strndup(const char *s, size_t n); char *strndup(const char *s, size_t n);