Browse Source

libobs/util: Add function to get executable path

jp9000 6 years ago
parent
commit
0e8fe8bbbc
4 changed files with 85 additions and 0 deletions
  1. 31 0
      libobs/util/platform-cocoa.m
  2. 27 0
      libobs/util/platform-nix.c
  3. 25 0
      libobs/util/platform-windows.c
  4. 2 0
      libobs/util/platform.h

+ 31 - 0
libobs/util/platform-cocoa.m

@@ -24,11 +24,13 @@
 #include <time.h>
 #include <unistd.h>
 #include <sys/types.h>
+#include <sys/param.h>
 #include <sys/sysctl.h>
 
 #include <CoreServices/CoreServices.h>
 #include <mach/mach.h>
 #include <mach/mach_time.h>
+#include <mach-o/dyld.h>
 
 #include <IOKit/pwr_mgt/IOPMLib.h>
 
@@ -140,6 +142,35 @@ char *os_get_program_data_path_ptr(const char *name)
 	return os_get_path_ptr_internal(name, NSLocalDomainMask);
 }
 
+char *os_get_executable_path_ptr(const char *name)
+{
+	char exe[PATH_MAX];
+	char abs_path[PATH_MAX];
+	uint32_t size = sizeof(exe);
+	struct dstr path;
+	char *slash;
+
+	if (_NSGetExecutablePath(exe, &size) != 0) {
+		return NULL;
+	}
+
+	if (!realpath(exe, abs_path)) {
+		return NULL;
+	}
+
+	dstr_init_copy(&path, abs_path);
+	slash = strrchr(path.array, '/');
+	if (slash) {
+		size_t len = slash - path.array + 1;
+		dstr_resize(&path, len);
+	}
+
+	if (name && *name) {
+		dstr_cat(&path, name);
+	}
+	return path.array;
+}
+
 struct os_cpu_usage_info {
 	int64_t last_cpu_time;
 	int64_t last_sys_time;

+ 27 - 0
libobs/util/platform-nix.c

@@ -33,6 +33,7 @@
 #if !defined(__APPLE__)
 #include <sys/times.h>
 #include <sys/wait.h>
+#include <libgen.h>
 #ifdef __FreeBSD__
 #include <sys/param.h>
 #include <sys/queue.h>
@@ -265,6 +266,32 @@ char *os_get_program_data_path_ptr(const char *name)
 	return str;
 }
 
+char *os_get_executable_path_ptr(const char *name)
+{
+	char exe[PATH_MAX];
+	ssize_t count = readlink("/proc/self/exe", exe, PATH_MAX);
+	const char *path_out = NULL;
+	struct dstr path;
+
+	if (count == -1) {
+		return NULL;
+	}
+
+	path_out = dirname(exe);
+	if (!path_out) {
+		return NULL;
+	}
+
+	dstr_init_copy(&path, path_out);
+	dstr_cat(&path, "/");
+
+	if (name && *name) {
+		dstr_cat(&path, name);
+	}
+
+	return path.array;
+}
+
 #endif
 
 bool os_file_exists(const char *path)

+ 25 - 0
libobs/util/platform-windows.c

@@ -295,6 +295,31 @@ char *os_get_program_data_path_ptr(const char *name)
 	return os_get_path_ptr_internal(name, CSIDL_COMMON_APPDATA);
 }
 
+char *os_get_executable_path_ptr(const char *name)
+{
+	char *ptr;
+	char *slash;
+	wchar_t path_utf16[MAX_PATH];
+	struct dstr path;
+
+	GetModuleFileNameW(NULL, path_utf16, MAX_PATH);
+
+	os_wcs_to_utf8_ptr(path_utf16, 0, &ptr);
+	dstr_init_move_array(&path, ptr);
+	dstr_replace(&path, "\\", "/");
+	slash = strrchr(path.array, '/');
+	if (slash) {
+		size_t len = slash - path.array + 1;
+		dstr_resize(&path, len);
+	}
+
+	if (name && *name) {
+		dstr_cat(&path, name);
+	}
+
+	return path.array;
+}
+
 bool os_file_exists(const char *path)
 {
 	WIN32_FIND_DATAW wfd;

+ 2 - 0
libobs/util/platform.h

@@ -111,6 +111,8 @@ EXPORT char *os_get_config_path_ptr(const char *name);
 EXPORT int os_get_program_data_path(char *dst, size_t size, const char *name);
 EXPORT char *os_get_program_data_path_ptr(const char *name);
 
+EXPORT char *os_get_executable_path_ptr(const char *name);
+
 EXPORT bool os_file_exists(const char *path);
 
 EXPORT size_t os_get_abs_path(const char *path, char *abspath, size_t size);