X-Git-Url: http://nsz.repo.hu/git/?a=blobdiff_plain;f=src%2Fmisc%2Frealpath.c;h=88c849cda33de1e285a5b1eec7dfe9c222d013e5;hb=43e9f652bf4b2195b04fc14c93db591b30a7b790;hp=183351462519b99912265da7a87a11489dd8f1b9;hpb=f2d08cf7558176af7ef36cf5b5213e676b02d7ac;p=musl diff --git a/src/misc/realpath.c b/src/misc/realpath.c index 18335146..88c849cd 100644 --- a/src/misc/realpath.c +++ b/src/misc/realpath.c @@ -1,10 +1,13 @@ #include -#include #include #include #include #include #include +#include +#include "syscall.h" + +void __procfdname(char *, unsigned); char *realpath(const char *restrict filename, char *restrict resolved) { @@ -12,38 +15,31 @@ 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; return 0; } - fd = open(filename, O_RDONLY|O_NONBLOCK|O_CLOEXEC); + fd = sys_open(filename, O_PATH|O_NONBLOCK|O_CLOEXEC); if (fd < 0) return 0; - snprintf(buf, sizeof buf, "/proc/self/fd/%d", fd); - - if (!resolved) { - alloc = 1; - resolved = malloc(PATH_MAX); - if (!resolved) return 0; - } + __procfdname(buf, fd); - 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; + __syscall(SYS_close, fd); + return resolved ? strcpy(resolved, tmp) : strdup(tmp); err: - if (alloc) free(resolved); - close(fd); + __syscall(SYS_close, fd); return 0; }