summaryrefslogtreecommitdiffstats
path: root/src/binary_location.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/binary_location.h')
-rw-r--r--src/binary_location.h54
1 files changed, 54 insertions, 0 deletions
diff --git a/src/binary_location.h b/src/binary_location.h
new file mode 100644
index 0000000..303a3bd
--- /dev/null
+++ b/src/binary_location.h
@@ -0,0 +1,54 @@
+/* This file is part of Patchage.
+ * Copyright 2008-2014 David Robillard <http://drobilla.net>
+ *
+ * Patchage is free software: you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License as published by the Free
+ * Software Foundation, either version 3 of the License, or (at your option)
+ * any later version.
+ *
+ * Patchage is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Patchage. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef _GNU_SOURCE
+ #define _GNU_SOURCE
+#endif
+
+#include <assert.h>
+#include <limits.h>
+#include <stdlib.h>
+#include <dlfcn.h>
+
+#include <string>
+
+/** Return the absolute path of the binary. */
+static std::string
+binary_location()
+{
+ Dl_info dli;
+ std::string loc;
+ const int ret = dladdr((void*)&binary_location, &dli);
+ if (ret) {
+ char* const bin_loc = (char*)calloc(PATH_MAX, 1);
+ if (realpath(dli.dli_fname, bin_loc)) {
+ loc = bin_loc;
+ }
+ free(bin_loc);
+ }
+ return loc;
+}
+
+/** Return the absolute path of the bundle (binary parent directory). */
+static std::string
+bundle_location()
+{
+ const std::string binary = binary_location();
+ if (binary.empty()) {
+ return "";
+ }
+ return binary.substr(0, binary.find_last_of('/'));
+}