Merge pull request #209 from diamant3/add-realpath

add `realpath()` function
This commit is contained in:
Ben
2024-06-16 18:05:20 +10:00
committed by GitHub
2 changed files with 37 additions and 1 deletions

View File

@@ -29,7 +29,8 @@ GLUE_OBJS = __dummy_passwd.o __psp_heap_blockid.o __psp_free_heap.o _fork.o _wai
_rename.o _getpid.o _kill.o _sbrk.o _gettimeofday.o _times.o ftime.o clock_getres.o clock_gettime.o clock_settime.o \
_isatty.o symlink.o truncate.o chmod.o fchmod.o pathconf.o readlink.o utime.o fchown.o _getentropy.o getpwuid.o \
fsync.o getpwnam.o getuid.o geteuid.o basename.o statvfs.o \
openat.o renameat.o fchmodat.o fstatat.o mkdirat.o faccessat.o fchownat.o linkat.o readlinkat.o unlinkat.o
openat.o renameat.o fchmodat.o fstatat.o mkdirat.o faccessat.o fchownat.o linkat.o readlinkat.o unlinkat.o \
realpath.o
INIT_OBJS = __libpthreadglue_init.o __libcglue_init.o __libcglue_deinit.o _exit.o abort.o exit.o

View File

@@ -1179,3 +1179,38 @@ int unlinkat(int dirfd, const char *pathname, int flags)
}
}
#endif /* F_unlinkat */
#ifdef F_realpath
char *realpath(const char *path, char *resolved_path)
{
if (path == NULL) {
errno = EINVAL;
return NULL;
}
if (strlen(path) > PATH_MAX) {
errno = ENAMETOOLONG;
return NULL;
}
/* check if file or directory exist */
struct stat st;
if (stat(path, &st) < 0) {
errno = ENOENT;
return NULL;
}
// if resolved_path arg is NULL, use malloc instead
if (resolved_path == NULL) {
resolved_path = (char *)malloc(PATH_MAX * sizeof(char));
if (resolved_path == NULL) {
errno = ENOMEM;
return NULL;
}
}
__path_absolute(path, resolved_path, PATH_MAX);
return resolved_path;
}
#endif /* F_realpath */