/* * vim:ts=8:sw=3:sts=8:noexpandtab:cino=>5n-3f0^-2{2 */ #include "e.h" /* local subsystem functions */ static int _e_prefix_try_proc(void); static int _e_prefix_try_argv(char *argv0); /* local subsystem globals */ static char *_exe_path = NULL; static char *_prefix_path = NULL; int e_prefix_determine(char *argv0) { char *p; if (_exe_path) free(_exe_path); _exe_path = NULL; if (_prefix_path) free(_prefix_path); _prefix_path = NULL; if (!_e_prefix_try_proc()) { if (!_e_prefix_try_argv(argv0)) return 0; } /* _exe_path is now a full absolute path TO this exe - figure out rest */ /* if * exe = /blah/whatever/bin/exe * then * prefix = /blah/whatever * bin_dir = /blah/whatever/bin * data_dir = /blah/whatever/share/enlightenment * locale_dir = /blah/whatever/share/locale * lib_dir = /blah/whatever/lib */ p = strrchr(_exe_path, '/'); if (p) { p--; while (p >= _exe_path) { if (*p == '/') { _prefix_path = malloc(p - _exe_path + 1); if (_prefix_path) { strncpy(_prefix_path, _exe_path, p - _exe_path); _prefix_path[p - _exe_path] = 0; printf("DYNAMIC DETERMINED PREFIX: %s\n", _prefix_path); return 1; } else { free(_exe_path); _exe_path = NULL; return 0; } } p--; } } free(_exe_path); _exe_path = NULL; return 0; } const char * e_prefix_get(void) { return _prefix_path; } /* local subsystem functions */ static int _e_prefix_try_proc(void) { FILE *f; char buf[4096]; void *func = NULL; func = (void *)_e_prefix_try_proc; f = fopen("/proc/self/maps", "r"); if (!f) return 0; while (fgets(buf, sizeof(buf), f)) { int len; char *p, mode[5] = ""; unsigned long ptr1 = 0, ptr2 = 0; len = strlen(buf); if (buf[len - 1] == '\n') { buf[len - 1] = 0; len--; } if (sscanf(buf, "%lx-%lx %4s", &ptr1, &ptr2, mode) == 3) { if (!strcmp(mode, "r-xp")) { if (((void *)ptr1 <= func) && (func < (void *)ptr2)) { p = strchr(buf, '/'); if (p) { if (len > 10) { if (!strcmp(buf + len - 10, " (deleted)")) buf[len - 10] = 0; } _exe_path = strdup(p); fclose(f); return 1; } else break; } } } } fclose(f); return 0; } static int _e_prefix_try_argv(char *argv0) { char *path, *p, *cp, *s; int len, lenexe; #ifdef PATH_MAX char buf[PATH_MAX], buf2[PATH_MAX], buf3[PATH_MAX]; #else char buf[4096], buf2[4096], buf3[4096]; #endif /* 1. is argv0 abs path? */ if (argv0[0] == '/') { _exe_path = strdup(argv0); if (access(_exe_path, X_OK) == 0) return 1; free(_exe_path); _exe_path = NULL; return 0; } /* 2. relative path */ if (strchr(argv0, '/')) { if (getcwd(buf3, sizeof(buf3))) { snprintf(buf2, sizeof(buf2), "%s/%s", buf3, argv0); if (realpath(buf2, buf)) { _exe_path = strdup(buf); if (access(_exe_path, X_OK) == 0) return 1; free(_exe_path); _exe_path = NULL; } } } /* 3. argv0 no path - look in PATH */ path = getenv("PATH"); if (!path) return 0; p = path; cp = p; lenexe = strlen(argv0); while (p = strchr(cp, ':')) { len = p - cp; s = malloc(len + 1 + lenexe + 1); if (s) { strncpy(s, cp, len); s[len] = '/'; strcpy(s + len + 1, argv0); if (realpath(s, buf)) { if (access(buf, X_OK) == 0) { _exe_path = strdup(buf); free(s); return 1; } } free(s); } cp = p + 1; } len = strlen(cp); s = malloc(len + 1 + lenexe + 1); if (s) { strncpy(s, cp, len); s[len] = '/'; strcpy(s + len + 1, argv0); if (realpath(s, buf)) { if (access(buf, X_OK) == 0) { _exe_path = strdup(buf); free(s); return 1; } } free(s); } /* 4. big problems. arg[0] != executable - weird execution */ return 0; }