Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
PATCH: better support for BSDs for $ZSH_EXEPATH
- X-seq: zsh-workers 53085
- From: Oliver Kiddle <opk@xxxxxxx>
- To: Zsh workers <zsh-workers@xxxxxxx>
- Subject: PATCH: better support for BSDs for $ZSH_EXEPATH
- Date: Sat, 14 Sep 2024 00:59:25 +0200
- Archived-at: <https://zsh.org/workers/53085>
- List-id: <zsh-workers.zsh.org>
To implement $ZSH_EXEPATH there is Mac and Linux specific solutions
followed by a fallback using argv[0] and a $PATH search. This adds a
better approach for NetBSD, FreeBSD and DragonflyBSD based on sysctl(3).
I've also moved a block that started looking at argv[0] down because
there's no need to worry about whether that starts with '-' if the other
approaches work. I also fixed a comment typo and silenced a compiler
warning by changing a strncpy() to memcpy() - the compiler isn't clever
enough to see that a null gets added a couple of lines later.
Oliver
diff --git a/Src/init.c b/Src/init.c
index 70e878e20..2f914f96b 100644
--- a/Src/init.c
+++ b/Src/init.c
@@ -36,6 +36,10 @@
#include "version.h"
+#ifdef HAVE_SYS_SYSCTL_H
+#include <sys/sysctl.h>
+#endif
+
/**/
int noexitct = 0;
@@ -913,12 +917,6 @@ getmypath(const char *name, const char *cwd)
char *buf;
int namelen;
- if (!name)
- return NULL;
- if (*name == '-')
- ++name;
- if ((namelen = strlen(name)) == 0)
- return NULL;
#if defined(__APPLE__)
{
uint32_t n = PATH_MAX;
@@ -934,6 +932,19 @@ getmypath(const char *name, const char *cwd)
else
free(buf);
}
+#elif defined(KERN_PROC_PATHNAME)
+ {
+#ifdef __NetBSD__
+ int mib[4] = { CTL_KERN, KERN_PROC_ARGS, getpid(), KERN_PROC_PATHNAME };
+#else
+ int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 };
+#endif
+ size_t len = PATH_MAX;
+ buf = (char *) zalloc(PATH_MAX);
+ if (!sysctl(mib, 4, buf, &len, NULL, 0) && len > 1)
+ return buf;
+ free(buf);
+ }
#elif defined(PROC_SELF_EXE)
{
ssize_t n;
@@ -947,6 +958,13 @@ getmypath(const char *name, const char *cwd)
free(buf);
}
#endif
+
+ if (!name)
+ return NULL;
+ if (*name == '-')
+ ++name;
+ if ((namelen = strlen(name)) == 0)
+ return NULL;
/* guess the absolute pathname of 'name' */
if (name[namelen-1] == '/') /* name should not end with '/' */
return NULL;
@@ -964,7 +982,7 @@ getmypath(const char *name, const char *cwd)
}
#ifdef HAVE_REALPATH
else {
- /* search each dir in PARH */
+ /* search each dir in PATH */
const char *path, *sep;
char *real, *try;
int pathlen, dirlen;
@@ -978,7 +996,7 @@ getmypath(const char *name, const char *cwd)
do {
sep = strchr(path, ':');
dirlen = sep ? sep - path : strlen(path);
- strncpy(try, path, dirlen);
+ memcpy(try, path, dirlen);
try[dirlen] = '/';
try[dirlen+1] = '\0';
strcat(try, name);
diff --git a/configure.ac b/configure.ac
index a3f6543a9..74f2b0c1a 100644
--- a/configure.ac
+++ b/configure.ac
@@ -636,7 +636,7 @@ fi
AC_CHECK_HEADERS(sys/time.h sys/times.h sys/select.h termcap.h termio.h \
termios.h sys/param.h sys/filio.h string.h memory.h \
limits.h fcntl.h libc.h sys/utsname.h sys/resource.h \
- sys/random.h \
+ sys/sysctl.h sys/random.h \
locale.h errno.h stdio.h stdarg.h varargs.h stdlib.h \
unistd.h sys/capability.h \
utmp.h utmpx.h sys/types.h pwd.h grp.h poll.h sys/mman.h \
Messages sorted by:
Reverse Date,
Date,
Thread,
Author