Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add realpath() function #209

Merged
merged 5 commits into from
Jun 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/libcglue/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
35 changes: 35 additions & 0 deletions src/libcglue/glue.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */