summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authormidipix <writeonce@midipix.org>2020-12-26 00:45:51 +0000
committermidipix <writeonce@midipix.org>2020-12-26 00:50:28 +0000
commit9ca35be2ab917dc88bcf5716ccf6cde20e709191 (patch)
tree42e8a492f693014de335afb9dbfbc266384a5712
parentc141a081bd2c74e7c409416941e4952ef5b774ee (diff)
downloadslibtool-9ca35be2ab917dc88bcf5716ccf6cde20e709191.tar.bz2
slibtool-9ca35be2ab917dc88bcf5716ccf6cde20e709191.tar.xz
helper api: added slbt_realpath().
-rw-r--r--include/slibtool/slibtool.h1
-rw-r--r--project/common.mk1
-rw-r--r--src/helper/slbt_realpath.c89
3 files changed, 91 insertions, 0 deletions
diff --git a/include/slibtool/slibtool.h b/include/slibtool/slibtool.h
index de49275..ee70c79 100644
--- a/include/slibtool/slibtool.h
+++ b/include/slibtool/slibtool.h
@@ -300,6 +300,7 @@ slbt_api int slbt_archive_import (const struct slbt_driver_ctx *, struct
slbt_api int slbt_copy_file (const struct slbt_driver_ctx *, struct slbt_exec_ctx *,
char * src, char * dst);
slbt_api int slbt_dump_machine (const char * compiler, char * machine, size_t bufsize);
+slbt_api int slbt_realpath (int, const char *, int, char *, size_t);
/* utility api */
slbt_api int slbt_main (char **, char **,
diff --git a/project/common.mk b/project/common.mk
index e08e900..cd653bc 100644
--- a/project/common.mk
+++ b/project/common.mk
@@ -4,6 +4,7 @@ API_SRCS = \
src/helper/slbt_archive_import.c \
src/helper/slbt_copy_file.c \
src/helper/slbt_dump_machine.c \
+ src/helper/slbt_realpath.c \
src/logic/slbt_exec_compile.c \
src/logic/slbt_exec_ctx.c \
src/logic/slbt_exec_execute.c \
diff --git a/src/helper/slbt_realpath.c b/src/helper/slbt_realpath.c
new file mode 100644
index 0000000..4c7446e
--- /dev/null
+++ b/src/helper/slbt_realpath.c
@@ -0,0 +1,89 @@
+
+/*******************************************************************/
+/* slibtool: a skinny libtool implementation, written in C */
+/* Copyright (C) 2016--2020 Z. Gilboa */
+/* Released under the Standard MIT License; see COPYING.SLIBTOOL. */
+/*******************************************************************/
+
+#include <fcntl.h>
+#include <stdlib.h>
+#include <limits.h>
+#include <errno.h>
+#include <sys/stat.h>
+#include <slibtool/slibtool.h>
+
+#include "slibtool_readlink_impl.h"
+
+#ifdef _MIDIPIX_ABI
+#include <sys/fs.h>
+#endif
+
+#ifndef ENOTSUP
+#define ENOTSUP EOPNOTSUPP
+#endif
+
+int slbt_realpath(
+ int fdat,
+ const char * path,
+ int options,
+ char * buf,
+ size_t buflen)
+{
+ int ret;
+ int fd;
+ int fdproc;
+ struct stat st;
+ struct stat stproc;
+ char procfspath[36];
+
+ /* common validation */
+ if (!buf || (options & O_CREAT)) {
+ errno = EINVAL;
+ return -1;
+ }
+
+ /* framework-based wrapper */
+#ifdef _MIDIPIX_ABI
+ return __fs_rpath(fdat,path,options,buf,buflen);
+#endif
+
+ /* buflen */
+ if (buflen < PATH_MAX) {
+ errno = ENOBUFS;
+ return -1;
+ }
+
+ /* AT_FDCWD */
+ if (fdat == AT_FDCWD) {
+ return realpath(path,buf) ? 0 : -1;
+ }
+
+ /* /proc/self/fd */
+ if ((fd = openat(fdat,path,options,0)) < 0)
+ return -1;
+
+ sprintf(procfspath,"/proc/self/fd/%d",fd);
+
+ if (slbt_readlink(procfspath,buf,buflen)) {
+ close(fd);
+ return -1;
+ }
+
+ if ((fdproc = openat(AT_FDCWD,buf,options|O_NOFOLLOW,0)) < 0) {
+ close(fd);
+ errno = ELOOP;
+ return -1;
+ }
+
+ ret = fstat(fd,&st) || fstat(fdproc,&stproc);
+
+ close(fd);
+ close(fdproc);
+
+ if (ret || (st.st_dev != stproc.st_dev) || (st.st_ino != stproc.st_ino)) {
+ errno = ENOTSUP;
+ return -1;
+ }
+
+ return 0;
+}