* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
* USA
*
- * $Id: unix.c 26180 2008-11-11 00:00:12Z androsyn $
*/
+
#include <librb_config.h>
#include <rb_lib.h>
#include <crt_externs.h>
#endif
+#if defined(__FreeBSD__) || defined(__DragonFly__)
+#include <sys/types.h>
+#include <sys/sysctl.h>
+#endif
+
#if defined(HAVE_SPAWN_H) && defined(HAVE_POSIX_SPAWN)
#include <spawn.h>
rb_path_to_self(void)
{
static char path_buf[4096];
+ size_t path_len = sizeof(path_buf);
#if defined(HAVE_GETEXECNAME)
char *s = getexecname();
if (s == NULL)
return NULL;
realpath(s, path_buf);
return path_buf;
-#elif defined(__linux__)
- if (readlink("/proc/self/exe", path_buf, sizeof path_buf) != -1)
+#elif defined(__linux__) || (defined(__FreeBSD__) && !defined(KERN_PROC_PATHNAME))
+ if (readlink("/proc/self/exe", path_buf, path_len) != -1)
+ return path_buf;
+ return NULL;
+#elif defined(__FreeBSD__) || defined(__DragonFly__)
+ int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 };
+ if (sysctl(mib, 4, path_buf, &path_len, NULL, 0) == 0)
return path_buf;
return NULL;
#elif defined(HAVE_DLINFO)
if (map == NULL)
return NULL;
realpath(map->l_name, path_buf);
+ return path_buf;
#elif defined(__APPLE__)
char tmp_path[4096];
uint32_t pathlen = 4096;
realpath(tmp_path, path_buf);
return path_buf;
+#else
+ return NULL;
#endif
}