diff --git a/src/misc/realpath.c b/src/misc/realpath.c index 2b84649716b4bc08f004b0707f6ed44f20c3aa5f..43d4018358f718c69455192930ec73ffe540e4de 100644 --- a/src/misc/realpath.c +++ b/src/misc/realpath.c @@ -5,6 +5,7 @@ #include #include #include +#include void __procfdname(char *, unsigned); @@ -14,7 +15,7 @@ char *realpath(const char *restrict filename, char *restrict resolved) ssize_t r; struct stat st1, st2; char buf[15+3*sizeof(int)]; - int alloc = 0; + char tmp[PATH_MAX]; if (!filename) { errno = EINVAL; @@ -25,27 +26,20 @@ char *realpath(const char *restrict filename, char *restrict resolved) if (fd < 0) return 0; __procfdname(buf, fd); - if (!resolved) { - alloc = 1; - resolved = malloc(PATH_MAX); - if (!resolved) return 0; - } - - r = readlink(buf, resolved, PATH_MAX-1); + r = readlink(buf, tmp, sizeof tmp - 1); if (r < 0) goto err; - resolved[r] = 0; + tmp[r] = 0; fstat(fd, &st1); - r = stat(resolved, &st2); + r = stat(tmp, &st2); if (r<0 || st1.st_dev != st2.st_dev || st1.st_ino != st2.st_ino) { if (!r) errno = ELOOP; goto err; } close(fd); - return resolved; + return resolved ? strcpy(resolved, tmp) : strdup(tmp); err: - if (alloc) free(resolved); close(fd); return 0; }