|
@@ -36,19 +36,10 @@
|
|
|
|
|
|
#define MAX_READLINKS 32
|
|
|
|
|
|
-#ifdef __STDC__
|
|
|
char *realpath(const char *path, char got_path[])
|
|
|
-#else
|
|
|
-char *realpath(path, got_path)
|
|
|
-const char *path;
|
|
|
-char got_path[];
|
|
|
-#endif
|
|
|
{
|
|
|
char copy_path[PATH_MAX];
|
|
|
-
|
|
|
-
|
|
|
- char *max_path;
|
|
|
- char *new_path;
|
|
|
+ char *max_path, *new_path, *allocated_path;
|
|
|
size_t path_len;
|
|
|
int readlinks = 0;
|
|
|
#ifdef S_IFLNK
|
|
@@ -72,12 +63,13 @@ char got_path[];
|
|
|
|
|
|
strcpy(copy_path + (PATH_MAX-1) - path_len, path);
|
|
|
path = copy_path + (PATH_MAX-1) - path_len;
|
|
|
+ allocated_path = got_path ? NULL : (got_path = malloc(PATH_MAX));
|
|
|
max_path = got_path + PATH_MAX - 2;
|
|
|
new_path = got_path;
|
|
|
if (*path != '/') {
|
|
|
|
|
|
if (!getcwd(new_path, PATH_MAX - 1))
|
|
|
- return NULL;
|
|
|
+ goto err;
|
|
|
new_path += strlen(new_path);
|
|
|
if (new_path[-1] != '/')
|
|
|
*new_path++ = '/';
|
|
@@ -114,6 +106,8 @@ char got_path[];
|
|
|
while (*path != '\0' && *path != '/') {
|
|
|
if (new_path > max_path) {
|
|
|
__set_errno(ENAMETOOLONG);
|
|
|
+ err:
|
|
|
+ free(allocated_path);
|
|
|
return NULL;
|
|
|
}
|
|
|
*new_path++ = *path++;
|
|
@@ -122,7 +116,7 @@ char got_path[];
|
|
|
|
|
|
if (readlinks++ > MAX_READLINKS) {
|
|
|
__set_errno(ELOOP);
|
|
|
- return NULL;
|
|
|
+ goto err;
|
|
|
}
|
|
|
path_len = strlen(path);
|
|
|
|
|
@@ -133,13 +127,13 @@ char got_path[];
|
|
|
if (link_len < 0) {
|
|
|
|
|
|
if (errno != EINVAL) {
|
|
|
- return NULL;
|
|
|
+ goto err;
|
|
|
}
|
|
|
} else {
|
|
|
|
|
|
if (path_len + link_len >= PATH_MAX - 2) {
|
|
|
__set_errno(ENAMETOOLONG);
|
|
|
- return NULL;
|
|
|
+ goto err;
|
|
|
}
|
|
|
|
|
|
|