summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormidipix <writeonce@midipix.org>2019-11-23 23:40:41 +0000
committermidipix <writeonce@midipix.org>2019-11-24 06:09:07 +0000
commitbb09419950c72469316f1271cb8f3e56035b48cf (patch)
tree14a694b559b4d15649c0920fe13fc343997b039c
parent47891ed2b208b1edd88c7a119fc061e02fe575be (diff)
downloadsltdl-bb09419950c72469316f1271cb8f3e56035b48cf.tar.bz2
sltdl-bb09419950c72469316f1271cb8f3e56035b48cf.tar.xz
search path: lt_dlpathopen(): initial implementation.
-rw-r--r--include/sltdl/sltdl.h1
-rw-r--r--src/core/lt_path.c47
2 files changed, 48 insertions, 0 deletions
diff --git a/include/sltdl/sltdl.h b/include/sltdl/sltdl.h
index a65fe8b..bd0b5bb 100644
--- a/include/sltdl/sltdl.h
+++ b/include/sltdl/sltdl.h
@@ -27,6 +27,7 @@ lt_api int lt_dlinit(void);
lt_api int lt_dlexit(void);
/* library search path */
+lt_api int lt_dlpathopen(const char *, const char **);
lt_api int lt_dladdsearchdir(const char *);
lt_api int lt_dlinsertsearchdir(const char *, const char *);
lt_api int lt_dlsetsearchpath(const char *);
diff --git a/src/core/lt_path.c b/src/core/lt_path.c
index 14eb0eb..5d58b03 100644
--- a/src/core/lt_path.c
+++ b/src/core/lt_path.c
@@ -5,9 +5,11 @@
/*******************************************************************/
#include <limits.h>
+#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <unistd.h>
#include <pthread.h>
#include <sltdl/sltdl.h>
#include "sltdl_core.h"
@@ -240,3 +242,48 @@ int lt_dlinsertsearchdir(const char * mark, const char * path)
return lt_sunlock(ret);
}
+
+int lt_dlpathopen(const char * module, const char ** extv)
+{
+ int fdat;
+ int fdmod;
+ char ** ppath;
+ const char ** pext;
+ size_t mlen;
+ size_t elen;
+ char path[1024];
+
+ if ((mlen = strlen(module)) >= sizeof(path))
+ return -1;
+
+ memcpy(path,module,mlen);
+
+ lt_slock();
+
+ for (ppath=lt_pathv; *ppath; ppath++) {
+ fdat = open(*ppath,O_RDONLY|O_DIRECTORY|O_CLOEXEC,0);
+
+ if (fdat >= 0) {
+ for (pext=extv; *pext; pext++) {
+ if (mlen + (elen = strlen(*pext)) >= (sizeof(path))) {
+ close(fdat);
+ return (-1);
+ }
+
+ memcpy(&path[mlen],*pext,elen);
+ path[mlen+elen] = 0;
+
+ fdmod = openat(fdat,path,O_EXEC|O_CLOEXEC,0);
+
+ if (fdmod >= 0) {
+ close(fdat);
+ return lt_sunlock(fdmod);
+ }
+ }
+
+ close(fdat);
+ }
+ }
+
+ return lt_sunlock(-1);
+}