zsh-workers
 help / color / mirror / code / Atom feed
* PATCH: better support for BSDs for $ZSH_EXEPATH
@ 2024-09-13 22:59 Oliver Kiddle
  2024-09-15 22:50 ` Oliver Kiddle
  0 siblings, 1 reply; 2+ messages in thread
From: Oliver Kiddle @ 2024-09-13 22:59 UTC (permalink / raw)
  To: Zsh workers

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 \


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: PATCH: better support for BSDs for $ZSH_EXEPATH
  2024-09-13 22:59 PATCH: better support for BSDs for $ZSH_EXEPATH Oliver Kiddle
@ 2024-09-15 22:50 ` Oliver Kiddle
  0 siblings, 0 replies; 2+ messages in thread
From: Oliver Kiddle @ 2024-09-15 22:50 UTC (permalink / raw)
  To: Zsh workers

I wrote:
> 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).

Apparently on Linux systems of a certain age, sys/sysctl.h exists but
including it prints warnings about use of a deprecated header. On the
newer system I tested against it no longer exists. I can't find a better
way to avoid this warning than the following.

Oliver

diff --git a/Src/init.c b/Src/init.c
index 2f914f96b..61f759ded 100644
--- a/Src/init.c
+++ b/Src/init.c
@@ -36,7 +36,7 @@
 
 #include "version.h"
 
-#ifdef HAVE_SYS_SYSCTL_H
+#if defined(HAVE_SYS_SYSCTL_H) && !defined(__linux)
 #include <sys/sysctl.h>
 #endif
 


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2024-09-15 22:51 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-09-13 22:59 PATCH: better support for BSDs for $ZSH_EXEPATH Oliver Kiddle
2024-09-15 22:50 ` Oliver Kiddle

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).